cuda-api-wrappers
Thin C++-flavored wrappers for the CUDA Runtime API
event.hpp
Go to the documentation of this file.
1 
10 #pragma once
11 #ifndef MULTI_WRAPPER_IMPLS_EVENT_HPP_
12 #define MULTI_WRAPPER_IMPLS_EVENT_HPP_
13 
14 #include "../device.hpp"
15 #include "../event.hpp"
16 #include "../stream.hpp"
17 #include "../primary_context.hpp"
18 #include "../virtual_memory.hpp"
19 #include "../current_context.hpp"
20 #include "../current_device.hpp"
21 
22 #include <type_traits>
23 #include <vector>
24 #include <algorithm>
25 
26 namespace cuda {
27 
28 namespace event {
29 
30 inline event_t create(
31  const context_t& context,
32  bool uses_blocking_sync,
33  bool records_timing,
34  bool interprocess)
35 {
37  context.device_id(),
38  context.handle(),
39  do_not_hold_primary_context_refcount_unit,
40  uses_blocking_sync,
41  records_timing,
42  interprocess);
43 }
44 
45 inline event_t create(
46  const device_t& device,
47  bool uses_blocking_sync,
48  bool records_timing,
49  bool interprocess)
50 {
51  // While it's possible that the device's primary context is
52  // currently active, we have no guarantee that it will not soon
53  // become inactive. So, we increase the PC refcount "on behalf"
54  // of the stream, to make sure the PC does not de-activate
55  //
56  // todo: consider having the event wrapper take care of the primary
57  // context refcount.
58  //
59  auto pc = device.primary_context(do_not_hold_primary_context_refcount_unit);
60  CAW_SET_SCOPE_CONTEXT(pc.handle());
61  device::primary_context::detail_::increase_refcount(device.id());
62  return event::detail_::create_in_current_context(
63  device.id(),
64  context::current::detail_::get_handle(),
65  do_hold_primary_context_refcount_unit,
66  uses_blocking_sync, records_timing, interprocess);
67 }
68 
69 namespace ipc {
70 
71 inline handle_t export_(const event_t& event)
72 {
73  return detail_::export_(event.handle());
74 }
75 
76 inline event_t import(const context_t& context, const handle_t& event_ipc_handle)
77 {
78  static constexpr const bool do_not_take_ownership { false };
79  static constexpr const bool do_not_own_pc_refcount_unit { false };
80  return event::wrap(
81  context.device_id(),
82  context.handle(),
83  detail_::import(event_ipc_handle),
84  do_not_take_ownership,
85  do_not_own_pc_refcount_unit);
86 }
87 
88 
89 inline event_t import(const device_t& device, const handle_t& event_ipc_handle)
90 {
91  auto pc = device.primary_context();
92  device::primary_context::detail_::increase_refcount(device.id());
93  auto handle = detail_::import(event_ipc_handle);
94  return event::wrap(
95  device.id(), context::current::detail_::get_handle(), handle,
96  do_not_take_ownership, do_hold_primary_context_refcount_unit);
97 }
98 
99 } // namespace ipc
100 
101 } // namespace event
102 
103 inline device_t event_t::device() const
104 {
105  return cuda::device::get(device_id());
106 }
107 
109 {
110  static constexpr const bool dont_take_ownership { false };
111  return context::wrap(device_id(), context_handle_, dont_take_ownership);
112 }
113 
114 inline void event_t::record(const stream_t& stream) const
115 {
116 #ifndef NDEBUG
117  if (stream.context_handle() != context_handle_) {
118  throw ::std::invalid_argument("Attempt to record an event on a stream in a different context");
119  }
120 #endif
121  event::detail_::enqueue(context_handle_, stream.handle(), handle_);
122 }
123 
124 inline void event_t::fire(const stream_t& stream) const
125 {
126  record(stream);
127  stream.synchronize();
128 }
129 
130 } // namespace cuda
131 
132 #endif // MULTI_WRAPPER_IMPLS_EVENT_HPP_
133 
context::handle_t context_handle() const noexcept
The raw CUDA handle for the context in which the represented stream is defined.
Definition: stream.hpp:260
Proxy class for a CUDA stream.
Definition: stream.hpp:246
event::handle_t handle() const noexcept
The raw CUDA handle for this event.
Definition: event.hpp:143
stream::handle_t handle() const noexcept
The raw CUDA handle for a stream which this class wraps.
Definition: stream.hpp:257
Wrapper class for a CUDA context.
Definition: context.hpp:244
void synchronize() const
Block or busy-wait until all previously-scheduled work on this stream has been completed.
Definition: stream.hpp:831
Definitions and functionality wrapping CUDA APIs.
Definition: array.hpp:22
handle_t export_(const event_t &event)
Enable use of an event which this process created by other processes.
Definition: event.hpp:71
Wrapper class for a CUDA event.
Definition: event.hpp:133
void fire(const stream_t &stream) const
Records the event and ensures it has occurred before returning (by synchronizing the stream)...
Definition: event.hpp:124
event_t create(const context_t &context, bool uses_blocking_sync=sync_by_busy_waiting, bool records_timing=do_record_timings, bool interprocess=not_interprocess)
creates a new event.
Definition: event.hpp:30
device::id_t id() const noexcept
Return the proxied device&#39;s ID.
Definition: device.hpp:594
CUipcEventHandle handle_t
The concrete value passed between processes, used to tell the CUDA Runtime API which event is desired...
Definition: ipc.hpp:260
event_t wrap(device::id_t device_id, context::handle_t context_handle, handle_t event_handle, bool take_ownership=false, bool hold_pc_refcount_unit=false) noexcept
Wrap an existing CUDA event in a event_t instance.
Definition: event.hpp:346
event_t create(const device_t &device, bool uses_blocking_sync=sync_by_busy_waiting, bool records_timing=do_record_timings, bool interprocess=not_interprocess)
creates a new event on (the primary execution context of) a device.
Definition: event.hpp:45
device::primary_context_t primary_context(bool hold_pc_refcount_unit=false) const
Produce a proxy for the device&#39;s primary context - the one used by runtime API calls.
Definition: device.hpp:152
device_t get(id_t id)
Returns a proxy for the CUDA device with a given id.
Definition: device.hpp:837
void record() const
Schedule a specified event to occur (= to fire) when all activities already scheduled on the event&#39;s ...
Definition: event.hpp:196
device_t device() const
The device w.r.t. which the event is defined.
Definition: event.hpp:103
Can be shared between processes. Must not be able to record timings.
Definition: constants.hpp:96
Wrapper class for a CUDA device.
Definition: device.hpp:135
context_t context() const
The context in which this stream was defined.
Definition: event.hpp:108