@@ -58,6 +58,9 @@ typedef struct umf_level_zero_memory_provider_params_t {
58
58
umf_level_zero_memory_provider_free_policy_t
59
59
freePolicy ; ///< Memory free policy
60
60
61
+ umf_level_zero_memory_provider_memory_exchange_policy_t
62
+ exchangePolicy ; ///< Memory exchange policy
63
+
61
64
uint32_t device_ordinal ;
62
65
char name [64 ];
63
66
} umf_level_zero_memory_provider_params_t ;
@@ -74,6 +77,8 @@ typedef struct ze_memory_provider_t {
74
77
75
78
ze_driver_memory_free_policy_ext_flags_t freePolicyFlags ;
76
79
80
+ umf_level_zero_memory_provider_memory_exchange_policy_t exchangePolicy ;
81
+
77
82
size_t min_page_size ;
78
83
79
84
uint32_t device_ordinal ;
@@ -253,6 +258,8 @@ umf_result_t umfLevelZeroMemoryProviderParamsCreate(
253
258
params -> resident_device_handles = NULL ;
254
259
params -> resident_device_count = 0 ;
255
260
params -> freePolicy = UMF_LEVEL_ZERO_MEMORY_PROVIDER_FREE_POLICY_DEFAULT ;
261
+ params -> exchangePolicy =
262
+ UMF_LEVEL_ZERO_MEMORY_PROVIDER_MEMORY_EXCHANGE_POLICY_IPC ;
256
263
params -> device_ordinal = 0 ;
257
264
strncpy (params -> name , DEFAULT_NAME , sizeof (params -> name ) - 1 );
258
265
params -> name [sizeof (params -> name ) - 1 ] = '\0' ;
@@ -374,6 +381,18 @@ umf_result_t umfLevelZeroMemoryProviderParamsSetFreePolicy(
374
381
return UMF_RESULT_SUCCESS ;
375
382
}
376
383
384
+ umf_result_t umfLevelZeroMemoryProviderParamsSetMemoryExchangePolicy (
385
+ umf_level_zero_memory_provider_params_handle_t hParams ,
386
+ umf_level_zero_memory_provider_memory_exchange_policy_t policy ) {
387
+ if (!hParams ) {
388
+ LOG_ERR ("Level Zero memory provider params handle is NULL" );
389
+ return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
390
+ }
391
+
392
+ hParams -> exchangePolicy = policy ;
393
+ return UMF_RESULT_SUCCESS ;
394
+ }
395
+
377
396
static ze_driver_memory_free_policy_ext_flags_t
378
397
umfFreePolicyToZePolicy (umf_level_zero_memory_provider_free_policy_t policy ) {
379
398
switch (policy ) {
@@ -401,6 +420,11 @@ static ze_relaxed_allocation_limits_exp_desc_t relaxed_device_allocation_desc =
401
420
.pNext = NULL ,
402
421
.flags = ZE_RELAXED_ALLOCATION_LIMITS_EXP_FLAG_MAX_SIZE };
403
422
423
+ static ze_external_memory_export_desc_t export_desc = {
424
+ .stype = ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_EXPORT_DESC ,
425
+ .pNext = NULL ,
426
+ .flags = ZE_EXTERNAL_MEMORY_TYPE_FLAG_OPAQUE_WIN32 };
427
+
404
428
static umf_result_t ze_memory_provider_alloc_helper (void * provider , size_t size ,
405
429
size_t alignment ,
406
430
int update_stats ,
@@ -421,11 +445,28 @@ static umf_result_t ze_memory_provider_alloc_helper(void *provider, size_t size,
421
445
case UMF_MEMORY_TYPE_DEVICE : {
422
446
ze_device_mem_alloc_desc_t dev_desc = {
423
447
.stype = ZE_STRUCTURE_TYPE_DEVICE_MEM_ALLOC_DESC ,
424
- .pNext = use_relaxed_allocation (ze_provider , size )
425
- ? & relaxed_device_allocation_desc
426
- : NULL ,
448
+ .pNext = NULL ,
427
449
.flags = 0 ,
428
450
.ordinal = ze_provider -> device_ordinal };
451
+ void * lastNext = & dev_desc .pNext ;
452
+
453
+ ze_relaxed_allocation_limits_exp_desc_t
454
+ relaxed_device_allocation_desc_copy =
455
+ relaxed_device_allocation_desc ;
456
+ if (use_relaxed_allocation (ze_provider , size )) {
457
+ * (uintptr_t * )lastNext =
458
+ (uintptr_t )& relaxed_device_allocation_desc_copy ;
459
+ lastNext = & relaxed_device_allocation_desc_copy .pNext ;
460
+ }
461
+
462
+ // TODO - win only? other mem types?
463
+ ze_external_memory_export_desc_t export_desc_copy = export_desc ;
464
+ if (ze_provider -> exchangePolicy ==
465
+ UMF_LEVEL_ZERO_MEMORY_PROVIDER_MEMORY_EXCHANGE_POLICY_IMPORT_EXPORT ) {
466
+ * (uintptr_t * )lastNext = (uintptr_t )& export_desc_copy ;
467
+ lastNext = & export_desc_copy .pNext ;
468
+ }
469
+
429
470
ze_result = g_ze_ops .zeMemAllocDevice (ze_provider -> context , & dev_desc ,
430
471
size , alignment ,
431
472
ze_provider -> device , resultPtr );
@@ -599,6 +640,7 @@ static umf_result_t ze_memory_provider_initialize(const void *params,
599
640
ze_provider -> memory_type = umf2ze_memory_type (ze_params -> memory_type );
600
641
ze_provider -> freePolicyFlags =
601
642
umfFreePolicyToZePolicy (ze_params -> freePolicy );
643
+ ze_provider -> exchangePolicy = ze_params -> exchangePolicy ;
602
644
ze_provider -> min_page_size = 0 ;
603
645
ze_provider -> device_ordinal = ze_params -> device_ordinal ;
604
646
@@ -755,6 +797,7 @@ static umf_result_t ze_memory_provider_allocation_split(void *provider,
755
797
756
798
typedef struct ze_ipc_data_t {
757
799
int pid ;
800
+ size_t size ;
758
801
ze_ipc_mem_handle_t ze_handle ;
759
802
} ze_ipc_data_t ;
760
803
@@ -770,20 +813,45 @@ static umf_result_t ze_memory_provider_get_ipc_handle(void *provider,
770
813
const void * ptr ,
771
814
size_t size ,
772
815
void * providerIpcData ) {
773
- (void )size ;
774
-
775
816
ze_result_t ze_result ;
776
817
ze_ipc_data_t * ze_ipc_data = (ze_ipc_data_t * )providerIpcData ;
777
818
struct ze_memory_provider_t * ze_provider =
778
819
(struct ze_memory_provider_t * )provider ;
779
820
780
- ze_result = g_ze_ops .zeMemGetIpcHandle (ze_provider -> context , ptr ,
781
- & ze_ipc_data -> ze_handle );
782
- if (ze_result != ZE_RESULT_SUCCESS ) {
783
- LOG_ERR ("zeMemGetIpcHandle() failed." );
784
- return ze2umf_result (ze_result );
821
+ if (ze_provider -> exchangePolicy ==
822
+ UMF_LEVEL_ZERO_MEMORY_PROVIDER_MEMORY_EXCHANGE_POLICY_IPC ) {
823
+ ze_result = g_ze_ops .zeMemGetIpcHandle (ze_provider -> context , ptr ,
824
+ & ze_ipc_data -> ze_handle );
825
+
826
+ if (ze_result != ZE_RESULT_SUCCESS ) {
827
+ LOG_ERR ("zeMemGetIpcHandle() failed." );
828
+ return ze2umf_result (ze_result );
829
+ }
830
+ } else { // UMF_LEVEL_ZERO_MEMORY_PROVIDER_MEMORY_EXCHANGE_POLICY_IMPORT_EXPORT
831
+ ze_external_memory_export_fd_t fd_desc = {
832
+ .stype = ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_EXPORT_FD ,
833
+ .pNext = NULL ,
834
+ .flags = ZE_EXTERNAL_MEMORY_TYPE_FLAG_OPAQUE_WIN32 ,
835
+ .fd = 0 };
836
+
837
+ ze_memory_allocation_properties_t mem_alloc_props = {
838
+ .stype = ZE_STRUCTURE_TYPE_MEMORY_ALLOCATION_PROPERTIES ,
839
+ .pNext = & fd_desc ,
840
+ .type = 0 ,
841
+ .id = 0 ,
842
+ .pageSize = 0 };
843
+
844
+ ze_result = g_ze_ops .zeMemGetAllocProperties (ze_provider -> context , ptr ,
845
+ & mem_alloc_props , NULL );
846
+ if (ze_result != ZE_RESULT_SUCCESS ) {
847
+ LOG_ERR ("zeMemGetAllocProperties() failed." );
848
+ return ze2umf_result (ze_result );
849
+ }
850
+
851
+ memcpy (& ze_ipc_data -> ze_handle , & fd_desc .fd , sizeof (fd_desc .fd ));
785
852
}
786
853
854
+ ze_ipc_data -> size = size ;
787
855
ze_ipc_data -> pid = utils_getpid ();
788
856
789
857
return UMF_RESULT_SUCCESS ;
@@ -834,14 +902,40 @@ static umf_result_t ze_memory_provider_open_ipc_handle(void *provider,
834
902
memcpy (& ze_ipc_handle , & fd_local , sizeof (fd_local ));
835
903
}
836
904
837
- ze_result = g_ze_ops .zeMemOpenIpcHandle (
838
- ze_provider -> context , ze_provider -> device , ze_ipc_handle , 0 , ptr );
839
- if (fd_local != -1 ) {
840
- (void )utils_close_fd (fd_local );
841
- }
842
- if (ze_result != ZE_RESULT_SUCCESS ) {
843
- LOG_ERR ("zeMemOpenIpcHandle() failed." );
844
- return ze2umf_result (ze_result );
905
+ if (ze_provider -> exchangePolicy ==
906
+ UMF_LEVEL_ZERO_MEMORY_PROVIDER_MEMORY_EXCHANGE_POLICY_IPC ) {
907
+ ze_result = g_ze_ops .zeMemOpenIpcHandle (
908
+ ze_provider -> context , ze_provider -> device , ze_ipc_handle , 0 , ptr );
909
+ if (fd_local != -1 ) {
910
+ (void )utils_close_fd (fd_local );
911
+ }
912
+ if (ze_result != ZE_RESULT_SUCCESS ) {
913
+ LOG_ERR ("zeMemOpenIpcHandle() failed." );
914
+ return ze2umf_result (ze_result );
915
+ }
916
+ } else { // UMF_LEVEL_ZERO_MEMORY_PROVIDER_MEMORY_EXCHANGE_POLICY_IMPORT_EXPORT
917
+ ze_external_memory_import_fd_t import_fd = {
918
+ .stype = ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMPORT_FD ,
919
+ .pNext = NULL ,
920
+ .flags = ZE_EXTERNAL_MEMORY_TYPE_FLAG_DMA_BUF ,
921
+ .fd = fd_local };
922
+
923
+ ze_device_mem_alloc_desc_t alloc_desc = {
924
+ .stype = ZE_STRUCTURE_TYPE_DEVICE_MEM_ALLOC_DESC ,
925
+ .pNext = & import_fd ,
926
+ .flags = 0 ,
927
+ .ordinal = 0 };
928
+ ze_result = g_ze_ops .zeMemAllocDevice (ze_provider -> context , & alloc_desc ,
929
+ ze_ipc_data -> size , 0 ,
930
+ ze_provider -> device , & ptr );
931
+ if (ze_result != ZE_RESULT_SUCCESS ) {
932
+ LOG_ERR ("zeMemAllocDevice() failed." );
933
+ return ze2umf_result (ze_result );
934
+ }
935
+
936
+ if (fd_local != -1 ) {
937
+ (void )utils_close_fd (fd_local );
938
+ }
845
939
}
846
940
847
941
return UMF_RESULT_SUCCESS ;
@@ -1027,6 +1121,14 @@ umf_result_t umfLevelZeroMemoryProviderParamsSetFreePolicy(
1027
1121
return UMF_RESULT_ERROR_NOT_SUPPORTED ;
1028
1122
}
1029
1123
1124
+ umf_result_t umfLevelZeroMemoryProviderParamsSetMemoryExchangePolicy (
1125
+ umf_level_zero_memory_provider_params_handle_t hParams ,
1126
+ umf_level_zero_memory_provider_memory_exchange_policy_t policy ) {
1127
+ (void )hParams ;
1128
+ (void )policy ;
1129
+ return UMF_RESULT_ERROR_NOT_SUPPORTED ;
1130
+ }
1131
+
1030
1132
umf_result_t umfLevelZeroMemoryProviderParamsSetDeviceOrdinal (
1031
1133
umf_level_zero_memory_provider_params_handle_t hParams ,
1032
1134
uint32_t deviceOrdinal ) {
0 commit comments