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 
18 #include <atomic>
19 #include <cassert>
20 #include <condition_variable>
21 #include <optional>
22 
23 namespace sycl {
24 inline namespace _V1 {
25 namespace ext::oneapi::experimental::detail {
26 class graph_impl;
27 }
28 class context;
29 namespace detail {
30 class plugin;
31 class context_impl;
32 using ContextImplPtr = std::shared_ptr<sycl::detail::context_impl>;
33 class queue_impl;
34 using QueueImplPtr = std::shared_ptr<sycl::detail::queue_impl>;
35 class event_impl;
36 using EventImplPtr = std::shared_ptr<sycl::detail::event_impl>;
37 
38 class event_impl {
39 public:
40  enum HostEventState : int {
44  };
45 
51  event_impl(std::optional<HostEventState> State = HES_Complete)
52  : MIsInitialized(false), MHostEvent(State), MIsFlushed(true),
53  MState(State.value_or(HES_Complete)) {
54  // Need to fail in event() constructor if there are problems with the
55  // ONEAPI_DEVICE_SELECTOR. Deferring may lead to conficts with noexcept
56  // event methods. This ::get() call uses static vars to read and parse the
57  // ODS env var exactly once.
59  }
60 
68  event_impl(sycl::detail::pi::PiEvent Event, const context &SyclContext);
69  event_impl(const QueueImplPtr &Queue);
70 
75  //
77  bool is_host();
78 
88  void wait(std::shared_ptr<sycl::detail::event_impl> Self,
89  bool *Success = nullptr);
90 
99  void wait_and_throw(std::shared_ptr<sycl::detail::event_impl> Self);
100 
112  template <typename Param> typename Param::return_type get_profiling_info();
113 
117  template <typename Param> typename Param::return_type get_info();
118 
122  template <typename Param>
123  typename Param::return_type get_backend_info() const;
124 
125  ~event_impl();
126 
132  void waitInternal(bool *Success = nullptr);
133 
135  void setComplete();
136 
147 
152 
155  const PluginPtr &getPlugin();
156 
163  void setContextImpl(const ContextImplPtr &Context);
164 
166  void setStateIncomplete();
167 
173  void *getCommand() { return MCommand; }
174 
180  void setCommand(void *Command) { MCommand = Command; }
181 
186 
191 
195  std::vector<std::shared_ptr<event_impl>> &getPreparedDepsEvents() {
196  return MPreparedDepsEvents;
197  }
198 
202  std::vector<std::shared_ptr<event_impl>> &getPreparedHostDepsEvents() {
204  }
205 
209  std::vector<EventImplPtr> getWaitList();
210 
214  void flushIfNeeded(const QueueImplPtr &UserQueue);
215 
218 
221 
225  bool isDiscarded() const { return MState == HES_Discarded; }
226 
232 
236  void setWorkerQueue(const QueueImplPtr &WorkerQueue) {
237  MWorkerQueue = WorkerQueue;
238  };
239 
243  void setSubmittedQueue(const QueueImplPtr &SubmittedQueue) {
244  MSubmittedQueue = SubmittedQueue;
245  };
246 
250  void associateWithQueue(const QueueImplPtr &Queue);
251 
256  bool isNOP() { return !MCommand && !getHandleRef(); }
257 
260  void setSubmissionTime();
261 
264  void setHostEnqueueTime();
265 
267  uint64_t getSubmissionTime();
268 
269  QueueImplPtr getSubmittedQueue() const { return MSubmittedQueue.lock(); };
270 
278  bool isInitialized() const noexcept { return MIsInitialized; }
279 
283  bool isCompleted();
284 
285  void attachEventToComplete(const EventImplPtr &Event) {
286  std::lock_guard<std::mutex> Lock(MMutex);
287  MPostCompleteEvents.push_back(Event);
288  }
289 
291 
294  return MContext;
295  }
296 
297  // Sets a sync point which is used when this event represents an enqueue to a
298  // Command Buffer.
300  MSyncPoint = SyncPoint;
301  }
302 
303  // Get the sync point associated with this event.
305 
307  std::shared_ptr<ext::oneapi::experimental::detail::graph_impl> Graph) {
308  MGraph = Graph;
309  }
310 
311  std::shared_ptr<ext::oneapi::experimental::detail::graph_impl>
312  getCommandGraph() const {
313  return MGraph.lock();
314  }
315 
318  }
319 
322  }
323 
324  // Sets a command-buffer command when this event represents an enqueue to a
325  // Command Buffer.
326  void
329  }
330 
332  return MCommandBufferCommand;
333  }
334 
335  const std::vector<EventImplPtr> &getPostCompleteEvents() const {
336  return MPostCompleteEvents;
337  }
338 
339 protected:
340  // When instrumentation is enabled emits trace event for event wait begin and
341  // returns the telemetry event generated for the wait
342  void *instrumentationProlog(std::string &Name, int32_t StreamID,
343  uint64_t &instance_id) const;
344  // Uses events generated by the Prolog and emits event wait done event
345  void instrumentationEpilog(void *TelementryEvent, const std::string &Name,
346  int32_t StreamID, uint64_t IId) const;
347  void checkProfilingPreconditions() const;
348  // Events constructed without a context will lazily use the default context
349  // when needed.
351  bool MIsInitialized = true;
352  bool MIsContextInitialized = false;
354  // Stores submission time of command associated with event
355  uint64_t MSubmitTime = 0;
356  uint64_t MHostBaseTime = 0;
358  bool MHostEvent = true;
359  std::unique_ptr<HostProfilingInfo> MHostProfilingInfo;
360  void *MCommand = nullptr;
361  std::weak_ptr<queue_impl> MQueue;
362  bool MIsProfilingEnabled = false;
363  bool MFallbackProfiling = false;
364 
365  std::weak_ptr<queue_impl> MWorkerQueue;
366  std::weak_ptr<queue_impl> MSubmittedQueue;
367 
369  std::vector<EventImplPtr> MPreparedDepsEvents;
370  std::vector<EventImplPtr> MPreparedHostDepsEvents;
371 
372  std::vector<EventImplPtr> MPostCompleteEvents;
373 
376  std::atomic<bool> MIsFlushed = false;
377 
378  // State of host event. Employed only for host events and event with no
379  // backend's representation (e.g. alloca). Used values are listed in
380  // HostEventState enum.
381  std::atomic<int> MState;
382 
383  std::mutex MMutex;
384  std::condition_variable cv;
385 
388  std::weak_ptr<ext::oneapi::experimental::detail::graph_impl> MGraph;
391 
392  // If this event represents a submission to a
393  // sycl::detail::pi::PiExtCommandBuffer the sync point for that submission is
394  // stored here.
396 
397  // If this event represents a submission to a
398  // sycl::detail::pi::PiExtCommandBuffer the command-buffer command
399  // (if any) associated with that submission is stored here.
401 
402  friend std::vector<sycl::detail::pi::PiEvent>
403  getOrWaitEvents(std::vector<sycl::event> DepEvents,
404  std::shared_ptr<sycl::detail::context_impl> Context);
405 };
406 
407 } // namespace detail
408 } // namespace _V1
409 } // namespace sycl
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:50
The Command class represents some action that needs to be performed on one or more memory objects.
Definition: commands.hpp:107
Profiling info for the host execution.
static const char * get()
Definition: config.hpp:115
Param::return_type get_backend_info() const
Queries this SYCL event for SYCL backend-specific information.
void setCommandGraph(std::shared_ptr< ext::oneapi::experimental::detail::graph_impl > Graph)
Definition: event_impl.hpp:306
void attachEventToComplete(const EventImplPtr &Event)
Definition: event_impl.hpp:285
void setSubmittedQueue(const QueueImplPtr &SubmittedQueue)
Sets original queue used for submission.
Definition: event_impl.hpp:243
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:195
void checkProfilingPreconditions() const
Definition: event_impl.cpp:285
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:499
void * instrumentationProlog(std::string &Name, int32_t StreamID, uint64_t &instance_id) const
Definition: event_impl.cpp:193
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:372
void cleanDepEventsThroughOneLevel()
Cleans dependencies of this event's dependencies.
Definition: event_impl.cpp:550
void waitInternal(bool *Success=nullptr)
Waits for the event with respect to device type.
Definition: event_impl.cpp:61
void setComplete()
Marks this event as completed.
Definition: event_impl.cpp:94
pi_native_handle getNative()
Gets the native handle of the SYCL event.
Definition: event_impl.cpp:483
bool MEventFromSubmittedExecCommandBuffer
Indicates that the event results from a command graph submission.
Definition: event_impl.hpp:390
void * getCommand()
Returns command that is associated with the event.
Definition: event_impl.hpp:173
std::unique_ptr< HostProfilingInfo > MHostProfilingInfo
Definition: event_impl.hpp:359
std::vector< std::shared_ptr< event_impl > > & getPreparedHostDepsEvents()
Returns vector of host event dependencies.
Definition: event_impl.hpp:202
bool isContextInitialized() const noexcept
Definition: event_impl.hpp:290
void setCommandBufferCommand(sycl::detail::pi::PiExtCommandBufferCommand Command)
Definition: event_impl.hpp:327
void setEventFromSubmittedExecCommandBuffer(bool value)
Definition: event_impl.hpp:316
void setContextImpl(const ContextImplPtr &Context)
Associate event with the context.
Definition: event_impl.cpp:139
void setHostEnqueueTime()
Calling this function to capture the host timestamp to use profiling base time.
Definition: event_impl.cpp:583
std::weak_ptr< ext::oneapi::experimental::detail::graph_impl > MGraph
Store the command graph associated with this event, if any.
Definition: event_impl.hpp:388
bool isDiscarded() const
Checks if this event is discarded by SYCL implementation.
Definition: event_impl.hpp:225
const ContextImplPtr & getContextImpl()
Returns context that is associated with this event.
Definition: event_impl.cpp:127
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:517
std::weak_ptr< queue_impl > MSubmittedQueue
Definition: event_impl.hpp:366
void setSyncPoint(sycl::detail::pi::PiExtSyncPoint SyncPoint)
Definition: event_impl.hpp:299
std::atomic< int > MState
Definition: event_impl.hpp:381
sycl::detail::pi::PiEvent MEvent
Definition: event_impl.hpp:353
void instrumentationEpilog(void *TelementryEvent, const std::string &Name, int32_t StreamID, uint64_t IId) const
Definition: event_impl.cpp:229
event_impl(std::optional< HostEventState > State=HES_Complete)
Constructs a ready SYCL event.
Definition: event_impl.hpp:51
bool isEventFromSubmittedExecCommandBuffer() const
Definition: event_impl.hpp:320
void associateWithQueue(const QueueImplPtr &Queue)
Associate event with provided queue.
Definition: event_impl.cpp:175
sycl::detail::pi::PiExtSyncPoint MSyncPoint
Definition: event_impl.hpp:395
bool isCompleted()
Checks if this event is complete.
Definition: event_impl.cpp:593
sycl::detail::pi::PiExtSyncPoint getSyncPoint() const
Definition: event_impl.hpp:304
QueueImplPtr getWorkerQueue()
Returns worker queue for command.
Definition: event_impl.hpp:231
std::vector< EventImplPtr > MPreparedHostDepsEvents
Definition: event_impl.hpp:370
void wait(std::shared_ptr< sycl::detail::event_impl > Self, bool *Success=nullptr)
Waits for the event.
Definition: event_impl.cpp:245
QueueImplPtr getSubmittedQueue() const
Definition: event_impl.hpp:269
void setStateIncomplete()
Clear the event state.
Definition: event_impl.cpp:137
std::condition_variable cv
Definition: event_impl.hpp:384
sycl::detail::pi::PiExtCommandBufferCommand getCommandBufferCommand() const
Definition: event_impl.hpp:331
std::vector< EventImplPtr > MPreparedDepsEvents
Dependency events prepared for waiting by backend.
Definition: event_impl.hpp:369
bool isInitialized() const noexcept
Checks if an event is in a fully intialized state.
Definition: event_impl.hpp:278
void setSubmissionTime()
Calling this function queries the current device timestamp and sets it as submission time for the com...
Definition: event_impl.cpp:560
void cleanupDependencyEvents()
Cleans dependencies of this event_impl.
Definition: event_impl.cpp:544
const std::vector< EventImplPtr > & getPostCompleteEvents() const
Definition: event_impl.hpp:335
void wait_and_throw(std::shared_ptr< sycl::detail::event_impl > Self)
Waits for the event.
Definition: event_impl.cpp:277
void setWorkerQueue(const QueueImplPtr &WorkerQueue)
Sets worker queue for command.
Definition: event_impl.hpp:236
void setCommand(void *Command)
Associates this event with the command.
Definition: event_impl.hpp:180
const PluginPtr & getPlugin()
Definition: event_impl.cpp:132
bool isNOP()
Indicates if this event is not associated with any command and doesn't have native handle.
Definition: event_impl.hpp:256
sycl::detail::pi::PiEvent & getHandleRef()
Returns raw interoperability event handle.
Definition: event_impl.cpp:125
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:185
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:376
std::weak_ptr< queue_impl > MWorkerQueue
Definition: event_impl.hpp:365
sycl::detail::pi::PiExtCommandBufferCommand MCommandBufferCommand
Definition: event_impl.hpp:400
std::weak_ptr< queue_impl > MQueue
Definition: event_impl.hpp:361
std::shared_ptr< ext::oneapi::experimental::detail::graph_impl > getCommandGraph() const
Definition: event_impl.hpp:312
ContextImplPtr getContextImplPtr()
Definition: event_impl.hpp:292
::pi_ext_sync_point PiExtSyncPoint
Definition: pi.hpp:156
::pi_ext_command_buffer_command PiExtCommandBufferCommand
Definition: pi.hpp:159
std::shared_ptr< sycl::detail::context_impl > ContextImplPtr
Definition: event_impl.hpp:32
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:34
Definition: access.hpp:18
uintptr_t pi_native_handle
Definition: pi.h:217
C++ wrapper of extern "C" PI interfaces.
_Abi const simd< _Tp, _Abi > & noexcept
Definition: simd.hpp:1324