Skip to content

Commit 4efa820

Browse files
[SYCL] Expand device_global map and make initialization order agnostic (#5902)
This commit makes the following changes: * Adds the unique identifier to the device_global map entries. * Makes the map of USM allocations for device_globals private and add context to the map key. * Makes the order of initialization of device_global entries in the map order agnostic, i.e. the runtime will not care if the integration header or the device-image initialization happens first. * Fixes the export of the `add` function for the map, as used by the integration header. Signed-off-by: Steffen Larsen <[email protected]>
1 parent 537e51b commit 4efa820

File tree

7 files changed

+80
-26
lines changed

7 files changed

+80
-26
lines changed

sycl/include/CL/sycl/detail/device_global_map.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace sycl {
1313
namespace detail {
1414
namespace device_global_map {
1515

16-
void add(void *DeviceGlobalPtr, const char *UniqueId);
16+
__SYCL_EXPORT void add(const void *DeviceGlobalPtr, const char *UniqueId);
1717

1818
} // namespace device_global_map
1919
} // namespace detail

sycl/source/detail/device_global_map.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ namespace sycl {
1313
namespace detail {
1414
namespace device_global_map {
1515

16-
void add(void *DeviceGlobalPtr, const char *UniqueId) {
17-
detail::ProgramManager::getInstance().addDeviceGlobalEntry(DeviceGlobalPtr,
18-
UniqueId);
16+
__SYCL_EXPORT void add(const void *DeviceGlobalPtr, const char *UniqueId) {
17+
detail::ProgramManager::getInstance().addOrInitDeviceGlobalEntry(
18+
DeviceGlobalPtr, UniqueId);
1919
}
2020

2121
} // namespace device_global_map

sycl/source/detail/device_global_map_entry.hpp

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
#pragma once
1010

1111
#include <cstdint>
12-
#include <unordered_map>
12+
#include <map>
13+
#include <mutex>
1314

1415
__SYCL_INLINE_NAMESPACE(cl) {
1516
namespace sycl {
@@ -19,23 +20,39 @@ namespace detail {
1920
class device_impl;
2021

2122
struct DeviceGlobalMapEntry {
23+
// The unique identifier of the device_global.
24+
std::string MUniqueId;
2225
// Pointer to the device_global on host.
23-
void *MDeviceGlobalPtr;
26+
const void *MDeviceGlobalPtr;
2427
// Size of the underlying type in the device_global.
2528
std::uint32_t MDeviceGlobalTSize;
2629
// True if the device_global has been decorated with device_image_scope
2730
bool MIsDeviceImageScopeDecorated;
28-
// Map between devices and corresponding USM allocations for the
29-
// device_global. This should always be empty if MIsDeviceImageScopeDecorated
30-
// is true.
31-
std::unordered_map<std::shared_ptr<device_impl>, void *> MDeviceToUSMPtrMap;
3231

33-
// Constructor only initializes with the pointer to the device_global as the
34-
// additional information is loaded after.
35-
DeviceGlobalMapEntry(void *DeviceGlobalPtr)
36-
: MDeviceGlobalPtr(DeviceGlobalPtr), MDeviceGlobalTSize(0),
37-
MIsDeviceImageScopeDecorated(false) {}
32+
// Constructor for only initializing ID and pointer. The other members will
33+
// be initialized later.
34+
DeviceGlobalMapEntry(std::string UniqueId, const void *DeviceGlobalPtr)
35+
: MUniqueId(UniqueId), MDeviceGlobalPtr(DeviceGlobalPtr),
36+
MDeviceGlobalTSize(0), MIsDeviceImageScopeDecorated(false) {}
37+
38+
// Constructor for only initializing ID, type size, and device image scope
39+
// flag. The pointer to the device global will be initialized later.
40+
DeviceGlobalMapEntry(std::string UniqueId, std::uint32_t DeviceGlobalTSize,
41+
bool IsDeviceImageScopeDecorated)
42+
: MUniqueId(UniqueId), MDeviceGlobalPtr(nullptr),
43+
MDeviceGlobalTSize(DeviceGlobalTSize),
44+
MIsDeviceImageScopeDecorated(IsDeviceImageScopeDecorated) {}
45+
46+
// Initialize the pointer to the associated device_global.
47+
void initialize(const void *DeviceGlobalPtr) {
48+
assert(DeviceGlobalPtr && "Device global pointer cannot be null");
49+
assert(!MDeviceGlobalPtr &&
50+
"Device global pointer has already been initialized.");
51+
MDeviceGlobalPtr = DeviceGlobalPtr;
52+
}
3853

54+
// Initialize the device_global's element type size and the flag signalling
55+
// if the device_global has the device_image_scope property.
3956
void initialize(std::uint32_t DeviceGlobalTSize,
4057
bool IsDeviceImageScopeDecorated) {
4158
assert(DeviceGlobalTSize != 0 && "Device global initialized with 0 size.");
@@ -44,6 +61,14 @@ struct DeviceGlobalMapEntry {
4461
MDeviceGlobalTSize = DeviceGlobalTSize;
4562
MIsDeviceImageScopeDecorated = IsDeviceImageScopeDecorated;
4663
}
64+
65+
private:
66+
// Map from a device and a context to the associated USM allocation for the
67+
// device_global. This should always be empty if MIsDeviceImageScopeDecorated
68+
// is true.
69+
std::map<std::pair<const device_impl *, const context_impl *>, void *>
70+
MDeviceToUSMPtrMap;
71+
std::mutex MDeviceToUSMPtrMapMutex;
4772
};
4873

4974
} // namespace detail

sycl/source/detail/program_manager/program_manager.cpp

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,20 @@ void ProgramManager::addImages(pi_device_binaries DeviceBinary) {
12171217
*reinterpret_cast<const std::uint32_t *>(&DeviceGlobalInfo[8]);
12181218
const std::uint32_t DeviceImageScopeDecorated =
12191219
*reinterpret_cast<const std::uint32_t *>(&DeviceGlobalInfo[12]);
1220-
Entry->second.initialize(TypeSize, DeviceImageScopeDecorated);
1220+
1221+
auto ExistingDeviceGlobal = m_DeviceGlobals.find(DeviceGlobal->Name);
1222+
if (ExistingDeviceGlobal != m_DeviceGlobals.end()) {
1223+
// If it has already been registered we update the information.
1224+
ExistingDeviceGlobal->second->initialize(TypeSize,
1225+
DeviceImageScopeDecorated);
1226+
} else {
1227+
// If it has not already been registered we create a new entry.
1228+
// Note: Pointer to the device global is not available here, so it
1229+
// cannot be set until registration happens.
1230+
auto EntryUPtr = std::make_unique<DeviceGlobalMapEntry>(
1231+
DeviceGlobal->Name, TypeSize, DeviceImageScopeDecorated);
1232+
m_DeviceGlobals.emplace(DeviceGlobal->Name, std::move(EntryUPtr));
1233+
}
12211234
}
12221235
}
12231236
m_DeviceImages[KSId].reset(new std::vector<RTDeviceBinaryImageUPtr>());
@@ -1469,13 +1482,23 @@ kernel_id ProgramManager::getBuiltInKernelID(const std::string &KernelName) {
14691482
return KernelID->second;
14701483
}
14711484

1472-
void ProgramManager::addDeviceGlobalEntry(void *DeviceGlobalPtr,
1473-
const char *UniqueId) {
1485+
void ProgramManager::addOrInitDeviceGlobalEntry(const void *DeviceGlobalPtr,
1486+
const char *UniqueId) {
14741487
std::lock_guard<std::mutex> DeviceGlobalsGuard(m_DeviceGlobalsMutex);
14751488

1476-
assert(m_DeviceGlobals.find(UniqueId) == m_DeviceGlobals.end() &&
1477-
"Device global has already been registered.");
1478-
m_DeviceGlobals.insert({UniqueId, DeviceGlobalMapEntry(DeviceGlobalPtr)});
1489+
auto ExistingDeviceGlobal = m_DeviceGlobals.find(UniqueId);
1490+
if (ExistingDeviceGlobal != m_DeviceGlobals.end()) {
1491+
// Update the existing information and add the entry to the pointer map.
1492+
ExistingDeviceGlobal->second->initialize(DeviceGlobalPtr);
1493+
m_Ptr2DeviceGlobal.insert(
1494+
{DeviceGlobalPtr, ExistingDeviceGlobal->second.get()});
1495+
return;
1496+
}
1497+
1498+
auto EntryUPtr =
1499+
std::make_unique<DeviceGlobalMapEntry>(UniqueId, DeviceGlobalPtr);
1500+
auto NewEntry = m_DeviceGlobals.emplace(UniqueId, std::move(EntryUPtr));
1501+
m_Ptr2DeviceGlobal.insert({DeviceGlobalPtr, NewEntry.first->second.get()});
14791502
}
14801503

14811504
std::vector<device_image_plain>

sycl/source/detail/program_manager/program_manager.hpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,10 @@ class ProgramManager {
183183
// built-in kernel name.
184184
kernel_id getBuiltInKernelID(const std::string &KernelName);
185185

186-
// The function inserts a device_global entry into the device_global map.
187-
void addDeviceGlobalEntry(void *DeviceGlobalPtr, const char *UniqueId);
186+
// The function inserts or initializes a device_global entry into the
187+
// device_global map.
188+
void addOrInitDeviceGlobalEntry(const void *DeviceGlobalPtr,
189+
const char *UniqueId);
188190

189191
// The function returns a vector of SYCL device images that are compiled with
190192
// the required state and at least one device from the passed list of devices.
@@ -387,10 +389,12 @@ class ProgramManager {
387389
using KernelNameWithOSModule = std::pair<std::string, OSModuleHandle>;
388390
std::set<KernelNameWithOSModule> m_KernelUsesAssert;
389391

390-
// Map between device_global unique ids and associated information.
391-
std::unordered_map<std::string, DeviceGlobalMapEntry> m_DeviceGlobals;
392+
// Maps between device_global identifiers and associated information.
393+
std::unordered_map<std::string, std::unique_ptr<DeviceGlobalMapEntry>>
394+
m_DeviceGlobals;
395+
std::unordered_map<const void *, DeviceGlobalMapEntry *> m_Ptr2DeviceGlobal;
392396

393-
/// Protects m_DeviceGlobals.
397+
/// Protects m_DeviceGlobals and m_Ptr2DeviceGlobal.
394398
std::mutex m_DeviceGlobalsMutex;
395399
};
396400
} // namespace detail

sycl/test/abi/sycl_symbols_linux.dump

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3895,6 +3895,7 @@ _ZN2cl4sycl6detail16AccessorImplHostD1Ev
38953895
_ZN2cl4sycl6detail16AccessorImplHostD2Ev
38963896
_ZN2cl4sycl6detail17HostProfilingInfo3endEv
38973897
_ZN2cl4sycl6detail17HostProfilingInfo5startEv
3898+
_ZN2cl4sycl6detail17device_global_map3addEPKvPKc
38983899
_ZN2cl4sycl6detail18convertChannelTypeE22_pi_image_channel_type
38993900
_ZN2cl4sycl6detail18convertChannelTypeENS0_18image_channel_typeE
39003901
_ZN2cl4sycl6detail18get_kernel_id_implENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

sycl/test/abi/sycl_symbols_windows.dump

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,7 @@
10901090
?acospi@__host_std@cl@@YA?AVhalf@half_impl@detail@sycl@2@V34562@@Z
10911091
?acospi@__host_std@cl@@YAMM@Z
10921092
?acospi@__host_std@cl@@YANN@Z
1093+
?add@device_global_map@detail@sycl@cl@@YAXPEBXPEBD@Z
10931094
?addHostAccessorAndWait@detail@sycl@cl@@YAXPEAVAccessorImplHost@123@@Z
10941095
?addOrReplaceAccessorProperties@SYCLMemObjT@detail@sycl@cl@@QEAAXAEBVproperty_list@34@@Z
10951096
?addReduction@handler@sycl@cl@@AEAAXAEBV?$shared_ptr@$$CBX@std@@@Z

0 commit comments

Comments
 (0)