📄 isamin.cu
字号:
/* * Copyright 1993-2008 NVIDIA Corporation. All rights reserved. * * NOTICE TO USER: * * This source code is subject to NVIDIA ownership rights under U.S. and * international Copyright laws. * * This software and the information contained herein is being provided * under the terms and conditions of a Source Code License Agreement. * * NVIDIA MAKES NO REPRESENTATION ABOUT THE SUITABILITY OF THIS SOURCE * CODE FOR ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR * IMPLIED WARRANTY OF ANY KIND. NVIDIA DISCLAIMS ALL WARRANTIES WITH * REGARD TO THIS SOURCE CODE, INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE. * IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL, * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE * OR PERFORMANCE OF THIS SOURCE CODE. * * U.S. Government End Users. This source code is a "commercial item" as * that term is defined at 48 C.F.R. 2.101 (OCT 1995), consisting of * "commercial computer software" and "commercial computer software * documentation" as such terms are used in 48 C.F.R. 12.212 (SEPT 1995) * and is provided to the U.S. Government only as a commercial end item. * Consistent with 48 C.F.R.12.212 and 48 C.F.R. 227.7202-1 through * 227.7202-4 (JUNE 1995), all U.S. Government End Users acquire the * source code with only those rights set forth herein. *//* This file contains the implementation of the BLAS-1 function isamin */#include <stdlib.h>#include <assert.h>#include <string.h>#include <stdio.h>#include <limits.h>#include <math.h>#include "cublas.h" /* CUBLAS public header file */#include "cublasP.h" /* CUBLAS private header file */texture<float> texX;__global__ void isamin_gld_main (struct cublasIsaminParams parms);__global__ void isamin_tex_main (struct cublasIsaminParams parms);/* * int * isamin (int n, const float *x, int incx) * * finds the smallest index of the minimum magnitude element of single * precision vector x; that is, the result is the first i, i = 0 to n - 1, * that minimizes abs(x[1 + i * incx])). * * Input * ----- * n number of elements in input vector * x single precision vector with n elements * incx storage spacing between elements of x * * Output * ------ * returns the smallest index (0 if n <= 0 or incx <= 0) * * Reference: http://www.netlib.org/scilib/blass.f * * Error status for this function can be retrieved via cublasGetError(). * * Error Status * ------------ * CUBLAS_STATUS_NOT_INITIALIZED if CUBLAS library has not been initialized * CUBLAS_STATUS_EXECUTION_FAILED if function failed to launch on GPU */__host__ int CUBLASAPI cublasIsamin (int n, const float *x, int incx){ struct cublasContext *ctx = CUBLAS_GET_CTX(); struct cublasIsaminParams params; float *devPtrTmx; int *devPtrTix; cublasStatus status; cudaError_t cudaStat; int nbrCtas; int threadsPerCta; int idx = 0; int *tix; float *tmx; int i, jmin; float smin, xabs; volatile union { float f; int i; } cvt; int sizeX = n * (imax (1, abs(incx))); size_t texXOfs = 0; int useTexture; if (!cublasInitialized (ctx)) { cublasSetError (ctx, CUBLAS_STATUS_NOT_INITIALIZED); return idx; } /* early out if nothing to do */ if ((n <= 0) || (incx <= 0)) { return idx; } if (n < CUBLAS_ISAMIN_CTAS) { nbrCtas = n; threadsPerCta = CUBLAS_ISAMIN_THREAD_COUNT; } else { nbrCtas = CUBLAS_ISAMIN_CTAS; threadsPerCta = CUBLAS_ISAMIN_THREAD_COUNT; } useTexture = sizeX < CUBLAS_MAX_1DBUF_SIZE; /* Currently, the overhead for using textures is high. Do not use texture * for vectors that are short, or those that are aligned and have unit * stride and thus have nicely coalescing GLDs. */ if ((n < 100000) || /* experimental bound */ ((sizeX == n) && (!(((uintptr_t) x) % CUBLAS_WORD_ALIGN)))) { useTexture = 0; } if (useTexture) { if ((cudaStat=cudaBindTexture (&texXOfs,texX,x,sizeX*sizeof(x[0]))) != cudaSuccess) { cublasSetError (ctx, CUBLAS_STATUS_MAPPING_ERROR); return idx; } texXOfs /= sizeof(x[0]); } /* allocate memory to collect results (index, minimum), one per CTA */ status = cublasAlloc (nbrCtas, sizeof(tmx[0]), (void**)&devPtrTmx); if (status != CUBLAS_STATUS_SUCCESS) { cublasSetError (ctx, status); return idx; } status = cublasAlloc (nbrCtas, sizeof(tix[0]), (void**)&devPtrTix); if (status != CUBLAS_STATUS_SUCCESS) { cublasFree (devPtrTmx); cublasSetError (ctx, status); return idx; } /* allocate small buffer to retrieve the per-CTA results */ tmx = (float *) calloc (nbrCtas, sizeof(tmx[0])); if (!tmx) { cublasFree (devPtrTmx); cublasFree (devPtrTix); cublasSetError (ctx, CUBLAS_STATUS_ALLOC_FAILED); return idx; } tix = (int *) calloc (nbrCtas, sizeof(tix[0])); if (!tix) { cublasFree (devPtrTmx); cublasFree (devPtrTix); free (tmx); cublasSetError (ctx, CUBLAS_STATUS_ALLOC_FAILED); return idx; } memset (¶ms, 0, sizeof(params)); params.n = n; params.sx = x; params.incx = incx; params.resMin = devPtrTmx; params.resPos = devPtrTix; params.texXOfs = (int)texXOfs; cudaStat = cudaGetLastError(); /* clear error status */ if (useTexture) { isamin_tex_main<<<nbrCtas,threadsPerCta>>>(params); } else { isamin_gld_main<<<nbrCtas,threadsPerCta>>>(params); } cudaStat = cudaGetLastError(); /* check for launch error */ if (cudaStat != cudaSuccess) { cublasSetError (ctx, CUBLAS_STATUS_EXECUTION_FAILED); cublasFree (devPtrTmx); cublasFree (devPtrTix); free (tmx); free (tix); return idx; } /* Get idx/min results from each CTA */ status = cublasGetVector (nbrCtas, sizeof(tmx[0]), devPtrTmx, 1, tmx, 1); if (status != CUBLAS_STATUS_SUCCESS) { cublasSetError (ctx, CUBLAS_STATUS_INTERNAL_ERROR); cublasFree (devPtrTmx); cublasFree (devPtrTix); free (tmx); free (tix); return idx; } status = cublasGetVector (nbrCtas, sizeof(tix[0]), devPtrTix, 1, tix, 1); if (status != CUBLAS_STATUS_SUCCESS) { cublasSetError (ctx, CUBLAS_STATUS_INTERNAL_ERROR); cublasFree (devPtrTmx); cublasFree (devPtrTix); free (tmx); free (tix); return idx; } /* find smallest index of minimum value among CTA results */ cvt.i = 0x7f800000; /* IEEE-754 single infinity */ smin = cvt.f; for (i = 0; i < nbrCtas; i++) { xabs = tmx[i]; jmin = tix[i]; if ((xabs < smin) || ((xabs == smin) && (jmin < idx))) { idx = jmin; smin = xabs; } } /* translate result from 0-indexed to 1-indexed */ idx++; status = cublasFree (devPtrTmx); if (status != CUBLAS_STATUS_SUCCESS) { cublasSetError (ctx, CUBLAS_STATUS_INTERNAL_ERROR); /* corruption ? */ } status = cublasFree (devPtrTix); if (status != CUBLAS_STATUS_SUCCESS) { cublasSetError (ctx, CUBLAS_STATUS_INTERNAL_ERROR); /* corruption ? */ } free (tmx); free (tix); if (useTexture) { if ((cudaStat = cudaUnbindTexture (texX)) != cudaSuccess) { cublasSetError (ctx, CUBLAS_STATUS_INTERNAL_ERROR); } } return idx; }__shared__ int partialIMin[CUBLAS_ISAMIN_THREAD_COUNT];__shared__ float partialSMin[CUBLAS_ISAMIN_THREAD_COUNT];__global__ void isamin_gld_main (struct cublasIsaminParams parms){#undef USE_TEX#define USE_TEX 0#include "isamin.h"}__global__ void isamin_tex_main (struct cublasIsaminParams parms){#undef USE_TEX#define USE_TEX 1#include "isamin.h"}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -