5 #ifndef CUDA_API_WRAPPERS_PRIMARY_CONTEXT_HPP_ 6 #define CUDA_API_WRAPPERS_PRIMARY_CONTEXT_HPP_ 16 class primary_context_t;
19 namespace primary_context {
24 context::flags_t flags;
31 auto status = cuDevicePrimaryCtxGetState(device_id, &result.flags, &result.is_active);
32 throw_if_error(status,
"Failed obtaining the state of the primary context for " 33 + device::detail_::identify(device_id));
40 return raw_state(device_id).flags & ~CU_CTX_MAP_HOST;
45 return raw_state(device_id).is_active;
51 auto result = cuDevicePrimaryCtxRelease(device_id);
57 auto status = decrease_refcount_nothrow(device_id);
58 throw_if_error_lazy(status,
"Failed releasing the reference to the primary context for " + device::detail_::identify(device_id));
64 auto status = cuDevicePrimaryCtxRetain(&primary_context_handle, device_id);
66 "Failed obtaining (and possibly creating, and adding a reference count to) the primary context for " 67 + device::detail_::identify(device_id));
68 return primary_context_handle;
73 obtain_and_increase_refcount(device_id);
84 inline bool is_active(
const device_t& device);
91 void destroy(
const device_t& device);
95 inline primary_context_t
wrap(
98 bool decrease_refcount_on_destruct) noexcept;
132 bool owns_refcount_unit_;
139 bool decrease_refcount_on_destruction) noexcept
141 owns_refcount_unit_(decrease_refcount_on_destruction) { }
152 stream_t default_stream()
const noexcept;
162 :
context_t(other), owns_refcount_unit_(other.owns_refcount_unit_)
164 if (owns_refcount_unit_) {
165 primary_context::detail_::obtain_and_increase_refcount(device_id_);
173 if (owns_refcount_unit_) {
175 device::primary_context::detail_::decrease_refcount_nothrow(device_id_);
178 primary_context::detail_::decrease_refcount(device_id_);
189 namespace primary_context {
196 auto handle = obtain_and_increase_refcount(device_id);
197 if (not with_refcount_increase) {
198 decrease_refcount(device_id);
239 bool decrease_refcount_on_destruct) noexcept
241 return {device_id, handle, decrease_refcount_on_destruct};
266 handle_t get_primary_for_same_device(
handle_t handle,
bool assume_active =
false);
269 inline bool is_primary(
handle_t handle)
271 return is_primary_for_device(handle, get_device_id(handle));
280 namespace primary_context {
286 auto current_context = context::current::detail_::get_handle();
287 return context::detail_::is_primary_for_device(current_context, device_id);
293 inline bool is_current()
295 auto device_id = context::current::detail_::get_device_id();
296 return detail_::is_current(device_id);
Proxy class for a CUDA stream.
Definition: stream.hpp:246
Wrapper class for a CUDA context.
Definition: context.hpp:244
Definitions and functionality wrapping CUDA APIs.
Definition: array.hpp:22
CUcontext handle_t
Raw CUDA driver handle for a context; see {context_t}.
Definition: types.hpp:878
A class for holding the primary context of a CUDA device.
Definition: primary_context.hpp:112
CUdevice id_t
Numeric ID of a CUDA device used by the CUDA Runtime API.
Definition: types.hpp:850
void throw_if_error(status_t status, const ::std::string &message) noexcept(false)
Do nothing...
Definition: error.hpp:335
Contains a proxy class for CUDA execution contexts.
#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
device_t wrap(id_t id) NOEXCEPT_IF_NDEBUG
Returns a wrapper for the CUDA device with a given id.
Definition: device.hpp:825
bool is_active(const device_t &device)
Definition: device.hpp:34
Wrapper class for a CUDA device.
Definition: device.hpp:135
CUresult status_t
Indicates either the result (success or error index) of a CUDA Runtime or Driver API call...
Definition: types.hpp:77