Skip to content

Commit 02a3c1b

Browse files
charlesstollsunkuamzn
authored andcommitted
hmem: Add dmabuf support detection infrastructure
feat: Add dmabuf capability tracking for HMEM interfaces Problem: - Need infrastructure to detect and query dmabuf support per HMEM interface - Providers need to know if dmabuf is available before attempting registration Solution: - Added ofi_hmem_is_dmabuf_supported() query function in include/ofi_hmem.h - Implemented dmabuf support detection in src/hmem_cuda.c - Implemented dmabuf support detection in src/hmem_neuron.c - Added stub implementations in src/hmem_rocr.c and src/hmem_synapseai.c - Updated src/hmem.c to expose the new interface - Made use_dmabuf environment variable for ROCr true by default to match behavior of others Co-authored-by: Nick Mazzilli <nmazzill@amazon.com> Signed-off-by: Charles Stoll <stollcha@amazon.com>
1 parent 9f1402f commit 02a3c1b

File tree

7 files changed

+111
-9
lines changed

7 files changed

+111
-9
lines changed

include/ofi_hmem.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ int rocr_hmem_get_dmabuf_fd(const void *addr, uint64_t size, int *dmabuf_fd,
171171
int rocr_hmem_put_dmabuf_fd(int fd);
172172
void *rocr_alloc(size_t size);
173173
void rocr_free(void *ptr);
174+
bool rocr_is_dmabuf_requested(void);
174175

175176
int cuda_copy_to_dev(uint64_t device, void *dev, const void *host, size_t size);
176177
int cuda_copy_from_dev(uint64_t device, void *host, const void *dev, size_t size);
@@ -195,6 +196,7 @@ bool cuda_is_ipc_enabled(void);
195196
int cuda_get_ipc_handle_size(size_t *size);
196197
bool cuda_is_gdrcopy_enabled(void);
197198
bool cuda_is_dmabuf_supported(void);
199+
bool cuda_is_dmabuf_requested(void);
198200
int cuda_get_dmabuf_fd(const void *addr, uint64_t size, int *fd,
199201
uint64_t *offset);
200202
int cuda_put_dmabuf_fd(int fd);
@@ -247,6 +249,7 @@ int ze_dev_reg_copy_from_hmem(uint64_t handle, void *dest, const void *src,
247249
size_t size);
248250
int ze_hmem_get_dmabuf_fd(const void *addr, uint64_t size, int *fd,
249251
uint64_t *offset);
252+
bool ze_is_dmabuf_requested(void);
250253

251254
int neuron_copy_to_dev(uint64_t device, void *dev, const void *host, size_t size);
252255
int neuron_copy_from_dev(uint64_t device, void *host, const void *dev, size_t size);
@@ -259,6 +262,7 @@ void neuron_free(void **handle);
259262
int neuron_get_dmabuf_fd(const void *addr, uint64_t size, int *fd,
260263
uint64_t *offset);
261264
int neuron_put_dmabuf_fd(int fd);
265+
bool neuron_is_dmabuf_requested(void);
262266

263267
int synapseai_init(void);
264268
int synapseai_cleanup(void);
@@ -272,6 +276,7 @@ bool synapseai_is_addr_valid(const void *addr, uint64_t *device,
272276
uint64_t *flags);
273277
int synapseai_host_register(void *ptr, size_t size);
274278
int synapseai_host_unregister(void *ptr);
279+
bool synapseai_is_dmabuf_requested(void);
275280

276281
static inline int ofi_memcpy(uint64_t device, void *dest, const void *src,
277282
size_t size)
@@ -462,5 +467,5 @@ int ofi_hmem_dev_reg_copy_from_hmem(enum fi_hmem_iface iface, uint64_t handle,
462467
int ofi_hmem_get_dmabuf_fd(enum fi_hmem_iface, const void *addr, uint64_t size,
463468
int *fd, uint64_t *offset);
464469
int ofi_hmem_put_dmabuf_fd(enum fi_hmem_iface iface, int fd);
465-
470+
bool ofi_hmem_is_dmabuf_env_var_enabled(enum fi_hmem_iface iface);
466471
#endif /* _OFI_HMEM_H_ */

src/hmem.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,3 +831,32 @@ int ofi_hmem_put_dmabuf_fd(enum fi_hmem_iface iface, int fd)
831831
{
832832
return hmem_ops[iface].put_dmabuf_fd(fd);
833833
}
834+
835+
/**
836+
* @brief Check if DMABUF is enabled for a specific HMEM interface
837+
*
838+
* This function checks the environment variables to determine if DMABUF
839+
* should be used for the specified HMEM interface.
840+
*
841+
* @param[in] iface The HMEM interface to check
842+
* @return true if DMABUF is enabled for the interface, false otherwise
843+
*/
844+
bool ofi_hmem_is_dmabuf_env_var_enabled(enum fi_hmem_iface iface)
845+
{
846+
switch (iface) {
847+
case FI_HMEM_SYSTEM:
848+
return false;
849+
case FI_HMEM_CUDA:
850+
return cuda_is_dmabuf_requested();
851+
case FI_HMEM_NEURON:
852+
return neuron_is_dmabuf_requested();
853+
case FI_HMEM_SYNAPSEAI:
854+
return synapseai_is_dmabuf_requested();
855+
case FI_HMEM_ROCR:
856+
return rocr_is_dmabuf_requested();
857+
case FI_HMEM_ZE:
858+
return ze_is_dmabuf_requested();
859+
default:
860+
return false;
861+
}
862+
}

src/hmem_cuda.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ int cuda_hmem_init(void)
786786
"this variable is not checked. (default: true)");
787787

788788
fi_param_define(NULL, "hmem_cuda_use_dmabuf", FI_PARAM_BOOL,
789-
"Use dma-buf for sharing buffer with hardware. (default:true)");
789+
"Use dma-buf for sharing buffer with hardware. (default: true)");
790790

791791
ret = cuda_hmem_dl_init();
792792
if (ret != FI_SUCCESS)
@@ -959,13 +959,17 @@ bool cuda_is_gdrcopy_enabled(void)
959959
return cuda_attr.use_gdrcopy;
960960
}
961961

962-
bool cuda_is_dmabuf_supported(void)
962+
bool cuda_is_dmabuf_requested(void)
963963
{
964964
int use_dmabuf = 1;
965965

966966
fi_param_get_bool(NULL, "hmem_cuda_use_dmabuf", &use_dmabuf);
967+
return use_dmabuf;
968+
}
967969

968-
return use_dmabuf && cuda_attr.dmabuf_supported;
970+
bool cuda_is_dmabuf_supported(void)
971+
{
972+
return cuda_is_dmabuf_requested() && cuda_attr.dmabuf_supported;
969973
}
970974

971975
#else
@@ -1068,6 +1072,11 @@ bool cuda_is_dmabuf_supported(void)
10681072
return false;
10691073
}
10701074

1075+
bool cuda_is_dmabuf_requested(void)
1076+
{
1077+
return false;
1078+
}
1079+
10711080
int cuda_get_dmabuf_fd(const void *addr, uint64_t size, int *fd,
10721081
uint64_t *offset)
10731082
{

src/hmem_neuron.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ int neuron_hmem_init(void)
152152
int ret;
153153
uint32_t total_nc_count;
154154

155+
fi_param_define(NULL, "hmem_neuron_use_dmabuf", FI_PARAM_BOOL,
156+
"Use DMABUF for Neuron device memory registration. (Default: true)");
157+
155158
ret = neuron_dl_init();
156159
if (ret)
157160
return ret;
@@ -255,6 +258,14 @@ int neuron_put_dmabuf_fd(int fd)
255258
return FI_SUCCESS;
256259
}
257260

261+
bool neuron_is_dmabuf_requested(void)
262+
{
263+
int use_dmabuf = 1;
264+
265+
fi_param_get_bool(NULL, "hmem_neuron_use_dmabuf", &use_dmabuf);
266+
return use_dmabuf;
267+
}
268+
258269
#else
259270

260271
int neuron_copy_to_dev(uint64_t device, void *dev, const void *host, size_t size)
@@ -308,4 +319,9 @@ int neuron_put_dmabuf_fd(int fd)
308319
return -FI_ENOSYS;
309320
}
310321

322+
bool neuron_is_dmabuf_requested(void)
323+
{
324+
return false;
325+
}
326+
311327
#endif /* HAVE_NEURON */

src/hmem_rocr.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,8 +1081,8 @@ int rocr_hmem_init(void)
10811081
"Threshold for switching to hsa memcpy for device-to-host"
10821082
" copies. (Default 16384");
10831083

1084-
fi_param_define(NULL, "hmem_rocr_use_dmabuf", FI_PARAM_INT,
1085-
"Use dma-buf for sharing buffer with hardware. (default:0)");
1084+
fi_param_define(NULL, "hmem_rocr_use_dmabuf", FI_PARAM_BOOL,
1085+
"Use dma-buf for sharing buffer with hardware. (default: true)");
10861086

10871087
ret = rocr_hmem_dl_init();
10881088
if (ret != FI_SUCCESS)
@@ -1257,14 +1257,20 @@ int rocr_dev_reg_copy_from_hmem(uint64_t handle, void *dest, const void *src,
12571257
return FI_SUCCESS;
12581258
}
12591259

1260+
bool rocr_is_dmabuf_requested(void)
1261+
{
1262+
int use_dmabuf = 1;
1263+
1264+
fi_param_get_int(NULL, "hmem_rocr_use_dmabuf", &use_dmabuf);
1265+
return use_dmabuf;
1266+
}
1267+
12601268
static bool rocr_is_dmabuf_supported(void)
12611269
{
12621270
hsa_status_t hsa_ret;
1263-
int use_dmabuf = 0;
12641271
bool dmabuf_support = false, dmabuf_kernel = false;
12651272

1266-
fi_param_get_int(NULL, "hmem_rocr_use_dmabuf", &use_dmabuf);
1267-
if (!use_dmabuf)
1273+
if (!rocr_is_dmabuf_requested())
12681274
goto out;
12691275

12701276
/* HSA_AMD_SYSTEM_INFO_DMABUF_SUPPORTED = 0x204, we use the number
@@ -1511,4 +1517,9 @@ void rocr_free(void *ptr)
15111517
return;
15121518
}
15131519

1520+
bool rocr_is_dmabuf_requested(void)
1521+
{
1522+
return false;
1523+
}
1524+
15141525
#endif /* HAVE_ROCR */

src/hmem_synapseai.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ int synapseai_init(void)
106106
synStatus status;
107107
uint32_t device_count = 0;
108108

109+
fi_param_define(NULL, "hmem_synapseai_use_dmabuf", FI_PARAM_BOOL,
110+
"Use DMABUF for SynapseAI device memory registration. (Default: true)");
111+
109112
err = synapseai_dl_init();
110113
if (err)
111114
return err;
@@ -206,6 +209,14 @@ int synapseai_get_dmabuf_fd(const void *addr, uint64_t size, int *fd,
206209
return FI_SUCCESS;
207210
}
208211

212+
bool synapseai_is_dmabuf_requested(void)
213+
{
214+
int use_dmabuf = 1;
215+
216+
fi_param_get_bool(NULL, "hmem_synapseai_use_dmabuf", &use_dmabuf);
217+
return use_dmabuf;
218+
}
219+
209220
#else
210221
int synapseai_init(void)
211222
{
@@ -250,4 +261,9 @@ int synapseai_get_dmabuf_fd(const void *addr, uint64_t size, int *fd,
250261
{
251262
return -FI_ENOSYS;
252263
}
264+
265+
bool synapseai_is_dmabuf_requested(void)
266+
{
267+
return false;
268+
}
253269
#endif /* HAVE_SYNAPSEAI */

src/hmem_ze.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,9 @@ int ze_hmem_init(void)
681681
if (enginestr)
682682
sscanf(enginestr, "%d.%d", &ordinal, &index);
683683

684+
fi_param_define(NULL, "hmem_ze_use_dmabuf", FI_PARAM_BOOL,
685+
"Use DMABUF for ZE device memory registration. (Default: true)");
686+
684687
ret = ze_hmem_dl_init();
685688
if (ret)
686689
return ret;
@@ -1088,6 +1091,14 @@ int ze_hmem_get_dmabuf_fd(const void *addr, uint64_t size, int *fd,
10881091
return 0;
10891092
}
10901093

1094+
bool ze_is_dmabuf_requested(void)
1095+
{
1096+
int use_dmabuf = 1;
1097+
1098+
fi_param_get_int(NULL, "hmem_ze_use_dmabuf", &use_dmabuf);
1099+
return use_dmabuf;
1100+
}
1101+
10911102
struct ze_dev_reg_handle {
10921103
void *base_dev;
10931104
void *base_host;
@@ -1349,4 +1360,9 @@ int ze_hmem_get_dmabuf_fd(const void *addr, uint64_t size, int *fd,
13491360
return -FI_ENOSYS;
13501361
}
13511362

1363+
bool ze_is_dmabuf_requested(void)
1364+
{
1365+
return false;
1366+
}
1367+
13521368
#endif /* HAVE_ZE */

0 commit comments

Comments
 (0)