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 {
25 inline namespace _V1 {
26 namespace ext::oneapi::experimental::detail {
27 class graph_impl;
28 }
29 class context;
30 namespace detail {
31 class plugin;
32 class context_impl;
33 using ContextImplPtr = std::shared_ptr<sycl::detail::context_impl>;
34 class queue_impl;
35 using QueueImplPtr = std::shared_ptr<sycl::detail::queue_impl>;
36 class event_impl;
37 using EventImplPtr = std::shared_ptr<sycl::detail::event_impl>;
38 
39 class event_impl {
40 public:
41  enum HostEventState : int {
45  };
46 
52  event_impl(std::optional<HostEventState> State = HES_Complete)
53  : MIsInitialized(false), MHostEvent(State), MIsFlushed(true),
54  MState(State.value_or(HES_Complete)) {}
55 
63  event_impl(sycl::detail::pi::PiEvent Event, const context &SyclContext);
64  event_impl(const QueueImplPtr &Queue);
65 
70  //
72  bool is_host();
73 
79  void wait(std::shared_ptr<sycl::detail::event_impl> Self);
80 
89  void wait_and_throw(std::shared_ptr<sycl::detail::event_impl> Self);
90 
102  template <typename Param> typename Param::return_type get_profiling_info();
103 
107  template <typename Param> typename Param::return_type get_info();
108 
109  ~event_impl();
110 
112  void waitInternal();
113 
115  void setComplete();
116 
127 
132 
135  const PluginPtr &getPlugin();
136 
143  void setContextImpl(const ContextImplPtr &Context);
144 
146  void setStateIncomplete();
147 
153  void *getCommand() { return MCommand; }
154 
160  void setCommand(void *Command) { MCommand = Command; }
161 
166 
171 
175  std::vector<std::shared_ptr<event_impl>> &getPreparedDepsEvents() {
176  return MPreparedDepsEvents;
177  }
178 
182  std::vector<std::shared_ptr<event_impl>> &getPreparedHostDepsEvents() {
184  }
185 
189  std::vector<EventImplPtr> getWaitList();
190 
194  void flushIfNeeded(const QueueImplPtr &UserQueue);
195 
198 
201 
205  bool isDiscarded() const { return MState == HES_Discarded; }
206 
212 
216  void setWorkerQueue(const QueueImplPtr &WorkerQueue) {
217  MWorkerQueue = WorkerQueue;
218  };
219 
223  void setSubmittedQueue(const QueueImplPtr &SubmittedQueue) {
224  MSubmittedQueue = SubmittedQueue;
225  };
226 
229  void setSubmissionTime();
230 
233  void setHostEnqueueTime();
234 
236  uint64_t getSubmissionTime();
237 
238  QueueImplPtr getSubmittedQueue() const { return MSubmittedQueue.lock(); };
239 
247  bool isInitialized() const noexcept { return MIsInitialized; }
248 
252  bool isCompleted();
253 
254  void attachEventToComplete(const EventImplPtr &Event) {
255  std::lock_guard<std::mutex> Lock(MMutex);
256  MPostCompleteEvents.push_back(Event);
257  }
258 
260 
263  return MContext;
264  }
265 
266  // Sets a sync point which is used when this event represents an enqueue to a
267  // Command Bufferr.
269  MSyncPoint = SyncPoint;
270  }
271 
272  // Get the sync point associated with this event.
274 
276  std::shared_ptr<ext::oneapi::experimental::detail::graph_impl> Graph) {
277  MGraph = Graph;
278  }
279 
280  std::shared_ptr<ext::oneapi::experimental::detail::graph_impl>
281  getCommandGraph() const {
282  return MGraph.lock();
283  }
284 
287  }
288 
291  }
292 
293 protected:
294  // When instrumentation is enabled emits trace event for event wait begin and
295  // returns the telemetry event generated for the wait
296  void *instrumentationProlog(std::string &Name, int32_t StreamID,
297  uint64_t &instance_id) const;
298  // Uses events generated by the Prolog and emits event wait done event
299  void instrumentationEpilog(void *TelementryEvent, const std::string &Name,
300  int32_t StreamID, uint64_t IId) const;
301  void checkProfilingPreconditions() const;
302  // Events constructed without a context will lazily use the default context
303  // when needed.
305  bool MIsInitialized = true;
306  bool MIsContextInitialized = false;
308  // Stores submission time of command associated with event
309  uint64_t MSubmitTime = 0;
310  uint64_t MHostBaseTime = 0;
312  bool MHostEvent = true;
313  std::unique_ptr<HostProfilingInfo> MHostProfilingInfo;
314  void *MCommand = nullptr;
315  std::weak_ptr<queue_impl> MQueue;
316  const bool MIsProfilingEnabled = false;
317  const bool MFallbackProfiling = false;
318 
319  std::weak_ptr<queue_impl> MWorkerQueue;
320  std::weak_ptr<queue_impl> MSubmittedQueue;
321 
323  std::vector<EventImplPtr> MPreparedDepsEvents;
324  std::vector<EventImplPtr> MPreparedHostDepsEvents;
325 
326  std::vector<EventImplPtr> MPostCompleteEvents;
327 
330  std::atomic<bool> MIsFlushed = false;
331 
332  // State of host event. Employed only for host events and event with no
333  // backend's representation (e.g. alloca). Used values are listed in
334  // HostEventState enum.
335  std::atomic<int> MState;
336 
337  std::mutex MMutex;
338  std::condition_variable cv;
339 
342  std::weak_ptr<ext::oneapi::experimental::detail::graph_impl> MGraph;
345 
346  // If this event represents a submission to a
347  // sycl::detail::pi::PiExtCommandBuffer the sync point for that submission is
348  // stored here.
350 
351  friend std::vector<sycl::detail::pi::PiEvent>
352  getOrWaitEvents(std::vector<sycl::event> DepEvents,
353  std::shared_ptr<sycl::detail::context_impl> Context);
354 };
355 
356 } // namespace detail
357 } // namespace _V1
358 } // namespace sycl
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:51
The Command class represents some action that needs to be performed on one or more memory objects.
Definition: commands.hpp:102
Profiling info for the host execution.
void setCommandGraph(std::shared_ptr< ext::oneapi::experimental::detail::graph_impl > Graph)
Definition: event_impl.hpp:275
void attachEventToComplete(const EventImplPtr &Event)
Definition: event_impl.hpp:254
void setSubmittedQueue(const QueueImplPtr &SubmittedQueue)
Sets original queue used for submission.
Definition: event_impl.hpp:223
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:175
void checkProfilingPreconditions() const
Definition: event_impl.cpp:267
Param::return_type get_profiling_info()
Queries this event for profiling information.
std::vector< EventImplPtr > getWaitList()
Returns vector of event_impl that this event_impl depends on.
Definition: event_impl.cpp:415
void * instrumentationProlog(std::string &Name, int32_t StreamID, uint64_t &instance_id) const
Definition: event_impl.cpp:176
friend std::vector< sycl::detail::pi::PiEvent > getOrWaitEvents(std::vector< sycl::event > DepEvents, std::shared_ptr< sycl::detail::context_impl > Context)
std::vector< EventImplPtr > MPostCompleteEvents
Definition: event_impl.hpp:326
void cleanDepEventsThroughOneLevel()
Cleans dependencies of this event's dependencies.
Definition: event_impl.cpp:466
void setComplete()
Marks this event as completed.
Definition: event_impl.cpp:81
pi_native_handle getNative()
Gets the native handle of the SYCL event.
Definition: event_impl.cpp:399
bool MEventFromSubmittedExecCommandBuffer
Indicates that the event results from a command graph submission.
Definition: event_impl.hpp:344
void * getCommand()
Returns command that is associated with the event.
Definition: event_impl.hpp:153
std::unique_ptr< HostProfilingInfo > MHostProfilingInfo
Definition: event_impl.hpp:313
std::vector< std::shared_ptr< event_impl > > & getPreparedHostDepsEvents()
Returns vector of host event dependencies.
Definition: event_impl.hpp:182
bool isContextInitialized() const noexcept
Definition: event_impl.hpp:259
void setEventFromSubmittedExecCommandBuffer(bool value)
Definition: event_impl.hpp:285
void setContextImpl(const ContextImplPtr &Context)
Associate event with the context.
Definition: event_impl.cpp:126
void setHostEnqueueTime()
Calling this function to capture the host timestamp to use profiling base time.
Definition: event_impl.cpp:499
std::weak_ptr< ext::oneapi::experimental::detail::graph_impl > MGraph
Store the command graph associated with this event, if any.
Definition: event_impl.hpp:342
bool isDiscarded() const
Checks if this event is discarded by SYCL implementation.
Definition: event_impl.hpp:205
const ContextImplPtr & getContextImpl()
Returns context that is associated with this event.
Definition: event_impl.cpp:114
void flushIfNeeded(const QueueImplPtr &UserQueue)
Performs a flush on the queue associated with this event if the user queue is different and the task ...
Definition: event_impl.cpp:433
std::weak_ptr< queue_impl > MSubmittedQueue
Definition: event_impl.hpp:320
void setSyncPoint(sycl::detail::pi::PiExtSyncPoint SyncPoint)
Definition: event_impl.hpp:268
std::atomic< int > MState
Definition: event_impl.hpp:335
sycl::detail::pi::PiEvent MEvent
Definition: event_impl.hpp:307
void instrumentationEpilog(void *TelementryEvent, const std::string &Name, int32_t StreamID, uint64_t IId) const
Definition: event_impl.cpp:212
event_impl(std::optional< HostEventState > State=HES_Complete)
Constructs a ready SYCL event.
Definition: event_impl.hpp:52
bool isEventFromSubmittedExecCommandBuffer() const
Definition: event_impl.hpp:289
sycl::detail::pi::PiExtSyncPoint MSyncPoint
Definition: event_impl.hpp:349
bool isCompleted()
Checks if this event is complete.
Definition: event_impl.cpp:509
sycl::detail::pi::PiExtSyncPoint getSyncPoint() const
Definition: event_impl.hpp:273
QueueImplPtr getWorkerQueue()
Returns worker queue for command.
Definition: event_impl.hpp:211
std::vector< EventImplPtr > MPreparedHostDepsEvents
Definition: event_impl.hpp:324
QueueImplPtr getSubmittedQueue() const
Definition: event_impl.hpp:238
void setStateIncomplete()
Clear the event state.
Definition: event_impl.cpp:124
std::condition_variable cv
Definition: event_impl.hpp:338
void waitInternal()
Waits for the event with respect to device type.
Definition: event_impl.cpp:61
std::vector< EventImplPtr > MPreparedDepsEvents
Dependency events prepared for waiting by backend.
Definition: event_impl.hpp:323
bool isInitialized() const noexcept
Checks if an event is in a fully intialized state.
Definition: event_impl.hpp:247
void setSubmissionTime()
Calling this function queries the current device timestamp and sets it as submission time for the com...
Definition: event_impl.cpp:476
void cleanupDependencyEvents()
Cleans dependencies of this event_impl.
Definition: event_impl.cpp:460
void wait_and_throw(std::shared_ptr< sycl::detail::event_impl > Self)
Waits for the event.
Definition: event_impl.cpp:259
void setWorkerQueue(const QueueImplPtr &WorkerQueue)
Sets worker queue for command.
Definition: event_impl.hpp:216
void wait(std::shared_ptr< sycl::detail::event_impl > Self)
Waits for the event.
Definition: event_impl.cpp:228
void setCommand(void *Command)
Associates this event with the command.
Definition: event_impl.hpp:160
const PluginPtr & getPlugin()
Definition: event_impl.cpp:119
sycl::detail::pi::PiEvent & getHandleRef()
Returns raw interoperability event handle.
Definition: event_impl.cpp:112
bool is_host()
Checks if this event is a SYCL host event.
Definition: event_impl.cpp:50
HostProfilingInfo * getHostProfilingInfo()
Returns host profiling information.
Definition: event_impl.hpp:165
std::atomic< bool > MIsFlushed
Indicates that the task associated with this event has been submitted by the queue to the device.
Definition: event_impl.hpp:330
std::weak_ptr< queue_impl > MWorkerQueue
Definition: event_impl.hpp:319
std::weak_ptr< queue_impl > MQueue
Definition: event_impl.hpp:315
std::shared_ptr< ext::oneapi::experimental::detail::graph_impl > getCommandGraph() const
Definition: event_impl.hpp:281
ContextImplPtr getContextImplPtr()
Definition: event_impl.hpp:261
::pi_ext_sync_point PiExtSyncPoint
Definition: pi.hpp:156
std::shared_ptr< sycl::detail::context_impl > ContextImplPtr
Definition: event_impl.hpp:33
std::shared_ptr< event_impl > EventImplPtr
Definition: cg.hpp:43
std::shared_ptr< plugin > PluginPtr
Definition: pi.hpp:48
std::shared_ptr< sycl::detail::queue_impl > QueueImplPtr
Definition: event_impl.hpp:35
Definition: access.hpp:18
uintptr_t pi_native_handle
Definition: pi.h:204
C++ wrapper of extern "C" PI interfaces.
_Abi const simd< _Tp, _Abi > & noexcept
Definition: simd.hpp:1324