Skip to content

Commit 86511c5

Browse files
[SYCL] Merge USMAllocContext for Sub-Devices and Sub-Sub-Devices (#8012)
**Problem:** Currently, sub-devices and sub-sub-devices have different USM allocation contexts (USMAllocContext) but they share the same ze_device_handle. This causes issues with USM memory allocation, for instance, double-free. **Proposed Fix:** I have merged the USMAllocContext of the sub-device and sub-sub-device.
1 parent 78b7b49 commit 86511c5

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

sycl/plugins/level_zero/pi_level_zero.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -770,17 +770,21 @@ pi_device _pi_context::getRootDevice() const {
770770
pi_result _pi_context::initialize() {
771771

772772
// Helper lambda to create various USM allocators for a device.
773+
// Note that the CCS devices and their respective subdevices share a
774+
// common ze_device_handle and therefore, also share USM allocators.
773775
auto createUSMAllocators = [this](pi_device Device) {
774776
SharedMemAllocContexts.emplace(
775-
std::piecewise_construct, std::make_tuple(Device),
777+
std::piecewise_construct, std::make_tuple(Device->ZeDevice),
776778
std::make_tuple(std::unique_ptr<SystemMemory>(
777779
new USMSharedMemoryAlloc(this, Device))));
780+
778781
SharedReadOnlyMemAllocContexts.emplace(
779-
std::piecewise_construct, std::make_tuple(Device),
782+
std::piecewise_construct, std::make_tuple(Device->ZeDevice),
780783
std::make_tuple(std::unique_ptr<SystemMemory>(
781784
new USMSharedReadOnlyMemoryAlloc(this, Device))));
785+
782786
DeviceMemAllocContexts.emplace(
783-
std::piecewise_construct, std::make_tuple(Device),
787+
std::piecewise_construct, std::make_tuple(Device->ZeDevice),
784788
std::make_tuple(std::unique_ptr<SystemMemory>(
785789
new USMDeviceMemoryAlloc(this, Device))));
786790
};
@@ -807,8 +811,9 @@ pi_result _pi_context::initialize() {
807811
std::unique_ptr<SystemMemory>(new USMHostMemoryAlloc(this)));
808812

809813
// We may allocate memory to this root device so create allocators.
810-
if (SingleRootDevice && DeviceMemAllocContexts.find(SingleRootDevice) ==
811-
DeviceMemAllocContexts.end()) {
814+
if (SingleRootDevice &&
815+
DeviceMemAllocContexts.find(SingleRootDevice->ZeDevice) ==
816+
DeviceMemAllocContexts.end()) {
812817
createUSMAllocators(SingleRootDevice);
813818
}
814819

@@ -8191,7 +8196,7 @@ pi_result piextUSMDeviceAlloc(void **ResultPtr, pi_context Context,
81918196
}
81928197

81938198
try {
8194-
auto It = Context->DeviceMemAllocContexts.find(Device);
8199+
auto It = Context->DeviceMemAllocContexts.find(Device->ZeDevice);
81958200
if (It == Context->DeviceMemAllocContexts.end())
81968201
return PI_ERROR_INVALID_VALUE;
81978202

@@ -8269,7 +8274,7 @@ pi_result piextUSMSharedAlloc(void **ResultPtr, pi_context Context,
82698274
try {
82708275
auto &Allocator = (DeviceReadOnly ? Context->SharedReadOnlyMemAllocContexts
82718276
: Context->SharedMemAllocContexts);
8272-
auto It = Allocator.find(Device);
8277+
auto It = Allocator.find(Device->ZeDevice);
82738278
if (It == Allocator.end())
82748279
return PI_ERROR_INVALID_VALUE;
82758280

@@ -8432,10 +8437,11 @@ static pi_result USMFreeHelper(pi_context Context, void *Ptr,
84328437
PI_ASSERT(Device, PI_ERROR_INVALID_DEVICE);
84338438

84348439
auto DeallocationHelper =
8435-
[Context, Device, Ptr, OwnZeMemHandle](
8436-
std::unordered_map<pi_device, USMAllocContext> &AllocContextMap) {
8440+
[Context, Device, Ptr,
8441+
OwnZeMemHandle](std::unordered_map<ze_device_handle_t, USMAllocContext>
8442+
&AllocContextMap) {
84378443
try {
8438-
auto It = AllocContextMap.find(Device);
8444+
auto It = AllocContextMap.find(Device->ZeDevice);
84398445
if (It == AllocContextMap.end())
84408446
return PI_ERROR_INVALID_VALUE;
84418447

sycl/plugins/level_zero/pi_level_zero.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,12 @@ struct _pi_context : _pi_object {
462462
// Store USM allocator context(internal allocator structures)
463463
// for USM shared and device allocations. There is 1 allocator context
464464
// per each pair of (context, device) per each memory type.
465-
std::unordered_map<pi_device, USMAllocContext> DeviceMemAllocContexts;
466-
std::unordered_map<pi_device, USMAllocContext> SharedMemAllocContexts;
467-
std::unordered_map<pi_device, USMAllocContext> SharedReadOnlyMemAllocContexts;
465+
std::unordered_map<ze_device_handle_t, USMAllocContext>
466+
DeviceMemAllocContexts;
467+
std::unordered_map<ze_device_handle_t, USMAllocContext>
468+
SharedMemAllocContexts;
469+
std::unordered_map<ze_device_handle_t, USMAllocContext>
470+
SharedReadOnlyMemAllocContexts;
468471

469472
// Since L0 native runtime does not distinguisg "shared device_read_only"
470473
// vs regular "shared" allocations, we have keep track of it to use

0 commit comments

Comments
 (0)