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 std::vector<sycl::detail::pi::PiEvent>
25 getOrWaitEvents(std::vector<sycl::event> DepEvents, ContextImplPtr Context) {
26  std::vector<sycl::detail::pi::PiEvent> Events;
27  for (auto SyclEvent : DepEvents) {
28  auto SyclEventImplPtr = detail::getSyclObjImpl(SyclEvent);
29  // throwaway events created with empty constructor will not have a context
30  // (which is set lazily) calling getContextImpl() would set that
31  // context, which we wish to avoid as it is expensive.
32  if (!SyclEventImplPtr->isContextInitialized() &&
33  !SyclEventImplPtr->is_host()) {
34  continue;
35  }
36  // The fusion command and its event are associated with a non-host context,
37  // but still does not produce a PI event.
38  bool NoPiEvent =
39  SyclEventImplPtr->MCommand &&
40  !static_cast<Command *>(SyclEventImplPtr->MCommand)->producesPiEvent();
41  if (SyclEventImplPtr->is_host() ||
42  SyclEventImplPtr->getContextImpl() != Context || NoPiEvent) {
43  // Call wait, because the command for the event might not have been
44  // enqueued when kernel fusion is happening.
45  SyclEventImplPtr->wait(SyclEventImplPtr);
46  } else {
47  // In this path nullptr native event means that the command has not been
48  // enqueued. It may happen if async enqueue in a host task is involved.
49  // This should affect only shortcut functions, which bypass the graph.
50  if (SyclEventImplPtr->getHandleRef() == nullptr) {
51  std::vector<Command *> AuxCmds;
52  Scheduler::getInstance().enqueueCommandForCG(SyclEventImplPtr, AuxCmds,
53  BLOCKING);
54  }
55  Events.push_back(SyclEventImplPtr->getHandleRef());
56  }
57  }
58  return Events;
59 }
60 
61 void waitEvents(std::vector<sycl::event> DepEvents) {
62  for (auto SyclEvent : DepEvents) {
63  detail::getSyclObjImpl(SyclEvent)->waitInternal();
64  }
65 }
66 
67 void markBufferAsInternal(const std::shared_ptr<buffer_impl> &BufImpl) {
68  BufImpl->markAsInternal();
69 }
70 
71 } // namespace detail
72 } // namespace _V1
73 } // namespace sycl
sycl::_V1::ContextImplPtr
std::shared_ptr< sycl::detail::context_impl > ContextImplPtr
Definition: helpers.cpp:22
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
event_impl.hpp
context_impl.hpp
sycl::_V1::detail::ContextImplPtr
std::shared_ptr< sycl::detail::context_impl > ContextImplPtr
Definition: event_impl.hpp:33
sycl::_V1::detail::waitEvents
void waitEvents(std::vector< sycl::event > DepEvents)
Definition: helpers.cpp:61
event.hpp
detail
---— Error handling, matching OpenCL plugin semantics.
Definition: common.hpp:44
helpers.hpp
sycl
Definition: access.hpp:18
sycl::_V1::detail::markBufferAsInternal
void markBufferAsInternal(const std::shared_ptr< buffer_impl > &BufImpl)
Definition: helpers.cpp:67
queue_impl.hpp
commands.hpp
sycl::_V1::detail::getOrWaitEvents
std::vector< sycl::detail::pi::PiEvent > getOrWaitEvents(std::vector< sycl::event > DepEvents, std::shared_ptr< sycl::detail::context_impl > Context)
sycl::_V1::detail::Scheduler::enqueueCommandForCG
void enqueueCommandForCG(EventImplPtr NewEvent, std::vector< Command * > &AuxilaryCmds, BlockingT Blocking=NON_BLOCKING)
Definition: scheduler.cpp:165
sycl::_V1::detail::BLOCKING
@ BLOCKING
Definition: commands.hpp:51
sycl::_V1::detail::Scheduler::getInstance
static Scheduler & getInstance()
Definition: scheduler.cpp:260
sycl::_V1::detail::getSyclObjImpl
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: impl_utils.hpp:30
buffer_impl.hpp