Skip to content

Commit beb09e8

Browse files
committed
[SYCL] moved populateDeviceCacheIfNeeded to pi_platform
Signed-off-by: Sergey V Maslov <[email protected]>
1 parent b0751ee commit beb09e8

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

sycl/plugins/level_zero/pi_level_zero.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,8 +1076,6 @@ static pi_result copyModule(ze_context_handle_t ZeContext,
10761076

10771077
static bool setEnvVar(const char *var, const char *value);
10781078

1079-
static pi_result populateDeviceCacheIfNeeded(pi_platform Platform);
1080-
10811079
// Forward declarations for mock implementations of Level Zero APIs that
10821080
// do not yet work in the driver.
10831081
// TODO: Remove these mock definitions when they work in the driver.
@@ -1335,7 +1333,7 @@ pi_result piextPlatformCreateWithNativeHandle(pi_native_handle NativeHandle,
13351333
// Return NULL if no such PI device found.
13361334
pi_device _pi_platform::getDeviceFromNativeHandle(ze_device_handle_t ZeDevice) {
13371335

1338-
pi_result Res = populateDeviceCacheIfNeeded(Platform);
1336+
pi_result Res = populateDeviceCacheIfNeeded();
13391337
if (Res != PI_SUCCESS) {
13401338
return nullptr;
13411339
}
@@ -1356,7 +1354,7 @@ pi_result piDevicesGet(pi_platform Platform, pi_device_type DeviceType,
13561354

13571355
PI_ASSERT(Platform, PI_INVALID_PLATFORM);
13581356

1359-
pi_result Res = populateDeviceCacheIfNeeded(Platform);
1357+
pi_result Res = Platform->populateDeviceCacheIfNeeded();
13601358
if (Res != PI_SUCCESS) {
13611359
return Res;
13621360
}
@@ -1415,14 +1413,13 @@ pi_result piDevicesGet(pi_platform Platform, pi_device_type DeviceType,
14151413
}
14161414

14171415
// Check the device cache and load it if necessary.
1418-
static pi_result populateDeviceCacheIfNeeded(pi_platform Platform) {
1419-
std::lock_guard<std::mutex> Lock(Platform->PiDevicesCacheMutex);
1416+
pi_result _pi_platform::populateDeviceCacheIfNeeded() {
1417+
std::lock_guard<std::mutex> Lock(PiDevicesCacheMutex);
14201418

1421-
if (Platform->DeviceCachePopulated) {
1419+
if (DeviceCachePopulated) {
14221420
return PI_SUCCESS;
14231421
}
14241422

1425-
ze_driver_handle_t ZeDriver = Platform->ZeDriver;
14261423
uint32_t ZeDeviceCount = 0;
14271424
ZE_CALL(zeDeviceGet, (ZeDriver, &ZeDeviceCount, nullptr));
14281425

@@ -1431,8 +1428,7 @@ static pi_result populateDeviceCacheIfNeeded(pi_platform Platform) {
14311428
ZE_CALL(zeDeviceGet, (ZeDriver, &ZeDeviceCount, ZeDevices.data()));
14321429

14331430
for (uint32_t I = 0; I < ZeDeviceCount; ++I) {
1434-
std::unique_ptr<_pi_device> Device(
1435-
new _pi_device(ZeDevices[I], Platform));
1431+
std::unique_ptr<_pi_device> Device(new _pi_device(ZeDevices[I], this));
14361432
pi_result Result = Device->initialize();
14371433
if (Result != PI_SUCCESS) {
14381434
return Result;
@@ -1453,27 +1449,27 @@ static pi_result populateDeviceCacheIfNeeded(pi_platform Platform) {
14531449
// cache.
14541450
for (uint32_t I = 0; I < SubDevicesCount; ++I) {
14551451
std::unique_ptr<_pi_device> PiSubDevice(
1456-
new _pi_device(ZeSubdevices[I], Platform, true));
1452+
new _pi_device(ZeSubdevices[I], this, true));
14571453
pi_result Result = PiSubDevice->initialize();
14581454
if (Result != PI_SUCCESS) {
14591455
delete[] ZeSubdevices;
14601456
return Result;
14611457
}
14621458
// save pointers to sub-devices for quick retrieval in the future.
14631459
Device->SubDevices.push_back(PiSubDevice.get());
1464-
Platform->PiDevicesCache.push_back(std::move(PiSubDevice));
1460+
PiDevicesCache.push_back(std::move(PiSubDevice));
14651461
}
14661462
delete[] ZeSubdevices;
14671463

14681464
// Save the root device in the cache for future uses.
1469-
Platform->PiDevicesCache.push_back(std::move(Device));
1465+
PiDevicesCache.push_back(std::move(Device));
14701466
}
14711467
} catch (const std::bad_alloc &) {
14721468
return PI_OUT_OF_HOST_MEMORY;
14731469
} catch (...) {
14741470
return PI_ERROR_UNKNOWN;
14751471
}
1476-
Platform->DeviceCachePopulated = true;
1472+
DeviceCachePopulated = true;
14771473
return PI_SUCCESS;
14781474
}
14791475

@@ -2022,7 +2018,7 @@ pi_result piDevicePartition(pi_device Device,
20222018
// Devices cache is normally created in piDevicesGet but still make
20232019
// sure that cache is populated.
20242020
//
2025-
pi_result Res = populateDeviceCacheIfNeeded(Device->Platform);
2021+
pi_result Res = Device->Platform->populateDeviceCacheIfNeeded();
20262022
if (Res != PI_SUCCESS) {
20272023
return Res;
20282024
}

sycl/plugins/level_zero/pi_level_zero.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,15 @@ struct _pi_platform {
8282
// Cache pi_devices for reuse
8383
std::vector<std::unique_ptr<_pi_device>> PiDevicesCache;
8484
std::mutex PiDevicesCacheMutex;
85-
pi_device getDeviceFromNativeHandle(ze_device_handle_t);
8685
bool DeviceCachePopulated = false;
8786

87+
// Check the device cache and load it if necessary.
88+
pi_result populateDeviceCacheIfNeeded();
89+
90+
// Return the PI device from cache that represents given native device.
91+
// If not found, then nullptr is returned.
92+
pi_device getDeviceFromNativeHandle(ze_device_handle_t);
93+
8894
// Current number of L0 Command Lists created on this platform.
8995
// this number must not exceed ZeMaxCommandListCache.
9096
std::atomic<int> ZeGlobalCommandListCount{0};

0 commit comments

Comments
 (0)