CMS 3D CMS Logo

nvmlCheck.h
Go to the documentation of this file.
1 #ifndef HeterogeneousCore_CUDAUtilities_nvmlCheck_h
2 #define HeterogeneousCore_CUDAUtilities_nvmlCheck_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 <nvml.h>
13 
14 // CMSSW headers
16 
17 namespace cms {
18  namespace cuda {
19 
20  [[noreturn]] inline void abortOnNvmlError(const char* file,
21  int line,
22  const char* cmd,
23  const char* error,
24  const char* message,
25  std::string_view description = std::string_view()) {
26  std::ostringstream out;
27  out << "\n";
28  out << file << ", line " << line << ":\n";
29  out << "nvmlCheck(" << cmd << ");\n";
30  out << error << ": " << message << "\n";
31  if (!description.empty())
32  out << description << "\n";
33  throw std::runtime_error(out.str());
34  }
35 
36  inline bool nvmlCheck_(const char* file,
37  int line,
38  const char* cmd,
39  nvmlReturn_t result,
40  std::string_view description = std::string_view()) {
41  if (LIKELY(result == NVML_SUCCESS))
42  return true;
43 
44  std::string error = "NVML Error " + std::to_string(result);
45  const char* message = nvmlErrorString(result);
46  abortOnNvmlError(file, line, cmd, error.c_str(), message, description);
47  return false;
48  }
49  } // namespace cuda
50 } // namespace cms
51 
52 #define nvmlCheck(ARG, ...) (cms::cuda::nvmlCheck_(__FILE__, __LINE__, #ARG, (ARG), ##__VA_ARGS__))
53 
54 #endif // HeterogeneousCore_CUDAUtilities_nvmlCheck_h
void abortOnNvmlError(const char *file, int line, const char *cmd, const char *error, const char *message, std::string_view description=std::string_view())
Definition: nvmlCheck.h:20
#define LIKELY(x)
Definition: Likely.h:20
static std::string to_string(const XMLCh *ch)
bool nvmlCheck_(const char *file, int line, const char *cmd, nvmlReturn_t result, std::string_view description=std::string_view())
Definition: nvmlCheck.h:36
Namespace of DDCMS conversion namespace.
list cmd
Definition: mps_setup.py:244