5 #ifndef CUDA_API_WRAPPERS_CURRENT_CONTEXT_HPP_ 6 #define CUDA_API_WRAPPERS_CURRENT_CONTEXT_HPP_ 18 class primary_context_t;
33 auto status = cuCtxGetCurrent(&handle);
34 if (status == cuda::status::not_yet_initialized) {
38 return (handle != context::detail_::none);
52 auto status = cuCtxGetCurrent(¤t_context_handle);
54 case CUDA_ERROR_NOT_INITIALIZED:
55 case CUDA_ERROR_INVALID_CONTEXT:
58 return (handle == current_context_handle);
61 "Failed determining whether there's a current context, or what it is");
65 struct status_and_handle_pair {
78 inline status_and_handle_pair get_with_status()
81 auto status = cuCtxGetCurrent(&handle);
82 if (status == status::not_yet_initialized) {
83 handle = context::detail_::none;
85 return { status, handle };
96 auto p = get_with_status();
102 inline context::flags_t get_flags()
104 context::flags_t result;
105 auto status = cuCtxGetFlags(&result);
114 auto result = cuCtxGetDevice(&device_id);
129 auto status = cuCtxPushCurrent(context_handle);
131 + context::detail_::identify(context_handle));
148 if (get_handle() == context_handle) {
return false; }
149 push(context_handle);
156 auto status = cuCtxPopCurrent(&popped_context_handle);
158 return popped_context_handle;
161 inline void set(
handle_t context_handle)
166 auto status = cuCtxSetCurrent(context_handle);
168 "Failed setting the current context to " + context::detail_::identify(context_handle));
179 bool hold_primary_context_ref_unit_;
184 :
scoped_override_t(
true, device_for_which_context_is_primary, context_handle) {}
201 #define CAW_SET_SCOPE_CONTEXT(context_handle_expr_) \ 202 const ::cuda::context::current::detail_::scoped_override_t caw_context_for_this_scope_(context_handle_expr_) 210 class scoped_ensurer_t {
212 bool context_was_pushed_on_construction;
214 explicit scoped_ensurer_t(
bool force_push,
handle_t fallback_context_handle)
215 : context_was_pushed_on_construction(force_push)
217 if (force_push) {
push(fallback_context_handle); }
220 explicit scoped_ensurer_t(
handle_t fallback_context_handle)
221 : scoped_ensurer_t(not
exists(), fallback_context_handle)
224 scoped_ensurer_t(
const scoped_ensurer_t&) =
delete;
225 scoped_ensurer_t(scoped_ensurer_t&&) =
delete;
227 scoped_ensurer_t& operator=(scoped_ensurer_t&&) =
delete;
228 scoped_ensurer_t& operator=(
const scoped_ensurer_t&) =
delete;
230 ~scoped_ensurer_t() {
if (context_was_pushed_on_construction) {
pop(); } }
250 using parent = detail_::scoped_override_t;
267 #define CUDA_CONTEXT_FOR_THIS_SCOPE(_cuda_context) \ 268 ::cuda::context::current::scoped_override_t set_context_for_this_scope{ _cuda_context } 278 auto status = cuCtxSynchronize();
290 auto status = cuCtxSynchronize();
293 + context::detail_::identify(current_context_handle));
303 auto status = cuCtxSynchronize();
306 + context::detail_::identify(current_context_handle, current_context_device_id));
318 #endif // CUDA_API_WRAPPERS_CURRENT_CONTEXT_HPP_ 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
bool push_if_not_on_top(const context_t &context)
Push a (reference to a) context onto the top of the context stack - unless that context is already at...
Definition: context.hpp:887
context_t pop()
Pop the top off of the context stack.
Definition: context.hpp:910
A (base?) class for exceptions raised by CUDA code; these errors are thrown by essentially all CUDA R...
Definition: error.hpp:271
void synchronize(const context_t &context)
Waits for all previously-scheduled tasks on all streams (= queues) in a CUDA context to conclude...
Definition: context.hpp:968
A RAII-based mechanism for pushing a context onto the context stack for what remains of the current (...
Definition: current_context.hpp:248
void push(const context_t &context)
Push a (reference to a) context onto the top of the context stack.
Definition: context.hpp:899
#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
Facilities for exception-based handling of Runtime and Driver API errors, including a basic exception...
Fundamental CUDA-related constants and enumerations, not dependent on any more complex abstractions...
bool exists()
Determine whether any CUDA context is current, or whether the context stack is empty/uninitialized.
Definition: current_context.hpp:30
Fundamental CUDA-related type definitions.
constexpr bool is_success(status_t status)
Determine whether the API call returning the specified status had succeeded.
Definition: error.hpp:203
CUresult status_t
Indicates either the result (success or error index) of a CUDA Runtime or Driver API call...
Definition: types.hpp:77