Skip to content
Merged
Changes from 11 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d4dffb4
[SYCL] Implemented Device and Platform cache in L0
bso-intel Jul 20, 2020
81b1bbd
[SYCL] Implement Device and Platform cache in L0
bso-intel Jul 25, 2020
1cc004d
fixed platform cache lifesycle
bso-intel Jul 29, 2020
3491603
revert
bso-intel Jul 31, 2020
fcb58ce
[SYCL] Implemented Device and Platform cache in L0
bso-intel Jul 20, 2020
de69459
[SYCL] Implement Device and Platform cache in L0
bso-intel Jul 25, 2020
9fd5d0b
fixed platform cache lifesycle
bso-intel Jul 29, 2020
ced7e61
revert
bso-intel Jul 31, 2020
c87a32e
clean up merge conflict
bso-intel Jul 31, 2020
3d81d27
Merge branch 'device-cache' of https://github.com/bso-intel/llvm into…
bso-intel Jul 31, 2020
d386e8b
revert merge conflict
bso-intel Jul 31, 2020
4dd593c
changed to range based loop
bso-intel Aug 5, 2020
ee31f0a
Merge remote-tracking branch 'upstream/sycl' into device-cache
bso-intel Aug 5, 2020
a34e648
fixed clang-format
bso-intel Aug 5, 2020
0a804b3
Merge remote-tracking branch 'upstream/sycl' into device-cache
bso-intel Aug 5, 2020
5d43bf5
Moved PiDevicesCache into _pi_platform
bso-intel Aug 5, 2020
550374e
take out return error
bso-intel Aug 6, 2020
493cd5a
Merge remote-tracking branch 'upstream/sycl' into device-cache
bso-intel Aug 6, 2020
8007bc5
moved device cache into _pi_platform struct
bso-intel Aug 6, 2020
20a07ad
added a flag to invalidate the cache.
bso-intel Aug 6, 2020
fabf4f2
removed special handling for root devices
bso-intel Aug 7, 2020
b04b1c0
renamed mutexes to match with cache name
bso-intel Aug 10, 2020
e2db14d
added more comments about cache invalidation logic
bso-intel Aug 10, 2020
2e0dbc1
removed invalidation logic
bso-intel Aug 10, 2020
e455095
added comment and fixed ref count issue
bso-intel Aug 11, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion sycl/plugins/level_zero/pi_level_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ class ReturnHelper {
size_t *param_value_size_ret;
};

// save discovered pi_devices & pi_platforms for quick return
static std::vector<pi_device> piDevicesCache;
static std::vector<pi_platform> piPlatformsCache;

} // anonymous namespace

// TODO:: In the following 4 methods we may want to distinguish read access vs.
Expand Down Expand Up @@ -448,6 +452,8 @@ pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms,
return PI_INVALID_VALUE;
}

static std::vector<pi_platform> piPlatformsCache;

// This is a good time to initialize Level Zero.
// TODO: We can still safely recover if something goes wrong during the init.
// Implement handling segfault using sigaction.
Expand Down Expand Up @@ -482,6 +488,16 @@ pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms,
assert(ZeDriverCount == 1);
ZE_CALL(zeDriverGet(&ZeDriverCount, &ZeDriver));

for (uint32_t i = 0; i < piPlatformsCache.size(); i++) {
if (piPlatformsCache[i]->ZeDriver == ZeDriver) {
Platforms[0] = piPlatformsCache[i];
if (NumPlatforms)
*NumPlatforms = 1;

return PI_SUCCESS;
}
}

try {
// TODO: figure out how/when to release this memory
*Platforms = new _pi_platform(ZeDriver);
Expand All @@ -507,6 +523,9 @@ pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms,
Platforms[0]->ZeDriverApiVersion =
std::to_string(ZE_MAJOR_VERSION(ZeApiVersion)) + std::string(".") +
std::to_string(ZE_MINOR_VERSION(ZeApiVersion));

// save a copy in the cache for future uses
piPlatformsCache.push_back(Platforms[0]);
} catch (const std::bad_alloc &) {
return PI_OUT_OF_HOST_MEMORY;
} catch (...) {
Expand Down Expand Up @@ -602,7 +621,18 @@ pi_result piDevicesGet(pi_platform Platform, pi_device_type DeviceType,
uint32_t ZeDeviceCount = 0;
const bool AskingForGPU = (DeviceType & PI_DEVICE_TYPE_GPU);
const bool AskingForDefault = (DeviceType == PI_DEVICE_TYPE_DEFAULT);
ZE_CALL(zeDeviceGet(ZeDriver, &ZeDeviceCount, nullptr));

if (piDevicesCache.size() != 0) {
for (uint32_t i = 0; i < piDevicesCache.size(); i++) {
if (piDevicesCache[i]->Platform == Platform) {
ZeDeviceCount++;
}
}
}
if (ZeDeviceCount == 0) {
ZE_CALL(zeDeviceGet(ZeDriver, &ZeDeviceCount, nullptr));
}

if (ZeDeviceCount == 0 || !(AskingForGPU || AskingForDefault)) {
if (NumDevices)
*NumDevices = 0;
Expand All @@ -618,6 +648,17 @@ pi_result piDevicesGet(pi_platform Platform, pi_device_type DeviceType,
return PI_SUCCESS;
}

// if devices are already captured in cache, return them from the cache.
uint32_t count = 0;
for (uint32_t i = 0; i < piDevicesCache.size(); i++) {
if (piDevicesCache[i]->Platform == Platform) {
Devices[count++] = piDevicesCache[i];
}
}
if (count == ZeDeviceCount) {
return PI_SUCCESS;
}

try {
std::vector<ze_device_handle_t> ZeDevices(ZeDeviceCount);
ZE_CALL(zeDeviceGet(ZeDriver, &ZeDeviceCount, ZeDevices.data()));
Expand All @@ -629,6 +670,8 @@ pi_result piDevicesGet(pi_platform Platform, pi_device_type DeviceType,
if (Result != PI_SUCCESS) {
return Result;
}
// save a copy in the cache for future uses.
piDevicesCache.push_back(Devices[I]);
}
}
} catch (const std::bad_alloc &) {
Expand Down Expand Up @@ -657,6 +700,13 @@ pi_result piDeviceRelease(pi_device Device) {
if (--(Device->RefCount) == 0) {
// Destroy the command list used for initializations
ZE_CALL(zeCommandListDestroy(Device->ZeCommandListInit));
// invalidate piDeviceCache entry
for (uint32_t i = 0; i < piDevicesCache.size(); i++) {
if (Device == piDevicesCache[i]) {
piDevicesCache.erase(piDevicesCache.begin() + i);
break;
}
}
delete Device;
}

Expand Down