Skip to content

Commit a8ab1ea

Browse files
[SYCL] Support for >1 Level-Zero drivers (#3418)
Signed-off-by: Sergey V Maslov <[email protected]>
1 parent 5d5a792 commit a8ab1ea

File tree

2 files changed

+62
-62
lines changed

2 files changed

+62
-62
lines changed

sycl/plugins/level_zero/pi_level_zero.cpp

Lines changed: 60 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ std::mutex ZeCall::GlobalLock;
7474
// Controls PI level tracing prints.
7575
static bool PrintPiTrace = false;
7676

77-
// Map Level Zero runtime error code to PI error code
77+
// Map Level Zero runtime error code to PI error code.
7878
static pi_result mapError(ze_result_t ZeResult) {
7979
// TODO: these mapping need to be clarified and synced with the PI API return
8080
// values, which is TBD.
@@ -592,6 +592,26 @@ pi_result _pi_queue::resetCommandListFenceEntry(
592592
return PI_SUCCESS;
593593
}
594594

595+
// Maximum Number of Command Lists that can be created.
596+
// This Value is initialized to 20000, but can be changed by the user
597+
// thru the environment variable SYCL_PI_LEVEL_ZERO_MAX_COMMAND_LIST_CACHE
598+
// ie SYCL_PI_LEVEL_ZERO_MAX_COMMAND_LIST_CACHE =10000.
599+
static const int ZeMaxCommandListCacheSize = [] {
600+
const char *CommandListCacheSize =
601+
std::getenv("SYCL_PI_LEVEL_ZERO_MAX_COMMAND_LIST_CACHE");
602+
pi_uint32 CommandListCacheSizeValue;
603+
try {
604+
CommandListCacheSizeValue =
605+
CommandListCacheSize ? std::stoi(CommandListCacheSize) : 20000;
606+
} catch (std::exception const &) {
607+
zePrint(
608+
"SYCL_PI_LEVEL_ZERO_MAX_COMMAND_LIST_CACHE: invalid value provided, "
609+
"default set.\n");
610+
CommandListCacheSizeValue = 20000;
611+
}
612+
return CommandListCacheSizeValue;
613+
}();
614+
595615
static const pi_uint32 ZeCommandListBatchSize = [] {
596616
// Default value of 0. This specifies to use dynamic batch size adjustment.
597617
pi_uint32 BatchSizeVal = 0;
@@ -692,7 +712,7 @@ pi_result _pi_context::getAvailableCommandList(
692712
// map.
693713
if ((*ZeCommandList == nullptr) &&
694714
(Queue->Device->Platform->ZeGlobalCommandListCount <
695-
Queue->Device->Platform->ZeMaxCommandListCache)) {
715+
ZeMaxCommandListCacheSize)) {
696716
ZE_CALL(zeCommandListCreate,
697717
(Queue->Context->ZeContext, Queue->Device->ZeDevice,
698718
&ZeCommandListDesc, ZeCommandList));
@@ -982,6 +1002,27 @@ static bool setEnvVar(const char *name, const char *value) {
9821002
return true;
9831003
}
9841004

1005+
pi_result _pi_platform::initialize() {
1006+
// Cache driver properties
1007+
ze_driver_properties_t ZeDriverProperties;
1008+
ZE_CALL(zeDriverGetProperties, (ZeDriver, &ZeDriverProperties));
1009+
uint32_t DriverVersion = ZeDriverProperties.driverVersion;
1010+
// Intel Level-Zero GPU driver stores version as:
1011+
// | 31 - 24 | 23 - 16 | 15 - 0 |
1012+
// | Major | Minor | Build |
1013+
auto VersionMajor = std::to_string((DriverVersion & 0xFF000000) >> 24);
1014+
auto VersionMinor = std::to_string((DriverVersion & 0x00FF0000) >> 16);
1015+
auto VersionBuild = std::to_string(DriverVersion & 0x0000FFFF);
1016+
ZeDriverVersion = VersionMajor + "." + VersionMinor + "." + VersionBuild;
1017+
1018+
ze_api_version_t ZeApiVersion;
1019+
ZE_CALL(zeDriverGetApiVersion, (ZeDriver, &ZeApiVersion));
1020+
ZeDriverApiVersion = std::to_string(ZE_MAJOR_VERSION(ZeApiVersion)) + "." +
1021+
std::to_string(ZE_MINOR_VERSION(ZeApiVersion));
1022+
1023+
return PI_SUCCESS;
1024+
}
1025+
9851026
pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms,
9861027
pi_uint32 *NumPlatforms) {
9871028

@@ -1048,58 +1089,27 @@ pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms,
10481089

10491090
const std::lock_guard<sycl::detail::SpinLock> Lock{*PiPlatformsCacheMutex};
10501091
if (!PiPlatformCachePopulated) {
1051-
const char *CommandListCacheSize =
1052-
std::getenv("SYCL_PI_LEVEL_ZERO_MAX_COMMAND_LIST_CACHE");
1053-
pi_uint32 CommandListCacheSizeValue;
10541092
try {
1055-
CommandListCacheSizeValue =
1056-
CommandListCacheSize ? std::stoi(CommandListCacheSize) : 20000;
1057-
} catch (std::exception const &) {
1058-
zePrint(
1059-
"SYCL_PI_LEVEL_ZERO_MAX_COMMAND_LIST_CACHE: invalid value provided, "
1060-
"default set.\n");
1061-
CommandListCacheSizeValue = 20000;
1062-
}
1063-
1064-
try {
1065-
10661093
// Level Zero does not have concept of Platforms, but Level Zero driver is
10671094
// the closest match.
10681095
uint32_t ZeDriverCount = 0;
10691096
ZE_CALL(zeDriverGet, (&ZeDriverCount, nullptr));
10701097
if (ZeDriverCount == 0) {
10711098
PiPlatformCachePopulated = true;
10721099
} else {
1073-
ze_driver_handle_t ZeDriver;
1074-
PI_ASSERT(ZeDriverCount == 1, PI_INVALID_VALUE);
1075-
1076-
ZE_CALL(zeDriverGet, (&ZeDriverCount, &ZeDriver));
1077-
pi_platform Platform = new _pi_platform(ZeDriver);
1078-
1079-
// Cache driver properties
1080-
ze_driver_properties_t ZeDriverProperties;
1081-
ZE_CALL(zeDriverGetProperties, (ZeDriver, &ZeDriverProperties));
1082-
uint32_t ZeDriverVersion = ZeDriverProperties.driverVersion;
1083-
// Intel Level-Zero GPU driver stores version as:
1084-
// | 31 - 24 | 23 - 16 | 15 - 0 |
1085-
// | Major | Minor | Build |
1086-
auto VersionMajor =
1087-
std::to_string((ZeDriverVersion & 0xFF000000) >> 24);
1088-
auto VersionMinor =
1089-
std::to_string((ZeDriverVersion & 0x00FF0000) >> 16);
1090-
auto VersionBuild = std::to_string(ZeDriverVersion & 0x0000FFFF);
1091-
Platform->ZeDriverVersion =
1092-
VersionMajor + "." + VersionMinor + "." + VersionBuild;
1093-
1094-
ze_api_version_t ZeApiVersion;
1095-
ZE_CALL(zeDriverGetApiVersion, (ZeDriver, &ZeApiVersion));
1096-
Platform->ZeDriverApiVersion =
1097-
std::to_string(ZE_MAJOR_VERSION(ZeApiVersion)) + "." +
1098-
std::to_string(ZE_MINOR_VERSION(ZeApiVersion));
1099-
1100-
Platform->ZeMaxCommandListCache = CommandListCacheSizeValue;
1101-
// Save a copy in the cache for future uses.
1102-
PiPlatformsCache->push_back(Platform);
1100+
std::vector<ze_driver_handle_t> ZeDrivers;
1101+
ZeDrivers.resize(ZeDriverCount);
1102+
1103+
ZE_CALL(zeDriverGet, (&ZeDriverCount, ZeDrivers.data()));
1104+
for (uint32_t I = 0; I < ZeDriverCount; ++I) {
1105+
pi_platform Platform = new _pi_platform(ZeDrivers[I]);
1106+
pi_result Result = Platform->initialize();
1107+
if (Result != PI_SUCCESS) {
1108+
return Result;
1109+
}
1110+
// Save a copy in the cache for future uses.
1111+
PiPlatformsCache->push_back(Platform);
1112+
}
11031113
PiPlatformCachePopulated = true;
11041114
}
11051115
} catch (const std::bad_alloc &) {
@@ -1109,16 +1119,10 @@ pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms,
11091119
}
11101120
}
11111121

1112-
if (Platforms && NumEntries > 0) {
1113-
uint32_t I = 0;
1114-
for (const pi_platform &CachedPlatform : *PiPlatformsCache) {
1115-
if (I < NumEntries) {
1116-
*Platforms++ = CachedPlatform;
1117-
I++;
1118-
} else {
1119-
break;
1120-
}
1121-
}
1122+
// Populate returned platforms from the cache.
1123+
if (Platforms) {
1124+
PI_ASSERT(NumEntries <= PiPlatformsCache->size(), PI_INVALID_PLATFORM);
1125+
std::copy_n(PiPlatformsCache->begin(), NumEntries, Platforms);
11221126
}
11231127

11241128
if (NumPlatforms)

sycl/plugins/level_zero/pi_level_zero.hpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ struct _pi_object {
6868

6969
struct _pi_platform {
7070
_pi_platform(ze_driver_handle_t Driver) : ZeDriver{Driver} {}
71+
// Performs initialization of a newly constructed PI platform.
72+
pi_result initialize();
7173

7274
// Level Zero lacks the notion of a platform, but there is a driver, which is
7375
// a pretty good fit to keep here.
@@ -83,12 +85,6 @@ struct _pi_platform {
8385
pi_device getDeviceFromNativeHandle(ze_device_handle_t);
8486
bool DeviceCachePopulated = false;
8587

86-
// Maximum Number of Command Lists that can be created.
87-
// This Value is initialized to 20000, but can be changed by the user
88-
// thru the environment variable SYCL_PI_LEVEL_ZERO_MAX_COMMAND_LIST_CACHE
89-
// ie SYCL_PI_LEVEL_ZERO_MAX_COMMAND_LIST_CACHE =10000.
90-
int ZeMaxCommandListCache = 0;
91-
9288
// Current number of L0 Command Lists created on this platform.
9389
// this number must not exceed ZeMaxCommandListCache.
9490
std::atomic<int> ZeGlobalCommandListCount{0};

0 commit comments

Comments
 (0)