Skip to content

Commit 20a07ad

Browse files
committed
added a flag to invalidate the cache.
Signed-off-by: Byoungro So <[email protected]>
1 parent 8007bc5 commit 20a07ad

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

sycl/plugins/level_zero/pi_level_zero.cpp

+18-17
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,8 @@ pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms,
461461
return PI_INVALID_VALUE;
462462
}
463463

464-
static std::vector<pi_platform> piPlatformsCache;
464+
static std::vector<pi_platform> PiPlatformsCache;
465+
static std::mutex PlatformsCacheMutex;
465466

466467
// This is a good time to initialize Level Zero.
467468
// 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,
497498
assert(ZeDriverCount == 1);
498499
ZE_CALL(zeDriverGet(&ZeDriverCount, &ZeDriver));
499500

500-
for (const pi_platform CachedPlatform : piPlatformsCache) {
501+
std::lock_guard<std::mutex> Lock(PlatformsCacheMutex);
502+
for (const pi_platform CachedPlatform : PiPlatformsCache) {
501503
if (CachedPlatform->ZeDriver == ZeDriver) {
502504
Platforms[0] = CachedPlatform;
503505
// if the caller sent a valid NumPlatforms pointer, set it here
@@ -535,7 +537,7 @@ pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms,
535537
std::to_string(ZE_MINOR_VERSION(ZeApiVersion));
536538

537539
// save a copy in the cache for future uses
538-
piPlatformsCache.push_back(Platforms[0]);
540+
PiPlatformsCache.push_back(Platforms[0]);
539541
} catch (const std::bad_alloc &) {
540542
return PI_OUT_OF_HOST_MEMORY;
541543
} catch (...) {
@@ -625,11 +627,19 @@ pi_result piDevicesGet(pi_platform Platform, pi_device_type DeviceType,
625627
pi_uint32 *NumDevices) {
626628

627629
assert(Platform);
628-
std::lock_guard<std::mutex> Lock(Platform->DeviceCacheMutex);
629630
ze_driver_handle_t ZeDriver = Platform->ZeDriver;
630631

631632
// 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+
633643
const bool AskingForGPU = (DeviceType & PI_DEVICE_TYPE_GPU);
634644
const bool AskingForDefault = (DeviceType == PI_DEVICE_TYPE_DEFAULT);
635645

@@ -695,21 +705,12 @@ pi_result piDeviceRetain(pi_device Device) {
695705

696706
pi_result piDeviceRelease(pi_device Device) {
697707
assert(Device);
698-
pi_platform Platform = Device->Platform;
699-
std::lock_guard<std::mutex> Lock(Platform->DeviceCacheMutex);
700708
// TODO: OpenCL says root-device ref-count remains unchanged (1),
701709
// but when would we free the device's data?
702710
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;
713714
}
714715

715716
return PI_SUCCESS;

sycl/plugins/level_zero/pi_level_zero.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ struct _pi_platform {
7474
// Cache pi_devices for reuse
7575
std::vector<pi_device> PiDevicesCache;
7676
std::mutex DeviceCacheMutex;
77+
// Flag to indicate PiDevicesCache is invalidated
78+
bool CacheInvalidated = false;
7779
};
7880

7981
struct _pi_device : _pi_object {

0 commit comments

Comments
 (0)