@@ -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 ;
0 commit comments