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 
33 inline void ensure_driver_is_initialized()
34 {
35  thread_local bool driver_known_to_be_initialized{false};
36  if (not driver_known_to_be_initialized) {
38  driver_known_to_be_initialized = true;
39  }
40 }
41 
42 namespace device {
43 
57 {
59  // This function is often called before any device is obtained (which is where we
60  // expect the driver to be initialized)
61  int device_count = 0; // Initializing, just to be on the safe side
62  status_t result = cuDeviceGetCount(&device_count);
63  switch (result) {
64  case status::no_device:
65  return 0;
66  case status::success:
67  break;
68  default:
69  throw runtime_error(result, "Failed obtaining the number of CUDA devices on the system");
70  }
71  if (device_count < 0) {
72  throw ::std::logic_error("cudaGetDeviceCount() reports an invalid number of CUDA devices");
73  }
74  return device_count;
75 }
76 
77 } // namespace device
78 
79 
80 } // namespace cuda
81 
82 #endif // CUDA_API_WRAPPERS_MISCELLANY_HPP_
All definitions and functionality wrapping the CUDA Runtime API.
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:56
CUdevice id_t
Numeric ID of a CUDA device used by the CUDA Runtime API.
Definition: types.hpp:752
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:280
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:136