Tensor Comprehensions
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cuda.h
Go to the documentation of this file.
1 
16 #pragma once
17 
18 #ifndef CUDA_HOME
19 #error "CUDA_HOME must be defined"
20 #endif // CUDA_HOME
21 
22 #ifndef CUB_HOME
23 #error "CUB_HOME must be defined"
24 #endif // CUB_HOME
25 
26 #include <sstream>
27 #include <stdexcept>
28 
29 #include <cuda.h>
30 #include <cuda_runtime.h>
31 
32 #include <glog/logging.h>
33 
34 #define TC_CUDA_DRIVERAPI_ENFORCE(condition) \
35  do { \
36  CUresult result = condition; \
37  if (result != CUDA_SUCCESS) { \
38  const char* msg; \
39  cuGetErrorName(result, &msg); \
40  std::stringstream ss; \
41  ss << "Error at: " << __FILE__ << ":" << __LINE__ << ": " << msg; \
42  LOG(WARNING) << ss.str(); \
43  throw std::runtime_error(ss.str().c_str()); \
44  } \
45  } while (0)
46 
47 #define TC_NVRTC_CHECK(condition) \
48  do { \
49  nvrtcResult result = condition; \
50  if (result != NVRTC_SUCCESS) { \
51  std::stringstream ss; \
52  ss << "Error at: " << __FILE__ << ":" << __LINE__ << ": " \
53  << nvrtcGetErrorString(result); \
54  LOG(WARNING) << ss.str(); \
55  throw std::runtime_error(ss.str().c_str()); \
56  } \
57  } while (0)
58 
59 #define TC_CUDA_RUNTIMEAPI_ENFORCE(condition) \
60  do { \
61  cudaError_t result = condition; \
62  if (result != cudaSuccess) { \
63  std::stringstream ss; \
64  ss << "Error at: " << __FILE__ << ":" << __LINE__ << ": " \
65  << cudaGetErrorString(result); \
66  LOG(WARNING) << ss.str(); \
67  throw std::runtime_error(ss.str().c_str()); \
68  } \
69  } while (0)
70 
71 namespace tc {
72 
73 struct WithDevice {
74  WithDevice(size_t g) : newGpu(g) {
75  int dev;
76  TC_CUDA_RUNTIMEAPI_ENFORCE(cudaGetDevice(&dev));
77  oldGpu = dev;
78  TC_CUDA_RUNTIMEAPI_ENFORCE(cudaSetDevice(newGpu));
79  }
80  ~WithDevice() noexcept(false) {
81  TC_CUDA_RUNTIMEAPI_ENFORCE(cudaSetDevice(oldGpu));
82  }
83  size_t oldGpu;
84  size_t newGpu;
85 };
86 
87 // Query the active device about the avaialble memory size.
88 size_t querySharedMemorySize();
89 
90 } // namespace tc
WithDevice(size_t g)
Definition: cuda.h:74
size_t querySharedMemorySize()
size_t newGpu
Definition: cuda.h:84
Definition: cuda.h:73
#define TC_CUDA_RUNTIMEAPI_ENFORCE(condition)
Definition: cuda.h:59
size_t oldGpu
Definition: cuda.h:83
~WithDevice() noexcept(false)
Definition: cuda.h:80