Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:05:46

0001 #ifndef HeterogeneousCore_CUDAUtilities_nvmlCheck_h
0002 #define HeterogeneousCore_CUDAUtilities_nvmlCheck_h
0003 
0004 // C++ standard headers
0005 #include <iostream>
0006 #include <sstream>
0007 #include <stdexcept>
0008 #include <string>
0009 #include <string_view>
0010 
0011 // CUDA headers
0012 #include <nvml.h>
0013 
0014 // CMSSW headers
0015 #include "FWCore/Utilities/interface/Likely.h"
0016 
0017 namespace cms {
0018   namespace cuda {
0019 
0020     [[noreturn]] inline void abortOnNvmlError(const char* file,
0021                                               int line,
0022                                               const char* cmd,
0023                                               const char* error,
0024                                               const char* message,
0025                                               std::string_view description = std::string_view()) {
0026       std::ostringstream out;
0027       out << "\n";
0028       out << file << ", line " << line << ":\n";
0029       out << "nvmlCheck(" << cmd << ");\n";
0030       out << error << ": " << message << "\n";
0031       if (!description.empty())
0032         out << description << "\n";
0033       throw std::runtime_error(out.str());
0034     }
0035 
0036     inline bool nvmlCheck_(const char* file,
0037                            int line,
0038                            const char* cmd,
0039                            nvmlReturn_t result,
0040                            std::string_view description = std::string_view()) {
0041       if (LIKELY(result == NVML_SUCCESS))
0042         return true;
0043 
0044       std::string error = "NVML Error " + std::to_string(result);
0045       const char* message = nvmlErrorString(result);
0046       abortOnNvmlError(file, line, cmd, error.c_str(), message, description);
0047       return false;
0048     }
0049   }  // namespace cuda
0050 }  // namespace cms
0051 
0052 #define nvmlCheck(ARG, ...) (cms::cuda::nvmlCheck_(__FILE__, __LINE__, #ARG, (ARG), ##__VA_ARGS__))
0053 
0054 #endif  // HeterogeneousCore_CUDAUtilities_nvmlCheck_h