Actual source code: petscdevice_cuda.h

  1: #pragma once

  3: #include <petscdevice.h>
  4: #include <petscpkg_version.h>

  6: #if defined(__NVCC__) || defined(__CUDACC__)
  7:   #define PETSC_USING_NVCC 1
  8: #endif

 10: #if PetscDefined(HAVE_CUDA)
 11:   #include <cuda.h>
 12:   #include <cuda_runtime.h>
 13:   #include <cublas_v2.h>
 14:   #include <cusolverDn.h>
 15:   #include <cusolverSp.h>
 16:   #include <cufft.h>

 18: /* cuBLAS does not have cublasGetErrorName(). We create one on our own. */
 19: PETSC_EXTERN const char *PetscCUBLASGetErrorName(cublasStatus_t); /* PETSC_EXTERN since it is exposed by the CHKERRCUBLAS macro */
 20: PETSC_EXTERN const char *PetscCUSolverGetErrorName(cusolverStatus_t);
 21: PETSC_EXTERN const char *PetscCUFFTGetErrorName(cufftResult);

 23:   /* REMOVE ME */
 24:   #define WaitForCUDA() cudaDeviceSynchronize()

 26:   /* CUDART_VERSION = 1000 x major + 10 x minor version */

 28:   /* Could not find exactly which CUDART_VERSION introduced cudaGetErrorName. At least it was in CUDA 8.0 (Sep. 2016) */
 29:   #if PETSC_PKG_CUDA_VERSION_GE(8, 0, 0)
 30:     #define PetscCallCUDAVoid(...) \
 31:       do { \
 32:         const cudaError_t _p_cuda_err__ = __VA_ARGS__; \
 33:         PetscCheckAbort(_p_cuda_err__ == cudaSuccess, PETSC_COMM_SELF, PETSC_ERR_GPU, "cuda error %d (%s) : %s", (PetscErrorCode)_p_cuda_err__, cudaGetErrorName(_p_cuda_err__), cudaGetErrorString(_p_cuda_err__)); \
 34:       } while (0)

 36:     #define PetscCallCUDA(...) \
 37:       do { \
 38:         const cudaError_t _p_cuda_err__ = __VA_ARGS__; \
 39:         PetscCheck(_p_cuda_err__ == cudaSuccess, PETSC_COMM_SELF, PETSC_ERR_GPU, "cuda error %d (%s) : %s", (PetscErrorCode)_p_cuda_err__, cudaGetErrorName(_p_cuda_err__), cudaGetErrorString(_p_cuda_err__)); \
 40:       } while (0)
 41:   #else /* PETSC_PKG_CUDA_VERSION_GE(8,0,0) */
 42:     #define PetscCallCUDA(...) \
 43:       do { \
 44:         const cudaError_t _p_cuda_err__ = __VA_ARGS__; \
 45:         PetscCheck(_p_cuda_err__ == cudaSuccess, PETSC_COMM_SELF, PETSC_ERR_GPU, "cuda error %d", (PetscErrorCode)_p_cuda_err__); \
 46:       } while (0)

 48:     #define PetscCallCUDAVoid(...) \
 49:       do { \
 50:         const cudaError_t _p_cuda_err__ = __VA_ARGS__; \
 51:         PetscCheckAbort(_p_cuda_err__ == cudaSuccess, PETSC_COMM_SELF, PETSC_ERR_GPU, "cuda error %d", (PetscErrorCode)_p_cuda_err__); \
 52:       } while (0)
 53:   #endif /* PETSC_PKG_CUDA_VERSION_GE(8,0,0) */
 54:   #define CHKERRCUDA(...) PetscCallCUDA(__VA_ARGS__)

 56:   #define PetscCUDACheckLaunch \
 57:     do { \
 58:       /* Check synchronous errors, i.e. pre-launch */ \
 59:       PetscCallCUDA(cudaGetLastError()); \
 60:       /* Check asynchronous errors, i.e. kernel failed (ULF) */ \
 61:       PetscCallCUDA(cudaDeviceSynchronize()); \
 62:     } while (0)

 64:   #define PetscCallCUBLAS(...) \
 65:     do { \
 66:       const cublasStatus_t _p_cublas_stat__ = __VA_ARGS__; \
 67:       if (PetscUnlikely(_p_cublas_stat__ != CUBLAS_STATUS_SUCCESS)) { \
 68:         const char *name = PetscCUBLASGetErrorName(_p_cublas_stat__); \
 69:         if (((_p_cublas_stat__ == CUBLAS_STATUS_NOT_INITIALIZED) || (_p_cublas_stat__ == CUBLAS_STATUS_ALLOC_FAILED)) && PetscDeviceInitialized(PETSC_DEVICE_CUDA)) { \
 70:           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_GPU_RESOURCE, \
 71:                   "cuBLAS error %d (%s). " \
 72:                   "Reports not initialized or alloc failed; " \
 73:                   "this indicates the GPU may have run out resources", \
 74:                   (PetscErrorCode)_p_cublas_stat__, name); \
 75:         } else { \
 76:           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_GPU, "cuBLAS error %d (%s)", (PetscErrorCode)_p_cublas_stat__, name); \
 77:         } \
 78:       } \
 79:     } while (0)
 80:   #define CHKERRCUBLAS(...) PetscCallCUBLAS(__VA_ARGS__)

 82:   #if (CUSPARSE_VER_MAJOR > 10 || CUSPARSE_VER_MAJOR == 10 && CUSPARSE_VER_MINOR >= 2) /* According to cuda/10.1.168 on OLCF Summit */
 83:     #define PetscCallCUSPARSE(...) \
 84:       do { \
 85:         const cusparseStatus_t _p_cusparse_stat__ = __VA_ARGS__; \
 86:         if (PetscUnlikely(_p_cusparse_stat__)) { \
 87:           const char *name  = cusparseGetErrorName(_p_cusparse_stat__); \
 88:           const char *descr = cusparseGetErrorString(_p_cusparse_stat__); \
 89:           PetscCheck((_p_cusparse_stat__ != CUSPARSE_STATUS_NOT_INITIALIZED) && (_p_cusparse_stat__ != CUSPARSE_STATUS_ALLOC_FAILED), PETSC_COMM_SELF, PETSC_ERR_GPU_RESOURCE, \
 90:                      "cuSPARSE errorcode %d (%s) : %s.; " \
 91:                      "this indicates the GPU has run out resources", \
 92:                      (int)_p_cusparse_stat__, name, descr); \
 93:           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_GPU, "cuSPARSE errorcode %d (%s) : %s", (int)_p_cusparse_stat__, name, descr); \
 94:         } \
 95:       } while (0)
 96:   #else /* (CUSPARSE_VER_MAJOR > 10 || CUSPARSE_VER_MAJOR == 10 && CUSPARSE_VER_MINOR >= 2) */
 97:     #define PetscCallCUSPARSE(...) \
 98:       do { \
 99:         const cusparseStatus_t _p_cusparse_stat__ = __VA_ARGS__; \
100:         PetscCheck(_p_cusparse_stat__ == CUSPARSE_STATUS_SUCCESS, PETSC_COMM_SELF, PETSC_ERR_GPU, "cuSPARSE errorcode %d", (PetscErrorCode)_p_cusparse_stat__); \
101:       } while (0)
102:   #endif /* (CUSPARSE_VER_MAJOR > 10 || CUSPARSE_VER_MAJOR == 10 && CUSPARSE_VER_MINOR >= 2) */
103:   #define CHKERRCUSPARSE(...) PetscCallCUSPARSE(__VA_ARGS__)

105:   #define PetscCallCUSOLVER(...) \
106:     do { \
107:       const cusolverStatus_t _p_cusolver_stat__ = __VA_ARGS__; \
108:       if (PetscUnlikely(_p_cusolver_stat__ != CUSOLVER_STATUS_SUCCESS)) { \
109:         const char *name = PetscCUSolverGetErrorName(_p_cusolver_stat__); \
110:         if (((_p_cusolver_stat__ == CUSOLVER_STATUS_NOT_INITIALIZED) || (_p_cusolver_stat__ == CUSOLVER_STATUS_ALLOC_FAILED) || (_p_cusolver_stat__ == CUSOLVER_STATUS_INTERNAL_ERROR)) && PetscDeviceInitialized(PETSC_DEVICE_CUDA)) { \
111:           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_GPU_RESOURCE, \
112:                   "cuSolver error %d (%s). " \
113:                   "This indicates the GPU may have run out resources", \
114:                   (PetscErrorCode)_p_cusolver_stat__, name); \
115:         } else { \
116:           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_GPU, "cuSolver error %d (%s)", (PetscErrorCode)_p_cusolver_stat__, name); \
117:         } \
118:       } \
119:     } while (0)
120:   #define CHKERRCUSOLVER(...) PetscCallCUSOLVER(__VA_ARGS__)

122:   #define PetscCallCUFFT(...) \
123:     do { \
124:       const cufftResult_t _p_cufft_stat__ = __VA_ARGS__; \
125:       if (PetscUnlikely(_p_cufft_stat__ != CUFFT_SUCCESS)) { \
126:         const char *name = PetscCUFFTGetErrorName(_p_cufft_stat__); \
127:         if (((_p_cufft_stat__ == CUFFT_SETUP_FAILED) || (_p_cufft_stat__ == CUFFT_ALLOC_FAILED)) && PetscDeviceInitialized(PETSC_DEVICE_CUDA)) { \
128:           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_GPU_RESOURCE, \
129:                   "cuFFT error %d (%s). " \
130:                   "Reports not initialized or alloc failed; " \
131:                   "this indicates the GPU has run out resources", \
132:                   (PetscErrorCode)_p_cufft_stat__, name); \
133:         } else { \
134:           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_GPU, "cuFFT error %d (%s)", (PetscErrorCode)_p_cufft_stat__, name); \
135:         } \
136:       } \
137:     } while (0)
138:   #define CHKERRCUFFT(...) PetscCallCUFFT(__VA_ARGS__)

140:   #define PetscCallCURAND(...) \
141:     do { \
142:       const curandStatus_t _p_curand_stat__ = __VA_ARGS__; \
143:       if (PetscUnlikely(_p_curand_stat__ != CURAND_STATUS_SUCCESS)) { \
144:         if (((_p_curand_stat__ == CURAND_STATUS_INITIALIZATION_FAILED) || (_p_curand_stat__ == CURAND_STATUS_ALLOCATION_FAILED)) && PetscDeviceInitialized(PETSC_DEVICE_CUDA)) { \
145:           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_GPU_RESOURCE, \
146:                   "cuRAND error %d. " \
147:                   "Reports not initialized or alloc failed; " \
148:                   "this indicates the GPU has run out resources", \
149:                   (PetscErrorCode)_p_curand_stat__); \
150:         } else { \
151:           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_GPU, "cuRand error %d", (PetscErrorCode)_p_curand_stat__); \
152:         } \
153:       } \
154:     } while (0)
155:   #define CHKERRCURAND(...) PetscCallCURAND(__VA_ARGS__)

157: PETSC_EXTERN cudaStream_t   PetscDefaultCudaStream; // The default stream used by PETSc
158: PETSC_EXTERN PetscErrorCode PetscCUBLASGetHandle(cublasHandle_t *);
159: PETSC_EXTERN PetscErrorCode PetscCUSOLVERDnGetHandle(cusolverDnHandle_t *);
160: PETSC_EXTERN PetscErrorCode PetscGetCurrentCUDAStream(cudaStream_t *);

162: #endif // PETSC_HAVE_CUDA

164: // these can also be defined in petscdevice_hip.h so we undef and define them *only* if the
165: // current compiler is NVCC. In this case if petscdevice_hip.h is included first, the macros
166: // would already be defined, but they would be empty since we cannot be using HCC at the same
167: // time.
168: #if PetscDefined(USING_NVCC)
169:   #undef PETSC_HOST_DECL
170:   #undef PETSC_DEVICE_DECL
171:   #undef PETSC_KERNEL_DECL
172:   #undef PETSC_SHAREDMEM_DECL
173:   #undef PETSC_FORCEINLINE
174:   #undef PETSC_CONSTMEM_DECL

176:   #define PETSC_HOST_DECL      __host__
177:   #define PETSC_DEVICE_DECL    __device__
178:   #define PETSC_KERNEL_DECL    __global__
179:   #define PETSC_SHAREDMEM_DECL __shared__
180:   #define PETSC_FORCEINLINE    __forceinline__
181:   #define PETSC_CONSTMEM_DECL  __constant__
182: #endif

184: #ifndef PETSC_HOST_DECL // use HOST_DECL as canary
185:   #define PETSC_HOST_DECL
186:   #define PETSC_DEVICE_DECL
187:   #define PETSC_KERNEL_DECL
188:   #define PETSC_SHAREDMEM_DECL
189:   #define PETSC_FORCEINLINE inline
190:   #define PETSC_CONSTMEM_DECL
191: #endif

193: #ifndef PETSC_DEVICE_DEFINED_DECLS_PRIVATE
194:   #define PETSC_DEVICE_DEFINED_DECLS_PRIVATE
195:   #define PETSC_HOSTDEVICE_DECL        PETSC_HOST_DECL PETSC_DEVICE_DECL
196:   #define PETSC_DEVICE_INLINE_DECL     PETSC_DEVICE_DECL PETSC_FORCEINLINE
197:   #define PETSC_HOSTDEVICE_INLINE_DECL PETSC_HOSTDEVICE_DECL PETSC_FORCEINLINE
198: #endif