Skip to content

Commit 61badcb

Browse files
committed
[L0] Implement Support for zeInitDrivers
- As of v1.10 of the L0 spec, zeInit and zeDriverGet is the old init pathway and the desired init api is zeInitDrivers. This new api allows for multi heterogenous drivers to coexist in a single L0 Process. Signed-off-by: Neil R. Spruit <[email protected]>
1 parent 9ae8e65 commit 61badcb

File tree

2 files changed

+54
-24
lines changed

2 files changed

+54
-24
lines changed

source/adapters/level_zero/adapter.cpp

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,25 @@ ur_result_t getZesDeviceHandle(zes_uuid_t coreDeviceUuid,
7878

7979
ur_result_t initPlatforms(PlatformVec &platforms,
8080
ze_result_t ZesResult) noexcept try {
81-
uint32_t ZeDriverCount = 0;
82-
ZE2UR_CALL(zeDriverGet, (&ZeDriverCount, nullptr));
83-
if (ZeDriverCount == 0) {
81+
if (!GlobalAdapter->initDriversFunctionPtr) {
82+
ZE2UR_CALL(zeDriverGet, (&GlobalAdapter->ZeDriverCount, nullptr));
83+
}
84+
if (GlobalAdapter->ZeDriverCount == 0) {
8485
return UR_RESULT_SUCCESS;
8586
}
8687

8788
std::vector<ze_driver_handle_t> ZeDrivers;
8889
std::vector<ze_device_handle_t> ZeDevices;
89-
ZeDrivers.resize(ZeDriverCount);
90-
91-
ZE2UR_CALL(zeDriverGet, (&ZeDriverCount, ZeDrivers.data()));
92-
for (uint32_t I = 0; I < ZeDriverCount; ++I) {
90+
ZeDrivers.resize(GlobalAdapter->ZeDriverCount);
91+
92+
if (GlobalAdapter->initDriversFunctionPtr) {
93+
ZE2UR_CALL(GlobalAdapter->initDriversFunctionPtr,
94+
(&GlobalAdapter->ZeDriverCount, ZeDrivers.data(),
95+
&GlobalAdapter->InitDriversDesc));
96+
} else {
97+
ZE2UR_CALL(zeDriverGet, (&GlobalAdapter->ZeDriverCount, ZeDrivers.data()));
98+
}
99+
for (uint32_t I = 0; I < GlobalAdapter->ZeDriverCount; ++I) {
93100
// Keep track of the first platform init for this Driver
94101
bool DriverPlatformInit = false;
95102
ze_device_properties_t device_properties{};
@@ -214,6 +221,15 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
214221
return std::atoi(UrRet);
215222
}();
216223

224+
// Dynamically load the new L0 apis separately.
225+
// This must be done to avoid attempting to use symbols that do
226+
// not exist in older loader runtimes.
227+
#ifdef _WIN32
228+
HMODULE processHandle = GetModuleHandle(NULL);
229+
#else
230+
HMODULE processHandle = nullptr;
231+
#endif
232+
217233
// initialize level zero only once.
218234
if (GlobalAdapter->ZeResult == std::nullopt) {
219235
// Setting these environment variables before running zeInit will enable
@@ -235,20 +251,37 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
235251
// called multiple times. Declaring the return value as "static" ensures
236252
// it's only called once.
237253

238-
// Init with all flags set to enable for all driver types to be init in
239-
// the application.
240-
ze_init_flags_t L0InitFlags = ZE_INIT_FLAG_GPU_ONLY;
241-
if (UrL0InitAllDrivers) {
242-
L0InitFlags |= ZE_INIT_FLAG_VPU_ONLY;
243-
}
254+
GlobalAdapter->initDriversFunctionPtr =
255+
(ze_pfnInitDrivers_t)ur_loader::LibLoader::getFunctionPtr(
256+
processHandle, "zeInitDrivers");
244257

245258
// Set ZES_ENABLE_SYSMAN by default if the user has not set it.
246259
if (UrSysManEnvInitEnabled) {
247260
setEnvVar("ZES_ENABLE_SYSMAN", "1");
248261
}
249-
logger::debug("\nzeInit with flags value of {}\n",
250-
static_cast<int>(L0InitFlags));
251-
GlobalAdapter->ZeResult = ZE_CALL_NOCHECK(zeInit, (L0InitFlags));
262+
263+
if (GlobalAdapter->initDriversFunctionPtr) {
264+
GlobalAdapter->InitDriversDesc.pNext = nullptr;
265+
GlobalAdapter->InitDriversDesc.flags = ZE_INIT_DRIVER_TYPE_FLAG_GPU;
266+
logger::debug("\nzeInitDrivers with flags value of {}\n",
267+
static_cast<int>(GlobalAdapter->InitDriversDesc.flags));
268+
GlobalAdapter->ZeResult =
269+
ZE_CALL_NOCHECK(GlobalAdapter->initDriversFunctionPtr,
270+
(&GlobalAdapter->ZeDriverCount, nullptr,
271+
&GlobalAdapter->InitDriversDesc));
272+
}
273+
274+
if (!GlobalAdapter->ZeResult) {
275+
// Init with all flags set to enable for all driver types to be init in
276+
// the application.
277+
ze_init_flags_t L0InitFlags = ZE_INIT_FLAG_GPU_ONLY;
278+
if (UrL0InitAllDrivers) {
279+
L0InitFlags |= ZE_INIT_FLAG_VPU_ONLY;
280+
}
281+
logger::debug("\nzeInit with flags value of {}\n",
282+
static_cast<int>(L0InitFlags));
283+
GlobalAdapter->ZeResult = ZE_CALL_NOCHECK(zeInit, (L0InitFlags));
284+
}
252285
}
253286
assert(GlobalAdapter->ZeResult !=
254287
std::nullopt); // verify that level-zero is initialized
@@ -265,14 +298,6 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
265298

266299
return;
267300
}
268-
// Dynamically load the new L0 SysMan separate init and new EXP apis
269-
// separately. This must be done to avoid attempting to use symbols that do
270-
// not exist in older loader runtimes.
271-
#ifdef _WIN32
272-
HMODULE processHandle = GetModuleHandle(NULL);
273-
#else
274-
HMODULE processHandle = nullptr;
275-
#endif
276301

277302
// Check if the user has enabled the default L0 SysMan initialization.
278303
const int UrSysmanZesinitEnable = [] {

source/adapters/level_zero/adapter.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <optional>
1818
#include <ur/ur.hpp>
1919
#include <ze_api.h>
20+
#include <ze_ddi.h>
2021
#include <zes_ddi.h>
2122

2223
using PlatformVec = std::vector<std::unique_ptr<ur_platform_handle_t_>>;
@@ -31,6 +32,10 @@ struct ur_adapter_handle_t_ {
3132
zes_pfnDriverGetDeviceByUuidExp_t getDeviceByUUIdFunctionPtr = nullptr;
3233
zes_pfnDriverGet_t getSysManDriversFunctionPtr = nullptr;
3334
zes_pfnInit_t sysManInitFunctionPtr = nullptr;
35+
ze_pfnInitDrivers_t initDriversFunctionPtr = nullptr;
36+
ze_init_driver_type_desc_t InitDriversDesc = {
37+
ZE_STRUCTURE_TYPE_INIT_DRIVER_TYPE_DESC, nullptr, 0};
38+
uint32_t ZeDriverCount = 0;
3439

3540
std::optional<ze_result_t> ZeResult;
3641
std::optional<ze_result_t> ZesResult;

0 commit comments

Comments
 (0)