Skip to content

Commit 6504ba0

Browse files
[SYCL] Create memory allocators for contexts with single root-device (intel#4317)
Signed-off-by: Sergey V Maslov <[email protected]>
1 parent 449721b commit 6504ba0

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

sycl/plugins/level_zero/pi_level_zero.hpp

+34-20
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,9 @@ struct _pi_context : _pi_object {
419419
OwnZeContext{OwnZeContext}, Devices{Devs, Devs + NumDevices},
420420
ZeCommandListInit{nullptr}, ZeEventPool{nullptr},
421421
NumEventsAvailableInEventPool{}, NumEventsUnreleasedInEventPool{} {
422+
// NOTE: one must additionally call initialize() to complete
423+
// PI context creation.
424+
422425
// Create USM allocator context for each pair (device, context).
423426
for (uint32_t I = 0; I < NumDevices; I++) {
424427
pi_device Device = Devs[I];
@@ -430,8 +433,6 @@ struct _pi_context : _pi_object {
430433
std::piecewise_construct, std::make_tuple(Device),
431434
std::make_tuple(std::unique_ptr<SystemMemory>(
432435
new USMDeviceMemoryAlloc(this, Device))));
433-
// NOTE: one must additionally call initialize() to complete
434-
// PI context creation.
435436
}
436437
// Create USM allocator context for host. Device and Shared USM allocations
437438
// are device-specific. Host allocations are not device-dependent therefore
@@ -441,27 +442,40 @@ struct _pi_context : _pi_object {
441442

442443
if (NumDevices == 1) {
443444
SingleRootDevice = Devices[0];
444-
return;
445-
}
446-
447-
// Check if we have context with subdevices of the same device (context may
448-
// include root device itself as well)
449-
SingleRootDevice =
450-
Devices[0]->RootDevice ? Devices[0]->RootDevice : Devices[0];
445+
} else {
446+
447+
// Check if we have context with subdevices of the same device (context
448+
// may include root device itself as well)
449+
SingleRootDevice =
450+
Devices[0]->RootDevice ? Devices[0]->RootDevice : Devices[0];
451+
452+
// For context with sub subdevices, the SingleRootDevice might still
453+
// not be the root device.
454+
// Check whether the SingleRootDevice is the subdevice or root device.
455+
if (SingleRootDevice->isSubDevice()) {
456+
SingleRootDevice = SingleRootDevice->RootDevice;
457+
}
451458

452-
// For context with sub subdevices, the SingleRootDevice might still
453-
// not be the root device.
454-
// Check whether the SingleRootDevice is the subdevice or root device.
455-
if (SingleRootDevice->isSubDevice()) {
456-
SingleRootDevice = SingleRootDevice->RootDevice;
459+
for (auto &Device : Devices) {
460+
if ((!Device->RootDevice && Device != SingleRootDevice) ||
461+
(Device->RootDevice && Device->RootDevice != SingleRootDevice)) {
462+
SingleRootDevice = nullptr;
463+
break;
464+
}
465+
}
457466
}
458467

459-
for (auto &Device : Devices) {
460-
if ((!Device->RootDevice && Device != SingleRootDevice) ||
461-
(Device->RootDevice && Device->RootDevice != SingleRootDevice)) {
462-
SingleRootDevice = nullptr;
463-
break;
464-
}
468+
// We may allocate memory to this root device so create allocators.
469+
if (SingleRootDevice && DeviceMemAllocContexts.find(SingleRootDevice) ==
470+
DeviceMemAllocContexts.end()) {
471+
SharedMemAllocContexts.emplace(
472+
std::piecewise_construct, std::make_tuple(SingleRootDevice),
473+
std::make_tuple(std::unique_ptr<SystemMemory>(
474+
new USMSharedMemoryAlloc(this, SingleRootDevice))));
475+
DeviceMemAllocContexts.emplace(
476+
std::piecewise_construct, std::make_tuple(SingleRootDevice),
477+
std::make_tuple(std::unique_ptr<SystemMemory>(
478+
new USMDeviceMemoryAlloc(this, SingleRootDevice))));
465479
}
466480
}
467481

0 commit comments

Comments
 (0)