Skip to content

Commit 42b88fb

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 c94dbc8 commit 42b88fb

File tree

2 files changed

+65
-25
lines changed

2 files changed

+65
-25
lines changed

source/adapters/level_zero/adapter.cpp

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,26 @@ 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->InitDriversSupported) {
82+
ZE2UR_CALL(zeDriverGet, (&GlobalAdapter->ZeDriverCount, nullptr));
83+
}
84+
printf("Driver count: %d\n", GlobalAdapter->ZeDriverCount);
85+
if (GlobalAdapter->ZeDriverCount == 0) {
8486
return UR_RESULT_SUCCESS;
8587
}
8688

8789
std::vector<ze_driver_handle_t> ZeDrivers;
8890
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) {
91+
ZeDrivers.resize(GlobalAdapter->ZeDriverCount);
92+
93+
if (GlobalAdapter->InitDriversSupported) {
94+
ZE2UR_CALL(GlobalAdapter->initDriversFunctionPtr,
95+
(&GlobalAdapter->ZeDriverCount, ZeDrivers.data(),
96+
&GlobalAdapter->InitDriversDesc));
97+
} else {
98+
ZE2UR_CALL(zeDriverGet, (&GlobalAdapter->ZeDriverCount, ZeDrivers.data()));
99+
}
100+
for (uint32_t I = 0; I < GlobalAdapter->ZeDriverCount; ++I) {
93101
// Keep track of the first platform init for this Driver
94102
bool DriverPlatformInit = false;
95103
ze_device_properties_t device_properties{};
@@ -184,6 +192,7 @@ Behavior Summary:
184192
ur_adapter_handle_t_::ur_adapter_handle_t_()
185193
: logger(logger::get_logger("level_zero")) {
186194

195+
printf("adapter handle\n");
187196
if (UrL0Debug & UR_L0_DEBUG_BASIC) {
188197
logger.setLegacySink(std::make_unique<ur_legacy_sink>());
189198
};
@@ -194,6 +203,7 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
194203
}
195204

196205
PlatformCache.Compute = [](Result<PlatformVec> &result) {
206+
printf("platform cache\n");
197207
static std::once_flag ZeCallCountInitialized;
198208
try {
199209
std::call_once(ZeCallCountInitialized, []() {
@@ -214,6 +224,15 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
214224
return std::atoi(UrRet);
215225
}();
216226

227+
// Dynamically load the new L0 apis separately.
228+
// This must be done to avoid attempting to use symbols that do
229+
// not exist in older loader runtimes.
230+
#ifdef _WIN32
231+
HMODULE processHandle = GetModuleHandle(NULL);
232+
#else
233+
HMODULE processHandle = nullptr;
234+
#endif
235+
217236
// initialize level zero only once.
218237
if (GlobalAdapter->ZeResult == std::nullopt) {
219238
// Setting these environment variables before running zeInit will enable
@@ -235,44 +254,59 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
235254
// called multiple times. Declaring the return value as "static" ensures
236255
// it's only called once.
237256

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-
}
257+
GlobalAdapter->initDriversFunctionPtr =
258+
(ze_pfnInitDrivers_t)ur_loader::LibLoader::getFunctionPtr(
259+
processHandle, "zeInitDrivers");
244260

245261
// Set ZES_ENABLE_SYSMAN by default if the user has not set it.
246262
if (UrSysManEnvInitEnabled) {
247263
setEnvVar("ZES_ENABLE_SYSMAN", "1");
248264
}
249-
logger::debug("\nzeInit with flags value of {}\n",
250-
static_cast<int>(L0InitFlags));
251-
GlobalAdapter->ZeResult = ZE_CALL_NOCHECK(zeInit, (L0InitFlags));
265+
266+
if (GlobalAdapter->initDriversFunctionPtr) {
267+
GlobalAdapter->InitDriversDesc.pNext = nullptr;
268+
GlobalAdapter->InitDriversDesc.flags = ZE_INIT_DRIVER_TYPE_FLAG_GPU;
269+
logger::debug("\nzeInitDrivers with flags value of {}\n",
270+
static_cast<int>(GlobalAdapter->InitDriversDesc.flags));
271+
*GlobalAdapter->ZeResult =
272+
ZE_CALL_NOCHECK(GlobalAdapter->initDriversFunctionPtr,
273+
(&GlobalAdapter->ZeDriverCount, nullptr,
274+
&GlobalAdapter->InitDriversDesc));
275+
if (*GlobalAdapter->ZeResult == ZE_RESULT_SUCCESS) {
276+
GlobalAdapter->InitDriversSupported = true;
277+
}
278+
}
279+
280+
if (*GlobalAdapter->ZeResult != ZE_RESULT_SUCCESS) {
281+
// Init with all flags set to enable for all driver types to be init in
282+
// the application.
283+
ze_init_flags_t L0InitFlags = ZE_INIT_FLAG_GPU_ONLY;
284+
if (UrL0InitAllDrivers) {
285+
L0InitFlags |= ZE_INIT_FLAG_VPU_ONLY;
286+
}
287+
logger::debug("\nzeInit with flags value of {}\n",
288+
static_cast<int>(L0InitFlags));
289+
*GlobalAdapter->ZeResult = ZE_CALL_NOCHECK(zeInit, (L0InitFlags));
290+
printf("ZeResult: %d\n", *GlobalAdapter->ZeResult);
291+
}
252292
}
253293
assert(GlobalAdapter->ZeResult !=
254294
std::nullopt); // verify that level-zero is initialized
255295
PlatformVec platforms;
256296

257297
// Absorb the ZE_RESULT_ERROR_UNINITIALIZED and just return 0 Platforms.
258298
if (*GlobalAdapter->ZeResult == ZE_RESULT_ERROR_UNINITIALIZED) {
299+
printf("level zero uninit\n");
259300
result = std::move(platforms);
260301
return;
261302
}
262303
if (*GlobalAdapter->ZeResult != ZE_RESULT_SUCCESS) {
263-
logger::error("zeInit: Level Zero initialization failure\n");
304+
printf("level zero init failed\n");
305+
logger::error("Level Zero initialization failure\n");
264306
result = ze2urResult(*GlobalAdapter->ZeResult);
265307

266308
return;
267309
}
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
276310

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

source/adapters/level_zero/adapter.hpp

Lines changed: 6 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,11 @@ 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;
39+
bool InitDriversSupported = false;
3440

3541
std::optional<ze_result_t> ZeResult;
3642
std::optional<ze_result_t> ZesResult;

0 commit comments

Comments
 (0)