@@ -461,7 +461,8 @@ pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms,
461
461
return PI_INVALID_VALUE;
462
462
}
463
463
464
- static std::vector<pi_platform> piPlatformsCache;
464
+ static std::vector<pi_platform> PiPlatformsCache;
465
+ static std::mutex PlatformsCacheMutex;
465
466
466
467
// This is a good time to initialize Level Zero.
467
468
// TODO: We can still safely recover if something goes wrong during the init.
@@ -497,7 +498,8 @@ pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms,
497
498
assert (ZeDriverCount == 1 );
498
499
ZE_CALL (zeDriverGet (&ZeDriverCount, &ZeDriver));
499
500
500
- for (const pi_platform CachedPlatform : piPlatformsCache) {
501
+ std::lock_guard<std::mutex> Lock (PlatformsCacheMutex);
502
+ for (const pi_platform CachedPlatform : PiPlatformsCache) {
501
503
if (CachedPlatform->ZeDriver == ZeDriver) {
502
504
Platforms[0 ] = CachedPlatform;
503
505
// if the caller sent a valid NumPlatforms pointer, set it here
@@ -535,7 +537,7 @@ pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms,
535
537
std::to_string (ZE_MINOR_VERSION (ZeApiVersion));
536
538
537
539
// save a copy in the cache for future uses
538
- piPlatformsCache .push_back (Platforms[0 ]);
540
+ PiPlatformsCache .push_back (Platforms[0 ]);
539
541
} catch (const std::bad_alloc &) {
540
542
return PI_OUT_OF_HOST_MEMORY;
541
543
} catch (...) {
@@ -625,11 +627,19 @@ pi_result piDevicesGet(pi_platform Platform, pi_device_type DeviceType,
625
627
pi_uint32 *NumDevices) {
626
628
627
629
assert (Platform);
628
- std::lock_guard<std::mutex> Lock (Platform->DeviceCacheMutex );
629
630
ze_driver_handle_t ZeDriver = Platform->ZeDriver ;
630
631
631
632
// Get number of devices supporting Level Zero
632
- uint32_t ZeDeviceCount = Platform->PiDevicesCache .size ();
633
+ uint32_t ZeDeviceCount = 0 ;
634
+ std::lock_guard<std::mutex> Lock (Platform->DeviceCacheMutex );
635
+ if (Platform->CacheInvalidated ) {
636
+ for (const pi_device CachedDevice : Platform->PiDevicesCache ) {
637
+ CachedDevice->initialize ();
638
+ }
639
+ Platform->CacheInvalidated = false ;
640
+ }
641
+ ZeDeviceCount = Platform->PiDevicesCache .size ();
642
+
633
643
const bool AskingForGPU = (DeviceType & PI_DEVICE_TYPE_GPU);
634
644
const bool AskingForDefault = (DeviceType == PI_DEVICE_TYPE_DEFAULT);
635
645
@@ -695,21 +705,12 @@ pi_result piDeviceRetain(pi_device Device) {
695
705
696
706
pi_result piDeviceRelease (pi_device Device) {
697
707
assert (Device);
698
- pi_platform Platform = Device->Platform ;
699
- std::lock_guard<std::mutex> Lock (Platform->DeviceCacheMutex );
700
708
// TODO: OpenCL says root-device ref-count remains unchanged (1),
701
709
// but when would we free the device's data?
702
710
if (--(Device->RefCount ) == 0 ) {
703
- // Destroy the command list used for initializations
704
- ZE_CALL (zeCommandListDestroy (Device->ZeCommandListInit ));
705
- // invalidate piDeviceCache entry
706
- for (uint32_t i = 0 ; i < Platform->PiDevicesCache .size (); i++) {
707
- if (Device == Platform->PiDevicesCache [i]) {
708
- Platform->PiDevicesCache .erase (Platform->PiDevicesCache .begin () + i);
709
- break ;
710
- }
711
- }
712
- delete Device;
711
+ pi_platform Platform = Device->Platform ;
712
+ std::lock_guard<std::mutex> Lock (Platform->DeviceCacheMutex );
713
+ Platform->CacheInvalidated = true ;
713
714
}
714
715
715
716
return PI_SUCCESS;
0 commit comments