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);
153 inline status_t pop_and_discard_nothrow()
156 auto status = cuCtxPopCurrent(&popped_context_handle);
163 auto status = cuCtxPopCurrent(&popped_context_handle);
165 return popped_context_handle;
168 inline void set(
handle_t context_handle)
173 auto status = cuCtxSetCurrent(context_handle);
175 "Failed setting the current context to " + context::detail_::identify(context_handle));
186 bool hold_primary_context_ref_unit_;
191 :
scoped_override_t(
true, device_for_which_context_is_primary, context_handle) {}
208 #define CAW_SET_SCOPE_CONTEXT(context_handle_expr_) \ 209 const ::cuda::context::current::detail_::scoped_override_t caw_context_for_this_scope_(context_handle_expr_) 217 class scoped_ensurer_t {
219 bool context_was_pushed_on_construction;
221 explicit scoped_ensurer_t(
bool force_push,
handle_t fallback_context_handle)
222 : context_was_pushed_on_construction(force_push)
224 if (force_push) {
push(fallback_context_handle); }
227 explicit scoped_ensurer_t(
handle_t fallback_context_handle)
228 : scoped_ensurer_t(not
exists(), fallback_context_handle)
231 scoped_ensurer_t(
const scoped_ensurer_t&) =
delete;
232 scoped_ensurer_t(scoped_ensurer_t&&) =
delete;
234 scoped_ensurer_t& operator=(scoped_ensurer_t&&) =
delete;
235 scoped_ensurer_t& operator=(
const scoped_ensurer_t&) =
delete;
237 ~scoped_ensurer_t() {
if (context_was_pushed_on_construction) {
pop(); } }
257 using parent = detail_::scoped_override_t;
274 #define CUDA_CONTEXT_FOR_THIS_SCOPE(_cuda_context) \ 275 ::cuda::context::current::scoped_override_t set_context_for_this_scope{ _cuda_context } 285 auto status = cuCtxSynchronize();
297 auto status = cuCtxSynchronize();
300 + context::detail_::identify(current_context_handle));
310 auto status = cuCtxSynchronize();
313 + context::detail_::identify(current_context_handle, current_context_device_id));
325 #endif // CUDA_API_WRAPPERS_CURRENT_CONTEXT_HPP_ 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
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:899
context_t pop()
Pop the top off of the context stack.
Definition: context.hpp:922
A (base?) class for exceptions raised by CUDA code; these errors are thrown by essentially all CUDA R...
Definition: error.hpp:282
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:980
A RAII-based mechanism for pushing a context onto the context stack for what remains of the current (...
Definition: current_context.hpp:255
void push(const context_t &context)
Push a (reference to a) context onto the top of the context stack.
Definition: context.hpp:911
#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
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:214
CUresult status_t
Indicates either the result (success or error index) of a CUDA Runtime or Driver API call...
Definition: types.hpp:74