cuda-api-wrappers
Thin C++-flavored wrappers for the CUDA Runtime API
current_device.hpp
Go to the documentation of this file.
1 
20 #ifndef CUDA_API_WRAPPERS_CURRENT_DEVICE_HPP_
21 #define CUDA_API_WRAPPERS_CURRENT_DEVICE_HPP_
22 
23 #include "constants.hpp"
24 #include "miscellany.hpp"
25 #include "current_context.hpp"
26 #include "primary_context.hpp"
27 
28 #include <cuda_runtime_api.h>
29 
30 namespace cuda {
31 
33 class device_t;
35 
36 namespace device {
37 
38 namespace current {
39 
40 namespace detail_ {
41 
45 inline id_t get_id()
46 {
47  static constexpr const id_t default_device_id { 0 };
48  context::handle_t current_context_handle;
49  auto status = cuCtxGetCurrent(&current_context_handle);
50  if (status == CUDA_ERROR_NOT_INITIALIZED) {
52  // Should we activate and push the default device's context? probably not.
53  return default_device_id;
54  }
55  throw_if_error_lazy(status,
56  "Failed obtaining the current context for determining which device is active");
57 
58  if (current_context_handle == context::detail_::none) {
59  // Should we activate and push the default device's context? probably not.
60  return default_device_id;
61  }
62  return cuda::context::current::detail_::get_device_id();
63  // ... which is the equivalent of doing:
64  //
65 }
66 
76 
88 inline context::handle_t set_with_aux_info(
89  id_t device_id,
90  bool driver_is_initialized,
91  context::handle_t current_context_handle = context::detail_::none,
92  context::handle_t device_pc_handle = context::detail_::none)
93 {
94  if (not driver_is_initialized) {
96  device_pc_handle = device::primary_context::detail_::obtain_and_increase_refcount(device_id);
97  context::current::detail_::set(device_pc_handle);
98  return device_pc_handle;
99  }
100  if (current_context_handle != context::detail_::none) {
101  if (current_context_handle == device_pc_handle) {
102  return device_pc_handle;
103  }
104  }
105  device_pc_handle = device::primary_context::detail_::obtain_and_increase_refcount(device_id);
106  if (current_context_handle == device_pc_handle) {
107  return device_pc_handle;
108  }
109  context::current::detail_::set(device_pc_handle); // Remember: This _replaces_ the current context
110  return device_pc_handle;
111 }
112 
128 inline void set(id_t device_id)
129 {
130  context::handle_t current_context_handle;
131  auto status = cuCtxGetCurrent(&current_context_handle);
132  bool driver_initialized = (status == CUDA_ERROR_NOT_INITIALIZED);
133  set_with_aux_info(device_id, driver_initialized, current_context_handle);
134  // Note: We can safely assume the refcount was increased.
135 }
137 
147 inline void set(const id_t *device_ids, size_t num_devices)
148 {
149  if (num_devices > static_cast<size_t>(cuda::device::count())) {
150  throw cuda::runtime_error(status::invalid_device, "More devices listed than exist on the system");
151  }
152  auto result = cudaSetValidDevices(const_cast<int *>(device_ids), static_cast<int>(num_devices));
153  throw_if_error_lazy(result,
154  "Failure setting the current device to any of the list of "
155  + ::std::to_string(num_devices) + " devices specified");
156 }
157 
158 } // namespace detail
159 
166 void set(const device_t& device);
167 
171 inline void set_to_default() { return detail_::set(device::default_device_id); }
172 
179 #define CUDA_DEVICE_FOR_THIS_SCOPE(_cuda_device) \
180 ::cuda::device::current::scoped_override_t scoped_device_override{ _cuda_device }
181 
182 
183 } // namespace current
184 } // namespace device
185 } // namespace cuda
186 
187 #endif // CUDA_API_WRAPPERS_CURRENT_DEVICE_HPP_
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
If the CUDA runtime has not been set to a specific device, this is the ID of the device it defaults t...
Definition: constants.hpp:53
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
CUarray handle_t
Raw CUDA driver handle for arrays (of any dimension)
Definition: array.hpp:34
Miscellaneous functionality which does not fit in another file, and does not depend on the main proxy...
Fundamental CUDA-related constants and enumerations, not dependent on any more complex abstractions...
void set_to_default()
Reset the CUDA Runtime API&#39;s current device to its default value - the default device.
Definition: current_device.hpp:171