DPC++ Runtime
Runtime libraries for oneAPI DPC++
helpers.cpp
Go to the documentation of this file.
1 //==---------------- helpers.cpp - SYCL helpers ---------------------------==//
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 
10 #include <sycl/detail/helpers.hpp>
11 
12 #include <detail/buffer_impl.hpp>
13 #include <detail/context_impl.hpp>
14 #include <detail/event_impl.hpp>
15 #include <detail/queue_impl.hpp>
16 #include <sycl/event.hpp>
17 
18 #include <memory>
19 
20 namespace sycl {
21 inline namespace _V1 {
22 using ContextImplPtr = std::shared_ptr<sycl::detail::context_impl>;
23 namespace detail {
24 // TODO: remove from public header files and implementation during the next ABI
25 // Breaking window. Not used any more.
26 std::vector<sycl::detail::pi::PiEvent>
27 getOrWaitEvents(std::vector<sycl::event> DepEvents, ContextImplPtr Context) {
28  std::vector<sycl::detail::pi::PiEvent> Events;
29  for (auto SyclEvent : DepEvents) {
30  auto SyclEventImplPtr = detail::getSyclObjImpl(SyclEvent);
31  // throwaway events created with empty constructor will not have a context
32  // (which is set lazily) calling getContextImpl() would set that
33  // context, which we wish to avoid as it is expensive.
34  if ((!SyclEventImplPtr->isContextInitialized() &&
35  !SyclEventImplPtr->is_host()) ||
36  SyclEventImplPtr->isNOP()) {
37  continue;
38  }
39  // The fusion command and its event are associated with a non-host context,
40  // but still does not produce a PI event.
41  bool NoPiEvent =
42  SyclEventImplPtr->MCommand &&
43  !static_cast<Command *>(SyclEventImplPtr->MCommand)->producesPiEvent();
44  if (SyclEventImplPtr->is_host() ||
45  SyclEventImplPtr->getContextImpl() != Context || NoPiEvent) {
46  // Call wait, because the command for the event might not have been
47  // enqueued when kernel fusion is happening.
48  SyclEventImplPtr->wait(SyclEventImplPtr);
49  } else {
50  // In this path nullptr native event means that the command has not been
51  // enqueued. It may happen if async enqueue in a host task is involved.
52  // This should affect only shortcut functions, which bypass the graph.
53  if (SyclEventImplPtr->getHandleRef() == nullptr) {
54  std::vector<Command *> AuxCmds;
55  Scheduler::getInstance().enqueueCommandForCG(SyclEventImplPtr, AuxCmds,
56  BLOCKING);
57  }
58  Events.push_back(SyclEventImplPtr->getHandleRef());
59  }
60  }
61  return Events;
62 }
63 
64 void waitEvents(std::vector<sycl::event> DepEvents) {
65  for (auto SyclEvent : DepEvents) {
66  detail::getSyclObjImpl(SyclEvent)->waitInternal();
67  }
68 }
69 
70 void markBufferAsInternal(const std::shared_ptr<buffer_impl> &BufImpl) {
71  BufImpl->markAsInternal();
72 }
73 
74 } // namespace detail
75 } // namespace _V1
76 } // namespace sycl
The Command class represents some action that needs to be performed on one or more memory objects.
Definition: commands.hpp:107
void enqueueCommandForCG(EventImplPtr NewEvent, std::vector< Command * > &AuxilaryCmds, BlockingT Blocking=NON_BLOCKING)
Definition: scheduler.cpp:166
static Scheduler & getInstance()
Definition: scheduler.cpp:261
std::vector< sycl::detail::pi::PiEvent > getOrWaitEvents(std::vector< sycl::event > DepEvents, std::shared_ptr< sycl::detail::context_impl > Context)
std::shared_ptr< sycl::detail::context_impl > ContextImplPtr
Definition: event_impl.hpp:32
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: impl_utils.hpp:30
void markBufferAsInternal(const std::shared_ptr< buffer_impl > &BufImpl)
Definition: helpers.cpp:70
void waitEvents(std::vector< sycl::event > DepEvents)
Definition: helpers.cpp:64
std::shared_ptr< sycl::detail::context_impl > ContextImplPtr
Definition: helpers.cpp:22
Definition: access.hpp:18