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 #include <unordered_set>
18 
19 #include <detail/pi_utils.hpp>
21 
22 namespace sycl {
23 inline namespace _V1 {
24 namespace detail {
25 
26 // Forward declaration
27 class context_impl;
28 class device_impl;
29 class platform_impl;
30 class queue_impl;
31 class event_impl;
32 using EventImplPtr = std::shared_ptr<sycl::detail::event_impl>;
33 
35  DeviceGlobalUSMMem(void *Ptr) : MPtr(Ptr) {}
37 
38  void *const &getPtr() const noexcept { return MPtr; }
39 
40  // Gets the initialization event if it exists. If not the OwnedPiEvent
41  // will contain no event.
42  OwnedPiEvent getInitEvent(const PluginPtr &Plugin);
43 
44 private:
45  void *MPtr;
46  std::mutex MInitEventMutex;
47  std::optional<sycl::detail::pi::PiEvent> MInitEvent;
48 
49  friend struct DeviceGlobalMapEntry;
50 };
51 
53  // The unique identifier of the device_global.
54  std::string MUniqueId;
55  // Pointer to the device_global on host.
56  const void *MDeviceGlobalPtr = nullptr;
57  // Images device_global are used by.
58  std::unordered_set<RTDeviceBinaryImage *> MImages;
59  // The image identifiers for the images using the device_global used by in the
60  // cache.
61  std::set<std::uintptr_t> MImageIdentifiers;
62  // Size of the underlying type in the device_global.
63  std::uint32_t MDeviceGlobalTSize = 0;
64  // True if the device_global has been decorated with device_image_scope.
66 
67  // Constructor for only initializing ID and pointer. The other members will
68  // be initialized later.
69  DeviceGlobalMapEntry(std::string UniqueId, const void *DeviceGlobalPtr)
70  : MUniqueId(UniqueId), MDeviceGlobalPtr(DeviceGlobalPtr) {}
71 
72  // Constructor for only initializing ID, type size, and device image scope
73  // flag. The pointer to the device global will be initialized later.
74  DeviceGlobalMapEntry(std::string UniqueId, RTDeviceBinaryImage *Img,
75  std::uint32_t DeviceGlobalTSize,
76  bool IsDeviceImageScopeDecorated)
77  : MUniqueId(UniqueId), MImages{Img},
78  MImageIdentifiers{reinterpret_cast<uintptr_t>(Img)},
79  MDeviceGlobalTSize(DeviceGlobalTSize),
80  MIsDeviceImageScopeDecorated(IsDeviceImageScopeDecorated) {}
81 
82  // Initialize the pointer to the associated device_global.
83  void initialize(const void *DeviceGlobalPtr) {
84  assert(DeviceGlobalPtr && "Device global pointer cannot be null");
85  assert(!MDeviceGlobalPtr &&
86  "Device global pointer has already been initialized.");
87  MDeviceGlobalPtr = DeviceGlobalPtr;
88  }
89 
90  // Initialize the device_global's element type size and the flag signalling
91  // if the device_global has the device_image_scope property.
92  void initialize(RTDeviceBinaryImage *Img, std::uint32_t DeviceGlobalTSize,
93  bool IsDeviceImageScopeDecorated) {
94  if (MDeviceGlobalTSize != 0) {
95  // The device global entry has already been initialized. This can happen
96  // if multiple images contain the device-global. They must agree on the
97  // information.
98  assert(MDeviceGlobalTSize == DeviceGlobalTSize &&
99  "Device global intializations disagree on type size.");
100  assert(
101  MIsDeviceImageScopeDecorated == IsDeviceImageScopeDecorated &&
102  "Device global intializations disagree on image scope decoration.");
103  return;
104  }
105  MImages.insert(Img);
106  MImageIdentifiers.insert(reinterpret_cast<uintptr_t>(Img));
107  MDeviceGlobalTSize = DeviceGlobalTSize;
108  MIsDeviceImageScopeDecorated = IsDeviceImageScopeDecorated;
109  }
110 
111  // Gets or allocates USM memory for a device_global.
113  getOrAllocateDeviceGlobalUSM(const std::shared_ptr<queue_impl> &QueueImpl);
114 
115  // Removes resources for device_globals associated with the context.
116  void removeAssociatedResources(const context_impl *CtxImpl);
117 
118 private:
119  // Map from a device and a context to the associated USM allocation for the
120  // device_global. This should always be empty if MIsDeviceImageScopeDecorated
121  // is true.
122  std::map<std::pair<const device_impl *, const context_impl *>,
124  MDeviceToUSMPtrMap;
125  std::mutex MDeviceToUSMPtrMapMutex;
126 };
127 
128 } // namespace detail
129 } // namespace _V1
130 } // namespace sycl
std::shared_ptr< event_impl > EventImplPtr
Definition: cg.hpp:43
std::shared_ptr< plugin > PluginPtr
Definition: pi.hpp:48
Definition: access.hpp:18
_Abi const simd< _Tp, _Abi > & noexcept
Definition: simd.hpp:1324
void initialize(const void *DeviceGlobalPtr)
DeviceGlobalMapEntry(std::string UniqueId, RTDeviceBinaryImage *Img, std::uint32_t DeviceGlobalTSize, bool IsDeviceImageScopeDecorated)
DeviceGlobalUSMMem & getOrAllocateDeviceGlobalUSM(const std::shared_ptr< queue_impl > &QueueImpl)
void removeAssociatedResources(const context_impl *CtxImpl)
DeviceGlobalMapEntry(std::string UniqueId, const void *DeviceGlobalPtr)
std::unordered_set< RTDeviceBinaryImage * > MImages
void initialize(RTDeviceBinaryImage *Img, std::uint32_t DeviceGlobalTSize, bool IsDeviceImageScopeDecorated)
OwnedPiEvent getInitEvent(const PluginPtr &Plugin)