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 
99  template <typename Param> typename Param::return_type get_profiling_info();
100 
104  template <typename Param> typename Param::return_type get_info();
105 
106  ~event_impl();
107 
109  void waitInternal();
110 
112  void setComplete();
113 
118  RT::PiEvent &getHandleRef();
123  const RT::PiEvent &getHandleRef() const;
124 
128  const ContextImplPtr &getContextImpl();
129 
132  const PluginPtr &getPlugin();
133 
140  void setContextImpl(const ContextImplPtr &Context);
141 
143  void setStateIncomplete();
144 
150  void *getCommand() { return MCommand; }
151 
157  void setCommand(void *Command) { MCommand = Command; }
158 
162  HostProfilingInfo *getHostProfilingInfo() { return MHostProfilingInfo.get(); }
163 
167  pi_native_handle getNative();
168 
172  std::vector<std::shared_ptr<event_impl>> &getPreparedDepsEvents() {
173  return MPreparedDepsEvents;
174  }
175 
179  std::vector<std::shared_ptr<event_impl>> &getPreparedHostDepsEvents() {
180  return MPreparedHostDepsEvents;
181  }
182 
186  std::vector<EventImplPtr> getWaitList();
187 
191  void flushIfNeeded(const QueueImplPtr &UserQueue);
192 
194  void cleanupDependencyEvents();
195 
197  void cleanDepEventsThroughOneLevel();
198 
202  bool isDiscarded() const { return MState == HES_Discarded; }
203 
208  QueueImplPtr getWorkerQueue() { return MWorkerQueue.lock(); };
209 
213  void setWorkerQueue(const QueueImplPtr &WorkerQueue) {
214  MWorkerQueue = WorkerQueue;
215  };
216 
220  void setSubmittedQueue(const QueueImplPtr &SubmittedQueue) {
221  MSubmittedQueue = SubmittedQueue;
222  };
223 
226  void setSubmissionTime();
227 
229  uint64_t getSubmissionTime();
230 
231  QueueImplPtr getSubmittedQueue() const { return MSubmittedQueue.lock(); };
232 
240  bool isInitialized() const noexcept { return MIsInitialized; }
241 
245  bool isCompleted();
246 
247  void attachEventToComplete(const EventImplPtr &Event) {
248  std::lock_guard<std::mutex> Lock(MMutex);
249  MPostCompleteEvents.push_back(Event);
250  }
251 
252  bool isContextInitialized() const noexcept { return MIsContextInitialized; }
253 
255  ensureContextInitialized();
256  return MContext;
257  }
258 
259 protected:
260  // When instrumentation is enabled emits trace event for event wait begin and
261  // returns the telemetry event generated for the wait
262  void *instrumentationProlog(std::string &Name, int32_t StreamID,
263  uint64_t &instance_id) const;
264  // Uses events generated by the Prolog and emits event wait done event
265  void instrumentationEpilog(void *TelementryEvent, const std::string &Name,
266  int32_t StreamID, uint64_t IId) const;
267  void checkProfilingPreconditions() const;
268  // Events constructed without a context will lazily use the default context
269  // when needed.
270  void ensureContextInitialized();
271  bool MIsInitialized = true;
272  bool MIsContextInitialized = false;
273  RT::PiEvent MEvent = nullptr;
274  // Stores submission time of command associated with event
275  uint64_t MSubmitTime = 0;
277  bool MHostEvent = true;
278  std::unique_ptr<HostProfilingInfo> MHostProfilingInfo;
279  void *MCommand = nullptr;
280  std::weak_ptr<queue_impl> MQueue;
281  const bool MIsProfilingEnabled = false;
282  const bool MLimitedProfiling = false;
283 
284  std::weak_ptr<queue_impl> MWorkerQueue;
285  std::weak_ptr<queue_impl> MSubmittedQueue;
286 
288  std::vector<EventImplPtr> MPreparedDepsEvents;
289  std::vector<EventImplPtr> MPreparedHostDepsEvents;
290 
291  std::vector<EventImplPtr> MPostCompleteEvents;
292 
295  std::atomic<bool> MIsFlushed = false;
296 
297  // State of host event. Employed only for host events and event with no
298  // backend's representation (e.g. alloca). Used values are listed in
299  // HostEventState enum.
300  std::atomic<int> MState;
301 
302  std::mutex MMutex;
303  std::condition_variable cv;
304 
305  friend std::vector<RT::PiEvent>
306  getOrWaitEvents(std::vector<sycl::event> DepEvents,
307  std::shared_ptr<sycl::detail::context_impl> Context);
308 };
309 
310 } // namespace detail
311 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
312 } // namespace sycl
sycl::_V1::detail::Command
The Command class represents some action that needs to be performed on one or more memory objects.
Definition: commands.hpp:99
sycl::_V1::detail::event_impl::isInitialized
bool isInitialized() const noexcept
Checks if an event is in a fully intialized state.
Definition: event_impl.hpp:240
sycl::_V1::detail::event_impl::setSubmittedQueue
void setSubmittedQueue(const QueueImplPtr &SubmittedQueue)
Sets original queue used for submission.
Definition: event_impl.hpp:220
sycl::_V1::detail::pi::getPlugin
const PluginPtr & getPlugin()
Definition: pi.cpp:504
sycl::_V1::detail::event_impl::getWorkerQueue
QueueImplPtr getWorkerQueue()
Returns worker queue for command.
Definition: event_impl.hpp:208
sycl::_V1::detail::event_impl::getSubmittedQueue
QueueImplPtr getSubmittedQueue() const
Definition: event_impl.hpp:231
sycl::_V1::detail::ContextImplPtr
std::shared_ptr< sycl::detail::context_impl > ContextImplPtr
Definition: event_impl.hpp:30
sycl::_V1::detail::event_impl::isDiscarded
bool isDiscarded() const
Checks if this event is discarded by SYCL implementation.
Definition: event_impl.hpp:202
sycl::_V1::detail::event_impl::getHostProfilingInfo
HostProfilingInfo * getHostProfilingInfo()
Returns host profiling information.
Definition: event_impl.hpp:162
stl.hpp
__SYCL_INLINE_VER_NAMESPACE
#define __SYCL_INLINE_VER_NAMESPACE(X)
Definition: defines_elementary.hpp:11
sycl::_V1::detail::event_impl::getContextImplPtr
ContextImplPtr getContextImplPtr()
Definition: event_impl.hpp:254
sycl::_V1::detail::event_impl::setWorkerQueue
void setWorkerQueue(const QueueImplPtr &WorkerQueue)
Sets worker queue for command.
Definition: event_impl.hpp:213
sycl
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
host_profiling_info.hpp
plugin.hpp
sycl::_V1::detail::event_impl::getPreparedDepsEvents
std::vector< std::shared_ptr< event_impl > > & getPreparedDepsEvents()
Returns vector of event dependencies.
Definition: event_impl.hpp:172
pi.hpp
sycl::_V1::detail::event_impl::setCommand
void setCommand(void *Command)
Associates this event with the command.
Definition: event_impl.hpp:157
sycl::_V1::detail::event_impl::MPostCompleteEvents
std::vector< EventImplPtr > MPostCompleteEvents
Definition: event_impl.hpp:291
sycl::_V1::detail::event_impl::MQueue
std::weak_ptr< queue_impl > MQueue
Definition: event_impl.hpp:280
sycl::_V1::detail::event_impl::MContext
ContextImplPtr MContext
Definition: event_impl.hpp:276
sycl::_V1::detail::event_impl::MSubmittedQueue
std::weak_ptr< queue_impl > MSubmittedQueue
Definition: event_impl.hpp:285
sycl::_V1::detail::event_impl
Definition: event_impl.hpp:36
cl.h
sycl::_V1::detail::event_impl::cv
std::condition_variable cv
Definition: event_impl.hpp:303
sycl::_V1::detail::getOrWaitEvents
std::vector< RT::PiEvent > getOrWaitEvents(std::vector< sycl::event > DepEvents, std::shared_ptr< sycl::detail::context_impl > Context)
sycl::_V1::detail::event_impl::MWorkerQueue
std::weak_ptr< queue_impl > MWorkerQueue
Definition: event_impl.hpp:284
sycl::_V1::detail::event_impl::HostEventState
HostEventState
Definition: event_impl.hpp:38
common.hpp
sycl::_V1::detail::EventImplPtr
std::shared_ptr< event_impl > EventImplPtr
Definition: cg.hpp:42
pi_native_handle
uintptr_t pi_native_handle
Definition: pi.h:146
sycl::_V1::detail::queue_impl
Definition: queue_impl.hpp:61
sycl::_V1::detail::event_impl::event_impl
event_impl(std::optional< HostEventState > State=HES_Complete)
Constructs a ready SYCL event.
Definition: event_impl.hpp:49
sycl::_V1::detail::QueueImplPtr
std::shared_ptr< sycl::detail::queue_impl > QueueImplPtr
Definition: event_impl.hpp:32
sycl::_V1::detail::event_impl::HES_Complete
@ HES_Complete
Definition: event_impl.hpp:40
sycl::_V1::detail::event_impl::isContextInitialized
bool isContextInitialized() const noexcept
Definition: event_impl.hpp:252
sycl::_V1::detail::event_impl::MPreparedDepsEvents
std::vector< EventImplPtr > MPreparedDepsEvents
Dependency events prepared for waiting by backend.
Definition: event_impl.hpp:288
info_desc.hpp
sycl::_V1::detail::event_impl::MState
std::atomic< int > MState
Definition: event_impl.hpp:300
sycl::_V1::detail::HostProfilingInfo
Profiling info for the host execution.
Definition: host_profiling_info.hpp:19
sycl::_V1::detail::event_impl::getPreparedHostDepsEvents
std::vector< std::shared_ptr< event_impl > > & getPreparedHostDepsEvents()
Returns vector of host event dependencies.
Definition: event_impl.hpp:179
sycl::_V1::detail::event_impl::getCommand
void * getCommand()
Returns command that is associated with the event.
Definition: event_impl.hpp:150
sycl::_V1::detail::event_impl::MPreparedHostDepsEvents
std::vector< EventImplPtr > MPreparedHostDepsEvents
Definition: event_impl.hpp:289
sycl::_V1::detail::event_impl::attachEventToComplete
void attachEventToComplete(const EventImplPtr &Event)
Definition: event_impl.hpp:247
sycl::_V1::detail::event_impl::MHostProfilingInfo
std::unique_ptr< HostProfilingInfo > MHostProfilingInfo
Definition: event_impl.hpp:278
sycl::_V1::detail::pi::PiEvent
::pi_event PiEvent
Definition: pi.hpp:138
sycl::_V1::detail::event_impl::MMutex
std::mutex MMutex
Definition: event_impl.hpp:302
sycl::_V1::context
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:41
sycl::_V1::detail::PluginPtr
std::shared_ptr< plugin > PluginPtr
Definition: pi.hpp:47