-
Notifications
You must be signed in to change notification settings - Fork 274
/
mpl_gpu_ze.c
3210 lines (2793 loc) · 101 KB
/
mpl_gpu_ze.c
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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) by Argonne National Laboratory
* See COPYRIGHT in top-level directory
*/
#include "mplconfig.h"
#ifdef MPL_HAVE_X86INTRIN_H
/* must come before mpl_trmem.h */
#include <x86intrin.h>
#endif
#include "mpl.h"
#include <assert.h>
#include <dlfcn.h>
MPL_SUPPRESS_OSX_HAS_NO_SYMBOLS_WARNING;
#ifdef MPL_HAVE_ZE
#include <dirent.h>
#if defined(MPL_HAVE_DRM_I915_DRM_H)
#include "drm/i915_drm.h"
#define MPL_ENABLE_DRMFD
#elif defined(MPL_HAVE_LIBDRM_I915_DRM_H)
#include "libdrm/i915_drm.h"
#define MPL_ENABLE_DRMFD
#endif
#include <sys/ioctl.h>
#include <sys/syscall.h> /* Definition of SYS_* constants */
#include "uthash.h"
#include "utlist.h"
/* Latest Level-zero Specification:
* http://spec.oneapi.com/level-zero/latest/index.html
*/
ze_context_handle_t ze_context;
ze_driver_handle_t ze_driver_handle;
/* ze_devices_handle contains all devices and subdevices. Indices [0, device_count) are
* devices while the rest are subdevices. Keeping them all in the same array allows for easy
* comparison of device handle when a device id is passed from the upper layer. The only time it
* matters if we have a subdevice vs a device is when creating or mapping an ipc handle. In these
* situations, we use the subdevice_map to find the upper device id for indexing into
* shared_device_fds, since these are only opened on the upper devices. */
ze_device_handle_t *ze_devices_handle = NULL;
static int gpu_initialized = 0;
static uint32_t device_count; /* Counts all local devices, does not include subdevices */
static uint32_t local_ze_device_count; /* Counts all local devices and subdevices */
static uint32_t global_ze_device_count; /* Counts all global devices and subdevices */
static int max_dev_id; /* Does not include subdevices */
static int max_subdev_id;
static char **device_list = NULL;
static int *engine_conversion = NULL;
#define MPL_ZE_EVENT_POOL_SIZE (4096)
typedef struct MPL_ze_event_pool {
ze_event_pool_handle_t pool;
struct MPL_ze_event_pool *next, *prev;
} MPL_ze_event_pool;
static MPL_ze_event_pool *ze_event_pools = NULL;
static MPL_gpu_event *ze_events = NULL;
typedef struct {
ze_command_queue_handle_t *cmdQueues;
MPL_cmdlist_pool_t *cmdList_pool;
ze_command_list_handle_t *cmdlists; /* immediate command lists */
unsigned int numQueues, curQueue;
} MPL_ze_engine_entry_t;
typedef struct {
int dev_id;
unsigned int numQueueGroups;
MPL_ze_engine_entry_t *engines;
MPL_gpu_event *prev_event[MPL_GPU_COPY_DIRECTION_TYPES]; /* for imemcpy */
MPL_cmdlist_pool_t *last_cmdList_entry[MPL_GPU_COPY_DIRECTION_TYPES]; /* for imemcopy */
#ifdef ZE_PCI_PROPERTIES_EXT_NAME
ze_pci_address_ext_t pci;
int pci_avail;
int sys_device_index;
#endif
} MPL_ze_device_entry_t;
static MPL_ze_device_entry_t *device_states;
/* Affinity mask contents */
typedef struct {
int num_dev;
int *dev_id;
int *subdev_id;
} affinity_mask_t;
static affinity_mask_t mask_contents;
#define MAX_GPU_STR_LEN 256
static char affinity_env[MAX_GPU_STR_LEN] = { 0 };
/* Mappings for translating between local and global device ids */
static int *local_to_global_map; /* [local_ze_device_count] */
static int *global_to_local_map; /* [global_ze_device_count] */
static int *global_to_root_map; /* [global_ze_device_count] */
/* Maps a subdevice id to the upper device id, specifically for indexing into shared_device_fds */
static int *subdevice_map = NULL;
/* Keeps the subdevice count for all locally visible devices */
static uint32_t *subdevice_count = NULL;
/* For drmfd */
typedef struct _physical_device_state {
int fd;
int domain, bus, device, function;
} physical_device_state;
static int physical_device_count = 0;
static physical_device_state *physical_device_states = NULL;
typedef struct {
const void *ptr;
int dev_id;
int handles[2];
uint32_t nhandles;
UT_hash_handle hh;
} MPL_ze_gem_hash_entry_t;
/*
this cache entry may cache two device pointers:
1. from remote device which is obtained by zeOpenIpcHandle
2. from a local device pointer which is obtained by zeGetIpchandle and mmap
*/
typedef struct {
uint64_t mem_id; /* key */
MPL_gpu_ipc_mem_handle_t ipc_handle;
int fds[2];
uint32_t nfds;
void *mapped_ptr;
size_t mapped_size; /* total size */
bool handle_cached;
UT_hash_handle hh;
} MPL_ze_ipc_handle_entry_t;
typedef struct {
uint64_t remote_mem_id;
int remote_dev_id;
pid_t remote_pid;
} MPL_ze_mapped_buffer_lookup_t;
typedef struct {
MPL_ze_mapped_buffer_lookup_t key;
void *ipc_buf;
void *mapped_ptr;
size_t mapped_size;
int fds[2];
int nfds; /* used when doing mmap */
UT_hash_handle hh;
} MPL_ze_mapped_buffer_entry_t;
static MPL_ze_gem_hash_entry_t *gem_hash = NULL;
static MPL_ze_ipc_handle_entry_t **ipc_cache_tracked = NULL;
static MPL_ze_mapped_buffer_entry_t **ipc_cache_mapped = NULL;
static MPL_ze_mapped_buffer_entry_t **ipc_cache_removal = NULL;
static MPL_ze_mapped_buffer_entry_t **mmap_cache_removal = NULL;
/* For pidfd */
#ifndef __NR_pidfd_open
#define __NR_pidfd_open 434 /* System call # on most architectures */
#endif
#ifndef __NR_pidfd_getfd
#define __NR_pidfd_getfd 438 /* System call # on most architectures */
#endif
typedef struct {
void *ptr;
uint64_t mem_id;
UT_hash_handle hh;
} MPL_ze_mem_id_entry_t;
static MPL_ze_mem_id_entry_t *mem_id_cache = NULL;
typedef struct gpu_free_hook {
void (*free_hook) (void *dptr);
struct gpu_free_hook *next;
} gpu_free_hook_s;
static MPL_initlock_t free_hook_mutex = MPL_INITLOCK_INITIALIZER;
pid_t mypid;
/* *INDENT-OFF* */
typedef ze_result_t (*pFnzexMemGetIpcHandles)(ze_context_handle_t, const void *, uint32_t *, ze_ipc_mem_handle_t *);
typedef ze_result_t (*pFnzexMemOpenIpcHandles)(ze_context_handle_t, ze_device_handle_t, uint32_t, ze_ipc_mem_handle_t *, ze_ipc_memory_flags_t, void **);
static pFnzexMemGetIpcHandles zexMemGetIpcHandles = NULL;
static pFnzexMemOpenIpcHandles zexMemOpenIpcHandles = NULL;
/* *INDENT-ON* */
/* Backend-specific functions */
/* *INDENT-OFF* */
typedef ze_result_t (*pFnzexDriverImportExternalPointer)(ze_driver_handle_t, void *, size_t);
typedef ze_result_t (*pFnzexDriverReleaseImportedPointer)(ze_driver_handle_t, void *);
typedef ze_result_t (*pFnzexDriverGetHostPointerBaseAddress)(ze_driver_handle_t, void *, void **);
static pFnzexDriverImportExternalPointer zexDriverImportExternalPointer = NULL;
static pFnzexDriverReleaseImportedPointer zexDriverReleaseImportedPointer = NULL;
static pFnzexDriverGetHostPointerBaseAddress zexDriverGetHostPointerBaseAddress = NULL;
/* *INDENT-ON* */
static int gpu_ze_init_driver(void);
static int fd_to_handle(int dev_fd, int fd, int *handle);
static int handle_to_fd(int dev_fd, int handle, int *fd);
static int close_handle(int dev_fd, int handle);
static int parse_affinity_mask();
static void get_max_dev_id(int *max_dev_id, int *max_subdev_id);
static int gpu_mem_hook_init(void);
static int remove_ipc_handle_entry(MPL_ze_mapped_buffer_entry_t * cache_entry, int dev_id);
static int MPL_event_pool_add_new_pool(void);
static void MPL_event_pool_destroy(void);
#ifdef ZE_PCI_PROPERTIES_EXT_NAME
static int search_physical_devices(ze_pci_address_ext_t pci);
#endif
/* For zeMemFree callbacks */
static gpu_free_hook_s *free_hook_chain = NULL;
static ze_result_t ZE_APICALL(*sys_zeMemFree) (ze_context_handle_t hContext, void *dptr) = NULL;
#define ZE_ERR_CHECK(ret) \
do { \
if (unlikely((ret) != ZE_RESULT_SUCCESS)) \
goto fn_fail; \
} while (0)
int MPL_gpu_get_dev_count(int *dev_cnt, int *dev_id, int *subdev_id)
{
int ret = MPL_SUCCESS;
if (!gpu_initialized) {
ret = MPL_gpu_init(0);
}
*dev_cnt = local_ze_device_count;
*dev_id = max_dev_id;
*subdev_id = max_subdev_id;
return ret;
}
int MPL_gpu_get_dev_list(int *dev_count, char ***dev_list, bool is_subdev)
{
int ret = MPL_SUCCESS;
if (!gpu_initialized) {
ret = MPL_gpu_init(0);
}
if (!is_subdev) {
device_list = (char **) MPL_malloc(device_count * sizeof(char *), MPL_MEM_OTHER);
assert(device_list != NULL);
for (int i = 0; i < device_count; ++i) {
int str_len = snprintf(NULL, 0, "%d", i);
device_list[i] = (char *) MPL_malloc((str_len + 1) * sizeof(char *), MPL_MEM_OTHER);
sprintf(device_list[i], "%d", i);
}
*dev_count = device_count;
*dev_list = device_list;
} else {
uint32_t driver_count = 0;
uint32_t *subdev_counts = NULL;
int total_subdev_count = 0;
ze_driver_handle_t *all_drivers = NULL;
ret = zeDriverGet(&driver_count, NULL);
assert(ret == ZE_RESULT_SUCCESS);
assert(device_count);
all_drivers = MPL_malloc(driver_count * sizeof(ze_driver_handle_t), MPL_MEM_OTHER);
assert(all_drivers);
ret = zeDriverGet(&driver_count, all_drivers);
assert(ret == ZE_RESULT_SUCCESS);
/* Find a driver instance with a GPU device */
for (int i = 0; i < driver_count; ++i) {
local_ze_device_count = 0;
ret = zeDeviceGet(all_drivers[i], &local_ze_device_count, NULL);
ze_devices_handle =
MPL_malloc(local_ze_device_count * sizeof(ze_device_handle_t), MPL_MEM_OTHER);
assert(ze_devices_handle);
subdev_counts = MPL_malloc(local_ze_device_count * sizeof(int), MPL_MEM_OTHER);
memset(subdev_counts, 0, local_ze_device_count * sizeof(int));
assert(subdev_counts);
ret = zeDeviceGet(all_drivers[i], &local_ze_device_count, ze_devices_handle);
assert(ret == ZE_RESULT_SUCCESS);
/* Check if the driver supports a gpu */
for (int d = 0; d < local_ze_device_count; ++d) {
ze_device_properties_t device_properties;
ret = zeDeviceGetProperties(ze_devices_handle[d], &device_properties);
assert(ret == ZE_RESULT_SUCCESS);
if (!(device_properties.flags & ZE_DEVICE_PROPERTY_FLAG_SUBDEVICE)) {
zeDeviceGetSubDevices(ze_devices_handle[d], &subdev_counts[d], NULL);
if (subdev_counts[d] == 0) {
/* ZE reports no subdevice when there is only one subdevice */
subdev_counts[d] = 1;
}
total_subdev_count += subdev_counts[d];
}
}
MPL_free(ze_devices_handle);
}
device_list = (char **) MPL_malloc(total_subdev_count * sizeof(char *), MPL_MEM_OTHER);
assert(device_list != NULL);
int idx = 0;
for (int i = 0; i < device_count; ++i) {
for (int j = 0; j < subdev_counts[i]; j++) {
int str_len = snprintf(NULL, 0, "%d.%d", i, j);
device_list[idx] =
(char *) MPL_malloc((str_len + 1) * sizeof(char *), MPL_MEM_OTHER);
sprintf(device_list[idx], "%d.%d", i, j);
device_list[idx][str_len] = 0;
idx++;
}
}
MPL_free(subdev_counts);
*dev_count = total_subdev_count;
*dev_list = device_list;
}
return ret;
}
int MPL_gpu_dev_affinity_to_env(int dev_count, char **dev_list, char **env)
{
int ret = MPL_SUCCESS;
memset(affinity_env, 0, MAX_GPU_STR_LEN);
if (dev_count == 0) {
snprintf(affinity_env, 3, "-1");
} else {
int str_offset = 0;
for (int i = 0; i < dev_count; ++i) {
if (i) {
MPL_strncpy(affinity_env + str_offset, ",", MAX_GPU_STR_LEN - str_offset);
str_offset++;
}
MPL_strncpy(affinity_env + str_offset, dev_list[i], MAX_GPU_STR_LEN - str_offset);
str_offset += strlen(dev_list[i]);
}
}
*env = affinity_env;
return ret;
}
int MPL_gpu_init_device_mappings(int max_devid, int max_subdevid)
{
int mpl_err = MPL_SUCCESS;
int global_dev_count = max_devid + 1;
int global_subdev_count = 0;
/* If max_subdevid is 0, then all procs use tile 0 as root devices, so subdevices aren't
* needed. */
if (max_subdevid == 0) {
global_ze_device_count = global_dev_count;
} else {
/* We can still have the situation where all procs use non-zero tile as root devices, but
* this can't be detected unless we also reduce subdevice count. Thus, consider them as
* subdevices in the global_to_local_map even if they are all root devices. */
global_subdev_count = max_subdevid + 1;
global_ze_device_count = global_dev_count * (global_subdev_count + 1);
}
/* Initialize local_to_global_map */
local_to_global_map = MPL_malloc(local_ze_device_count * sizeof(int), MPL_MEM_OTHER);
if (local_to_global_map == NULL) {
mpl_err = MPL_ERR_GPU_NOMEM;
goto fn_fail;
}
for (int i = 0; i < local_ze_device_count; ++i) {
local_to_global_map[i] = -1;
}
/* Initialize global_to_local_map */
global_to_local_map = MPL_malloc(global_ze_device_count * sizeof(int), MPL_MEM_OTHER);
if (global_to_local_map == NULL) {
mpl_err = MPL_ERR_GPU_NOMEM;
goto fn_fail;
}
for (int i = 0; i < global_ze_device_count; ++i) {
global_to_local_map[i] = -1;
}
/* Initialize global_to_root_map */
/* This is used during device comparison (MPL_gpu_query_is_same_dev) to support checking if two
* devices share the same root device even if one device isn't visible due to setting
* ZE_AFFINITY_MASK. This is necessary because the selection of copy engines defines the
* performance characteristic; if the comparison check is inaccurate (i.e.
* MPL_gpu_query_is_same_dev returns false when the two dev_ids do in fact reside on the same
* physical device), then a non-optimal copy engine will be selected.
*/
global_to_root_map = MPL_malloc(global_ze_device_count * sizeof(int), MPL_MEM_OTHER);
if (global_to_root_map == NULL) {
mpl_err = MPL_ERR_GPU_NOMEM;
goto fn_fail;
}
for (int i = 0; i < global_dev_count; ++i) {
global_to_root_map[i] = i;
for (int j = 0; j < global_subdev_count; j++) {
global_to_root_map[global_dev_count + i * global_subdev_count + j] = i;
}
}
if (mask_contents.num_dev > 0) {
int device, subdevice;
for (int i = 0; i < mask_contents.num_dev; ++i) {
device = mask_contents.dev_id[i];
subdevice = mask_contents.subdev_id[i];
/* Temporarily mark the device as visible. It might only be a subdevice that is
* visible. */
global_to_local_map[device] = 1;
/* Mark the subdevice(s) as visible. */
if (subdevice != -1) {
/* Handle special case where there are no subdevices among any device. */
if (global_subdev_count > 0) {
int idx = global_dev_count + device * global_subdev_count + subdevice;
global_to_local_map[idx] = 1;
}
} else {
int idx = global_dev_count + device * global_subdev_count;
for (int j = 0; j < global_subdev_count; ++j) {
global_to_local_map[idx + j] = 1;
}
}
}
} else {
for (int i = 0; i < global_dev_count; ++i) {
/* Set device as visible */
global_to_local_map[i] = 1;
/* Set subdevices as visible */
int idx = global_dev_count + i * global_subdev_count;
for (int j = 0; j < subdevice_count[i]; ++j) {
global_to_local_map[idx + j] = 1;
}
}
}
/* Setup global_to_local_map */
int local_dev_id = 0;
/* The root devices first */
for (int i = 0; i < global_dev_count; ++i) {
if (global_to_local_map[i] == 1) {
/* Check if the device has subdevices before setting its index. If it does not, then
* only the subdevice is visible. However, need to check for the special case that
* there are no subdevices among any device. */
if (subdevice_count[local_dev_id] || global_subdev_count == 0) {
global_to_local_map[i] = local_dev_id;
} else {
/* Find which subdevice is visible and give it the local device id since it is the
* root device. */
int idx = global_dev_count + i * global_subdev_count;
for (int j = 0; j < global_subdev_count; ++j) {
if (global_to_local_map[idx + j] == 1) {
global_to_local_map[idx + j] = local_dev_id;
}
}
/* Unset the device as the root device, since its subdevice is the root. */
global_to_local_map[i] = -1;
}
++local_dev_id;
}
}
/* The subdevices next */
for (int i = 0; i < global_dev_count; ++i) {
if (global_to_local_map[i] != -1) {
int idx = global_dev_count + i * global_subdev_count;
for (int j = 0; j < global_subdev_count; ++j) {
if (global_to_local_map[idx + j] == 1) {
global_to_local_map[idx + j] = local_dev_id;
++local_dev_id;
}
}
}
}
assert(local_dev_id == local_ze_device_count);
/* Setup local_to_global_map */
local_dev_id = 0;
for (int i = 0; i < global_ze_device_count; ++i) {
int local_id = global_to_local_map[i];
if (local_id != -1) {
local_to_global_map[local_id] = i;
}
}
fn_exit:
return mpl_err;
fn_fail:
goto fn_exit;
}
int MPL_gpu_init(int debug_summary)
{
int mpl_err = MPL_SUCCESS;
if (gpu_initialized) {
goto fn_exit;
}
MPL_gpu_info.debug_summary = debug_summary;
MPL_gpu_info.enable_ipc = true;
if (MPL_gpu_info.debug_summary) {
printf("==== GPU Init (ZE) ====\n");
}
mpl_err = gpu_ze_init_driver();
if (mpl_err != MPL_SUCCESS)
goto fn_fail;
MPL_gpu_info.debug_summary = debug_summary;
MPL_gpu_info.enable_ipc = true;
MPL_gpu_info.ipc_handle_type = MPL_GPU_IPC_HANDLE_SHAREABLE_FD;
max_dev_id = 0;
max_subdev_id = 0;
if (local_ze_device_count <= 0) {
gpu_initialized = 1;
goto fn_exit;
}
mpl_err = parse_affinity_mask();
if (mpl_err != MPL_SUCCESS)
goto fn_fail;
get_max_dev_id(&max_dev_id, &max_subdev_id);
if (likely(MPL_gpu_info.specialized_cache)) {
ipc_cache_tracked =
MPL_malloc(local_ze_device_count * sizeof(MPL_ze_ipc_handle_entry_t *), MPL_MEM_OTHER);
if (ipc_cache_tracked == NULL) {
mpl_err = MPL_ERR_GPU_NOMEM;
goto fn_fail;
}
ipc_cache_mapped =
MPL_malloc(local_ze_device_count * sizeof(MPL_ze_mapped_buffer_entry_t *),
MPL_MEM_OTHER);
if (ipc_cache_mapped == NULL) {
mpl_err = MPL_ERR_GPU_NOMEM;
goto fn_fail;
}
ipc_cache_removal =
MPL_malloc(local_ze_device_count * sizeof(MPL_ze_mapped_buffer_entry_t *),
MPL_MEM_OTHER);
if (ipc_cache_removal == NULL) {
mpl_err = MPL_ERR_GPU_NOMEM;
goto fn_fail;
}
mmap_cache_removal =
MPL_malloc(local_ze_device_count * sizeof(MPL_ze_mapped_buffer_entry_t *),
MPL_MEM_OTHER);
if (mmap_cache_removal == NULL) {
mpl_err = MPL_ERR_GPU_NOMEM;
goto fn_fail;
}
for (int i = 0; i < local_ze_device_count; ++i) {
ipc_cache_tracked[i] = NULL;
ipc_cache_mapped[i] = NULL;
ipc_cache_removal[i] = NULL;
mmap_cache_removal[i] = NULL;
}
MPL_gpu_free_hook_register(MPL_ze_ipc_remove_cache_handle);
}
/* Initialize gpu engine mapping */
engine_conversion = (int *) MPL_malloc(sizeof(int) * MPL_GPU_ENGINE_NUM_TYPES, MPL_MEM_OTHER);
engine_conversion[MPL_GPU_ENGINE_TYPE_COMPUTE] = 0; // Compute engine
engine_conversion[MPL_GPU_ENGINE_TYPE_COPY_HIGH_BANDWIDTH] = 1; // Main copy engine
engine_conversion[MPL_GPU_ENGINE_TYPE_COPY_LOW_LATENCY] = 2; // Link copy engine
mypid = getpid();
MPL_initlock_lock(&free_hook_mutex);
gpu_mem_hook_init();
MPL_initlock_unlock(&free_hook_mutex);
gpu_initialized = 1;
if (MPL_gpu_info.debug_summary) {
printf("device_count: %d\n", device_count);
printf("subdevice_count: %d\n", local_ze_device_count - device_count);
printf("=========================\n");
}
fn_exit:
return mpl_err;
fn_fail:
goto fn_exit;
}
/* Get dev_id for shared_device_fds from regular dev_id */
int MPL_gpu_get_root_device(int dev_id)
{
return subdevice_map[dev_id];
}
/* Get dev_id for shared_device_fds from regular dev_id */
static int get_physical_device(int dev_id)
{
#ifdef ZE_PCI_PROPERTIES_EXT_NAME
if (device_states[dev_id].sys_device_index != -1)
return device_states[dev_id].sys_device_index;
#endif
return subdevice_map[dev_id];
}
/* Get dev_id from device handle */
MPL_STATIC_INLINE_PREFIX int device_to_dev_id(MPL_gpu_device_handle_t device)
{
int dev_id = -1;
for (int d = 0; d < local_ze_device_count; d++) {
if (ze_devices_handle[d] == device) {
dev_id = d;
break;
}
}
return dev_id;
}
/* Get device from dev_id */
MPL_STATIC_INLINE_PREFIX int dev_id_to_device(int dev_id, MPL_gpu_device_handle_t * device)
{
int mpl_err = MPL_SUCCESS;
if (dev_id > local_ze_device_count) {
goto fn_fail;
}
*device = ze_devices_handle[dev_id];
fn_exit:
return mpl_err;
fn_fail:
mpl_err = MPL_ERR_GPU_INTERNAL;
goto fn_exit;
}
/* Functions for managing shareable IPC handles */
static int fd_to_handle(int dev_fd, int fd, int *handle)
{
#ifdef MPL_ENABLE_DRMFD
struct drm_prime_handle open_fd = { 0, 0, 0 };
open_fd.fd = fd;
int ret = ioctl(dev_fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &open_fd);
assert(ret != -1);
*handle = open_fd.handle;
return ret;
#else
return -1;
#endif
}
static int handle_to_fd(int dev_fd, int handle, int *fd)
{
#ifdef MPL_ENABLE_DRMFD
struct drm_prime_handle open_fd = { 0, 0, 0 };
open_fd.flags = DRM_CLOEXEC | DRM_RDWR;
open_fd.handle = handle;
int ret = ioctl(dev_fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &open_fd);
if (ret == -1)
perror("handle_to_fd");
assert(ret != -1);
*fd = open_fd.fd;
return ret;
#else
return -1;
#endif
}
static int close_handle(int dev_fd, int handle)
{
#ifdef MPL_ENABLE_DRMFD
struct drm_gem_close close = { 0, 0 };
close.handle = handle;
int ret = ioctl(dev_fd, DRM_IOCTL_GEM_CLOSE, &close);
assert(ret != -1);
return ret;
#else
return -1;
#endif
}
/* implicit scaling */
static inline void split_size(size_t size, size_t sizes[2])
{
/* calculate sizes */
const size_t alignment = 64 * 1024;;
size_t mask = alignment - 1;
size = (size + mask) & ~mask;
size_t n = size >> 16;
if (size < alignment) {
sizes[0] = size;
sizes[1] = 0;
} else if (n % 2) {
sizes[0] = (n + 1) / 2 * alignment;
sizes[1] = size - sizes[0];
} else {
sizes[0] = n / 2 * alignment;
sizes[1] = size - sizes[0];
}
assert(sizes[0]);
}
/* map two allocations into one contiguous buffer */
static int mmapFunction(int nfds, int *fds, size_t size, void **ptr)
{
int mpl_err = MPL_SUCCESS;
size_t split_sizes[2];
if (nfds == 1) {
*ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fds[0], 0);
if (*ptr == (void *) -1) {
mpl_err = MPL_ERR_GPU_INTERNAL;
perror("mmap device to host");
printf("mmap failed fd: %d size: %ld\n", fds[0], size);
goto fn_fail;
}
} else {
split_size(size, split_sizes);
void *buf = mmap(0, size, PROT_NONE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (buf == (void *) -1) {
mpl_err = MPL_ERR_GPU_INTERNAL;
perror("mmap");
printf("mmapFunction failed when reserving whole size\n");
goto fn_fail;
}
void *p =
mmap(buf, split_sizes[0], PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fds[0], 0);
if (p != buf) {
mpl_err = MPL_ERR_GPU_INTERNAL;
perror("mmap 1st tile");
printf("mmapFunction failed when mapping first tile \n");
goto fn_fail;
}
if (split_sizes[1]) {
char *p2 = (char *) buf + split_sizes[0];
p = mmap(p2, split_sizes[1], PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fds[1], 0);
if (p != (void *) p2) {
mpl_err = MPL_ERR_GPU_INTERNAL;
perror("mmap 2nd tile");
printf("mmapFunction failed when mapping second tile \n");
goto fn_fail;
}
}
*ptr = buf;
}
fn_exit:
return mpl_err;
fn_fail:
*ptr = NULL;
mpl_err = MPL_ERR_GPU_INTERNAL;
goto fn_exit;
}
/* munmap an implicit scaling buffer */
static int munmapFunction(int nfds, int *fds, void *ptr, size_t size)
{
int mpl_err = MPL_SUCCESS;
size_t split_sizes[2];
int ret;
if (nfds == 1) {
ret = munmap(ptr, size);
if (ret != 0) {
goto fn_fail;
}
close(fds[0]);
} else {
split_size(size, split_sizes);
void *ptr1 = ptr;
ret = munmap(ptr1, split_sizes[0]);
if (ret != 0) {
goto fn_fail;
}
close(fds[0]);
void *ptr2 = (char *) ptr + split_sizes[0];
ret = munmap(ptr2, split_sizes[1]);
if (ret != 0) {
goto fn_fail;
}
close(fds[1]);
}
fn_exit:
return mpl_err;
fn_fail:
mpl_err = MPL_ERR_GPU_INTERNAL;
goto fn_exit;
}
/*
cache utility functions for MPL_ze_ipc_handle_entry_t:
used to cache local device pointer's ipc handle and mmap'ed pointer.
mapped pointers can be from a remote IPC handle
*/
static inline void free_ipc_handle_cache(MPL_ze_ipc_handle_entry_t * cache_entry)
{
if (cache_entry->mapped_ptr) {
munmapFunction(cache_entry->nfds, cache_entry->fds, cache_entry->mapped_ptr,
cache_entry->mapped_size);
}
}
static inline int new_ipc_handle_cache(MPL_ze_ipc_handle_entry_t ** entry, int mem_id)
{
int mpl_err = MPL_SUCCESS;
MPL_ze_ipc_handle_entry_t *cache_entry;
cache_entry =
(MPL_ze_ipc_handle_entry_t *) MPL_malloc(sizeof(MPL_ze_ipc_handle_entry_t), MPL_MEM_OTHER);
if (cache_entry == NULL) {
mpl_err = MPL_ERR_GPU_NOMEM;
goto fn_fail;
}
memset(cache_entry, 0, sizeof(MPL_ze_ipc_handle_entry_t));
cache_entry->mem_id = mem_id;
cache_entry->handle_cached = false;
*entry = cache_entry;
fn_exit:
return mpl_err;
fn_fail:
*entry = NULL;
goto fn_exit;
}
/* Loads a ze driver */
static int gpu_ze_init_driver(void)
{
uint32_t driver_count = 0;
ze_result_t ret;
int ret_error = MPL_SUCCESS;
ze_driver_handle_t *all_drivers = NULL;
ze_init_flag_t flags = ZE_INIT_FLAG_GPU_ONLY;
ret = zeInit(flags);
ZE_ERR_CHECK(ret);
ret = zeDriverGet(&driver_count, NULL);
ZE_ERR_CHECK(ret);
if (driver_count == 0) {
if (MPL_gpu_info.debug_summary) {
printf("No Intel GPU library driver found.\n");
}
goto fn_fail;
}
all_drivers = MPL_malloc(driver_count * sizeof(ze_driver_handle_t), MPL_MEM_OTHER);
if (all_drivers == NULL) {
ret_error = MPL_ERR_GPU_NOMEM;
goto fn_fail;
}
ret = zeDriverGet(&driver_count, all_drivers);
ZE_ERR_CHECK(ret);
int i, d;
/* Find a driver instance with a GPU device */
for (i = 0; i < driver_count; ++i) {
device_count = 0;
ret = zeDeviceGet(all_drivers[i], &device_count, NULL);
ZE_ERR_CHECK(ret);
ze_devices_handle = MPL_malloc(device_count * sizeof(ze_device_handle_t), MPL_MEM_OTHER);
if (ze_devices_handle == NULL) {
ret_error = MPL_ERR_GPU_NOMEM;
goto fn_fail;
}
ret = zeDeviceGet(all_drivers[i], &device_count, ze_devices_handle);
ZE_ERR_CHECK(ret);
/* Check if the driver supports a gpu */
for (d = 0; d < device_count; ++d) {
ze_device_properties_t device_properties;
device_properties.stype = ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES;
device_properties.pNext = NULL;
ret = zeDeviceGetProperties(ze_devices_handle[d], &device_properties);
ZE_ERR_CHECK(ret);
if (ZE_DEVICE_TYPE_GPU == device_properties.type) {
ze_driver_handle = all_drivers[i];
break;
}
}
if (NULL != ze_driver_handle) {
break;
} else {
MPL_free(ze_devices_handle);
ze_devices_handle = NULL;
}
}
/* Setup subdevices */
local_ze_device_count = device_count;
if (ze_devices_handle != NULL) {
/* Count the subdevices */
subdevice_count = MPL_malloc(device_count * sizeof(uint32_t), MPL_MEM_OTHER);
if (subdevice_count == NULL) {
ret_error = MPL_ERR_GPU_NOMEM;
goto fn_fail;
}
for (d = 0; d < device_count; ++d) {
subdevice_count[d] = 0;
ret = zeDeviceGetSubDevices(ze_devices_handle[d], &subdevice_count[d], NULL);
ZE_ERR_CHECK(ret);
local_ze_device_count += subdevice_count[d];
}
subdevice_map = MPL_malloc(local_ze_device_count * sizeof(int), MPL_MEM_OTHER);
if (subdevice_map == NULL) {
ret_error = MPL_ERR_GPU_NOMEM;
goto fn_fail;
}
/* Add the subdevices to the device array */
ze_devices_handle =
MPL_realloc(ze_devices_handle,
local_ze_device_count * sizeof(ze_device_handle_t), MPL_MEM_OTHER);
if (ze_devices_handle == NULL) {
ret_error = MPL_ERR_GPU_NOMEM;
goto fn_fail;
}
int dev_id = device_count;
for (d = 0; d < device_count; ++d) {
ret =
zeDeviceGetSubDevices(ze_devices_handle[d], &subdevice_count[d],
&ze_devices_handle[dev_id]);
ZE_ERR_CHECK(ret);
/* Setup the subdevice map for shared_device_fds */
subdevice_map[d] = d;
for (i = 0; i < subdevice_count[d]; ++i) {
subdevice_map[dev_id + i] = d;
}
dev_id += subdevice_count[d];
}
} else {
if (MPL_gpu_info.debug_summary) {
printf("No Intel GPU device found.\n");
}
}
ze_context_desc_t contextDesc = {
.stype = ZE_STRUCTURE_TYPE_CONTEXT_DESC,
.pNext = NULL,
.flags = 0,
};
ret = zeContextCreate(ze_driver_handle, &contextDesc, &ze_context);
ZE_ERR_CHECK(ret);
device_states =
(MPL_ze_device_entry_t *) MPL_malloc(sizeof(MPL_ze_device_entry_t) * local_ze_device_count,
MPL_MEM_OTHER);
for (d = 0; d < local_ze_device_count; d++) {
unsigned int numQueueGroups = 0;
MPL_ze_device_entry_t *device_state = device_states + d;
device_state->dev_id = d;
memset(device_state->prev_event, 0,
MPL_GPU_COPY_DIRECTION_TYPES * sizeof(ze_event_handle_t));
memset(device_state->last_cmdList_entry, 0,
MPL_GPU_COPY_DIRECTION_TYPES * sizeof(MPL_cmdlist_pool_t *));
ret = zeDeviceGetCommandQueueGroupProperties(ze_devices_handle[d], &numQueueGroups, NULL);
ZE_ERR_CHECK(ret);
ze_command_queue_group_properties_t *queueProperties =
(ze_command_queue_group_properties_t *)
MPL_malloc(sizeof(ze_command_queue_group_properties_t) * numQueueGroups, MPL_MEM_OTHER);
ret =
zeDeviceGetCommandQueueGroupProperties(ze_devices_handle[d], &numQueueGroups,
queueProperties);
device_state->engines =
(MPL_ze_engine_entry_t *) MPL_malloc(sizeof(MPL_ze_engine_entry_t) * numQueueGroups,
MPL_MEM_OTHER);
device_state->numQueueGroups = numQueueGroups;