Actual source code: petscdevice_cuda.h

  1: #pragma once

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

  6: /* MANSEC = Sys */

  8: #if defined(__NVCC__) || defined(__CUDACC__)
  9:   #define PETSC_USING_NVCC 1
 10: #endif

 12: #if PetscDefined(HAVE_CUDA)
 13:   #include <cuda.h>
 14:   #include <cuda_runtime.h>
 15:   #include <cublas_v2.h>
 16:   #define DISABLE_CUSPARSE_DEPRECATED
 17:   #include <cusparse.h>
 18:   #include <cusolverDn.h>
 19:   #include <cusolverSp.h>
 20:   #include <cufft.h>
 21:   #include <curand.h>
 22:   #if PetscDefined(HAVE_NVML)
 23:     #include <nvml.h> // NVML comes with the NVIDIA GPU driver
 24:   #endif

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

 31:   #define PetscCallCUDAVoid(...) \
 32:     do { \
 33:       const cudaError_t _p_cuda_err__ = __VA_ARGS__; \
 34:       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__)); \
 35:     } while (0)

 37:   #define PetscCallCUDA(...) \
 38:     do { \
 39:       const cudaError_t _p_cuda_err__ = __VA_ARGS__; \
 40:       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__)); \
 41:     } while (0)
 42:   #define CHKERRCUDA(...) PetscCallCUDA(__VA_ARGS__)

 44:   #define PetscCUDACheckLaunch \
 45:     do { \
 46:       /* Check synchronous errors, i.e. pre-launch */ \
 47:       PetscCallCUDA(cudaGetLastError()); \
 48:       /* Check asynchronous errors, i.e. kernel failed (ULF) */ \
 49:       PetscCallCUDA(cudaDeviceSynchronize()); \
 50:     } while (0)

 52:   #define PetscCallCUBLAS(...) \
 53:     do { \
 54:       const cublasStatus_t _p_cublas_stat__ = __VA_ARGS__; \
 55:       if (PetscUnlikely(_p_cublas_stat__ != CUBLAS_STATUS_SUCCESS)) { \
 56:         const char *name = PetscCUBLASGetErrorName(_p_cublas_stat__); \
 57:         if ((_p_cublas_stat__ == CUBLAS_STATUS_NOT_INITIALIZED || _p_cublas_stat__ == CUBLAS_STATUS_ALLOC_FAILED) && PetscDeviceInitialized(PETSC_DEVICE_CUDA)) { \
 58:           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_GPU_RESOURCE, \
 59:                   "cuBLAS error %d (%s). " \
 60:                   "Reports not initialized or alloc failed; " \
 61:                   "this indicates the GPU may have run out resources", \
 62:                   (PetscErrorCode)_p_cublas_stat__, name); \
 63:         } else { \
 64:           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_GPU, "cuBLAS error %d (%s)", (PetscErrorCode)_p_cublas_stat__, name); \
 65:         } \
 66:       } \
 67:     } while (0)
 68:   #define CHKERRCUBLAS(...) PetscCallCUBLAS(__VA_ARGS__)

 70:   #define PetscCallCUSPARSE(...) \
 71:     do { \
 72:       const cusparseStatus_t _p_cusparse_stat__ = __VA_ARGS__; \
 73:       if (PetscUnlikely(_p_cusparse_stat__)) { \
 74:         const char *name  = cusparseGetErrorName(_p_cusparse_stat__); \
 75:         const char *descr = cusparseGetErrorString(_p_cusparse_stat__); \
 76:         PetscCheck((_p_cusparse_stat__ != CUSPARSE_STATUS_NOT_INITIALIZED) && (_p_cusparse_stat__ != CUSPARSE_STATUS_ALLOC_FAILED), PETSC_COMM_SELF, PETSC_ERR_GPU_RESOURCE, \
 77:                    "cuSPARSE errorcode %d (%s) : %s.; " \
 78:                    "this indicates the GPU has run out resources", \
 79:                    (int)_p_cusparse_stat__, name, descr); \
 80:         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_GPU, "cuSPARSE errorcode %d (%s) : %s", (int)_p_cusparse_stat__, name, descr); \
 81:       } \
 82:     } while (0)
 83:   #define CHKERRCUSPARSE(...) PetscCallCUSPARSE(__VA_ARGS__)

 85:   #define PetscCallCUSOLVER(...) \
 86:     do { \
 87:       const cusolverStatus_t _p_cusolver_stat__ = __VA_ARGS__; \
 88:       if (PetscUnlikely(_p_cusolver_stat__ != CUSOLVER_STATUS_SUCCESS)) { \
 89:         const char *name = PetscCUSolverGetErrorName(_p_cusolver_stat__); \
 90:         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)) { \
 91:           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_GPU_RESOURCE, \
 92:                   "cuSolver error %d (%s). " \
 93:                   "This indicates the GPU may have run out resources", \
 94:                   (PetscErrorCode)_p_cusolver_stat__, name); \
 95:         } else { \
 96:           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_GPU, "cuSolver error %d (%s)", (PetscErrorCode)_p_cusolver_stat__, name); \
 97:         } \
 98:       } \
 99:     } while (0)
100:   #define CHKERRCUSOLVER(...) PetscCallCUSOLVER(__VA_ARGS__)

102:   #define PetscCallCUFFT(...) \
103:     do { \
104:       const cufftResult_t _p_cufft_stat__ = __VA_ARGS__; \
105:       if (PetscUnlikely(_p_cufft_stat__ != CUFFT_SUCCESS)) { \
106:         const char *name = PetscCUFFTGetErrorName(_p_cufft_stat__); \
107:         if ((_p_cufft_stat__ == CUFFT_SETUP_FAILED || _p_cufft_stat__ == CUFFT_ALLOC_FAILED) && PetscDeviceInitialized(PETSC_DEVICE_CUDA)) { \
108:           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_GPU_RESOURCE, \
109:                   "cuFFT error %d (%s). " \
110:                   "Reports not initialized or alloc failed; " \
111:                   "this indicates the GPU has run out resources", \
112:                   (PetscErrorCode)_p_cufft_stat__, name); \
113:         } else { \
114:           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_GPU, "cuFFT error %d (%s)", (PetscErrorCode)_p_cufft_stat__, name); \
115:         } \
116:       } \
117:     } while (0)
118:   #define CHKERRCUFFT(...) PetscCallCUFFT(__VA_ARGS__)

120:   #define PetscCallCURAND(...) \
121:     do { \
122:       const curandStatus_t _p_curand_stat__ = __VA_ARGS__; \
123:       if (PetscUnlikely(_p_curand_stat__ != CURAND_STATUS_SUCCESS)) { \
124:         if ((_p_curand_stat__ == CURAND_STATUS_INITIALIZATION_FAILED || _p_curand_stat__ == CURAND_STATUS_ALLOCATION_FAILED) && PetscDeviceInitialized(PETSC_DEVICE_CUDA)) { \
125:           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_GPU_RESOURCE, \
126:                   "cuRAND error %d. " \
127:                   "Reports not initialized or alloc failed; " \
128:                   "this indicates the GPU has run out resources", \
129:                   (PetscErrorCode)_p_curand_stat__); \
130:         } else { \
131:           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_GPU, "cuRand error %d", (PetscErrorCode)_p_curand_stat__); \
132:         } \
133:       } \
134:     } while (0)
135:   #define CHKERRCURAND(...) PetscCallCURAND(__VA_ARGS__)

137: PETSC_EXTERN PetscErrorCode PetscCUBLASGetHandle(cublasHandle_t *);
138: PETSC_EXTERN PetscErrorCode PetscCUSOLVERDnGetHandle(cusolverDnHandle_t *);
139: PETSC_EXTERN PetscErrorCode PetscGetCurrentCUDAStream(cudaStream_t *);

141: #endif // PETSC_HAVE_CUDA

143: // these can also be defined in petscdevice_hip.h so we undef and define them *only* if the
144: // current compiler is NVCC. In this case if petscdevice_hip.h is included first, the macros
145: // would already be defined, but they would be empty since we cannot be using HCC at the same
146: // time.
147: #if PetscDefined(USING_NVCC)
148:   #undef PETSC_HOST_DECL
149:   #undef PETSC_DEVICE_DECL
150:   #undef PETSC_KERNEL_DECL
151:   #undef PETSC_SHAREDMEM_DECL
152:   #undef PETSC_FORCEINLINE
153:   #undef PETSC_CONSTMEM_DECL

155:   #define PETSC_HOST_DECL      __host__
156:   #define PETSC_DEVICE_DECL    __device__
157:   #define PETSC_KERNEL_DECL    __global__
158:   #define PETSC_SHAREDMEM_DECL __shared__
159:   #define PETSC_FORCEINLINE    __forceinline__
160:   #define PETSC_CONSTMEM_DECL  __constant__
161: #endif

163: #if !defined(PETSC_HOST_DECL) // use HOST_DECL as canary
164:   #define PETSC_HOST_DECL
165:   #define PETSC_DEVICE_DECL
166:   #define PETSC_KERNEL_DECL
167:   #define PETSC_SHAREDMEM_DECL
168:   #define PETSC_FORCEINLINE inline
169:   #define PETSC_CONSTMEM_DECL
170: #endif

172: #if !PetscDefined(DEVICE_DEFINED_DECLS_PRIVATE)
173:   #define PETSC_DEVICE_DEFINED_DECLS_PRIVATE
174:   #define PETSC_HOSTDEVICE_DECL        PETSC_HOST_DECL PETSC_DEVICE_DECL
175:   #define PETSC_DEVICE_INLINE_DECL     PETSC_DEVICE_DECL PETSC_FORCEINLINE
176:   #define PETSC_HOSTDEVICE_INLINE_DECL PETSC_HOSTDEVICE_DECL PETSC_FORCEINLINE
177: #endif