DPC++ Runtime
Runtime libraries for oneAPI DPC++
event_impl.hpp
Go to the documentation of this file.
1 //==---------------- event_impl.hpp - SYCL event ---------------------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #pragma once
10 
11 #include <detail/plugin.hpp>
12 #include <sycl/detail/cl.h>
13 #include <sycl/detail/common.hpp>
15 #include <sycl/detail/pi.hpp>
16 #include <sycl/info/info_desc.hpp>
17 #include <sycl/stl.hpp>
18 
19 #include <atomic>
20 #include <cassert>
21 #include <condition_variable>
22 #include <optional>
23 
24 namespace sycl {
26 class context;
27 namespace detail {
28 class plugin;
29 class context_impl;
30 using ContextImplPtr = std::shared_ptr<sycl::detail::context_impl>;
31 class queue_impl;
32 using QueueImplPtr = std::shared_ptr<sycl::detail::queue_impl>;
33 class event_impl;
34 using EventImplPtr = std::shared_ptr<sycl::detail::event_impl>;
35 
36 class event_impl {
37 public:
38  enum HostEventState : int {
39  HES_NotComplete = 0,
41  HES_Discarded
42  };
43 
49  event_impl(std::optional<HostEventState> State = HES_Complete)
50  : MIsInitialized(false), MHostEvent(State), MIsFlushed(true),
51  MState(State.value_or(HES_Complete)) {}
52 
60  event_impl(RT::PiEvent Event, const context &SyclContext);
61  event_impl(const QueueImplPtr &Queue);
62 
67  //
69  bool is_host();
70 
76  void wait(std::shared_ptr<sycl::detail::event_impl> Self);
77 
86  void wait_and_throw(std::shared_ptr<sycl::detail::event_impl> Self);
87 
92  void cleanupCommand(std::shared_ptr<sycl::detail::event_impl> Self) const;
93 
105  template <typename Param> typename Param::return_type get_profiling_info();
106 
110  template <typename Param> typename Param::return_type get_info();
111 
112  ~event_impl();
113 
115  void waitInternal();
116 
118  void setComplete();
119 
124  RT::PiEvent &getHandleRef();
129  const RT::PiEvent &getHandleRef() const;
130 
134  const ContextImplPtr &getContextImpl();
135 
138  const plugin &getPlugin();
139 
146  void setContextImpl(const ContextImplPtr &Context);
147 
149  void setStateIncomplete();
150 
156  void *getCommand() { return MCommand; }
157 
163  void setCommand(void *Command) { MCommand = Command; }
164 
168  HostProfilingInfo *getHostProfilingInfo() { return MHostProfilingInfo.get(); }
169 
173  pi_native_handle getNative();
174 
178  std::vector<std::shared_ptr<event_impl>> &getPreparedDepsEvents() {
179  return MPreparedDepsEvents;
180  }
181 
185  std::vector<std::shared_ptr<event_impl>> &getPreparedHostDepsEvents() {
186  return MPreparedHostDepsEvents;
187  }
188 
192  std::vector<EventImplPtr> getWaitList();
193 
197  void flushIfNeeded(const QueueImplPtr &UserQueue);
198 
200  void cleanupDependencyEvents();
201 
203  void cleanDepEventsThroughOneLevel();
204 
208  bool isDiscarded() const { return MState == HES_Discarded; }
209 
210  void setNeedsCleanupAfterWait(bool NeedsCleanupAfterWait) {
211  MNeedsCleanupAfterWait = NeedsCleanupAfterWait;
212  }
213  bool needsCleanupAfterWait() { return MNeedsCleanupAfterWait; }
214 
219  QueueImplPtr getWorkerQueue() { return MWorkerQueue.lock(); };
220 
224  void setWorkerQueue(const QueueImplPtr &WorkerQueue) {
225  MWorkerQueue = WorkerQueue;
226  };
227 
231  void setSubmittedQueue(const QueueImplPtr &SubmittedQueue) {
232  MSubmittedQueue = SubmittedQueue;
233  };
234 
235  QueueImplPtr getSubmittedQueue() const { return MSubmittedQueue.lock(); };
236 
244  bool isInitialized() const noexcept { return MIsInitialized; }
245 
246  void attachEventToComplete(const EventImplPtr &Event) {
247  std::lock_guard<std::mutex> Lock(MMutex);
248  MPostCompleteEvents.push_back(Event);
249  }
250 
251 protected:
252  // When instrumentation is enabled emits trace event for event wait begin and
253  // returns the telemetry event generated for the wait
254  void *instrumentationProlog(std::string &Name, int32_t StreamID,
255  uint64_t &instance_id) const;
256  // Uses events generated by the Prolog and emits event wait done event
257  void instrumentationEpilog(void *TelementryEvent, const std::string &Name,
258  int32_t StreamID, uint64_t IId) const;
259  void checkProfilingPreconditions() const;
260  // Events constructed without a context will lazily use the default context
261  // when needed.
262  void ensureContextInitialized();
263  bool MIsInitialized = true;
264  bool MIsContextInitialized = false;
265  RT::PiEvent MEvent = nullptr;
267  bool MHostEvent = true;
268  std::unique_ptr<HostProfilingInfo> MHostProfilingInfo;
269  void *MCommand = nullptr;
270  std::weak_ptr<queue_impl> MQueue;
271  const bool MIsProfilingEnabled = false;
272 
273  std::weak_ptr<queue_impl> MWorkerQueue;
274  std::weak_ptr<queue_impl> MSubmittedQueue;
275 
277  std::vector<EventImplPtr> MPreparedDepsEvents;
278  std::vector<EventImplPtr> MPreparedHostDepsEvents;
279 
280  std::vector<EventImplPtr> MPostCompleteEvents;
281 
284  std::atomic<bool> MIsFlushed = false;
285 
286  // State of host event. Employed only for host events and event with no
287  // backend's representation (e.g. alloca). Used values are listed in
288  // HostEventState enum.
289  std::atomic<int> MState;
290 
291  // A temporary workaround for the current limitations of post enqueue graph
292  // cleanup. Indicates that the command associated with this event isn't
293  // handled by post enqueue cleanup yet and has to be deleted by cleanup after
294  // wait.
295  bool MNeedsCleanupAfterWait = false;
296 
297  std::mutex MMutex;
298  std::condition_variable cv;
299 
300  friend std::vector<RT::PiEvent>
301  getOrWaitEvents(std::vector<sycl::event> DepEvents,
302  std::shared_ptr<sycl::detail::context_impl> Context);
303 };
304 
305 } // namespace detail
306 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
307 } // namespace sycl
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:41
The Command class represents some action that needs to be performed on one or more memory objects.
Definition: commands.hpp:95
Profiling info for the host execution.
void attachEventToComplete(const EventImplPtr &Event)
Definition: event_impl.hpp:246
void setSubmittedQueue(const QueueImplPtr &SubmittedQueue)
Sets original queue used for submission.
Definition: event_impl.hpp:231
Param::return_type get_info()
Queries this SYCL event for information.
std::vector< std::shared_ptr< event_impl > > & getPreparedDepsEvents()
Returns vector of event dependencies.
Definition: event_impl.hpp:178
Param::return_type get_profiling_info()
Queries this event for profiling information.
std::vector< EventImplPtr > MPostCompleteEvents
Definition: event_impl.hpp:280
void * getCommand()
Returns command that is associated with the event.
Definition: event_impl.hpp:156
std::unique_ptr< HostProfilingInfo > MHostProfilingInfo
Definition: event_impl.hpp:268
std::vector< std::shared_ptr< event_impl > > & getPreparedHostDepsEvents()
Returns vector of host event dependencies.
Definition: event_impl.hpp:185
bool isDiscarded() const
Checks if this event is discarded by SYCL implementation.
Definition: event_impl.hpp:208
std::weak_ptr< queue_impl > MSubmittedQueue
Definition: event_impl.hpp:274
std::atomic< int > MState
Definition: event_impl.hpp:289
event_impl(std::optional< HostEventState > State=HES_Complete)
Constructs a ready SYCL event.
Definition: event_impl.hpp:49
void setNeedsCleanupAfterWait(bool NeedsCleanupAfterWait)
Definition: event_impl.hpp:210
QueueImplPtr getWorkerQueue()
Returns worker queue for command.
Definition: event_impl.hpp:219
friend std::vector< RT::PiEvent > getOrWaitEvents(std::vector< sycl::event > DepEvents, std::shared_ptr< sycl::detail::context_impl > Context)
std::vector< EventImplPtr > MPreparedHostDepsEvents
Definition: event_impl.hpp:278
QueueImplPtr getSubmittedQueue() const
Definition: event_impl.hpp:235
std::condition_variable cv
Definition: event_impl.hpp:298
std::vector< EventImplPtr > MPreparedDepsEvents
Dependency events prepared for waiting by backend.
Definition: event_impl.hpp:277
bool isInitialized() const noexcept
Checks if an event is in a fully intialized state.
Definition: event_impl.hpp:244
void setWorkerQueue(const QueueImplPtr &WorkerQueue)
Sets worker queue for command.
Definition: event_impl.hpp:224
void setCommand(void *Command)
Associates this event with the command.
Definition: event_impl.hpp:163
HostProfilingInfo * getHostProfilingInfo()
Returns host profiling information.
Definition: event_impl.hpp:168
std::weak_ptr< queue_impl > MWorkerQueue
Definition: event_impl.hpp:273
std::weak_ptr< queue_impl > MQueue
Definition: event_impl.hpp:270
The plugin class provides a unified interface to the underlying low-level runtimes for the device-agn...
Definition: plugin.hpp:90
#define __SYCL_INLINE_VER_NAMESPACE(X)
::pi_event PiEvent
Definition: pi.hpp:121
const plugin & getPlugin()
Definition: pi.cpp:509
std::shared_ptr< sycl::detail::context_impl > ContextImplPtr
Definition: event_impl.hpp:30
std::shared_ptr< event_impl > EventImplPtr
Definition: cg.hpp:42
std::shared_ptr< sycl::detail::queue_impl > QueueImplPtr
Definition: event_impl.hpp:32
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
uintptr_t pi_native_handle
Definition: pi.h:111
C++ wrapper of extern "C" PI interfaces.