Actual source code: petscdevice_hip.h
1: #pragma once
3: #include <petscdevice.h>
4: #include <petscpkg_version.h>
6: /* MANSEC = Sys */
7: /* SUBMANSEC = Device */
9: #if defined(__HCC__) || (defined(__clang__) && defined(__HIP__))
10: #define PETSC_USING_HCC 1
11: #endif
13: #if PetscDefined(HAVE_HIP)
14: #include <hip/hip_runtime.h>
16: // Use HIP complex types in hipBLAS to match cupmScalarPtrCast().
17: #if PETSC_PKG_HIP_VERSION_GE(6, 0, 0)
18: #define HIPBLAS_V2
19: #else
20: #define ROCM_MATHLIBS_API_USE_HIP_COMPLEX
21: #endif
22: #include <hipblas/hipblas.h>
23: #include <hipsparse/hipsparse.h>
25: #if PETSC_PKG_HIP_VERSION_LT(5, 4, 0)
26: #define HIPSPARSE_ORDER_COL HIPSPARSE_ORDER_COLUMN
27: #endif
29: #include <hipsolver/hipsolver.h>
30: #include <hip/hip_complex.h> // for hipComplex, hipDoubleComplex
32: /* hipBLAS, hipSPARSE and hipSolver does not have hip*GetErrorName(). We create one on our own. */
33: PETSC_EXTERN const char *PetscHIPBLASGetErrorName(hipblasStatus_t); /* PETSC_EXTERN since it is exposed by the CHKERRHIPBLAS macro */
34: PETSC_EXTERN const char *PetscHIPSPARSEGetErrorName(hipsparseStatus_t); /* PETSC_EXTERN since it is exposed by the CHKERRHIPSPARSE macro */
35: PETSC_EXTERN const char *PetscHIPSolverGetErrorName(hipsolverStatus_t); /* PETSC_EXTERN since it is exposed by the CHKERRHIPSOLVER macro */
37: #define PetscCallHIP(...) \
38: do { \
39: const hipError_t _p_hip_err__ = __VA_ARGS__; \
40: if (PetscUnlikely(_p_hip_err__ != hipSuccess)) { \
41: const char *name = hipGetErrorName(_p_hip_err__); \
42: const char *descr = hipGetErrorString(_p_hip_err__); \
43: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_GPU, "hip error %d (%s) : %s", (PetscErrorCode)_p_hip_err__, name, descr); \
44: } \
45: } while (0)
46: #define CHKERRHIP(...) PetscCallHIP(__VA_ARGS__)
48: #define PetscHIPCheckLaunch \
49: do { \
50: /* Check synchronous errors, i.e. pre-launch */ \
51: PetscCallHIP(hipGetLastError()); \
52: /* Check asynchronous errors, i.e. kernel failed (ULF) */ \
53: PetscCallHIP(hipDeviceSynchronize()); \
54: } while (0)
56: #define PetscCallHIPBLAS(...) \
57: do { \
58: const hipblasStatus_t _p_hipblas_stat__ = __VA_ARGS__; \
59: if (PetscUnlikely(_p_hipblas_stat__ != HIPBLAS_STATUS_SUCCESS)) { \
60: const char *name = PetscHIPBLASGetErrorName(_p_hipblas_stat__); \
61: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_GPU, "hipBLAS error %d (%s)", (PetscErrorCode)_p_hipblas_stat__, name); \
62: } \
63: } while (0)
64: #define CHKERRHIPBLAS(...) PetscCallHIPBLAS(__VA_ARGS__)
66: #define PetscCallHIPSPARSE(...) \
67: do { \
68: const hipsparseStatus_t _p_hipsparse_stat__ = __VA_ARGS__; \
69: if (PetscUnlikely(_p_hipsparse_stat__ != HIPSPARSE_STATUS_SUCCESS)) { \
70: const char *name = PetscHIPSPARSEGetErrorName(_p_hipsparse_stat__); \
71: PetscCheck((_p_hipsparse_stat__ != HIPSPARSE_STATUS_NOT_INITIALIZED) && (_p_hipsparse_stat__ != HIPSPARSE_STATUS_ALLOC_FAILED), PETSC_COMM_SELF, PETSC_ERR_GPU_RESOURCE, "hipSPARSE errorcode %d (%s): Reports not initialized or alloc failed; this indicates the GPU has run out resources", (int)_p_hipsparse_stat__, name); \
72: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_GPU, "hipSPARSE errorcode %d (%s)", (int)_p_hipsparse_stat__, name); \
73: } \
74: } while (0)
75: #define CHKERRHIPSPARSE(...) PetscCallHIPSPARSE(__VA_ARGS__)
77: #define PetscCallHIPSOLVER(...) \
78: do { \
79: const hipsolverStatus_t _p_hipsolver_stat__ = __VA_ARGS__; \
80: if (PetscUnlikely(_p_hipsolver_stat__ != HIPSOLVER_STATUS_SUCCESS)) { \
81: const char *name = PetscHIPSolverGetErrorName(_p_hipsolver_stat__); \
82: if ((_p_hipsolver_stat__ == HIPSOLVER_STATUS_NOT_INITIALIZED || _p_hipsolver_stat__ == HIPSOLVER_STATUS_ALLOC_FAILED || _p_hipsolver_stat__ == HIPSOLVER_STATUS_INTERNAL_ERROR) && PetscDeviceInitialized(PETSC_DEVICE_HIP)) { \
83: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_GPU_RESOURCE, \
84: "hipSolver error %d (%s). " \
85: "This indicates the GPU may have run out resources", \
86: (PetscErrorCode)_p_hipsolver_stat__, name); \
87: } else { \
88: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_GPU, "hipSolver error %d (%s)", (PetscErrorCode)_p_hipsolver_stat__, name); \
89: } \
90: } \
91: } while (0)
92: #define CHKERRHIPSOLVER(...) PetscCallHIPSOLVER(__VA_ARGS__)
94: PETSC_EXTERN PetscErrorCode PetscHIPBLASGetHandle(hipblasHandle_t *);
95: PETSC_EXTERN PetscErrorCode PetscHIPSOLVERGetHandle(hipsolverHandle_t *);
96: PETSC_EXTERN PetscErrorCode PetscGetCurrentHIPStream(hipStream_t *);
98: #endif // PETSC_HAVE_HIP
100: // these can also be defined in petscdevice_cuda.h so we undef and define them *only* if the
101: // current compiler is HCC. In this case if petscdevice_cuda.h is included first, the macros
102: // would already be defined, but they would be empty since we cannot be using NVCC at the same
103: // time.
104: #if PetscDefined(USING_HCC)
105: #undef PETSC_HOST_DECL
106: #undef PETSC_DEVICE_DECL
107: #undef PETSC_KERNEL_DECL
108: #undef PETSC_SHAREDMEM_DECL
109: #undef PETSC_FORCEINLINE
110: #undef PETSC_CONSTMEM_DECL
112: #define PETSC_HOST_DECL __host__
113: #define PETSC_DEVICE_DECL __device__
114: #define PETSC_KERNEL_DECL __global__
115: #define PETSC_SHAREDMEM_DECL __shared__
116: #define PETSC_FORCEINLINE __forceinline__
117: #define PETSC_CONSTMEM_DECL __constant__
118: #endif
120: #if !defined(PETSC_HOST_DECL) // use HOST_DECL as canary
121: #define PETSC_HOST_DECL
122: #define PETSC_DEVICE_DECL
123: #define PETSC_KERNEL_DECL
124: #define PETSC_SHAREDMEM_DECL
125: #define PETSC_FORCEINLINE inline
126: #define PETSC_CONSTMEM_DECL
127: #endif
129: #if !PetscDefined(DEVICE_DEFINED_DECLS_PRIVATE)
130: #define PETSC_DEVICE_DEFINED_DECLS_PRIVATE
131: #define PETSC_HOSTDEVICE_DECL PETSC_HOST_DECL PETSC_DEVICE_DECL
132: #define PETSC_DEVICE_INLINE_DECL PETSC_DEVICE_DECL PETSC_FORCEINLINE
133: #define PETSC_HOSTDEVICE_INLINE_DECL PETSC_HOSTDEVICE_DECL PETSC_FORCEINLINE
134: #endif