Skip to content

Commit e2db14d

Browse files
committed
added more comments about cache invalidation logic
Signed-off-by: Byoungro So <[email protected]>
1 parent b04b1c0 commit e2db14d

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

sycl/plugins/level_zero/pi_level_zero.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,9 @@ pi_result piDevicesGet(pi_platform Platform, pi_device_type DeviceType,
632632
// Get number of devices supporting Level Zero
633633
uint32_t ZeDeviceCount = 0;
634634
std::lock_guard<std::mutex> Lock(Platform->PiDevicesCacheMutex);
635+
// As soon as there was a call to piDeviceRelease(), the entire cache is
636+
// invalidated by setting the flag CacheInvalidated. We just need to
637+
// re-initialize cached pi_devices to reuse them.
635638
if (Platform->CacheInvalidated) {
636639
for (const pi_device CachedDevice : Platform->PiDevicesCache) {
637640
CachedDevice->initialize();
@@ -708,6 +711,14 @@ pi_result piDeviceRelease(pi_device Device) {
708711
// TODO: OpenCL says root-device ref-count remains unchanged (1),
709712
// but when would we free the device's data?
710713
if (--(Device->RefCount) == 0) {
714+
// We invalidate the entire cache as soon as any device is released.
715+
// The saved pi_devices in cache is still intact but flag CacheInvalided
716+
// will not allow the entire cached devices to be reused without
717+
// re-initializing them.
718+
// TODO: This means the cached pi_device live until the program ends.
719+
// If L0 RT does not do its own cleanup for Ze_Device_Handle upon tear-down,
720+
// we need to figure out a way to call
721+
// ZE_CALL(zeCommandListDestroy(Device->ZeCommandListInit));
711722
pi_platform Platform = Device->Platform;
712723
std::lock_guard<std::mutex> Lock(Platform->PiDevicesCacheMutex);
713724
Platform->CacheInvalidated = true;

sycl/plugins/level_zero/pi_level_zero.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ struct _pi_platform {
7575
std::vector<pi_device> PiDevicesCache;
7676
std::mutex PiDevicesCacheMutex;
7777
// Flag to indicate PiDevicesCache is invalidated
78+
// This flag is used in piDeviceRelease to invalidate the entire cache
79+
// whenever there is a call to piDeviceRelease for any cached device.
80+
// This flag is used in piDevicesGet to reuse the cache
81+
// without expensive calls to L0 RT.
7882
bool CacheInvalidated = false;
7983
};
8084

0 commit comments

Comments
 (0)