cuda-api-wrappers
Thin C++-flavored wrappers for the CUDA Runtime API
miscellany.hpp
Go to the documentation of this file.
1 
8 #ifndef CUDA_API_WRAPPERS_MISCELLANY_HPP_
9 #define CUDA_API_WRAPPERS_MISCELLANY_HPP_
10 
11 #include "types.hpp"
12 #include "error.hpp"
13 
14 #include <ostream>
15 #include <utility>
16 
17 namespace cuda {
18 
26 inline void initialize_driver()
27 {
28  static constexpr const unsigned dummy_flags{0}; // this is the only allowed value for flags
29  auto status = cuInit(dummy_flags);
30  throw_if_error_lazy(status, "Failed initializing the CUDA driver");
31 }
32 
41 {
42  thread_local bool driver_known_to_be_initialized{false};
43  if (not driver_known_to_be_initialized) {
45  driver_known_to_be_initialized = true;
46  }
47 }
48 
49 namespace device {
50 
64 {
66  // This function is often called before any device is obtained (which is where we
67  // expect the driver to be initialized)
68  int device_count = 0; // Initializing, just to be on the safe side
69  status_t result = cuDeviceGetCount(&device_count);
70  switch (result) {
71  case (status_t) status::no_device:
72  return 0;
73  case (status_t) status::success:
74  break;
75  default:
76  throw runtime_error(result, "Failed obtaining the number of CUDA devices on the system");
77  }
78  if (device_count < 0) {
79  throw ::std::logic_error("cudaGetDeviceCount() reports an invalid number of CUDA devices");
80  }
81  return device_count;
82 }
83 
84 } // namespace device
85 
86 
87 } // namespace cuda
88 
89 #endif // CUDA_API_WRAPPERS_MISCELLANY_HPP_
void ensure_driver_is_initialized()
A mechanism for ensuring a cuInit() call has been made, to use before making any other driver API cal...
Definition: miscellany.hpp:40
Definitions and functionality wrapping CUDA APIs.
Definition: array.hpp:22
device::id_t count()
Get the number of CUDA devices usable on the system (with the current CUDA library and kernel driver)...
Definition: miscellany.hpp:63
CUdevice id_t
Numeric ID of a CUDA device used by the CUDA Runtime API.
Definition: types.hpp:850
void initialize_driver()
Obtains the CUDA Runtime version.
Definition: miscellany.hpp:26
A (base?) class for exceptions raised by CUDA code; these errors are thrown by essentially all CUDA R...
Definition: error.hpp:271
#define throw_if_error_lazy(status__,...)
A macro for only throwing an error if we&#39;ve failed - which also ensures no string is constructed unle...
Definition: error.hpp:316
Facilities for exception-based handling of Runtime and Driver API errors, including a basic exception...
Fundamental CUDA-related type definitions.
CUresult status_t
Indicates either the result (success or error index) of a CUDA Runtime or Driver API call...
Definition: types.hpp:77