@@ -419,6 +419,9 @@ struct _pi_context : _pi_object {
419
419
OwnZeContext{OwnZeContext}, Devices{Devs, Devs + NumDevices},
420
420
ZeCommandListInit{nullptr }, ZeEventPool{nullptr },
421
421
NumEventsAvailableInEventPool{}, NumEventsUnreleasedInEventPool{} {
422
+ // NOTE: one must additionally call initialize() to complete
423
+ // PI context creation.
424
+
422
425
// Create USM allocator context for each pair (device, context).
423
426
for (uint32_t I = 0 ; I < NumDevices; I++) {
424
427
pi_device Device = Devs[I];
@@ -430,8 +433,6 @@ struct _pi_context : _pi_object {
430
433
std::piecewise_construct, std::make_tuple (Device),
431
434
std::make_tuple (std::unique_ptr<SystemMemory>(
432
435
new USMDeviceMemoryAlloc (this , Device))));
433
- // NOTE: one must additionally call initialize() to complete
434
- // PI context creation.
435
436
}
436
437
// Create USM allocator context for host. Device and Shared USM allocations
437
438
// are device-specific. Host allocations are not device-dependent therefore
@@ -441,27 +442,40 @@ struct _pi_context : _pi_object {
441
442
442
443
if (NumDevices == 1 ) {
443
444
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
+ }
451
458
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
+ }
457
466
}
458
467
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))));
465
479
}
466
480
}
467
481
0 commit comments