DPC++ Runtime
Runtime libraries for oneAPI DPC++
device_global_map_entry.hpp
Go to the documentation of this file.
1 //==----------------- device_global_map_entry.hpp --------------------------==//
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 <cassert>
12 #include <cstdint>
13 #include <map>
14 #include <mutex>
15 #include <optional>
16 #include <set>
17 
19 #include <sycl/detail/pi.hpp>
20 
21 namespace sycl {
23 namespace detail {
24 
25 // Forward declaration
26 class context_impl;
27 class device_impl;
28 class platform_impl;
29 class queue_impl;
30 
31 // RAII object for keeping ownership of a PI event.
32 struct OwnedPiEvent {
33  OwnedPiEvent(const plugin &Plugin) : MEvent{std::nullopt}, MPlugin{Plugin} {}
34  OwnedPiEvent(RT::PiEvent Event, const plugin &Plugin);
35  ~OwnedPiEvent();
36 
38  : MEvent(Other.MEvent), MPlugin(Other.MPlugin) {
39  Other.MEvent = std::nullopt;
40  }
41 
42  // Copy constructor explicitly deleted for simplicity as it is not currently
43  // used. Implement if needed.
44  OwnedPiEvent(const OwnedPiEvent &Other) = delete;
45 
46  operator bool() { return MEvent.has_value(); }
47 
48  RT::PiEvent GetEvent() { return *MEvent; }
49 
50  // Transfers the ownership of the event to the caller. The destructor will
51  // no longer release the event.
53  RT::PiEvent Event = *MEvent;
54  MEvent = std::nullopt;
55  return Event;
56  }
57 
58 private:
59  std::optional<RT::PiEvent> MEvent;
60  const plugin &MPlugin;
61 };
62 
64  DeviceGlobalUSMMem(void *Ptr) : MPtr(Ptr) {}
66 
67  void *const &getPtr() const noexcept { return MPtr; }
68 
69  // Gets the zero-initialization event if it exists. If not the OwnedPiEvent
70  // will contain no event.
71  OwnedPiEvent getZeroInitEvent(const plugin &Plugin);
72 
73 private:
74  void *MPtr;
75  std::mutex MZeroInitEventMutex;
76  std::optional<RT::PiEvent> MZeroInitEvent;
77 
78  friend struct DeviceGlobalMapEntry;
79 };
80 
82  // The unique identifier of the device_global.
83  std::string MUniqueId;
84  // Pointer to the device_global on host.
85  const void *MDeviceGlobalPtr = nullptr;
86  // The image identifiers for the images using the device_global used by in the
87  // cache.
88  std::set<std::uintptr_t> MImageIdentifiers;
89  // The kernel-set IDs for the images using the device_global.
90  std::set<KernelSetId> MKSIds;
91  // Size of the underlying type in the device_global.
92  std::uint32_t MDeviceGlobalTSize = 0;
93  // True if the device_global has been decorated with device_image_scope.
94  bool MIsDeviceImageScopeDecorated = false;
95 
96  // Constructor for only initializing ID and pointer. The other members will
97  // be initialized later.
98  DeviceGlobalMapEntry(std::string UniqueId, const void *DeviceGlobalPtr)
99  : MUniqueId(UniqueId), MDeviceGlobalPtr(DeviceGlobalPtr) {}
100 
101  // Constructor for only initializing ID, type size, and device image scope
102  // flag. The pointer to the device global will be initialized later.
103  DeviceGlobalMapEntry(std::string UniqueId, std::uintptr_t ImgId,
104  KernelSetId KSId, std::uint32_t DeviceGlobalTSize,
105  bool IsDeviceImageScopeDecorated)
106  : MUniqueId(UniqueId), MImageIdentifiers{ImgId}, MKSIds{KSId},
107  MDeviceGlobalTSize(DeviceGlobalTSize),
108  MIsDeviceImageScopeDecorated(IsDeviceImageScopeDecorated) {}
109 
110  // Initialize the pointer to the associated device_global.
111  void initialize(const void *DeviceGlobalPtr) {
112  assert(DeviceGlobalPtr && "Device global pointer cannot be null");
113  assert(!MDeviceGlobalPtr &&
114  "Device global pointer has already been initialized.");
115  MDeviceGlobalPtr = DeviceGlobalPtr;
116  }
117 
118  // Initialize the device_global's element type size and the flag signalling
119  // if the device_global has the device_image_scope property.
120  void initialize(std::uintptr_t ImgId, KernelSetId KSId,
121  std::uint32_t DeviceGlobalTSize,
122  bool IsDeviceImageScopeDecorated) {
123  if (MDeviceGlobalTSize != 0) {
124  // The device global entry has already been initialized. This can happen
125  // if multiple images contain the device-global. They must agree on the
126  // information.
127  assert(MDeviceGlobalTSize == DeviceGlobalTSize &&
128  "Device global intializations disagree on type size.");
129  assert(
130  MIsDeviceImageScopeDecorated == IsDeviceImageScopeDecorated &&
131  "Device global intializations disagree on image scope decoration.");
132  return;
133  }
134  MImageIdentifiers.insert(ImgId);
135  MKSIds.insert(KSId);
136  MDeviceGlobalTSize = DeviceGlobalTSize;
137  MIsDeviceImageScopeDecorated = IsDeviceImageScopeDecorated;
138  }
139 
140  // Gets or allocates USM memory for a device_global.
142  getOrAllocateDeviceGlobalUSM(const std::shared_ptr<queue_impl> &QueueImpl);
143 
144  // Removes resources for device_globals associated with the context.
145  void removeAssociatedResources(const context_impl *CtxImpl);
146 
147 private:
148  // Map from a device and a context to the associated USM allocation for the
149  // device_global. This should always be empty if MIsDeviceImageScopeDecorated
150  // is true.
151  std::map<std::pair<const device_impl *, const context_impl *>,
153  MDeviceToUSMPtrMap;
154  std::mutex MDeviceToUSMPtrMapMutex;
155 };
156 
157 } // namespace detail
158 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
159 } // namespace sycl
sycl::_V1::detail::DeviceGlobalMapEntry::DeviceGlobalMapEntry
DeviceGlobalMapEntry(std::string UniqueId, std::uintptr_t ImgId, KernelSetId KSId, std::uint32_t DeviceGlobalTSize, bool IsDeviceImageScopeDecorated)
Definition: device_global_map_entry.hpp:103
sycl::_V1::detail::DeviceGlobalMapEntry::MImageIdentifiers
std::set< std::uintptr_t > MImageIdentifiers
Definition: device_global_map_entry.hpp:88
__SYCL_INLINE_VER_NAMESPACE
#define __SYCL_INLINE_VER_NAMESPACE(X)
Definition: defines_elementary.hpp:11
sycl::_V1::detail::DeviceGlobalMapEntry::MKSIds
std::set< KernelSetId > MKSIds
Definition: device_global_map_entry.hpp:90
sycl::_V1::detail::DeviceGlobalMapEntry::initialize
void initialize(std::uintptr_t ImgId, KernelSetId KSId, std::uint32_t DeviceGlobalTSize, bool IsDeviceImageScopeDecorated)
Definition: device_global_map_entry.hpp:120
sycl::_V1::detail::OwnedPiEvent::GetEvent
RT::PiEvent GetEvent()
Definition: device_global_map_entry.hpp:48
sycl::_V1::detail::DeviceGlobalMapEntry
Definition: device_global_map_entry.hpp:81
sycl::_V1::detail::OwnedPiEvent::OwnedPiEvent
OwnedPiEvent(const plugin &Plugin)
Definition: device_global_map_entry.hpp:33
sycl
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
pi.hpp
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
defines_elementary.hpp
sycl::_V1::detail::DeviceGlobalUSMMem
Definition: device_global_map_entry.hpp:63
sycl::_V1::detail::DeviceGlobalUSMMem::getPtr
void *const & getPtr() const noexcept
Definition: device_global_map_entry.hpp:67
sycl::_V1::detail::DeviceGlobalMapEntry::MUniqueId
std::string MUniqueId
Definition: device_global_map_entry.hpp:83
sycl::_V1::detail::OwnedPiEvent::OwnedPiEvent
OwnedPiEvent(OwnedPiEvent &&Other)
Definition: device_global_map_entry.hpp:37
sycl::_V1::detail::KernelSetId
size_t KernelSetId
Definition: common.hpp:443
sycl::_V1::detail::DeviceGlobalMapEntry::initialize
void initialize(const void *DeviceGlobalPtr)
Definition: device_global_map_entry.hpp:111
sycl::_V1::detail::OwnedPiEvent
Definition: device_global_map_entry.hpp:32
sycl::_V1::detail::DeviceGlobalUSMMem::DeviceGlobalUSMMem
DeviceGlobalUSMMem(void *Ptr)
Definition: device_global_map_entry.hpp:64
sycl::_V1::detail::OwnedPiEvent::TransferOwnership
RT::PiEvent TransferOwnership()
Definition: device_global_map_entry.hpp:52
sycl::_V1::detail::DeviceGlobalMapEntry::DeviceGlobalMapEntry
DeviceGlobalMapEntry(std::string UniqueId, const void *DeviceGlobalPtr)
Definition: device_global_map_entry.hpp:98
sycl::_V1::detail::pi::PiEvent
::pi_event PiEvent
Definition: pi.hpp:136
sycl::_V1::detail::context_impl
Definition: context_impl.hpp:33