Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-06-18 23:27:02

0001 #ifndef HeterogeneousCore_CUDAUtilities_cudaCheck_h
0002 #define HeterogeneousCore_CUDAUtilities_cudaCheck_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 <cuda.h>
0013 #include <cuda_runtime.h>
0014 
0015 // CMSSW headers
0016 #include "FWCore/Utilities/interface/Likely.h"
0017 
0018 namespace cms {
0019   namespace cuda {
0020 
0021     [[noreturn]] inline void abortOnCudaError(const char* file,
0022                                               int line,
0023                                               const char* cmd,
0024                                               const char* error,
0025                                               const char* message,
0026                                               std::string_view description = std::string_view()) {
0027       std::ostringstream out;
0028       out << "\n";
0029       out << file << ", line " << line << ":\n";
0030       out << "cudaCheck(" << cmd << ");\n";
0031       out << error << ": " << message << "\n";
0032       if (!description.empty())
0033         out << description << "\n";
0034       throw std::runtime_error(out.str());
0035     }
0036 
0037     inline bool cudaCheck_(const char* file,
0038                            int line,
0039                            const char* cmd,
0040                            CUresult result,
0041                            std::string_view description = std::string_view()) {
0042       if (LIKELY(result == CUDA_SUCCESS))
0043         return true;
0044 
0045       const char* error;
0046       const char* message;
0047       cuGetErrorName(result, &error);
0048       cuGetErrorString(result, &message);
0049       abortOnCudaError(file, line, cmd, error, message, description);
0050       return false;
0051     }
0052 
0053     inline bool cudaCheck_(const char* file,
0054                            int line,
0055                            const char* cmd,
0056                            cudaError_t result,
0057                            std::string_view description = std::string_view()) {
0058       if (LIKELY(result == cudaSuccess))
0059         return true;
0060 
0061       const char* error = cudaGetErrorName(result);
0062       const char* message = cudaGetErrorString(result);
0063       abortOnCudaError(file, line, cmd, error, message, description);
0064       return false;
0065     }
0066   }  // namespace cuda
0067 }  // namespace cms
0068 
0069 #define cudaCheck(ARG, ...) (cms::cuda::cudaCheck_(__FILE__, __LINE__, #ARG, (ARG), ##__VA_ARGS__))
0070 
0071 #endif  // HeterogeneousCore_CUDAUtilities_cudaCheck_h