@@ -76,19 +76,92 @@ ur_result_t getZesDeviceHandle(zes_uuid_t coreDeviceUuid,
76
76
return UR_RESULT_ERROR_INVALID_ARGUMENT;
77
77
}
78
78
79
+ /* *
80
+ * @brief Initializes the platforms by querying Level Zero drivers and devices.
81
+ *
82
+ * This function initializes the platforms by querying the available Level Zero
83
+ * drivers and devices. It handles different behaviors based on the presence of
84
+ * drivers obtained through `zeDriverGet` and initialized drivers through
85
+ * `zeInitDrivers`.
86
+ *
87
+ * @param platforms A vector to store the initialized platform handles.
88
+ * @param ZesResult The result of a previous ZES (Level Zero System) operation.
89
+ * @return ur_result_t The result of the initialization process.
90
+ *
91
+ * The function performs the following steps:
92
+ * 1. Queries the number of Level Zero drivers using `zeDriverGet`.
93
+ * 2. If drivers are found, it retrieves their handles.
94
+ * 3. If no drivers are found in either `zeInitDrivers` or `zeDriverGet`,
95
+ * it logs a message and returns success.
96
+ * 4. If `zeInitDrivers` is supported by the global adapter, it retrieves
97
+ * their handles and properties.
98
+ * 5. It compares the drivers obtained from `zeDriverGet` and `zeInitDrivers`,
99
+ * adding unique drivers to the list.
100
+ * 6. If `zeInitDrivers` is not supported, it uses the drivers obtained
101
+ * from `zeDriverGet`.
102
+ * 7. For each driver, it queries the devices and checks if they are GPU
103
+ * devices.
104
+ * 8. If a GPU device is found, it initializes a platform for the driver and
105
+ * adds it to the platforms vector.
106
+ * 9. If ZES operations are successful, it populates the ZES/ZE device mapping
107
+ * for the devices into the platform.
108
+ * 10. The function handles exceptions and returns the appropriate result.
109
+ */
79
110
ur_result_t initPlatforms (PlatformVec &platforms,
80
111
ze_result_t ZesResult) noexcept try {
112
+ std::vector<ze_driver_handle_t > ZeDrivers;
113
+ std::vector<ze_driver_handle_t > ZeDriverGetHandles;
114
+ std::vector<ze_driver_handle_t > ZeInitDriversHandles;
115
+ std::vector<ze_device_handle_t > ZeDevices;
81
116
uint32_t ZeDriverCount = 0 ;
82
- ZE2UR_CALL (zeDriverGet, (&ZeDriverCount, nullptr ));
83
- if (ZeDriverCount == 0 ) {
117
+ uint32_t ZeDriverGetCount = 0 ;
118
+
119
+ auto ZeDriverGetResult =
120
+ ZE_CALL_NOCHECK (zeDriverGet, (&ZeDriverGetCount, nullptr ));
121
+ if (ZeDriverGetCount > 0 && ZeDriverGetResult == ZE_RESULT_SUCCESS) {
122
+ ZeDriverGetHandles.resize (ZeDriverGetCount);
123
+ ZE2UR_CALL (zeDriverGet, (&ZeDriverGetCount, ZeDriverGetHandles.data ()));
124
+ }
125
+ if (ZeDriverGetCount == 0 && GlobalAdapter->ZeInitDriversCount == 0 ) {
126
+ logger::debug (" \n No Valid L0 Drivers found.\n " );
84
127
return UR_RESULT_SUCCESS;
85
128
}
86
129
87
- std::vector<ze_driver_handle_t > ZeDrivers;
88
- std::vector<ze_device_handle_t > ZeDevices;
89
- ZeDrivers.resize (ZeDriverCount);
90
-
91
- ZE2UR_CALL (zeDriverGet, (&ZeDriverCount, ZeDrivers.data ()));
130
+ if (GlobalAdapter->InitDriversSupported ) {
131
+ ZeInitDriversHandles.resize (GlobalAdapter->ZeInitDriversCount );
132
+ ZeDrivers.resize (GlobalAdapter->ZeInitDriversCount );
133
+ ZE2UR_CALL (GlobalAdapter->initDriversFunctionPtr ,
134
+ (&GlobalAdapter->ZeInitDriversCount , ZeInitDriversHandles.data (),
135
+ &GlobalAdapter->InitDriversDesc ));
136
+ ZeDrivers.assign (ZeInitDriversHandles.begin (), ZeInitDriversHandles.end ());
137
+ if (ZeDriverGetCount > 0 && GlobalAdapter->ZeInitDriversCount > 0 ) {
138
+ for (uint32_t X = 0 ; X < GlobalAdapter->ZeInitDriversCount ; ++X) {
139
+ for (uint32_t Y = 0 ; Y < ZeDriverGetCount; ++Y) {
140
+ ZeStruct<ze_driver_properties_t > ZeDriverGetProperties;
141
+ ZeStruct<ze_driver_properties_t > ZeInitDriverProperties;
142
+ ZE2UR_CALL (zeDriverGetProperties,
143
+ (ZeDriverGetHandles[Y], &ZeDriverGetProperties));
144
+ ZE2UR_CALL (zeDriverGetProperties,
145
+ (ZeInitDriversHandles[X], &ZeInitDriverProperties));
146
+ // If zeDriverGet driver is different from zeInitDriver driver, add it
147
+ // to the list. This allows for older drivers to be used alongside
148
+ // newer drivers.
149
+ if (ZeDriverGetProperties.driverVersion !=
150
+ ZeInitDriverProperties.driverVersion ) {
151
+ logger::debug (" \n zeDriverHandle {} added to the zeInitDrivers list "
152
+ " of possible handles.\n " ,
153
+ ZeDriverGetHandles[Y]);
154
+ ZeDrivers.push_back (ZeDriverGetHandles[Y]);
155
+ }
156
+ }
157
+ }
158
+ }
159
+ } else {
160
+ ZeDrivers.resize (ZeDriverGetCount);
161
+ ZeDrivers.assign (ZeDriverGetHandles.begin (), ZeDriverGetHandles.end ());
162
+ }
163
+ ZeDriverCount = ZeDrivers.size ();
164
+ logger::debug (" \n {} L0 Drivers found.\n " , ZeDriverCount);
92
165
for (uint32_t I = 0 ; I < ZeDriverCount; ++I) {
93
166
// Keep track of the first platform init for this Driver
94
167
bool DriverPlatformInit = false ;
@@ -214,6 +287,15 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
214
287
return std::atoi (UrRet);
215
288
}();
216
289
290
+ // Dynamically load the new L0 apis separately.
291
+ // This must be done to avoid attempting to use symbols that do
292
+ // not exist in older loader runtimes.
293
+ #ifdef _WIN32
294
+ HMODULE processHandle = GetModuleHandle (NULL );
295
+ #else
296
+ HMODULE processHandle = nullptr ;
297
+ #endif
298
+
217
299
// initialize level zero only once.
218
300
if (GlobalAdapter->ZeResult == std::nullopt) {
219
301
// Setting these environment variables before running zeInit will enable
@@ -235,20 +317,80 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
235
317
// called multiple times. Declaring the return value as "static" ensures
236
318
// it's only called once.
237
319
320
+ // Set ZES_ENABLE_SYSMAN by default if the user has not set it.
321
+ if (UrSysManEnvInitEnabled) {
322
+ setEnvVar (" ZES_ENABLE_SYSMAN" , " 1" );
323
+ }
324
+
238
325
// Init with all flags set to enable for all driver types to be init in
239
326
// the application.
240
327
ze_init_flags_t L0InitFlags = ZE_INIT_FLAG_GPU_ONLY;
241
328
if (UrL0InitAllDrivers) {
242
329
L0InitFlags |= ZE_INIT_FLAG_VPU_ONLY;
243
330
}
244
-
245
- // Set ZES_ENABLE_SYSMAN by default if the user has not set it.
246
- if (UrSysManEnvInitEnabled) {
247
- setEnvVar (" ZES_ENABLE_SYSMAN" , " 1" );
248
- }
249
331
logger::debug (" \n zeInit with flags value of {}\n " ,
250
332
static_cast <int >(L0InitFlags));
251
- GlobalAdapter->ZeResult = ZE_CALL_NOCHECK (zeInit, (L0InitFlags));
333
+ GlobalAdapter->ZeInitResult = ZE_CALL_NOCHECK (zeInit, (L0InitFlags));
334
+ if (*GlobalAdapter->ZeInitResult != ZE_RESULT_SUCCESS) {
335
+ logger::debug (" \n zeInit failed with {}\n " ,
336
+ *GlobalAdapter->ZeInitResult );
337
+ }
338
+
339
+ bool useInitDrivers = false ;
340
+ zel_version_t loader_version = {};
341
+ size_t num_components;
342
+ auto result = zelLoaderGetVersions (&num_components, nullptr );
343
+ if (result == ZE_RESULT_SUCCESS) {
344
+ zel_component_version_t *versions =
345
+ new zel_component_version_t [num_components];
346
+ result = zelLoaderGetVersions (&num_components, versions);
347
+ if (result == ZE_RESULT_SUCCESS) {
348
+ for (size_t i = 0 ; i < num_components; ++i) {
349
+ if (strncmp (versions[i].component_name , " loader" ,
350
+ strlen (" loader" )) == 0 ) {
351
+ loader_version = versions[i].component_lib_version ;
352
+ logger::debug (" \n Level Zero Loader Version: {}.{}.{}\n " ,
353
+ loader_version.major , loader_version.minor ,
354
+ loader_version.patch );
355
+ break ;
356
+ }
357
+ }
358
+ }
359
+ delete[] versions;
360
+ if (loader_version.major > 1 ||
361
+ (loader_version.major == 1 && loader_version.minor > 19 ) ||
362
+ (loader_version.major == 1 && loader_version.minor == 19 &&
363
+ loader_version.patch >= 2 )) {
364
+ useInitDrivers = true ;
365
+ }
366
+ }
367
+
368
+ if (useInitDrivers) {
369
+ GlobalAdapter->initDriversFunctionPtr =
370
+ (ze_pfnInitDrivers_t)ur_loader::LibLoader::getFunctionPtr (
371
+ processHandle, " zeInitDrivers" );
372
+ if (GlobalAdapter->initDriversFunctionPtr ) {
373
+ logger::debug (" \n zeInitDrivers with flags value of {}\n " ,
374
+ static_cast <int >(GlobalAdapter->InitDriversDesc .flags ));
375
+ GlobalAdapter->ZeInitDriversResult =
376
+ ZE_CALL_NOCHECK (GlobalAdapter->initDriversFunctionPtr ,
377
+ (&GlobalAdapter->ZeInitDriversCount , nullptr ,
378
+ &GlobalAdapter->InitDriversDesc ));
379
+ if (*GlobalAdapter->ZeInitDriversResult == ZE_RESULT_SUCCESS) {
380
+ GlobalAdapter->InitDriversSupported = true ;
381
+ } else {
382
+ logger::debug (" \n zeInitDrivers failed with {}\n " ,
383
+ *GlobalAdapter->ZeInitDriversResult );
384
+ }
385
+ }
386
+ }
387
+
388
+ if (*GlobalAdapter->ZeInitResult == ZE_RESULT_SUCCESS ||
389
+ *GlobalAdapter->ZeInitDriversResult == ZE_RESULT_SUCCESS) {
390
+ GlobalAdapter->ZeResult = ZE_RESULT_SUCCESS;
391
+ } else {
392
+ GlobalAdapter->ZeResult = ZE_RESULT_ERROR_UNINITIALIZED;
393
+ }
252
394
}
253
395
assert (GlobalAdapter->ZeResult !=
254
396
std::nullopt); // verify that level-zero is initialized
@@ -260,19 +402,11 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
260
402
return ;
261
403
}
262
404
if (*GlobalAdapter->ZeResult != ZE_RESULT_SUCCESS) {
263
- logger::error (" zeInit: Level Zero initialization failure\n " );
405
+ logger::error (" Level Zero initialization failure\n " );
264
406
result = ze2urResult (*GlobalAdapter->ZeResult );
265
407
266
408
return ;
267
409
}
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
276
410
277
411
// Check if the user has enabled the default L0 SysMan initialization.
278
412
const int UrSysmanZesinitEnable = [] {
0 commit comments