Block-Structured AMR Software Framework
AMReX_GpuError.H
Go to the documentation of this file.
1 #ifndef AMREX_GPU_ERROR_H_
2 #define AMREX_GPU_ERROR_H_
3 #include <AMReX_Config.H>
4 
5 #include <AMReX_Utility.H>
6 #include <string>
7 
8 #ifdef AMREX_USE_HIP
9 #include <hip/hip_runtime.h>
10 #endif
11 
12 #if defined(AMREX_USE_GPU) && !defined(AMREX_GPU_NO_ERROR_CHECK) && !defined(AMREX_USE_SYCL)
13 
14 namespace amrex {
15 
16 #ifdef AMREX_USE_HIP
17 
18 using gpuError_t = hipError_t;
19 constexpr gpuError_t gpuSuccess = hipSuccess;
20 
21 inline gpuError_t
22 gpuGetLastError () {
23  return hipGetLastError();
24 }
25 
26 inline const char*
28  return hipGetErrorString(error);
29 }
30 
31 #else
32 
33 using gpuError_t = cudaError_t;
34 constexpr gpuError_t gpuSuccess = cudaSuccess;
35 
36 inline gpuError_t
38  return cudaGetLastError();
39 }
40 
41 inline const char*
43  return cudaGetErrorString(error);
44 }
45 
46 #endif
47 
48 namespace Gpu {
49 
50  inline void ErrorCheck (const char* file, int line) noexcept
51  {
53  if (gpuSuccess != err) {
54  std::string errStr(std::string("GPU last error detected in file ") + file
55  + " line " + std::to_string(line)
56  + ": " + std::string(gpuGetErrorString(err)));
57  amrex::Abort(errStr);
58  }
59  }
60 }
61 }
62 
63 #define AMREX_GPU_SAFE_CALL(call) { \
64  amrex::gpuError_t amrex_i_err = call; \
65  if (amrex::gpuSuccess != amrex_i_err) { \
66  std::string errStr(std::string("GPU error in file ") + __FILE__ \
67  + " line " + std::to_string(__LINE__) \
68  + " " + amrex::gpuGetErrorString(amrex_i_err)); \
69  amrex::Abort(errStr); \
70  }}
71 
72 #ifdef AMREX_USE_CUDA
73 #define AMREX_CUDA_SAFE_CALL(call) { \
74  cudaError_t amrex_i_err = call; \
75  if (cudaSuccess != amrex_i_err) { \
76  std::string errStr(std::string("CUDA error ") + std::to_string(amrex_i_err) \
77  + std::string(" in file ") + __FILE__ \
78  + " line " + std::to_string(__LINE__) \
79  + ": " + cudaGetErrorString(amrex_i_err)); \
80  amrex::Abort(errStr); \
81  }}
82 
83 #define AMREX_CURAND_SAFE_CALL(x) do { if((x)!=CURAND_STATUS_SUCCESS) { \
84  std::string errStr(std::string("CURAND error in file ") + __FILE__ \
85  + " line " + std::to_string(__LINE__)); \
86  amrex::Abort(errStr); }} while(0)
87 
88 #define AMREX_CUFFT_SAFE_CALL(call) { \
89  cufftResult_t amrex_i_err = call; \
90  if (CUFFT_SUCCESS != amrex_i_err) { \
91  std::string errStr(std::string("CUFFT error ")+std::to_string(amrex_i_err) \
92  + std::string(" in file ") + __FILE__ \
93  + " line " + std::to_string(__LINE__)); \
94  amrex::Abort(errStr); \
95  }}
96 
97 #endif
98 
99 #ifdef AMREX_USE_HIP
100 #define AMREX_HIP_SAFE_CALL(call) { \
101  hipError_t amrex_i_err = call; \
102  if (hipSuccess != amrex_i_err) { \
103  std::string errStr(std::string("HIP error in file ") + __FILE__ \
104  + " line " + std::to_string(__LINE__) \
105  + " " + hipGetErrorString(amrex_i_err)); \
106  amrex::Abort(errStr); \
107  }}
108 
109 #define AMREX_HIPRAND_SAFE_CALL(x) do { if((x)!=HIPRAND_STATUS_SUCCESS) { \
110  std::string errStr(std::string("HIPRAND error in file ") + __FILE__ \
111  + " line " + std::to_string(__LINE__)); \
112  amrex::Abort(errStr); }} while(0)
113 
114 #define AMREX_ROCFFT_SAFE_CALL(call) { \
115  auto amrex_i_err = call; \
116  if (rocfft_status_success != amrex_i_err) { \
117  std::string errStr(std::string("rocFFT error ")+std::to_string(amrex_i_err) \
118  + std::string(" in file ") + __FILE__ \
119  + " line " + std::to_string(__LINE__)); \
120  amrex::Abort(errStr); \
121  }}
122 
123 #endif
124 
125 #define AMREX_GPU_ERROR_CHECK() amrex::Gpu::ErrorCheck(__FILE__, __LINE__)
126 
127 #else
128 
129 #define AMREX_GPU_SAFE_CALL(call) (call)
130 #define AMREX_GPU_ERROR_CHECK() ((void)0)
131 
132 #endif
133 
134 #endif
void ErrorCheck(const char *file, int line) noexcept
Definition: AMReX_GpuError.H:50
Definition: AMReX_Amr.cpp:49
const char * gpuGetErrorString(gpuError_t error)
Definition: AMReX_GpuError.H:42
gpuError_t gpuGetLastError()
Definition: AMReX_GpuError.H:37
cudaError_t gpuError_t
Definition: AMReX_GpuError.H:33
void Abort(const std::string &msg)
Print out message to cerr and exit via abort().
Definition: AMReX.cpp:225
constexpr gpuError_t gpuSuccess
Definition: AMReX_GpuError.H:34