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 #endif
88 
89 #ifdef AMREX_USE_HIP
90 #define AMREX_HIP_SAFE_CALL(call) { \
91  hipError_t amrex_i_err = call; \
92  if (hipSuccess != amrex_i_err) { \
93  std::string errStr(std::string("HIP error in file ") + __FILE__ \
94  + " line " + std::to_string(__LINE__) \
95  + " " + hipGetErrorString(amrex_i_err)); \
96  amrex::Abort(errStr); \
97  }}
98 
99 #define AMREX_HIPRAND_SAFE_CALL(x) do { if((x)!=HIPRAND_STATUS_SUCCESS) { \
100  std::string errStr(std::string("HIPRAND error in file ") + __FILE__ \
101  + " line " + std::to_string(__LINE__)); \
102  amrex::Abort(errStr); }} while(0)
103 #endif
104 
105 #define AMREX_GPU_ERROR_CHECK() amrex::Gpu::ErrorCheck(__FILE__, __LINE__)
106 
107 #else
108 
109 #define AMREX_GPU_SAFE_CALL(call) (call)
110 #define AMREX_GPU_ERROR_CHECK() ((void)0)
111 
112 #endif
113 
114 #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:221
constexpr gpuError_t gpuSuccess
Definition: AMReX_GpuError.H:34