DPC++ Runtime
Runtime libraries for oneAPI DPC++
device_global_map_entry.cpp
Go to the documentation of this file.
1 //==------------------ device_global_map_entry.cpp -------------------------==//
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 
11 #include <detail/event_info.hpp>
13 #include <detail/queue_impl.hpp>
14 #include <detail/usm/usm_impl.hpp>
15 
16 namespace sycl {
18 namespace detail {
19 
20 OwnedPiEvent::OwnedPiEvent(RT::PiEvent Event, const plugin &Plugin)
21  : MEvent(Event), MPlugin(Plugin) {
22  // Retain the event to share ownership of it.
23  MPlugin.call<PiApiKind::piEventRetain>(*MEvent);
24 }
25 
27  // Release the event if the ownership was not transferred.
28  if (MEvent.has_value())
29  MPlugin.call<PiApiKind::piEventRelease>(*MEvent);
30 }
31 
33  // removeAssociatedResources is expected to have cleaned up both the pointer
34  // and the event. When asserts are enabled the values are set, so we check
35  // these here.
36  assert(MPtr == nullptr && "MPtr has not been cleaned up.");
37  assert(!MZeroInitEvent.has_value() &&
38  "MZeroInitEvent has not been cleaned up.");
39 }
40 
42  std::lock_guard<std::mutex> Lock(MZeroInitEventMutex);
43  // If there is a zero-init event we can remove it if it is done.
44  if (MZeroInitEvent.has_value()) {
45  if (get_event_info<info::event::command_execution_status>(
46  *MZeroInitEvent, Plugin) == info::event_command_status::complete) {
47  Plugin.call<PiApiKind::piEventRelease>(*MZeroInitEvent);
48  MZeroInitEvent = {};
49  return OwnedPiEvent(Plugin);
50  } else {
51  return OwnedPiEvent(*MZeroInitEvent, Plugin);
52  }
53  }
54  return OwnedPiEvent(Plugin);
55 }
56 
58  const std::shared_ptr<queue_impl> &QueueImpl) {
60  "USM allocations should not be acquired for device_global with "
61  "device_image_scope property.");
62  const std::shared_ptr<context_impl> &CtxImpl = QueueImpl->getContextImplPtr();
63  const std::shared_ptr<device_impl> &DevImpl = QueueImpl->getDeviceImplPtr();
64  std::lock_guard<std::mutex> Lock(MDeviceToUSMPtrMapMutex);
65 
66  auto DGUSMPtr = MDeviceToUSMPtrMap.find({DevImpl.get(), CtxImpl.get()});
67  if (DGUSMPtr != MDeviceToUSMPtrMap.end())
68  return DGUSMPtr->second;
69 
70  void *NewDGUSMPtr = detail::usm::alignedAllocInternal(
71  0, MDeviceGlobalTSize, CtxImpl.get(), DevImpl.get(),
72  sycl::usm::alloc::device);
73 
74  auto NewAllocIt = MDeviceToUSMPtrMap.emplace(
75  std::piecewise_construct,
76  std::forward_as_tuple(DevImpl.get(), CtxImpl.get()),
77  std::forward_as_tuple(NewDGUSMPtr));
78  assert(NewAllocIt.second &&
79  "USM allocation for device and context already happened.");
80  DeviceGlobalUSMMem &NewAlloc = NewAllocIt.first->second;
81 
82  // Zero-initialize here and save the event.
83  {
84  std::lock_guard<std::mutex> Lock(NewAlloc.MZeroInitEventMutex);
85  RT::PiEvent InitEvent;
86  MemoryManager::fill_usm(NewAlloc.MPtr, QueueImpl, MDeviceGlobalTSize, 0,
87  std::vector<RT::PiEvent>{}, &InitEvent);
88  NewAlloc.MZeroInitEvent = InitEvent;
89  }
90 
91  CtxImpl->addAssociatedDeviceGlobal(MDeviceGlobalPtr);
92  return NewAlloc;
93 }
94 
96  const context_impl *CtxImpl) {
97  std::lock_guard<std::mutex> Lock{MDeviceToUSMPtrMapMutex};
98  for (device Device : CtxImpl->getDevices()) {
99  auto USMPtrIt =
100  MDeviceToUSMPtrMap.find({getSyclObjImpl(Device).get(), CtxImpl});
101  if (USMPtrIt != MDeviceToUSMPtrMap.end()) {
102  DeviceGlobalUSMMem &USMMem = USMPtrIt->second;
103  detail::usm::freeInternal(USMMem.MPtr, CtxImpl);
104  if (USMMem.MZeroInitEvent.has_value())
106  *USMMem.MZeroInitEvent);
107 #ifndef NDEBUG
108  // For debugging we set the event and memory to some recognizable values
109  // to allow us to check that this cleanup happens before erasure.
110  USMMem.MPtr = nullptr;
111  USMMem.MZeroInitEvent = {};
112 #endif
113  MDeviceToUSMPtrMap.erase(USMPtrIt);
114  }
115  }
116 }
117 
118 } // namespace detail
119 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
120 } // namespace sycl
piEventRelease
pi_result piEventRelease(pi_event event)
Definition: pi_esimd_emulator.cpp:1488
event_info.hpp
sycl::_V1::detail::DeviceGlobalUSMMem::~DeviceGlobalUSMMem
~DeviceGlobalUSMMem()
Definition: device_global_map_entry.cpp:32
sycl::_V1::info::event_command_status::complete
@ complete
context_impl.hpp
sycl::_V1::detail::DeviceGlobalMapEntry::MDeviceGlobalPtr
const void * MDeviceGlobalPtr
Definition: device_global_map_entry.hpp:85
__SYCL_INLINE_VER_NAMESPACE
#define __SYCL_INLINE_VER_NAMESPACE(X)
Definition: defines_elementary.hpp:11
sycl::_V1::detail::DeviceGlobalMapEntry::getOrAllocateDeviceGlobalUSM
DeviceGlobalUSMMem & getOrAllocateDeviceGlobalUSM(const std::shared_ptr< queue_impl > &QueueImpl)
Definition: device_global_map_entry.cpp:57
usm_impl.hpp
sycl::_V1::detail::usm::alignedAllocInternal
void * alignedAllocInternal(size_t Alignment, size_t Size, const context_impl *CtxImpl, const device_impl *DevImpl, alloc Kind, const property_list &PropList)
Definition: usm_impl.cpp:125
sycl
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
queue_impl.hpp
device_global_map_entry.hpp
sycl::_V1::detail::DeviceGlobalMapEntry::removeAssociatedResources
void removeAssociatedResources(const context_impl *CtxImpl)
Definition: device_global_map_entry.cpp:95
sycl::_V1::detail::OwnedPiEvent::~OwnedPiEvent
~OwnedPiEvent()
Definition: device_global_map_entry.cpp:26
sycl::_V1::detail::plugin
The plugin class provides a unified interface to the underlying low-level runtimes for the device-agn...
Definition: plugin.hpp:90
sycl::_V1::detail::DeviceGlobalMapEntry::MIsDeviceImageScopeDecorated
bool MIsDeviceImageScopeDecorated
Definition: device_global_map_entry.hpp:94
sycl::_V1::detail::context_impl::getPlugin
const plugin & getPlugin() const
Definition: context_impl.hpp:109
sycl::_V1::detail::DeviceGlobalUSMMem::getZeroInitEvent
OwnedPiEvent getZeroInitEvent(const plugin &Plugin)
Definition: device_global_map_entry.cpp:41
sycl::_V1::detail::DeviceGlobalUSMMem
Definition: device_global_map_entry.hpp:63
sycl::_V1::device
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:49
sycl::_V1::detail::usm::freeInternal
void freeInternal(void *Ptr, const context_impl *CtxImpl)
Definition: usm_impl.cpp:254
sycl::_V1::detail::context_impl::getDevices
const std::vector< device > & getDevices() const
Unlike ‘get_info<info::context::devices>’, this function returns a reference.
Definition: context_impl.hpp:141
piEventRetain
pi_result piEventRetain(pi_event event)
Definition: pi_esimd_emulator.cpp:1478
sycl::_V1::detail::plugin::call
void call(ArgsT... Args) const
Calls the API, traces the call, checks the result.
Definition: plugin.hpp:217
sycl::_V1::detail::MemoryManager::fill_usm
static void fill_usm(void *DstMem, QueueImplPtr Queue, size_t Len, int Pattern, std::vector< RT::PiEvent > DepEvents, RT::PiEvent *OutEvent)
Definition: memory_manager.cpp:867
sycl::_V1::detail::OwnedPiEvent
Definition: device_global_map_entry.hpp:32
sycl::_V1::detail::pi::PiEvent
::pi_event PiEvent
Definition: pi.hpp:136
sycl::_V1::detail::context_impl
Definition: context_impl.hpp:33
sycl::_V1::detail::getSyclObjImpl
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: common.hpp:300
sycl::_V1::detail::DeviceGlobalMapEntry::MDeviceGlobalTSize
std::uint32_t MDeviceGlobalTSize
Definition: device_global_map_entry.hpp:92
memory_manager.hpp