-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathutils_level_zero.cpp
More file actions
763 lines (684 loc) · 26 KB
/
utils_level_zero.cpp
File metadata and controls
763 lines (684 loc) · 26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
/*
* Copyright (C) 2024-2025 Intel Corporation
*
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "utils_level_zero.h"
#include <memory>
#include <stdlib.h>
#include "utils_concurrency.h"
#include "utils_load_library.h"
struct libze_ops {
ze_result_t (*zeInit)(ze_init_flags_t flags);
ze_result_t (*zeDriverGet)(uint32_t *pCount, ze_driver_handle_t *phDrivers);
ze_result_t (*zeDeviceGet)(ze_driver_handle_t hDriver, uint32_t *pCount,
ze_device_handle_t *phDevices);
ze_result_t (*zeDeviceGetProperties)(
ze_device_handle_t hDevice, ze_device_properties_t *pDeviceProperties);
ze_result_t (*zeContextCreate)(ze_driver_handle_t hDriver,
const ze_context_desc_t *desc,
ze_context_handle_t *phContext);
ze_result_t (*zeContextDestroy)(ze_context_handle_t hContext);
ze_result_t (*zeCommandQueueCreate)(
ze_context_handle_t hContext, ze_device_handle_t hDevice,
const ze_command_queue_desc_t *desc,
ze_command_queue_handle_t *phCommandQueue);
ze_result_t (*zeCommandQueueDestroy)(
ze_command_queue_handle_t hCommandQueue);
ze_result_t (*zeCommandQueueExecuteCommandLists)(
ze_command_queue_handle_t hCommandQueue, uint32_t numCommandLists,
ze_command_list_handle_t *phCommandLists, ze_fence_handle_t hFence);
ze_result_t (*zeCommandQueueSynchronize)(
ze_command_queue_handle_t hCommandQueue, uint64_t timeout);
ze_result_t (*zeCommandListCreate)(ze_context_handle_t hContext,
ze_device_handle_t hDevice,
const ze_command_list_desc_t *desc,
ze_command_list_handle_t *phCommandList);
ze_result_t (*zeCommandListDestroy)(ze_command_list_handle_t hCommandList);
ze_result_t (*zeCommandListClose)(ze_command_list_handle_t hCommandList);
ze_result_t (*zeCommandListAppendMemoryCopy)(
ze_command_list_handle_t hCommandList, void *dstptr, const void *srcptr,
size_t size, ze_event_handle_t hSignalEvent, uint32_t numWaitEvents,
ze_event_handle_t *phWaitEvents);
ze_result_t (*zeCommandListAppendMemoryFill)(
ze_command_list_handle_t hCommandList, void *ptr, const void *pattern,
size_t pattern_size, size_t size, ze_event_handle_t hSignalEvent,
uint32_t numWaitEvents, ze_event_handle_t *phWaitEvents);
ze_result_t (*zeMemGetAllocProperties)(
ze_context_handle_t hContext, const void *ptr,
ze_memory_allocation_properties_t *pMemAllocProperties,
ze_device_handle_t *phDevice);
ze_result_t (*zeMemAllocDevice)(ze_context_handle_t,
const ze_device_mem_alloc_desc_t *, size_t,
size_t, ze_device_handle_t, void **);
ze_result_t (*zeMemFree)(ze_context_handle_t, void *);
ze_result_t (*zeDeviceGetMemoryProperties)(
ze_device_handle_t hDevice, uint32_t *pCount,
ze_device_memory_properties_t *pMemProperties);
} libze_ops;
#if USE_DLOPEN
// Generic no-op stub function for all callbacks
template <typename... Args> ze_result_t noop_stub(Args &&...) {
return ZE_RESULT_SUCCESS; // Always return ZE_RESULT_SUCCESS
}
struct DlHandleCloser {
void operator()(void *dlHandle) {
if (dlHandle) {
// Reset all function pointers to no-op stubs in case the library
// but some other global object still try to call Level Zero functions.
libze_ops.zeInit = [](auto... args) { return noop_stub(args...); };
libze_ops.zeDriverGet = [](auto... args) {
return noop_stub(args...);
};
libze_ops.zeDeviceGet = [](auto... args) {
return noop_stub(args...);
};
libze_ops.zeDeviceGetProperties = [](auto... args) {
return noop_stub(args...);
};
libze_ops.zeContextCreate = [](auto... args) {
return noop_stub(args...);
};
libze_ops.zeContextDestroy = [](auto... args) {
return noop_stub(args...);
};
libze_ops.zeCommandQueueCreate = [](auto... args) {
return noop_stub(args...);
};
libze_ops.zeCommandQueueDestroy = [](auto... args) {
return noop_stub(args...);
};
libze_ops.zeCommandQueueExecuteCommandLists = [](auto... args) {
return noop_stub(args...);
};
libze_ops.zeCommandQueueSynchronize = [](auto... args) {
return noop_stub(args...);
};
libze_ops.zeCommandListCreate = [](auto... args) {
return noop_stub(args...);
};
libze_ops.zeCommandListDestroy = [](auto... args) {
return noop_stub(args...);
};
libze_ops.zeCommandListClose = [](auto... args) {
return noop_stub(args...);
};
libze_ops.zeCommandListAppendMemoryCopy = [](auto... args) {
return noop_stub(args...);
};
libze_ops.zeCommandListAppendMemoryFill = [](auto... args) {
return noop_stub(args...);
};
libze_ops.zeMemGetAllocProperties = [](auto... args) {
return noop_stub(args...);
};
libze_ops.zeMemAllocDevice = [](auto... args) {
return noop_stub(args...);
};
libze_ops.zeMemFree = [](auto... args) {
return noop_stub(args...);
};
libze_ops.zeDeviceGetMemoryProperties = [](auto... args) {
return noop_stub(args...);
};
utils_close_library(dlHandle);
}
}
};
std::unique_ptr<void, DlHandleCloser> zeDlHandle = nullptr;
int InitLevelZeroOps() {
#ifdef _WIN32
const char *lib_name = "ze_loader.dll";
#else
const char *lib_name = "libze_loader.so.1";
#endif
// Load Level Zero symbols
#if OPEN_ZE_LIBRARY_GLOBAL
// NOTE UMF_UTIL_OPEN_LIBRARY_GLOBAL adds all loaded symbols to the
// global symbol table.
int open_flags = UMF_UTIL_OPEN_LIBRARY_GLOBAL;
#else
int open_flags = 0;
#endif
zeDlHandle = std::unique_ptr<void, DlHandleCloser>(
utils_open_library(lib_name, open_flags));
*(void **)&libze_ops.zeInit =
utils_get_symbol_addr(zeDlHandle.get(), "zeInit", lib_name);
if (libze_ops.zeInit == nullptr) {
fprintf(stderr, "zeInit symbol not found in %s\n", lib_name);
return -1;
}
*(void **)&libze_ops.zeDriverGet =
utils_get_symbol_addr(zeDlHandle.get(), "zeDriverGet", lib_name);
if (libze_ops.zeDriverGet == nullptr) {
fprintf(stderr, "zeDriverGet symbol not found in %s\n", lib_name);
return -1;
}
*(void **)&libze_ops.zeDeviceGet =
utils_get_symbol_addr(zeDlHandle.get(), "zeDeviceGet", lib_name);
if (libze_ops.zeDeviceGet == nullptr) {
fprintf(stderr, "zeDeviceGet symbol not found in %s\n", lib_name);
return -1;
}
*(void **)&libze_ops.zeDeviceGetProperties = utils_get_symbol_addr(
zeDlHandle.get(), "zeDeviceGetProperties", lib_name);
if (libze_ops.zeDeviceGetProperties == nullptr) {
fprintf(stderr, "zeDeviceGetProperties symbol not found in %s\n",
lib_name);
return -1;
}
*(void **)&libze_ops.zeContextCreate =
utils_get_symbol_addr(zeDlHandle.get(), "zeContextCreate", lib_name);
if (libze_ops.zeContextCreate == nullptr) {
fprintf(stderr, "zeContextCreate symbol not found in %s\n", lib_name);
return -1;
}
*(void **)&libze_ops.zeContextDestroy =
utils_get_symbol_addr(zeDlHandle.get(), "zeContextDestroy", lib_name);
if (libze_ops.zeContextDestroy == nullptr) {
fprintf(stderr, "zeContextDestroy symbol not found in %s\n", lib_name);
return -1;
}
*(void **)&libze_ops.zeCommandQueueCreate = utils_get_symbol_addr(
zeDlHandle.get(), "zeCommandQueueCreate", lib_name);
if (libze_ops.zeCommandQueueCreate == nullptr) {
fprintf(stderr, "zeCommandQueueCreate symbol not found in %s\n",
lib_name);
return -1;
}
*(void **)&libze_ops.zeCommandQueueDestroy = utils_get_symbol_addr(
zeDlHandle.get(), "zeCommandQueueDestroy", lib_name);
if (libze_ops.zeCommandQueueDestroy == nullptr) {
fprintf(stderr, "zeCommandQueueDestroy symbol not found in %s\n",
lib_name);
return -1;
}
*(void **)&libze_ops.zeCommandQueueExecuteCommandLists =
utils_get_symbol_addr(zeDlHandle.get(),
"zeCommandQueueExecuteCommandLists", lib_name);
if (libze_ops.zeCommandQueueExecuteCommandLists == nullptr) {
fprintf(stderr,
"zeCommandQueueExecuteCommandLists symbol not found in %s\n",
lib_name);
return -1;
}
*(void **)&libze_ops.zeCommandQueueSynchronize = utils_get_symbol_addr(
zeDlHandle.get(), "zeCommandQueueSynchronize", lib_name);
if (libze_ops.zeCommandQueueSynchronize == nullptr) {
fprintf(stderr, "zeCommandQueueSynchronize symbol not found in %s\n",
lib_name);
return -1;
}
*(void **)&libze_ops.zeCommandListCreate = utils_get_symbol_addr(
zeDlHandle.get(), "zeCommandListCreate", lib_name);
if (libze_ops.zeCommandListCreate == nullptr) {
fprintf(stderr, "zeCommandListCreate symbol not found in %s\n",
lib_name);
return -1;
}
*(void **)&libze_ops.zeCommandListDestroy = utils_get_symbol_addr(
zeDlHandle.get(), "zeCommandListDestroy", lib_name);
if (libze_ops.zeCommandListDestroy == nullptr) {
fprintf(stderr, "zeCommandListDestroy symbol not found in %s\n",
lib_name);
return -1;
}
*(void **)&libze_ops.zeCommandListClose =
utils_get_symbol_addr(zeDlHandle.get(), "zeCommandListClose", lib_name);
if (libze_ops.zeCommandListClose == nullptr) {
fprintf(stderr, "zeCommandListClose symbol not found in %s\n",
lib_name);
return -1;
}
*(void **)&libze_ops.zeCommandListAppendMemoryCopy = utils_get_symbol_addr(
zeDlHandle.get(), "zeCommandListAppendMemoryCopy", lib_name);
if (libze_ops.zeCommandListAppendMemoryCopy == nullptr) {
fprintf(stderr,
"zeCommandListAppendMemoryCopy symbol not found in %s\n",
lib_name);
return -1;
}
*(void **)&libze_ops.zeCommandListAppendMemoryFill = utils_get_symbol_addr(
zeDlHandle.get(), "zeCommandListAppendMemoryFill", lib_name);
if (libze_ops.zeCommandListAppendMemoryFill == nullptr) {
fprintf(stderr,
"zeCommandListAppendMemoryFill symbol not found in %s\n",
lib_name);
return -1;
}
*(void **)&libze_ops.zeMemGetAllocProperties = utils_get_symbol_addr(
zeDlHandle.get(), "zeMemGetAllocProperties", lib_name);
if (libze_ops.zeMemGetAllocProperties == nullptr) {
fprintf(stderr, "zeMemGetAllocProperties symbol not found in %s\n",
lib_name);
return -1;
}
*(void **)&libze_ops.zeMemAllocDevice =
utils_get_symbol_addr(zeDlHandle.get(), "zeMemAllocDevice", lib_name);
if (libze_ops.zeMemAllocDevice == nullptr) {
fprintf(stderr, "zeMemAllocDevice symbol not found in %s\n", lib_name);
return -1;
}
*(void **)&libze_ops.zeMemFree =
utils_get_symbol_addr(zeDlHandle.get(), "zeMemFree", lib_name);
if (libze_ops.zeMemFree == nullptr) {
fprintf(stderr, "zeMemFree symbol not found in %s\n", lib_name);
return -1;
}
*(void **)&libze_ops.zeDeviceGetMemoryProperties = utils_get_symbol_addr(
zeDlHandle.get(), "zeDeviceGetMemoryProperties", lib_name);
if (libze_ops.zeDeviceGetMemoryProperties == nullptr) {
fprintf(stderr, "zeDeviceGetMemoryProperties symbol not found in %s\n",
lib_name);
return -1;
}
return 0;
}
#else // USE_DLOPEN
int InitLevelZeroOps() {
// Level Zero is linked statically but we prepare ops structure to
// make test code consistent
libze_ops.zeInit = zeInit;
libze_ops.zeDriverGet = zeDriverGet;
libze_ops.zeDeviceGet = zeDeviceGet;
libze_ops.zeDeviceGetProperties = zeDeviceGetProperties;
libze_ops.zeContextCreate = zeContextCreate;
libze_ops.zeContextDestroy = zeContextDestroy;
libze_ops.zeCommandQueueCreate = zeCommandQueueCreate;
libze_ops.zeCommandQueueDestroy = zeCommandQueueDestroy;
libze_ops.zeCommandQueueExecuteCommandLists =
zeCommandQueueExecuteCommandLists;
libze_ops.zeCommandQueueSynchronize = zeCommandQueueSynchronize;
libze_ops.zeCommandListCreate = zeCommandListCreate;
libze_ops.zeCommandListDestroy = zeCommandListDestroy;
libze_ops.zeCommandListClose = zeCommandListClose;
libze_ops.zeCommandListAppendMemoryCopy = zeCommandListAppendMemoryCopy;
libze_ops.zeCommandListAppendMemoryFill = zeCommandListAppendMemoryFill;
libze_ops.zeMemGetAllocProperties = zeMemGetAllocProperties;
libze_ops.zeMemAllocDevice = zeMemAllocDevice;
libze_ops.zeMemFree = zeMemFree;
libze_ops.zeDeviceGetMemoryProperties = zeDeviceGetMemoryProperties;
return 0;
}
#endif // USE_DLOPEN
static int utils_ze_init_level_zero_lib(void) {
ze_init_flag_t flags = ZE_INIT_FLAG_GPU_ONLY;
ze_result_t result = libze_ops.zeInit(flags);
if (result != ZE_RESULT_SUCCESS) {
return -1;
}
return 0;
}
static UTIL_ONCE_FLAG level_zero_init_flag = UTIL_ONCE_FLAG_INIT;
static int InitResult;
static void utils_ze_init_level_zero_once(void) {
InitResult = InitLevelZeroOps();
if (InitResult != 0) {
return;
}
InitResult = utils_ze_init_level_zero_lib();
}
int utils_ze_init_level_zero(void) {
utils_init_once(&level_zero_init_flag, utils_ze_init_level_zero_once);
return InitResult;
}
int utils_ze_get_drivers(uint32_t *drivers_num_,
ze_driver_handle_t **drivers_) {
int ret = 0;
ze_result_t ze_result;
ze_driver_handle_t *drivers = NULL;
uint32_t drivers_num = 0;
ze_result = libze_ops.zeDriverGet(&drivers_num, NULL);
if (ze_result != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeDriverGet() failed!\n");
ret = -1;
goto fn_fail;
}
if (drivers_num == 0) {
goto fn_exit;
}
drivers =
(ze_driver_handle_t *)malloc(drivers_num * sizeof(ze_driver_handle_t));
if (!drivers) {
ret = -1;
goto fn_fail;
}
ze_result = libze_ops.zeDriverGet(&drivers_num, drivers);
if (ze_result != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeDriverGet() failed!\n");
ret = -1;
goto fn_fail;
}
fn_exit:
*drivers_num_ = drivers_num;
*drivers_ = drivers;
return ret;
fn_fail:
*drivers_num_ = 0;
if (drivers) {
free(drivers);
*drivers_ = NULL;
}
return ret;
}
int utils_ze_get_devices(ze_driver_handle_t driver, uint32_t *devices_num_,
ze_device_handle_t **devices_) {
ze_result_t ze_result;
int ret = 0;
uint32_t devices_num = 0;
ze_device_handle_t *devices = NULL;
ze_result = libze_ops.zeDeviceGet(driver, &devices_num, NULL);
if (ze_result != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeDeviceGet() failed!\n");
ret = -1;
goto fn_fail;
}
if (devices_num == 0) {
goto fn_exit;
}
devices =
(ze_device_handle_t *)malloc(devices_num * sizeof(ze_device_handle_t));
if (!devices) {
ret = -1;
goto fn_fail;
}
ze_result = libze_ops.zeDeviceGet(driver, &devices_num, devices);
if (ze_result != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeDeviceGet() failed!\n");
ret = -1;
goto fn_fail;
}
fn_exit:
*devices_num_ = devices_num;
*devices_ = devices;
return ret;
fn_fail:
devices_num = 0;
if (devices) {
free(devices);
devices = NULL;
}
return ret;
}
int utils_ze_find_driver_with_gpu(uint32_t *driver_idx,
ze_driver_handle_t *driver_) {
int ret = 0;
ze_result_t ze_result;
uint32_t drivers_num = 0;
ze_device_handle_t *devices = NULL;
ze_driver_handle_t *drivers = NULL;
ze_driver_handle_t driver_with_gpus = NULL;
ret = utils_ze_get_drivers(&drivers_num, &drivers);
if (ret) {
goto fn_fail;
}
/* Find a driver with GPU */
for (uint32_t i = 0; i < drivers_num; ++i) {
uint32_t devices_num = 0;
ze_driver_handle_t driver = drivers[i];
ret = utils_ze_get_devices(driver, &devices_num, &devices);
if (ret) {
goto fn_fail;
}
for (uint32_t d = 0; d < devices_num; ++d) {
ze_device_handle_t device = devices[d];
ze_device_properties_t device_properties;
device_properties.stype = ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES;
device_properties.pNext = NULL;
ze_result =
libze_ops.zeDeviceGetProperties(device, &device_properties);
if (ze_result != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeDeviceGetProperties() failed!\n");
ret = -1;
goto fn_fail;
}
if (device_properties.type == ZE_DEVICE_TYPE_GPU) {
driver_with_gpus = driver;
*driver_idx = i;
break;
}
}
if (devices) {
free(devices);
devices = NULL;
}
if (driver_with_gpus != NULL) {
goto fn_exit;
}
}
fn_fail:
if (devices) {
free(devices);
}
fn_exit:
*driver_ = driver_with_gpus;
if (drivers) {
free(drivers);
}
return ret;
}
int utils_ze_find_gpu_device(ze_driver_handle_t driver,
ze_device_handle_t *device_) {
int ret = -1;
uint32_t devices_num = 0;
ze_device_handle_t *devices = NULL;
ze_device_handle_t device;
ret = utils_ze_get_devices(driver, &devices_num, &devices);
if (ret) {
return ret;
}
for (uint32_t d = 0; d < devices_num; ++d) {
device = devices[d];
ze_device_properties_t device_properties;
device_properties.stype = ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES;
device_properties.pNext = NULL;
ze_result_t ze_result =
libze_ops.zeDeviceGetProperties(device, &device_properties);
if (ze_result != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeDeviceGetProperties() failed!\n");
ret = -1;
break;
}
if (device_properties.type == ZE_DEVICE_TYPE_GPU) {
*device_ = device;
ret = 0;
break;
}
}
if (devices) {
free(devices);
}
return ret;
}
int utils_ze_level_zero_fill(ze_context_handle_t context,
ze_device_handle_t device, void *ptr, size_t size,
const void *pattern, size_t pattern_size) {
int ret = 0;
ze_command_queue_desc_t commandQueueDesc = {
ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC,
NULL,
0,
0,
0,
ZE_COMMAND_QUEUE_MODE_DEFAULT,
ZE_COMMAND_QUEUE_PRIORITY_NORMAL};
ze_command_list_desc_t commandListDesc = {
ZE_STRUCTURE_TYPE_COMMAND_LIST_DESC, 0, 0,
ZE_COMMAND_LIST_FLAG_RELAXED_ORDERING};
ze_command_queue_handle_t hCommandQueue;
ze_result_t ze_result = libze_ops.zeCommandQueueCreate(
context, device, &commandQueueDesc, &hCommandQueue);
if (ze_result != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeCommandQueueCreate() failed!\n");
return -1;
}
ze_command_list_handle_t hCommandList;
ze_result = libze_ops.zeCommandListCreate(context, device, &commandListDesc,
&hCommandList);
if (ze_result != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeCommandListCreate() failed!\n");
ret = -1;
goto err_queue_destroy;
}
// fill memory with a pattern
ze_result = libze_ops.zeCommandListAppendMemoryFill(
hCommandList, ptr, pattern, pattern_size, size, NULL, 0, NULL);
if (ze_result != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeCommandListAppendMemoryFill() failed!\n");
ret = -1;
goto err_list_destroy;
}
// close and execute the command list
ze_result = libze_ops.zeCommandListClose(hCommandList);
if (ze_result != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeCommandListClose() failed!\n");
ret = -1;
goto err_list_destroy;
}
ze_result = libze_ops.zeCommandQueueExecuteCommandLists(
hCommandQueue, 1, &hCommandList, NULL);
if (ze_result != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeCommandQueueExecuteCommandLists() failed!\n");
ret = -1;
goto err_list_destroy;
}
// sync
ze_result = libze_ops.zeCommandQueueSynchronize(hCommandQueue, UINT64_MAX);
if (ze_result != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeCommandQueueSynchronize() failed!\n");
ret = -1;
goto err_list_destroy;
}
// cleanup
err_list_destroy:
ze_result = libze_ops.zeCommandListDestroy(hCommandList);
if (ze_result != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeCommandListDestroy() failed!\n");
ret = -1;
}
err_queue_destroy:
ze_result = libze_ops.zeCommandQueueDestroy(hCommandQueue);
if (ze_result != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeCommandQueueDestroy() failed!\n");
ret = -1;
}
return ret;
}
int utils_ze_level_zero_copy(ze_context_handle_t context,
ze_device_handle_t device, void *dst_ptr,
const void *src_ptr, size_t size) {
int ret = 0;
ze_command_queue_desc_t commandQueueDesc = {
ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC,
NULL,
0,
0,
0,
ZE_COMMAND_QUEUE_MODE_DEFAULT,
ZE_COMMAND_QUEUE_PRIORITY_NORMAL};
ze_command_list_desc_t commandListDesc = {
ZE_STRUCTURE_TYPE_COMMAND_LIST_DESC, 0, 0,
ZE_COMMAND_LIST_FLAG_RELAXED_ORDERING};
ze_command_queue_handle_t hCommandQueue;
ze_result_t ze_result = libze_ops.zeCommandQueueCreate(
context, device, &commandQueueDesc, &hCommandQueue);
if (ze_result != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeCommandQueueCreate() failed!\n");
return -1;
}
ze_command_list_handle_t hCommandList;
ze_result = libze_ops.zeCommandListCreate(context, device, &commandListDesc,
&hCommandList);
if (ze_result != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeCommandListCreate() failed!\n");
ret = -1;
goto err_queue_destroy;
}
// copy from device memory to host memory
ze_result = libze_ops.zeCommandListAppendMemoryCopy(
hCommandList, dst_ptr, src_ptr, size, NULL, 0, NULL);
if (ze_result != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeCommandListAppendMemoryCopy() failed!\n");
ret = -1;
goto err_list_destroy;
}
// close and execute the command list
ze_result = libze_ops.zeCommandListClose(hCommandList);
if (ze_result != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeCommandListClose() failed!\n");
ret = -1;
goto err_list_destroy;
}
ze_result = libze_ops.zeCommandQueueExecuteCommandLists(
hCommandQueue, 1, &hCommandList, NULL);
if (ze_result != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeCommandQueueExecuteCommandLists() failed!\n");
ret = -1;
goto err_list_destroy;
}
ze_result = libze_ops.zeCommandQueueSynchronize(hCommandQueue, UINT64_MAX);
if (ze_result != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeCommandQueueSynchronize() failed!\n");
ret = -1;
goto err_list_destroy;
}
// cleanup
err_list_destroy:
ze_result = libze_ops.zeCommandListDestroy(hCommandList);
if (ze_result != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeCommandListDestroy() failed!\n");
ret = -1;
}
err_queue_destroy:
ze_result = libze_ops.zeCommandQueueDestroy(hCommandQueue);
if (ze_result != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeCommandQueueDestroy() failed!\n");
ret = -1;
}
return ret;
}
int utils_ze_create_context(ze_driver_handle_t driver,
ze_context_handle_t *context) {
ze_result_t ze_result;
ze_context_desc_t ctxtDesc;
ctxtDesc.stype = ZE_STRUCTURE_TYPE_CONTEXT_DESC;
ctxtDesc.pNext = NULL;
ctxtDesc.flags = 0;
ze_result = libze_ops.zeContextCreate(driver, &ctxtDesc, context);
if (ze_result != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeContextCreate() failed!\n");
return -1;
}
return 0;
}
int utils_ze_destroy_context(ze_context_handle_t context) {
ze_result_t ze_result;
ze_result = libze_ops.zeContextDestroy(context);
if (ze_result != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeContextDestroy() failed!\n");
return -1;
}
return 0;
}
ze_memory_type_t utils_ze_get_mem_type(ze_context_handle_t context, void *ptr) {
ze_device_handle_t device = NULL;
ze_memory_allocation_properties_t alloc_props;
alloc_props.stype = ZE_STRUCTURE_TYPE_MEMORY_ALLOCATION_PROPERTIES;
alloc_props.pNext = NULL;
alloc_props.type = ZE_MEMORY_TYPE_UNKNOWN;
alloc_props.id = 0;
alloc_props.pageSize = 0;
libze_ops.zeMemGetAllocProperties(context, ptr, &alloc_props, &device);
return alloc_props.type;
}
int64_t utils_ze_get_num_memory_properties(ze_device_handle_t device) {
uint32_t pCount = 0;
ze_result_t ze_result =
libze_ops.zeDeviceGetMemoryProperties(device, &pCount, nullptr);
if (ze_result != ZE_RESULT_SUCCESS) {
fprintf(stderr, "zeDeviceGetMemoryProperties() failed!\n");
return -1;
}
return static_cast<int64_t>(pCount);
}