Skip to content

Commit

Permalink
Fixes #314: Now pushing the context when recording an event, period. …
Browse files Browse the repository at this point in the history
…Also,

* A scoped context setter variable was mis-named as a device setter (probably forgot to rename it when making the big switch to being driver-based).
* Rephrased an error message regarding enqueuing an event on a stream in another context.
  • Loading branch information
eyalroz committed Apr 13, 2022
1 parent cd65496 commit 3a037bb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 23 deletions.
44 changes: 30 additions & 14 deletions src/cuda/api/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,29 @@ namespace event {

namespace detail_ {

inline void enqueue_in_current_context(stream::handle_t stream_handle, handle_t event_handle)
{
auto status = cuEventRecord(event_handle, stream_handle);
cuda::throw_if_error(status,
"Failed recording " + event::detail_::identify(event_handle)
+ " on " + stream::detail_::identify(stream_handle));
}

/**
* Schedule a specified event to occur (= to fire) when all activities
* already scheduled on the stream have concluded.
*
* @param stream_handle handle of the stream (=queue) where to enqueue the event occurrence
* @param event_handle Event to be made to occur on stream @ref stream_handle
*/
inline void enqueue(stream::handle_t stream_handle, handle_t event_handle) {
auto status = cuEventRecord(event_handle, stream_handle);
cuda::throw_if_error(status,
"Failed recording " + event::detail_::identify(event_handle)
+ " on " + stream::detail_::identify(stream_handle));
inline void enqueue(context::handle_t context_handle, stream::handle_t stream_handle, handle_t event_handle) {
context::current::detail_::scoped_override_t context_for_this_scope { context_handle };
enqueue_in_current_context(stream_handle, event_handle);
}

constexpr unsigned inline make_flags(bool uses_blocking_sync, bool records_timing, bool interprocess)
using flags_t = unsigned int;

constexpr flags_t inline make_flags(bool uses_blocking_sync, bool records_timing, bool interprocess)
{
return
( uses_blocking_sync ? CU_EVENT_BLOCKING_SYNC : 0 )
Expand Down Expand Up @@ -169,7 +177,7 @@ class event_t {
*/
void record() const
{
event::detail_::enqueue(stream::default_stream_handle, handle_);
event::detail_::enqueue(context_handle_, stream::default_stream_handle, handle_);
}

/**
Expand Down Expand Up @@ -281,6 +289,19 @@ inline ::std::string identify(const event_t& event)
return identify(event.handle(), event.context_handle(), event.device_id());
}

// Note: For now, event_t's need their device's ID - even if it's the current device;
// that explains the requirement in this function's interface
inline handle_t create_raw_in_current_context(flags_t flags = 0u)
{
cuda::event::handle_t new_event_handle;
auto status = cuEventCreate(&new_event_handle, flags);
cuda::throw_if_error(status, "failed creating a CUDA event associated with the current device");
// Note: We're trusting CUDA to actually have succeeded if it reports success,
// so we're not checking the newly-created event handle - which is really just
// a pointer - for nullness
return new_event_handle;
}

// Note: For now, event_t's need their device's ID - even if it's the current device;
// that explains the requirement in this function's interface
inline event_t create_in_current_context(
Expand All @@ -291,14 +312,9 @@ inline event_t create_in_current_context(
bool interprocess)
{
auto flags = make_flags(uses_blocking_sync, records_timing, interprocess);
cuda::event::handle_t new_event_handle;
auto status = cuEventCreate(&new_event_handle, flags);
cuda::throw_if_error(status, "failed creating a CUDA event associated with the current device");
// Note: We're trusting CUDA to actually have succeeded if it reports success,
// so we're not checking the newly-created event handle - which is really just
// a pointer - for nullness
auto handle = create_raw_in_current_context(flags);
bool take_ownership = true;
return wrap(current_device_id, current_context_handle, new_event_handle, take_ownership);
return wrap(current_device_id, current_context_handle, handle, take_ownership);
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/cuda/api/multi_wrapper_impls/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,12 @@ inline context_t event_t::context() const

inline void event_t::record(const stream_t& stream) const
{
// Note:
// TODO: Perhaps check the context match here, rather than have the Runtime API call fail?
event::detail_::enqueue(stream.handle(), handle_);
#ifndef NDEBUG
if (stream.context_handle() != context_handle_) {
throw std::invalid_argument("Attempt to record an event on a stream in a different context");
}
#endif
event::detail_::enqueue(context_handle_, stream.handle(), handle_);
}

inline void event_t::fire(const stream_t& stream) const
Expand Down
12 changes: 6 additions & 6 deletions src/cuda/api/multi_wrapper_impls/stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ inline event_t& stream_t::enqueue_t::event(event_t& existing_event)
auto context_handle = associated_stream.context_handle_;
auto stream_context_handle_ = associated_stream.context_handle_;
if (existing_event.context_handle() != stream_context_handle_) {
throw ::std::invalid_argument("Attempt to enqueue "
+ event::detail_::identify(existing_event)
+ ", to be triggered by " + stream::detail_::identify(associated_stream));
throw ::std::invalid_argument(
"Attempt to enqueue " + event::detail_::identify(existing_event)
+ " on a stream in a different context: " + stream::detail_::identify(associated_stream));
}
context::current::detail_::scoped_override_t set_device_for_this_scope(context_handle);
stream::detail_::record_event_in_current_context(device_id, context_handle,
associated_stream.handle_,existing_event.handle());
context::current::detail_::scoped_override_t set_context_for_this_scope(context_handle);
stream::detail_::record_event_in_current_context(
device_id, context_handle, associated_stream.handle_,existing_event.handle());
return existing_event;
}

Expand Down

0 comments on commit 3a037bb

Please sign in to comment.