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 
17 
18 namespace sycl {
20 namespace detail {
21 
22 // Forward declaration
23 class device_impl;
24 class context_impl;
25 
27  // The unique identifier of the device_global.
28  std::string MUniqueId;
29  // Pointer to the device_global on host.
30  const void *MDeviceGlobalPtr;
31  // Size of the underlying type in the device_global.
32  std::uint32_t MDeviceGlobalTSize;
33  // True if the device_global has been decorated with device_image_scope
35 
36  // Constructor for only initializing ID and pointer. The other members will
37  // be initialized later.
38  DeviceGlobalMapEntry(std::string UniqueId, const void *DeviceGlobalPtr)
39  : MUniqueId(UniqueId), MDeviceGlobalPtr(DeviceGlobalPtr),
40  MDeviceGlobalTSize(0), MIsDeviceImageScopeDecorated(false) {}
41 
42  // Constructor for only initializing ID, type size, and device image scope
43  // flag. The pointer to the device global will be initialized later.
44  DeviceGlobalMapEntry(std::string UniqueId, std::uint32_t DeviceGlobalTSize,
45  bool IsDeviceImageScopeDecorated)
46  : MUniqueId(UniqueId), MDeviceGlobalPtr(nullptr),
47  MDeviceGlobalTSize(DeviceGlobalTSize),
48  MIsDeviceImageScopeDecorated(IsDeviceImageScopeDecorated) {}
49 
50  // Initialize the pointer to the associated device_global.
51  void initialize(const void *DeviceGlobalPtr) {
52  assert(DeviceGlobalPtr && "Device global pointer cannot be null");
53  assert(!MDeviceGlobalPtr &&
54  "Device global pointer has already been initialized.");
55  MDeviceGlobalPtr = DeviceGlobalPtr;
56  }
57 
58  // Initialize the device_global's element type size and the flag signalling
59  // if the device_global has the device_image_scope property.
60  void initialize(std::uint32_t DeviceGlobalTSize,
61  bool IsDeviceImageScopeDecorated) {
62  assert(DeviceGlobalTSize != 0 && "Device global initialized with 0 size.");
63  if (MDeviceGlobalTSize != 0) {
64  // The device global entry has already been initialized. This can happen
65  // if multiple images contain the device-global. They must agree on the
66  // information.
67  assert(MDeviceGlobalTSize == DeviceGlobalTSize &&
68  "Device global intializations disagree on type size.");
69  assert(
70  MIsDeviceImageScopeDecorated == IsDeviceImageScopeDecorated &&
71  "Device global intializations disagree on image scope decoration.");
72  return;
73  }
74  MDeviceGlobalTSize = DeviceGlobalTSize;
75  MIsDeviceImageScopeDecorated = IsDeviceImageScopeDecorated;
76  }
77 
78 private:
79  // Map from a device and a context to the associated USM allocation for the
80  // device_global. This should always be empty if MIsDeviceImageScopeDecorated
81  // is true.
82  std::map<std::pair<const device_impl *, const context_impl *>, void *>
83  MDeviceToUSMPtrMap;
84  std::mutex MDeviceToUSMPtrMapMutex;
85 };
86 
87 } // namespace detail
88 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
89 } // namespace sycl
#define __SYCL_INLINE_VER_NAMESPACE(X)
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
DeviceGlobalMapEntry(std::string UniqueId, std::uint32_t DeviceGlobalTSize, bool IsDeviceImageScopeDecorated)
void initialize(std::uint32_t DeviceGlobalTSize, bool IsDeviceImageScopeDecorated)
void initialize(const void *DeviceGlobalPtr)
DeviceGlobalMapEntry(std::string UniqueId, const void *DeviceGlobalPtr)