forked from flagos-ai/FlagCX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcuda_adaptor.cc
More file actions
1007 lines (904 loc) · 33.2 KB
/
Copy pathcuda_adaptor.cc
File metadata and controls
1007 lines (904 loc) · 33.2 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
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
#include "nvidia_adaptor.h"
#ifdef USE_NVIDIA_ADAPTOR
#include "adaptor.h"
#include "alloc.h"
#include "param.h"
#include <mutex>
#include <unistd.h>
#include <unordered_map>
static std::mutex gVmmHandleMapMtx;
static std::unordered_map<void *, CUmemGenericAllocationHandle> gVmmHandleMap;
std::map<flagcxMemcpyType_t, cudaMemcpyKind> memcpy_type_map = {
{flagcxMemcpyHostToDevice, cudaMemcpyHostToDevice},
{flagcxMemcpyDeviceToHost, cudaMemcpyDeviceToHost},
{flagcxMemcpyDeviceToDevice, cudaMemcpyDeviceToDevice},
};
flagcxResult_t cudaAdaptorDeviceSynchronize() {
DEVCHECK(cudaDeviceSynchronize());
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorDeviceMemcpy(void *dst, void *src, size_t size,
flagcxMemcpyType_t type,
flagcxStream_t stream, void *args) {
if (stream == NULL) {
DEVCHECK(cudaMemcpy(dst, src, size, memcpy_type_map[type]));
} else {
DEVCHECK(
cudaMemcpyAsync(dst, src, size, memcpy_type_map[type], stream->base));
}
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorDeviceMemset(void *ptr, int value, size_t size,
flagcxMemType_t type,
flagcxStream_t stream) {
if (type == flagcxMemHost) {
memset(ptr, value, size);
} else {
if (stream == NULL) {
DEVCHECK(cudaMemset(ptr, value, size));
} else {
DEVCHECK(cudaMemsetAsync(ptr, value, size, stream->base));
}
}
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorDeviceMalloc(void **ptr, size_t size,
flagcxMemType_t type,
flagcxStream_t stream) {
if (type == flagcxMemHost) {
DEVCHECK(cudaHostAlloc(ptr, size, cudaHostAllocMapped));
} else if (type == flagcxMemManaged) {
DEVCHECK(cudaMallocManaged(ptr, size, cudaMemAttachGlobal));
} else {
if (stream == NULL) {
DEVCHECK(cudaMalloc(ptr, size));
} else {
DEVCHECK(cudaMallocAsync(ptr, size, stream->base));
}
}
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorDeviceFree(void *ptr, flagcxMemType_t type,
flagcxStream_t stream) {
if (type == flagcxMemHost) {
DEVCHECK(cudaFreeHost(ptr));
} else if (type == flagcxMemManaged) {
DEVCHECK(cudaFree(ptr));
} else {
if (stream == NULL) {
DEVCHECK(cudaFree(ptr));
} else {
DEVCHECK(cudaFreeAsync(ptr, stream->base));
}
}
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorSetDevice(int dev) {
DEVCHECK(cudaSetDevice(dev));
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorGetDevice(int *dev) {
DEVCHECK(cudaGetDevice(dev));
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorGetDeviceCount(int *count) {
DEVCHECK(cudaGetDeviceCount(count));
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorGetVendor(char *vendor) {
strcpy(vendor, "NVIDIA");
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorHostGetDevicePointer(void **pDevice, void *pHost) {
if (pDevice == NULL || pHost == NULL) {
return flagcxInvalidArgument;
}
DEVCHECK(cudaHostGetDevicePointer(pDevice, pHost, 0));
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorGdrMemAlloc(void **ptr, size_t size,
void *memHandle) {
if (ptr == NULL) {
return flagcxInvalidArgument;
}
#if CUDART_VERSION >= 12010
if (!flagcxParamVmmEnable()) {
DEVCHECK(cudaMalloc(ptr, size));
return flagcxSuccess;
}
size_t memGran = 0;
CUdevice currentDev;
CUmemAllocationProp memprop = {};
CUmemGenericAllocationHandle handle = (CUmemGenericAllocationHandle)-1;
int cudaDev;
int flag;
CUresult cuRes;
DEVCHECK(cudaGetDevice(&cudaDev));
DEVCHECK(cuDeviceGet(¤tDev, cudaDev));
size_t handleSize = size;
int requestedHandleTypes = CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR;
// Query device to see if FABRIC handle support is available
#if CUDART_VERSION >= 12040
flag = 0;
DEVCHECK(cuDeviceGetAttribute(
&flag, CU_DEVICE_ATTRIBUTE_HANDLE_TYPE_FABRIC_SUPPORTED, currentDev));
if (flag)
requestedHandleTypes |= CU_MEM_HANDLE_TYPE_FABRIC;
#endif
memprop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
memprop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
memprop.requestedHandleTypes =
(CUmemAllocationHandleType)requestedHandleTypes;
memprop.location.id = currentDev;
// Query device to see if RDMA support is available
flag = 0;
DEVCHECK(cuDeviceGetAttribute(
&flag, CU_DEVICE_ATTRIBUTE_GPU_DIRECT_RDMA_WITH_CUDA_VMM_SUPPORTED,
currentDev));
INFO(FLAGCX_INIT,
"[gdrMemAlloc] dev=%d GPU_DIRECT_RDMA_WITH_CUDA_VMM_SUPPORTED=%d "
"size=%zu",
cudaDev, flag, size);
if (flag)
memprop.allocFlags.gpuDirectRDMACapable = 1;
DEVCHECK(cuMemGetAllocationGranularity(&memGran, &memprop,
CU_MEM_ALLOC_GRANULARITY_RECOMMENDED));
ALIGN_SIZE(handleSize, memGran);
INFO(FLAGCX_INIT,
"[gdrMemAlloc] memGran=%zu handleSize=%zu gpuDirectRDMACapable=%d",
memGran, handleSize, (int)memprop.allocFlags.gpuDirectRDMACapable);
/* Allocate the physical memory on the device */
DEVCHECK(cuMemCreate(&handle, handleSize, &memprop, 0));
/* Reserve a virtual address range */
cuRes = cuMemAddressReserve((CUdeviceptr *)ptr, handleSize, memGran, 0, 0);
if (cuRes != CUDA_SUCCESS) {
WARN("[gdrMemAlloc] cuMemAddressReserve FAILED: %d", (int)cuRes);
cuMemRelease(handle);
return flagcxUnhandledDeviceError;
}
/* Map the virtual address range to the physical allocation */
cuRes = cuMemMap((CUdeviceptr)*ptr, handleSize, 0, handle, 0);
if (cuRes != CUDA_SUCCESS) {
WARN("[gdrMemAlloc] cuMemMap FAILED: %d", (int)cuRes);
cuMemAddressFree((CUdeviceptr)*ptr, handleSize);
cuMemRelease(handle);
*ptr = NULL;
return flagcxUnhandledDeviceError;
}
/* Set access for the current device */
CUmemAccessDesc accessDesc = {};
accessDesc.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
accessDesc.location.id = currentDev;
accessDesc.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
cuRes = cuMemSetAccess((CUdeviceptr)*ptr, handleSize, &accessDesc, 1);
if (cuRes != CUDA_SUCCESS) {
WARN("[gdrMemAlloc] cuMemSetAccess FAILED: %d", (int)cuRes);
cuMemUnmap((CUdeviceptr)*ptr, handleSize);
cuMemAddressFree((CUdeviceptr)*ptr, handleSize);
cuMemRelease(handle);
*ptr = NULL;
return flagcxUnhandledDeviceError;
}
INFO(FLAGCX_INIT, "[gdrMemAlloc] VMM alloc OK: ptr=%p size=%zu", *ptr,
handleSize);
/* Retain the handle so cuMemGetHandleForAddressRange can export DMA-BUF fds.
Released in cudaAdaptorGdrMemFree. */
{
std::lock_guard<std::mutex> lk(gVmmHandleMapMtx);
gVmmHandleMap[*ptr] = handle;
}
#else
DEVCHECK(cudaMalloc(ptr, size));
cudaPointerAttributes attrs;
DEVCHECK(cudaPointerGetAttributes(&attrs, *ptr));
unsigned flags = 1;
DEVCHECK(cuPointerSetAttribute(&flags, CU_POINTER_ATTRIBUTE_SYNC_MEMOPS,
(CUdeviceptr)attrs.devicePointer));
#endif
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorGdrMemFree(void *ptr, void *memHandle) {
if (ptr == NULL) {
return flagcxSuccess;
}
#if CUDART_VERSION >= 12010
if (!flagcxParamVmmEnable()) {
DEVCHECK(cudaFree(ptr));
return flagcxSuccess;
}
size_t size = 0;
DEVCHECK(cuMemGetAddressRange(NULL, &size, (CUdeviceptr)ptr));
DEVCHECK(cuMemUnmap((CUdeviceptr)ptr, size));
DEVCHECK(cuMemAddressFree((CUdeviceptr)ptr, size));
// Release the VMM handle we retained at alloc time
{
std::lock_guard<std::mutex> lk(gVmmHandleMapMtx);
auto it = gVmmHandleMap.find(ptr);
if (it != gVmmHandleMap.end()) {
cuMemRelease(it->second);
gVmmHandleMap.erase(it);
}
}
#else
DEVCHECK(cudaFree(ptr));
#endif
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorStreamCreate(flagcxStream_t *stream) {
(*stream) = NULL;
flagcxCalloc(stream, 1);
DEVCHECK(cudaStreamCreateWithFlags((cudaStream_t *)(*stream),
cudaStreamNonBlocking));
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorStreamDestroy(flagcxStream_t stream) {
if (stream != NULL) {
DEVCHECK(cudaStreamDestroy(stream->base));
free(stream);
stream = NULL;
}
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorStreamCopy(flagcxStream_t *newStream,
void *oldStream) {
(*newStream) = NULL;
flagcxCalloc(newStream, 1);
(*newStream)->base = (cudaStream_t)oldStream;
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorStreamFree(flagcxStream_t stream) {
if (stream != NULL) {
free(stream);
stream = NULL;
}
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorStreamSynchronize(flagcxStream_t stream) {
if (stream != NULL) {
DEVCHECK(cudaStreamSynchronize(stream->base));
}
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorStreamQuery(flagcxStream_t stream) {
flagcxResult_t res = flagcxSuccess;
if (stream != NULL) {
cudaError error = cudaStreamQuery(stream->base);
if (error == cudaSuccess) {
res = flagcxSuccess;
} else if (error == cudaErrorNotReady) {
res = flagcxInProgress;
} else {
res = flagcxUnhandledDeviceError;
}
}
return res;
}
flagcxResult_t cudaAdaptorStreamWaitEvent(flagcxStream_t stream,
flagcxEvent_t event) {
if (stream != NULL && event != NULL) {
DEVCHECK(
cudaStreamWaitEvent(stream->base, event->base, cudaEventWaitDefault));
}
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorStreamWaitValue64(flagcxStream_t stream, void *addr,
uint64_t value, int flags) {
(void)flags;
if (stream == NULL || addr == NULL)
return flagcxInvalidArgument;
CUstream cuStream = (CUstream)(stream->base);
CUresult err = cuStreamWaitValue64(cuStream, (CUdeviceptr)addr, value,
CU_STREAM_WAIT_VALUE_GEQ);
return (err == CUDA_SUCCESS) ? flagcxSuccess : flagcxUnhandledDeviceError;
}
flagcxResult_t cudaAdaptorStreamWriteValue64(flagcxStream_t stream, void *addr,
uint64_t value, int flags) {
(void)flags;
if (stream == NULL || addr == NULL)
return flagcxInvalidArgument;
CUstream cuStream = (CUstream)(stream->base);
CUresult err = cuStreamWriteValue64(cuStream, (CUdeviceptr)addr, value,
CU_STREAM_WRITE_VALUE_DEFAULT);
return (err == CUDA_SUCCESS) ? flagcxSuccess : flagcxUnhandledDeviceError;
}
flagcxResult_t cudaAdaptorEventCreate(flagcxEvent_t *event,
flagcxEventType_t eventType) {
(*event) = NULL;
flagcxCalloc(event, 1);
const unsigned int flags = (eventType == flagcxEventDefault)
? cudaEventDefault
: cudaEventDisableTiming;
DEVCHECK(cudaEventCreateWithFlags(&((*event)->base), flags));
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorEventDestroy(flagcxEvent_t event) {
if (event != NULL) {
DEVCHECK(cudaEventDestroy(event->base));
free(event);
event = NULL;
}
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorEventRecord(flagcxEvent_t event,
flagcxStream_t stream) {
if (event != NULL) {
if (stream != NULL) {
DEVCHECK(cudaEventRecordWithFlags(event->base, stream->base,
cudaEventRecordDefault));
} else {
DEVCHECK(cudaEventRecordWithFlags(event->base));
}
}
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorEventSynchronize(flagcxEvent_t event) {
if (event != NULL) {
DEVCHECK(cudaEventSynchronize(event->base));
}
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorEventQuery(flagcxEvent_t event) {
flagcxResult_t res = flagcxSuccess;
if (event != NULL) {
cudaError error = cudaEventQuery(event->base);
if (error == cudaSuccess) {
res = flagcxSuccess;
} else if (error == cudaErrorNotReady) {
res = flagcxInProgress;
} else {
res = flagcxUnhandledDeviceError;
}
}
return res;
}
flagcxResult_t cudaAdaptorEventElapsedTime(float *ms, flagcxEvent_t start,
flagcxEvent_t end) {
if (ms == NULL || start == NULL || end == NULL) {
return flagcxInvalidArgument;
}
cudaError_t error = cudaEventElapsedTime(ms, start->base, end->base);
if (error == cudaSuccess) {
return flagcxSuccess;
} else if (error == cudaErrorNotReady) {
return flagcxInProgress;
} else {
return flagcxUnhandledDeviceError;
}
}
flagcxResult_t cudaAdaptorIpcMemHandleCreate(flagcxIpcMemHandle_t *handle,
size_t *size) {
flagcxCalloc(handle, 1);
if (size != NULL) {
*size = sizeof(cudaIpcMemHandle_t);
}
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorIpcMemHandleGet(flagcxIpcMemHandle_t handle,
void *devPtr) {
if (handle == NULL || devPtr == NULL) {
return flagcxInvalidArgument;
}
DEVCHECK(cudaIpcGetMemHandle(&handle->base, devPtr));
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorIpcMemHandleOpen(flagcxIpcMemHandle_t handle,
void **devPtr) {
if (handle == NULL || devPtr == NULL || *devPtr != NULL) {
return flagcxInvalidArgument;
}
DEVCHECK(cudaIpcOpenMemHandle(devPtr, handle->base,
cudaIpcMemLazyEnablePeerAccess));
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorIpcMemHandleClose(void *devPtr) {
if (devPtr == NULL) {
return flagcxInvalidArgument;
}
DEVCHECK(cudaIpcCloseMemHandle(devPtr));
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorIpcMemHandleFree(flagcxIpcMemHandle_t handle) {
if (handle != NULL) {
free(handle);
}
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorLaunchHostFunc(flagcxStream_t stream,
void (*fn)(void *), void *args) {
if (stream != NULL) {
DEVCHECK(cudaLaunchHostFunc(stream->base, fn, args));
}
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorLaunchDeviceFunc(flagcxStream_t stream,
flagcxLaunchFunc_t fn, void *args) {
if (stream != NULL) {
fn(stream, args);
}
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorGetDeviceProperties(struct flagcxDevProps *props,
int dev) {
if (props == NULL) {
return flagcxInvalidArgument;
}
cudaDeviceProp devProp;
DEVCHECK(cudaGetDeviceProperties(&devProp, dev));
strncpy(props->name, devProp.name, sizeof(props->name) - 1);
props->name[sizeof(props->name) - 1] = '\0';
props->pciBusId = devProp.pciBusID;
props->pciDeviceId = devProp.pciDeviceID;
props->pciDomainId = devProp.pciDomainID;
// TODO: see if there's another way to get this info. In some cuda versions,
// cudaDeviceProp does not have `gpuDirectRDMASupported` field
// props->gdrSupported = devProp.gpuDirectRDMASupported;
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorGetDevicePciBusId(char *pciBusId, int len, int dev) {
if (pciBusId == NULL) {
return flagcxInvalidArgument;
}
DEVCHECK(cudaDeviceGetPCIBusId(pciBusId, len, dev));
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorGetDeviceByPciBusId(int *dev, const char *pciBusId) {
if (dev == NULL || pciBusId == NULL) {
return flagcxInvalidArgument;
}
DEVCHECK(cudaDeviceGetByPCIBusId(dev, pciBusId));
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorDmaSupport(bool *dmaBufferSupport) {
if (dmaBufferSupport == NULL)
return flagcxInvalidArgument;
#if CUDA_VERSION >= 11070
int flag = 0;
CUdevice dev;
int cudaDriverVersion = 0;
CUresult cuRes = cuDriverGetVersion(&cudaDriverVersion);
if (cuRes != CUDA_SUCCESS || cudaDriverVersion < 11070) {
*dmaBufferSupport = false;
return flagcxSuccess;
}
int deviceId = 0;
if (cudaGetDevice(&deviceId) != cudaSuccess) {
*dmaBufferSupport = false;
return flagcxSuccess;
}
CUresult devRes = cuDeviceGet(&dev, deviceId);
if (devRes != CUDA_SUCCESS) {
*dmaBufferSupport = false;
return flagcxSuccess;
}
CUresult attrRes =
cuDeviceGetAttribute(&flag, CU_DEVICE_ATTRIBUTE_DMA_BUF_SUPPORTED, dev);
if (attrRes != CUDA_SUCCESS || flag == 0) {
*dmaBufferSupport = false;
return flagcxSuccess;
}
*dmaBufferSupport = true;
return flagcxSuccess;
#else
*dmaBufferSupport = false;
return flagcxSuccess;
#endif
}
flagcxResult_t
cudaAdaptorMemGetHandleForAddressRange(void *handleOut, void *buffer,
size_t size, unsigned long long flags) {
CUdeviceptr dptr = (CUdeviceptr)buffer;
CUresult err = cuMemGetHandleForAddressRange(
handleOut, dptr, size, CU_MEM_RANGE_HANDLE_TYPE_DMA_BUF_FD, flags);
if (err != CUDA_SUCCESS) {
return flagcxUnhandledDeviceError;
}
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorHostRegister(void *ptr, size_t size) {
if (ptr == NULL || size == 0) {
return flagcxInvalidArgument;
}
DEVCHECK(cudaHostRegister(ptr, size, cudaHostRegisterMapped));
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorHostUnregister(void *ptr) {
if (ptr == NULL) {
return flagcxInvalidArgument;
}
DEVCHECK(cudaHostUnregister(ptr));
return flagcxSuccess;
}
// ==========================================================================
// Symmetric memory VMM functions
// ==========================================================================
#if CUDART_VERSION >= 12010
flagcxResult_t cudaAdaptorSymPhysAlloc(void *ptr, size_t size,
void **physHandle, void *shareableHandle,
size_t *handleSize, size_t *allocSize) {
if (ptr == NULL || physHandle == NULL || shareableHandle == NULL ||
handleSize == NULL || allocSize == NULL)
return flagcxInvalidArgument;
CUmemGenericAllocationHandle *cuHandle =
(CUmemGenericAllocationHandle *)malloc(
sizeof(CUmemGenericAllocationHandle));
if (cuHandle == NULL)
return flagcxSystemError;
// Retain the physical allocation handle from the VMM-backed pointer
CUresult retainRes = cuMemRetainAllocationHandle(cuHandle, ptr);
if (retainRes != CUDA_SUCCESS) {
WARN("[symPhysAlloc] cuMemRetainAllocationHandle FAILED: %d ptr=%p",
(int)retainRes, ptr);
free(cuHandle);
return flagcxUnhandledDeviceError;
}
// Discover actual physical allocation size (already granularity-aligned)
size_t actualAllocSize = 0;
DEVCHECK(cuMemGetAddressRange(NULL, &actualAllocSize, (CUdeviceptr)ptr));
*allocSize = actualAllocSize;
// Export as POSIX fd for IPC sharing
if (*handleSize < sizeof(int)) {
free(cuHandle);
return flagcxInvalidArgument;
}
CUresult exportRes = cuMemExportToShareableHandle(
shareableHandle, *cuHandle, CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR, 0);
if (exportRes != CUDA_SUCCESS) {
WARN("[symPhysAlloc] cuMemExportToShareableHandle FAILED: %d",
(int)exportRes);
free(cuHandle);
return flagcxUnhandledDeviceError;
}
INFO(FLAGCX_INIT, "[symPhysAlloc] ptr=%p allocSize=%zu fd=%d", ptr,
actualAllocSize, *(int *)shareableHandle);
*handleSize = sizeof(int); // POSIX fd is an int
*physHandle = cuHandle;
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorSymPhysFree(void *physHandle) {
if (physHandle == NULL)
return flagcxSuccess;
CUmemGenericAllocationHandle *cuHandle =
(CUmemGenericAllocationHandle *)physHandle;
cuMemRelease(*cuHandle);
free(cuHandle);
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorSymFlatMap(void *peerHandles[], int nPeers,
int selfIndex, void *selfPhysHandle,
size_t allocSize, void **flatBase) {
if (peerHandles == NULL || selfPhysHandle == NULL || flatBase == NULL ||
nPeers <= 0 || allocSize == 0)
return flagcxInvalidArgument;
CUmemGenericAllocationHandle selfHandle =
*(CUmemGenericAllocationHandle *)selfPhysHandle;
// allocSize is already granularity-aligned (from cuMemGetAddressRange)
size_t totalSize = allocSize * nPeers;
// Reserve the full VA range
CUdeviceptr base = 0;
DEVCHECK(cuMemAddressReserve(&base, totalSize, 0, 0, 0));
// Import and map each peer's physical memory
int cudaDev;
DEVCHECK(cudaGetDevice(&cudaDev));
CUmemAccessDesc accessDesc = {};
accessDesc.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
accessDesc.location.id = cudaDev;
accessDesc.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
for (int i = 0; i < nPeers; i++) {
CUmemGenericAllocationHandle peerHandle;
if (i == selfIndex) {
peerHandle = selfHandle;
} else {
int fd = *(int *)peerHandles[i];
DEVCHECK(cuMemImportFromShareableHandle(
&peerHandle, (void *)(uintptr_t)fd,
CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR));
}
CUdeviceptr slot = base + (CUdeviceptr)i * allocSize;
DEVCHECK(cuMemMap(slot, allocSize, 0, peerHandle, 0));
DEVCHECK(cuMemSetAccess(slot, allocSize, &accessDesc, 1));
if (i != selfIndex) {
cuMemRelease(peerHandle);
}
}
*flatBase = (void *)base;
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorSymFlatUnmap(void *flatBase, size_t allocSize,
int nPeers) {
if (flatBase == NULL)
return flagcxSuccess;
CUdeviceptr base = (CUdeviceptr)flatBase;
size_t totalSize = allocSize * nPeers;
DEVCHECK(cuMemUnmap(base, totalSize));
DEVCHECK(cuMemAddressFree(base, totalSize));
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorSymMulticastSupported(int *supported) {
if (supported == NULL)
return flagcxInvalidArgument;
*supported = 0;
int cudaDev;
DEVCHECK(cudaGetDevice(&cudaDev));
CUdevice dev;
DEVCHECK(cuDeviceGet(&dev, cudaDev));
CUresult res = cuDeviceGetAttribute(
supported, CU_DEVICE_ATTRIBUTE_MULTICAST_SUPPORTED, dev);
if (res != CUDA_SUCCESS)
*supported = 0;
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorSymMulticastCreate(size_t allocSize,
int nLocalDevices,
const int *localDeviceOrdinals,
void **mcHandle,
int *shareableFd) {
if (mcHandle == NULL || shareableFd == NULL || nLocalDevices <= 0 ||
localDeviceOrdinals == NULL)
return flagcxInvalidArgument;
*mcHandle = NULL;
*shareableFd = -1;
CUmemGenericAllocationHandle handle = 0;
int fd = -1;
CUresult err;
// Get multicast granularity and align size
CUmulticastObjectProp mcProp = {};
mcProp.numDevices = (unsigned int)nLocalDevices;
mcProp.size = allocSize;
mcProp.handleTypes = CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR;
size_t mcGran = 0;
err = cuMulticastGetGranularity(&mcGran, &mcProp,
CU_MULTICAST_GRANULARITY_RECOMMENDED);
if (err != CUDA_SUCCESS)
return flagcxUnhandledDeviceError;
mcProp.size = ((allocSize + mcGran - 1) / mcGran) * mcGran;
err = cuMulticastCreate(&handle, &mcProp);
if (err != CUDA_SUCCESS)
return flagcxUnhandledDeviceError;
// Add all local devices using explicit ordinals
for (int i = 0; i < nLocalDevices; i++) {
CUdevice peerDev;
err = cuDeviceGet(&peerDev, localDeviceOrdinals[i]);
if (err != CUDA_SUCCESS)
goto cleanup_handle;
err = cuMulticastAddDevice(handle, peerDev);
if (err != CUDA_SUCCESS)
goto cleanup_handle;
}
// Export as POSIX FD for sharing with peers
err = cuMemExportToShareableHandle(
&fd, handle, CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR, 0);
if (err != CUDA_SUCCESS)
goto cleanup_handle;
// Store handle as heap-allocated value
{
CUmemGenericAllocationHandle *handlePtr =
(CUmemGenericAllocationHandle *)malloc(
sizeof(CUmemGenericAllocationHandle));
if (handlePtr == NULL)
goto cleanup_fd;
*handlePtr = handle;
*mcHandle = handlePtr;
*shareableFd = fd;
}
return flagcxSuccess;
cleanup_fd:
close(fd);
cleanup_handle:
cuMemRelease(handle);
return flagcxUnhandledDeviceError;
}
flagcxResult_t cudaAdaptorSymMulticastBind(void *mcHandle, int importFd,
void *physHandle, size_t allocSize,
int localRank, int nLocalDevices,
void **mcBase, size_t *mcMapSize) {
if (mcBase == NULL || physHandle == NULL || mcMapSize == NULL)
return flagcxInvalidArgument;
*mcBase = NULL;
*mcMapSize = 0;
CUmemGenericAllocationHandle cuMcHandle;
bool imported = (mcHandle == NULL);
if (mcHandle != NULL) {
// Rank 0: already has the handle from symMulticastCreate
cuMcHandle = *(CUmemGenericAllocationHandle *)mcHandle;
} else {
// Other ranks: import from FD
if (importFd < 0)
return flagcxInvalidArgument;
CUresult res = cuMemImportFromShareableHandle(
&cuMcHandle, (void *)(intptr_t)importFd,
CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR);
if (res != CUDA_SUCCESS) {
WARN("symMulticastBind: cuMemImportFromShareableHandle failed: %d", res);
return flagcxUnhandledDeviceError;
}
}
CUmemGenericAllocationHandle cuPhysHandle =
*(CUmemGenericAllocationHandle *)physHandle;
// Bind this rank's physical allocation to the multicast object.
// Use cuMulticastBindMem (takes physical handle), not cuMulticastBindAddr
// (which takes a virtual address).
CUresult res =
cuMulticastBindMem(cuMcHandle, 0, cuPhysHandle, 0, allocSize, 0);
if (res != CUDA_SUCCESS) {
WARN("symMulticastBind: cuMulticastBindMem failed: %d (localRank=%d "
"allocSize=%zu)",
res, localRank, allocSize);
if (imported)
cuMemRelease(cuMcHandle);
return flagcxUnhandledDeviceError;
}
// Get multicast granularity to compute aligned total size
CUmulticastObjectProp mcProp = {};
mcProp.numDevices = (unsigned int)nLocalDevices;
mcProp.size = allocSize;
mcProp.handleTypes = CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR;
size_t mcGran = 0;
res = cuMulticastGetGranularity(&mcGran, &mcProp,
CU_MULTICAST_GRANULARITY_RECOMMENDED);
if (res != CUDA_SUCCESS) {
WARN("symMulticastBind: cuMulticastGetGranularity failed: %d", res);
if (imported)
cuMemRelease(cuMcHandle);
return flagcxUnhandledDeviceError;
}
size_t alignedSize = ((allocSize + mcGran - 1) / mcGran) * mcGran;
// Reserve VA and map the multicast handle
CUdeviceptr mcVa = 0;
res = cuMemAddressReserve(&mcVa, alignedSize, mcGran, 0, 0);
if (res != CUDA_SUCCESS) {
WARN("symMulticastBind: cuMemAddressReserve failed: %d", res);
if (imported)
cuMemRelease(cuMcHandle);
return flagcxUnhandledDeviceError;
}
res = cuMemMap(mcVa, alignedSize, 0, cuMcHandle, 0);
if (res != CUDA_SUCCESS) {
WARN("symMulticastBind: cuMemMap failed: %d", res);
cuMemAddressFree(mcVa, alignedSize);
if (imported)
cuMemRelease(cuMcHandle);
return flagcxUnhandledDeviceError;
}
// Set access for the current device
int cudaDev;
DEVCHECK(cudaGetDevice(&cudaDev));
CUmemAccessDesc accessDesc = {};
accessDesc.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
accessDesc.location.id = cudaDev;
accessDesc.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
res = cuMemSetAccess(mcVa, alignedSize, &accessDesc, 1);
if (res != CUDA_SUCCESS) {
WARN("symMulticastBind: cuMemSetAccess failed: %d", res);
cuMemUnmap(mcVa, alignedSize);
cuMemAddressFree(mcVa, alignedSize);
if (imported)
cuMemRelease(cuMcHandle);
return flagcxUnhandledDeviceError;
}
*mcBase = (void *)mcVa;
*mcMapSize = alignedSize;
if (imported)
cuMemRelease(cuMcHandle);
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorSymMulticastTeardown(void *mcBase, size_t mcMapSize) {
if (mcBase == NULL)
return flagcxSuccess;
CUdeviceptr va = (CUdeviceptr)mcBase;
DEVCHECK(cuMemUnmap(va, mcMapSize));
DEVCHECK(cuMemAddressFree(va, mcMapSize));
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorSymMulticastFree(void *mcHandle) {
if (mcHandle == NULL)
return flagcxSuccess;
CUmemGenericAllocationHandle handle =
*(CUmemGenericAllocationHandle *)mcHandle;
DEVCHECK(cuMemRelease(handle));
free(mcHandle);
return flagcxSuccess;
}
#else // CUDART_VERSION < 12010
flagcxResult_t cudaAdaptorSymPhysAlloc(void *, size_t, void **, void *,
size_t *, size_t *) {
return flagcxNotSupported;
}
flagcxResult_t cudaAdaptorSymPhysFree(void *) { return flagcxNotSupported; }
flagcxResult_t cudaAdaptorSymFlatMap(void *[], int, int, void *, size_t,
void **) {
return flagcxNotSupported;
}
flagcxResult_t cudaAdaptorSymFlatUnmap(void *, size_t, int) {
return flagcxNotSupported;
}
flagcxResult_t cudaAdaptorSymMulticastSupported(int *supported) {
if (supported)
*supported = 0;
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorSymMulticastCreate(size_t, int, const int *, void **,
int *) {
return flagcxNotSupported;
}
flagcxResult_t cudaAdaptorSymMulticastBind(void *, int, void *, size_t, int,
int, void **, size_t *) {
return flagcxNotSupported;
}
flagcxResult_t cudaAdaptorSymMulticastTeardown(void *, size_t) {
return flagcxSuccess;
}
flagcxResult_t cudaAdaptorSymMulticastFree(void *) { return flagcxSuccess; }
#endif // CUDART_VERSION >= 12010
flagcxResult_t cudaAdaptorGetLastError() {
cudaError_t err = cudaGetLastError();
return err == cudaSuccess ? flagcxSuccess : flagcxSystemError;
}
struct flagcxDeviceAdaptor cudaAdaptor {
"CUDA",
// Basic functions
cudaAdaptorDeviceSynchronize, cudaAdaptorDeviceMemcpy,
cudaAdaptorDeviceMemset, cudaAdaptorDeviceMalloc, cudaAdaptorDeviceFree,
cudaAdaptorSetDevice, cudaAdaptorGetDevice, cudaAdaptorGetDeviceCount,
cudaAdaptorGetVendor, cudaAdaptorHostGetDevicePointer,
// GDR functions
NULL, // flagcxResult_t (*memHandleInit)(int dev_id, void **memHandle);
NULL, // flagcxResult_t (*memHandleDestroy)(int dev, void *memHandle);
cudaAdaptorGdrMemAlloc, cudaAdaptorGdrMemFree,
NULL, // flagcxResult_t (*hostShareMemAlloc)(void **ptr, size_t size, void
// *memHandle);
NULL, // flagcxResult_t (*hostShareMemFree)(void *ptr, void *memHandle);
NULL, // flagcxResult_t (*gdrPtrMmap)(void **pcpuptr, void *devptr, size_t
// sz);
NULL, // flagcxResult_t (*gdrPtrMunmap)(void *cpuptr, size_t sz);
// Stream functions
cudaAdaptorStreamCreate, cudaAdaptorStreamDestroy, cudaAdaptorStreamCopy,
cudaAdaptorStreamFree, cudaAdaptorStreamSynchronize,
cudaAdaptorStreamQuery, cudaAdaptorStreamWaitEvent,
cudaAdaptorStreamWaitValue64, cudaAdaptorStreamWriteValue64,
// Event functions
cudaAdaptorEventCreate, cudaAdaptorEventDestroy, cudaAdaptorEventRecord,
cudaAdaptorEventSynchronize, cudaAdaptorEventQuery,
cudaAdaptorEventElapsedTime,
// IpcMemHandle functions
cudaAdaptorIpcMemHandleCreate, cudaAdaptorIpcMemHandleGet,
cudaAdaptorIpcMemHandleOpen, cudaAdaptorIpcMemHandleClose,
cudaAdaptorIpcMemHandleFree,
// Kernel launch
NULL, // flagcxResult_t (*launchKernel)(void *func, unsigned int block_x,
// unsigned int block_y, unsigned int block_z, unsigned int grid_x,
// unsigned int grid_y, unsigned int grid_z, void **args, size_t
// share_mem, void *stream, void *memHandle);
NULL, // flagcxResult_t (*copyArgsInit)(void **args);
NULL, // flagcxResult_t (*copyArgsFree)(void *args);
cudaAdaptorLaunchDeviceFunc, // flagcxResult_t
// (*launchDeviceFunc)(flagcxStream_t stream,
// void *args);
// Others
cudaAdaptorGetDeviceProperties, // flagcxResult_t
// (*getDeviceProperties)(struct
// flagcxDevProps *props, int dev);
cudaAdaptorGetDevicePciBusId, // flagcxResult_t (*getDevicePciBusId)(char
// *pciBusId, int len, int dev);
cudaAdaptorGetDeviceByPciBusId, // flagcxResult_t
// (*getDeviceByPciBusId)(int
// *dev, const char *pciBusId);
cudaAdaptorLaunchHostFunc,
// DMA buffer
cudaAdaptorDmaSupport, // flagcxResult_t (*dmaSupport)(bool
// *dmaBufferSupport);
cudaAdaptorMemGetHandleForAddressRange, // flagcxResult_t
// (*memGetHandleForAddressRange)(void
// *handleOut, void *buffer,
// size_t size, unsigned long long
// flags);
cudaAdaptorHostRegister, // flagcxResult_t (*hostRegister)(void *,
// size_t);
cudaAdaptorHostUnregister, // flagcxResult_t (*hostUnregister)(void *);
// Symmetric memory VMM functions
cudaAdaptorSymPhysAlloc, cudaAdaptorSymPhysFree, cudaAdaptorSymFlatMap,