Example: biology

cuBLAS Library - NVIDIA Developer

| March 2022cuBLAS LibraryUser GuidecuBLAS | 1 Chapter cuBLAS Library is an implementation of BLAS (Basic Linear Algebra Subprograms) on topof the NVIDIA CUDA runtime. It allows the user to access the computational resources ofNVIDIA Graphics Processing Unit (GPU).The cuBLAS Library exposes three sets of API: The cuBLAS API, which is simply called cuBLAS API in this document (starting with ), The cuBLASXt API (starting with CUDA ), and The cuBLASLt API (starting with CUDA )To use the cuBLAS API, the application must allocate the required matrices and vectors in theGPU memory space, fill them with data, call the sequence of desired cuBLAS functions, andthen upload the results from the GPU memory space back to the host. The cuBLAS API alsoprovides helper functions for writing and retrieving data from the use the cuBLASXt API, the application may have the data on the Host or any of the devicesinvolved in the computation, and the Library will take care of dispatching the operation to, andtransferring the data to, one or multiple GPUs present in the system, depending on the cuBLASLt is a lightweight Library dedicated to GEneral Matrix-to-ma

should convert to using the new API if it requires sophisticated and optimal stream parallelism, or if it calls cuBLAS routines concurrently from multiple threads. For the rest of the document, the new cuBLAS Library API will simply be referred to as the cuBLAS Library API.

Tags:

  Using, Library, Routines, Bucal, Cublas library

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of cuBLAS Library - NVIDIA Developer

1 | March 2022cuBLAS LibraryUser GuidecuBLAS | 1 Chapter cuBLAS Library is an implementation of BLAS (Basic Linear Algebra Subprograms) on topof the NVIDIA CUDA runtime. It allows the user to access the computational resources ofNVIDIA Graphics Processing Unit (GPU).The cuBLAS Library exposes three sets of API: The cuBLAS API, which is simply called cuBLAS API in this document (starting with ), The cuBLASXt API (starting with CUDA ), and The cuBLASLt API (starting with CUDA )To use the cuBLAS API, the application must allocate the required matrices and vectors in theGPU memory space, fill them with data, call the sequence of desired cuBLAS functions, andthen upload the results from the GPU memory space back to the host. The cuBLAS API alsoprovides helper functions for writing and retrieving data from the use the cuBLASXt API, the application may have the data on the Host or any of the devicesinvolved in the computation, and the Library will take care of dispatching the operation to, andtransferring the data to, one or multiple GPUs present in the system, depending on the cuBLASLt is a lightweight Library dedicated to GEneral Matrix-to-matrix Multiply (GEMM)operations with a new flexible API.

2 This Library adds flexibility in matrix data layouts, inputtypes, compute types, and also in choosing the algorithmic implementations and heuristicsthrough parameter programmability. After a set of options for the intended GEMM operationare identified by the user, these options can be used repeatedly for different inputs. This isanalogous to how cuFFT and FFTW first create a plan and reuse for same size and type FFTswith different input Data layoutFor maximum compatibility with existing Fortran environments, the cuBLAS Library usescolumn-major storage, and 1-based indexing. Since C and C++ use row-major storage,applications written in these languages can not use the native array semantics for two-dimensional arrays. Instead, macros or inline functions should be defined to implementmatrices on top of one-dimensional arrays.

3 For Fortran code ported to C in mechanicalfashion, one may chose to retain 1-based indexing to avoid the need to transform loops. In thisIntroductioncuBLAS | 2case, the array index of a matrix element in row i and column j can be computed via thefollowing macro#define IDX2F(i,j,ld) ((((j)-1)*(ld))+((i)-1))Here, ld refers to the leading dimension of the matrix, which in the case of column-majorstorage is the number of rows of the allocated matrix (even if only a submatrix of it is beingused). For natively written C and C++ code, one would most likely choose 0-based indexing, inwhich case the array index of a matrix element in row i and column j can be computed viathe following macro#define IDX2C(i,j,ld) (((j)*(ld))+(i)) New and Legacy cuBLAS APIS tarting with version , the cuBLAS Library provides a new API, in addition to the existinglegacy API.

4 This section discusses why a new API is provided, the advantages of using it, andthe differences with the existing legacy new cuBLAS Library API can be used by including the header file . It hasthe following features that the legacy cuBLAS API does not have: The handle to the cuBLAS Library context is initialized using the function and is explicitlypassed to every subsequent Library function call. This allows the user to have more controlover the Library setup when using multiple host threads and multiple GPUs. This alsoallows the cuBLAS APIs to be reentrant. The scalars and can be passed by reference on the host or the device, instead of onlybeing allowed to be passed by value on the host. This change allows Library functions toexecute asynchronously using streams even when and are generated by a previouskernel.

5 When a Library routine returns a scalar result, it can be returned by reference on thehost or the device, instead of only being allowed to be returned by value only on the change allows Library routines to be called asynchronously when the scalar result isgenerated and returned by reference on the device resulting in maximum parallelism. The error status cublasStatus_t is returned by all cuBLAS Library function change facilitates debugging and simplifies software development. Note thatcublasStatus was renamed cublasStatus_t to be more consistent with other types inthe cuBLAS Library . The cublasAlloc() and cublasFree() functions have been deprecated. This changeremoves these unnecessary wrappers around cudaMalloc() and cudaFree(),respectively. The function cublasSetKernelStream() was renamed cublasSetStream() to be moreconsistent with the other CUDA legacy cuBLAS API, explained in more detail in the Appendix A, can be used by includingthe header file.

6 Since the legacy API is identical to the previously released cuBLASI ntroductioncuBLAS | 3library API, existing applications will work out of the box and automatically use this legacy APIwithout any source code general, new applications should not use the legacy cuBLAS API, and existing applicationsshould convert to using the new API if it requires sophisticated and optimal streamparallelism, or if it calls cuBLAS routines concurrently from multiple the rest of the document, the new cuBLAS Library API will simply be referred to as thecuBLAS Library mentioned earlier the interfaces to the legacy and the cuBLAS Library APIs are the headerfile and , respectively. In addition, applications using the cuBLAS Library need to link against: The DSO for Linux, The DLL for Windows, or The dynamic Library for Mac OS : The same dynamic Library implements both the new and legacy cuBLAS Example codeFor sample code references please see the two examples below.

7 They show an applicationwritten in C using the cuBLAS Library API with two indexing styles (Example 1. "ApplicationUsing C and cuBLAS : 1-based indexing" and Example 2. "Application using C and cuBLAS : 0-based Indexing").//Example 1. Application using C and cuBLAS : 1-based indexing//------------------------------ -----------------------------#include < >#include < >#include < >#include < >#include " "#define M 6#define N 5#define IDX2F(i,j,ld) ((((j)-1)*(ld))+((i)-1))static __inline__ void modify (cublasHandle_t handle, float *m, int ldm, int n, int p, int q, float alpha, float beta){ cublasSscal (handle, n-q+1, &alpha, &m[IDX2F(p,q,ldm)], ldm); cublasSscal (handle, ldm-p+1, &beta, &m[IDX2F(p,q,ldm)], 1);}int main (void){ cudaError_t cudaStat; cublasStatus_t stat; cublasHandle_t handle; int i, j; float* devPtrA; float* a = 0; a = (float *)malloc (M * N * sizeof (*a)); if (!)}

8 A) { printf ("host memory allocation failed"); return EXIT_FAILURE;IntroductioncuBLAS | 4 } for (j = 1; j <= N; j++) { for (i = 1; i <= M; i++) { a[IDX2F(i,j,M)] = (float)((i-1) * N + j); } } cudaStat = cudaMalloc ((void**)&devPtrA, M*N*sizeof(*a)); if (cudaStat != cudaSuccess) { printf ("device memory allocation failed"); return EXIT_FAILURE; } stat = cublasCreate( if (stat != CUBLAS_STATUS_SUCCESS) { printf (" cuBLAS initialization failed\n"); return EXIT_FAILURE; } stat = cublasSetMatrix (M, N, sizeof(*a), a, M, devPtrA, M); if (stat != CUBLAS_STATUS_SUCCESS) { printf ("data download failed"); cudaFree (devPtrA); cublasDestroy(handle); return EXIT_FAILURE; } modify (handle, devPtrA, M, N, 2, 3, , ); stat = cublasGetMatrix (M, N, sizeof(*a), devPtrA, M, a, M); if (stat !)

9 = CUBLAS_STATUS_SUCCESS) { printf ("data upload failed"); cudaFree (devPtrA); cublasDestroy(handle); return EXIT_FAILURE; } cudaFree (devPtrA); cublasDestroy(handle); for (j = 1; j <= N; j++) { for (i = 1; i <= M; i++) { printf ("% ", a[IDX2F(i,j,M)]); } printf ("\n"); } free(a); return EXIT_SUCCESS;}----------------------------------------------//Example 2. Application using C and cuBLAS : 0-based indexing//-----------------------------------------------------------#include < >#include < >#include < >#include < >#include " "#define M 6#define N 5#define IDX2C(i,j,ld) (((j)*(ld))+(i))static __inline__ void modify (cublasHandle_t handle, float *m, int ldm, int n, int p, int q, float alpha, float beta){ cublasSscal (handle, n-q, &alpha, &m[IDX2C(p,q,ldm)], ldm); cublasSscal (handle, ldm-p, &beta, &m[IDX2C(p,q,ldm)], 1);}int main (void){ cudaError_t cudaStat; cublasStatus_t stat; cublasHandle_t handle; int i, j;IntroductioncuBLAS | 5 float* devPtrA; float* a = 0; a = (float *)malloc (M * N * sizeof (*a)); if (!

10 A) { printf ("host memory allocation failed"); return EXIT_FAILURE; } for (j = 0; j < N; j++) { for (i = 0; i < M; i++) { a[IDX2C(i,j,M)] = (float)(i * N + j + 1); } } cudaStat = cudaMalloc ((void**)&devPtrA, M*N*sizeof(*a)); if (cudaStat != cudaSuccess) { printf ("device memory allocation failed"); return EXIT_FAILURE; } stat = cublasCreate( if (stat != CUBLAS_STATUS_SUCCESS) { printf (" cuBLAS initialization failed\n"); return EXIT_FAILURE; } stat = cublasSetMatrix (M, N, sizeof(*a), a, M, devPtrA, M); if (stat != CUBLAS_STATUS_SUCCESS) { printf ("data download failed"); cudaFree (devPtrA); cublasDestroy(handle); return EXIT_FAILURE; } modify (handle, devPtrA, M, N, 1, 2, , ); stat = cublasGetMatrix (M, N, sizeof(*a), devPtrA, M, a, M); if (stat !)


Related search queries