20 #ifndef CUDA_API_WRAPPERS_CURRENT_DEVICE_HPP_ 21 #define CUDA_API_WRAPPERS_CURRENT_DEVICE_HPP_ 28 #include <cuda_runtime_api.h> 49 auto status = cuCtxGetCurrent(¤t_context_handle);
50 if (status == CUDA_ERROR_NOT_INITIALIZED) {
56 "Failed obtaining the current context for determining which device is active");
58 if (current_context_handle == context::detail_::none) {
62 return cuda::context::current::detail_::get_device_id();
90 bool driver_is_initialized,
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;
100 if (current_context_handle != context::detail_::none) {
101 if (current_context_handle == device_pc_handle) {
102 return device_pc_handle;
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;
109 context::current::detail_::set(device_pc_handle);
110 return device_pc_handle;
128 inline void set(
id_t device_id)
131 auto status = cuCtxGetCurrent(¤t_context_handle);
132 bool driver_initialized = (status == CUDA_ERROR_NOT_INITIALIZED);
133 set_with_aux_info(device_id, driver_initialized, current_context_handle);
147 inline void set(
const id_t *device_ids,
size_t num_devices)
150 throw cuda::runtime_error(status::invalid_device,
"More devices listed than exist on the system");
152 auto result = cudaSetValidDevices(const_cast<int *>(device_ids), static_cast<int>(num_devices));
154 "Failure setting the current device to any of the list of " 155 + ::std::to_string(num_devices) +
" devices specified");
166 void set(
const device_t& device);
179 #define CUDA_DEVICE_FOR_THIS_SCOPE(_cuda_device) \ 180 ::cuda::device::current::scoped_override_t scoped_device_override{ _cuda_device } 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'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's current device to its default value - the default device.
Definition: current_device.hpp:171