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 return cuDevicePrimaryCtxRelease(device_id);
56 auto status = decrease_refcount_nothrow(device_id);
57 throw_if_error_lazy(status,
"Failed releasing the reference to the primary context for " + device::detail_::identify(device_id));
62 inline void decrease_refcount_in_dtor(
device::id_t device_id) noexcept
64 #if THROW_IN_DESTRUCTORS 65 decrease_refcount(device_id);
67 decrease_refcount_nothrow(device_id);
74 auto status = cuDevicePrimaryCtxRetain(&primary_context_handle, device_id);
76 "Failed obtaining (and possibly creating, and adding a reference count to) the primary context for " 77 + device::detail_::identify(device_id));
78 return primary_context_handle;
83 obtain_and_increase_refcount(device_id);
94 inline bool is_active(
const device_t& device);
101 void destroy(
const device_t& device);
105 inline primary_context_t
wrap(
108 bool decrease_refcount_on_destruct) noexcept;
142 bool owns_refcount_unit_;
149 bool decrease_refcount_on_destruction) noexcept
151 owns_refcount_unit_(decrease_refcount_on_destruction) { }
162 stream_t default_stream()
const noexcept;
166 friend class ::cuda::device_t;
172 :
context_t(other), owns_refcount_unit_(other.owns_refcount_unit_)
174 if (owns_refcount_unit_) {
175 primary_context::detail_::obtain_and_increase_refcount(device_id_);
183 if (owns_refcount_unit_) {
184 primary_context::detail_::decrease_refcount_in_dtor(device_id_);
194 namespace primary_context {
201 auto handle = obtain_and_increase_refcount(device_id);
202 if (not with_refcount_increase) {
203 decrease_refcount(device_id);
244 bool decrease_refcount_on_destruct) noexcept
246 return {device_id, handle, decrease_refcount_on_destruct};
271 handle_t get_primary_for_same_device(
handle_t handle,
bool assume_active =
false);
274 inline bool is_primary(
handle_t handle)
276 return is_primary_for_device(handle, get_device_id(handle));
285 namespace primary_context {
291 auto current_context = context::current::detail_::get_handle();
292 return context::detail_::is_primary_for_device(current_context, device_id);
298 inline bool is_current()
300 auto device_id = context::current::detail_::get_device_id();
301 return detail_::is_current(device_id);
Proxy class for a CUDA stream.
Definition: stream.hpp:258
Wrapper class for a CUDA context.
Definition: context.hpp:249
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:880
A class for holding the primary context of a CUDA device.
Definition: primary_context.hpp:122
CUdevice id_t
Numeric ID of a CUDA device used by the CUDA Runtime API.
Definition: types.hpp:852
void throw_if_error(status_t status, const ::std::string &message) noexcept(false)
Do nothing...
Definition: error.hpp:346
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:327
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:820
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:74