Skip to content

Commit 9ac62df

Browse files
committed
[L0] Use zesInit for SysMan API usage
- Change to using zesInit and zes data structures for accessing L0 SysMan functionality. - Updated Platform & Devices to store zes handles if sysman support is available. - Given Legacy Environment Variable from user, then fallback to old functionality. - Fixed Return code on error to be consistently unsupported enumeration. Signed-off-by: Neil R. Spruit <neil.r.spruit@intel.com>
1 parent 06c4cd0 commit 9ac62df

File tree

4 files changed

+102
-22
lines changed

4 files changed

+102
-22
lines changed

source/adapters/level_zero/adapter.cpp

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,30 @@ class ur_legacy_sink : public logger::Sink {
5252
};
5353
};
5454

55-
ur_result_t initPlatforms(PlatformVec &platforms) noexcept try {
55+
// Find the corresponding ZesDevice Handle for a given ZeDevice
56+
ur_result_t getZesDeviceHandle(zes_uuid_t coreDeviceUuid,
57+
zes_device_handle_t *ZesDevice,
58+
uint32_t *SubDeviceId, ze_bool_t *SubDevice) {
59+
uint32_t ZesDriverCount = 0;
60+
std::vector<zes_driver_handle_t> ZesDrivers;
61+
std::vector<zes_device_handle_t> ZesDevices;
62+
ze_result_t ZesResult = ZE_RESULT_ERROR_INVALID_ARGUMENT;
63+
ZE2UR_CALL(zesDriverGet, (&ZesDriverCount, nullptr));
64+
ZesDrivers.resize(ZesDriverCount);
65+
ZE2UR_CALL(zesDriverGet, (&ZesDriverCount, ZesDrivers.data()));
66+
for (uint32_t I = 0; I < ZesDriverCount; ++I) {
67+
ZesResult = ZE_CALL_NOCHECK(
68+
zesDriverGetDeviceByUuidExp,
69+
(ZesDrivers[I], coreDeviceUuid, ZesDevice, SubDevice, SubDeviceId));
70+
if (ZesResult == ZE_RESULT_SUCCESS) {
71+
return UR_RESULT_SUCCESS;
72+
}
73+
}
74+
return UR_RESULT_ERROR_INVALID_ARGUMENT;
75+
}
76+
77+
ur_result_t initPlatforms(PlatformVec &platforms,
78+
ze_result_t ZesResult) noexcept try {
5679
uint32_t ZeDriverCount = 0;
5780
ZE2UR_CALL(zeDriverGet, (&ZeDriverCount, nullptr));
5881
if (ZeDriverCount == 0) {
@@ -65,24 +88,37 @@ ur_result_t initPlatforms(PlatformVec &platforms) noexcept try {
6588

6689
ZE2UR_CALL(zeDriverGet, (&ZeDriverCount, ZeDrivers.data()));
6790
for (uint32_t I = 0; I < ZeDriverCount; ++I) {
91+
bool DriverInit = false;
6892
ze_device_properties_t device_properties{};
6993
device_properties.stype = ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES;
7094
uint32_t ZeDeviceCount = 0;
7195
ZE2UR_CALL(zeDeviceGet, (ZeDrivers[I], &ZeDeviceCount, nullptr));
7296
ZeDevices.resize(ZeDeviceCount);
7397
ZE2UR_CALL(zeDeviceGet, (ZeDrivers[I], &ZeDeviceCount, ZeDevices.data()));
98+
auto platform = std::make_unique<ur_platform_handle_t_>(ZeDrivers[I]);
7499
// Check if this driver has GPU Devices
75100
for (uint32_t D = 0; D < ZeDeviceCount; ++D) {
76101
ZE2UR_CALL(zeDeviceGetProperties, (ZeDevices[D], &device_properties));
77-
78102
if (ZE_DEVICE_TYPE_GPU == device_properties.type) {
79-
// If this Driver is a GPU, save it as a usable platform.
80-
auto platform = std::make_unique<ur_platform_handle_t_>(ZeDrivers[I]);
81-
UR_CALL(platform->initialize());
103+
if (!DriverInit) {
104+
// If this Driver is a GPU, save it as a usable platform.
105+
UR_CALL(platform->initialize());
82106

83-
// Save a copy in the cache for future uses.
84-
platforms.push_back(std::move(platform));
85-
break;
107+
// Save a copy in the cache for future uses.
108+
platforms.push_back(std::move(platform));
109+
DriverInit = true;
110+
}
111+
if (ZesResult == ZE_RESULT_SUCCESS) {
112+
ur_zes_device_handle_data_t ZesDeviceData;
113+
zes_uuid_t ZesUUID;
114+
std::memcpy(&ZesUUID, &device_properties.uuid, sizeof(zes_uuid_t));
115+
if (getZesDeviceHandle(
116+
ZesUUID, &ZesDeviceData.ZesDevice, &ZesDeviceData.SubDeviceId,
117+
&ZesDeviceData.SubDevice) == UR_RESULT_SUCCESS) {
118+
platforms.back()->ZedeviceToZesDeviceMap.insert(
119+
std::make_pair(ZeDevices[D], std::move(ZesDeviceData)));
120+
}
121+
}
86122
}
87123
}
88124
}
@@ -172,7 +208,9 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
172208
return;
173209
}
174210

175-
ur_result_t err = initPlatforms(platforms);
211+
GlobalAdapter->ZesResult = ZE_CALL_NOCHECK(zesInit, (0));
212+
213+
ur_result_t err = initPlatforms(platforms, *GlobalAdapter->ZesResult);
176214
if (err == UR_RESULT_SUCCESS) {
177215
result = std::move(platforms);
178216
} else {

source/adapters/level_zero/adapter.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ struct ur_adapter_handle_t_ {
2727
std::mutex Mutex;
2828

2929
std::optional<ze_result_t> ZeResult;
30+
std::optional<ze_result_t> ZesResult;
3031
ZeCache<Result<PlatformVec>> PlatformCache;
3132
logger::Logger &logger;
3233
};

source/adapters/level_zero/device.cpp

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -747,11 +747,13 @@ ur_result_t urDeviceGetInfo(
747747
}
748748

749749
case UR_DEVICE_INFO_GLOBAL_MEM_FREE: {
750-
if (getenv("ZES_ENABLE_SYSMAN") == nullptr) {
751-
setErrorMessage("Set ZES_ENABLE_SYSMAN=1 to obtain free memory",
752-
UR_RESULT_ERROR_UNINITIALIZED,
750+
bool SysManEnv = getenv_tobool("ZES_ENABLE_SYSMAN", false);
751+
if ((Device->Platform->ZedeviceToZesDeviceMap.size() == 0) && !SysManEnv) {
752+
setErrorMessage("SysMan support is unavailable on this system. Please "
753+
"check your level zero driver installation.",
754+
UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION,
753755
static_cast<int32_t>(ZE_RESULT_ERROR_UNINITIALIZED));
754-
return UR_RESULT_ERROR_ADAPTER_SPECIFIC;
756+
return UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION;
755757
}
756758
// Calculate the global memory size as the max limit that can be reported as
757759
// "free" memory for the user to allocate.
@@ -760,30 +762,57 @@ ur_result_t urDeviceGetInfo(
760762
// Currently this is only the one enumerated with ordinal 0.
761763
uint64_t FreeMemory = 0;
762764
uint32_t MemCount = 0;
763-
ZE2UR_CALL(zesDeviceEnumMemoryModules, (ZeDevice, &MemCount, nullptr));
765+
766+
zes_device_handle_t ZesDevice = Device->ZeDevice;
767+
struct ur_zes_device_handle_data_t ZesDeviceData = {};
768+
// If legacy sysman is enabled thru the environment variable, then zesInit
769+
// will fail, but sysman is still usable so go the legacy route.
770+
if (!SysManEnv) {
771+
auto It = Device->Platform->ZedeviceToZesDeviceMap.find(Device->ZeDevice);
772+
if (It == Device->Platform->ZedeviceToZesDeviceMap.end()) {
773+
// no matching device
774+
return UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION;
775+
} else {
776+
ZesDeviceData =
777+
Device->Platform->ZedeviceToZesDeviceMap[Device->ZeDevice];
778+
ZesDevice = ZesDeviceData.ZesDevice;
779+
}
780+
}
781+
782+
ZE2UR_CALL(zesDeviceEnumMemoryModules, (ZesDevice, &MemCount, nullptr));
764783
if (MemCount != 0) {
765784
std::vector<zes_mem_handle_t> ZesMemHandles(MemCount);
766785
ZE2UR_CALL(zesDeviceEnumMemoryModules,
767-
(ZeDevice, &MemCount, ZesMemHandles.data()));
786+
(ZesDevice, &MemCount, ZesMemHandles.data()));
768787
for (auto &ZesMemHandle : ZesMemHandles) {
769788
ZesStruct<zes_mem_properties_t> ZesMemProperties;
770789
ZE2UR_CALL(zesMemoryGetProperties, (ZesMemHandle, &ZesMemProperties));
771790
// For root-device report memory from all memory modules since that
772791
// is what totally available in the default implicit scaling mode.
773792
// For sub-devices only report memory local to them.
774-
if (!Device->isSubDevice() || Device->ZeDeviceProperties->subdeviceId ==
775-
ZesMemProperties.subdeviceId) {
776-
777-
ZesStruct<zes_mem_state_t> ZesMemState;
778-
ZE2UR_CALL(zesMemoryGetState, (ZesMemHandle, &ZesMemState));
779-
FreeMemory += ZesMemState.free;
793+
if (SysManEnv) {
794+
if (!Device->isSubDevice() ||
795+
Device->ZeDeviceProperties->subdeviceId ==
796+
ZesMemProperties.subdeviceId) {
797+
798+
ZesStruct<zes_mem_state_t> ZesMemState;
799+
ZE2UR_CALL(zesMemoryGetState, (ZesMemHandle, &ZesMemState));
800+
FreeMemory += ZesMemState.free;
801+
}
802+
} else {
803+
if (ZesDeviceData.SubDeviceId == ZesMemProperties.subdeviceId ||
804+
!ZesDeviceData.SubDevice) {
805+
ZesStruct<zes_mem_state_t> ZesMemState;
806+
ZE2UR_CALL(zesMemoryGetState, (ZesMemHandle, &ZesMemState));
807+
FreeMemory += ZesMemState.free;
808+
}
780809
}
781810
}
782811
}
783812
if (MemCount > 0) {
784813
return ReturnValue(std::min(GlobalMemSize, FreeMemory));
785814
} else {
786-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
815+
return UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION;
787816
}
788817
}
789818
case UR_DEVICE_INFO_MEMORY_CLOCK_RATE: {

source/adapters/level_zero/platform.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@
1212
#include "common.hpp"
1313
#include "ur_api.h"
1414
#include "ze_api.h"
15+
#include "zes_api.h"
1516

1617
struct ur_device_handle_t_;
1718

1819
typedef size_t DeviceId;
1920

21+
struct ur_zes_device_handle_data_t {
22+
zes_device_handle_t ZesDevice;
23+
uint32_t SubDeviceId;
24+
ze_bool_t SubDevice = false;
25+
};
26+
2027
struct ur_platform_handle_t_ : public _ur_platform {
2128
ur_platform_handle_t_(ze_driver_handle_t Driver)
2229
: ZeDriver{Driver}, ZeApiVersion{ZE_API_VERSION_CURRENT} {}
@@ -27,6 +34,11 @@ struct ur_platform_handle_t_ : public _ur_platform {
2734
// a pretty good fit to keep here.
2835
ze_driver_handle_t ZeDriver;
2936

37+
// Cache of the ZesDevices mapped to the ZeDevices for use in zes apis calls
38+
// based on a ze device handle.
39+
std::unordered_map<ze_device_handle_t, ur_zes_device_handle_data_t>
40+
ZedeviceToZesDeviceMap;
41+
3042
// Given a multi driver scenario, the driver handle must be translated to the
3143
// internal driver handle to allow calls to driver experimental apis.
3244
ze_driver_handle_t ZeDriverHandleExpTranslated;

0 commit comments

Comments
 (0)