CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
cudaCheck.h
Go to the documentation of this file.
1 #ifndef HeterogeneousCore_CUDAUtilities_cudaCheck_h
2 #define HeterogeneousCore_CUDAUtilities_cudaCheck_h
3 
4 // C++ standard headers
5 #include <iostream>
6 #include <sstream>
7 #include <stdexcept>
8 #include <string>
9 #include <string_view>
10 
11 // CUDA headers
12 #include <cuda.h>
13 #include <cuda_runtime.h>
14 
15 // CMSSW headers
17 
18 namespace cms {
19  namespace cuda {
20 
21  [[noreturn]] inline void abortOnCudaError(const char* file,
22  int line,
23  const char* cmd,
24  const char* error,
25  const char* message,
26  std::string_view description = std::string_view()) {
27  std::ostringstream out;
28  out << "\n";
29  out << file << ", line " << line << ":\n";
30  out << "cudaCheck(" << cmd << ");\n";
31  out << error << ": " << message << "\n";
32  if (!description.empty())
33  out << description << "\n";
34  throw std::runtime_error(out.str());
35  }
36 
37  inline bool cudaCheck_(const char* file,
38  int line,
39  const char* cmd,
40  CUresult result,
41  std::string_view description = std::string_view()) {
42  if (LIKELY(result == CUDA_SUCCESS))
43  return true;
44 
45  const char* error;
46  const char* message;
47  cuGetErrorName(result, &error);
48  cuGetErrorString(result, &message);
49  abortOnCudaError(file, line, cmd, error, message, description);
50  return false;
51  }
52 
53  inline bool cudaCheck_(const char* file,
54  int line,
55  const char* cmd,
56  cudaError_t result,
57  std::string_view description = std::string_view()) {
58  if (LIKELY(result == cudaSuccess))
59  return true;
60 
61  const char* error = cudaGetErrorName(result);
62  const char* message = cudaGetErrorString(result);
63  abortOnCudaError(file, line, cmd, error, message, description);
64  return false;
65  }
66  } // namespace cuda
67 } // namespace cms
68 
69 #define cudaCheck(ARG, ...) (cms::cuda::cudaCheck_(__FILE__, __LINE__, #ARG, (ARG), ##__VA_ARGS__))
70 
71 #endif // HeterogeneousCore_CUDAUtilities_cudaCheck_h
#define LIKELY(x)
Definition: Likely.h:20
tuple result
Definition: mps_fire.py:311
bool cudaCheck_(const char *file, int line, const char *cmd, CUresult result, std::string_view description=std::string_view())
Definition: cudaCheck.h:37
void abortOnCudaError(const char *file, int line, const char *cmd, const char *error, const char *message, std::string_view description=std::string_view())
Definition: cudaCheck.h:21
list cmd
Definition: mps_setup.py:244