-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy path13_shard.cpp
More file actions
1495 lines (1360 loc) · 48.6 KB
/
13_shard.cpp
File metadata and controls
1495 lines (1360 loc) · 48.6 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
// clang-format off
/*
Example run:
server$ ./build/13_shard
GPUs: 8, NICs: 32, Total Bandwidth: 3200 Gbps
PCIe Topology:
cuda:0(53:00.0) NUMA0 CPU 0-11 rdmap79s0 (4f:00.0) rdmap80s0 (50:00.0) rdmap81s0 (51:00.0) rdmap82s0 (52:00.0)
cuda:1(64:00.0) NUMA0 CPU12-23 rdmap96s0 (60:00.0) rdmap97s0 (61:00.0) rdmap98s0 (62:00.0) rdmap99s0 (63:00.0)
cuda:2(75:00.0) NUMA0 CPU24-35 rdmap113s0(71:00.0) rdmap114s0(72:00.0) rdmap115s0(73:00.0) rdmap116s0(74:00.0)
cuda:3(86:00.0) NUMA0 CPU36-47 rdmap130s0(82:00.0) rdmap131s0(83:00.0) rdmap132s0(84:00.0) rdmap133s0(85:00.0)
cuda:4(97:00.0) NUMA1 CPU48-59 rdmap147s0(93:00.0) rdmap148s0(94:00.0) rdmap149s0(95:00.0) rdmap150s0(96:00.0)
cuda:5(a8:00.0) NUMA1 CPU60-71 rdmap164s0(a4:00.0) rdmap165s0(a5:00.0) rdmap166s0(a6:00.0) rdmap167s0(a7:00.0)
cuda:6(b9:00.0) NUMA1 CPU72-83 rdmap181s0(b5:00.0) rdmap182s0(b6:00.0) rdmap183s0(b7:00.0) rdmap184s0(b8:00.0)
cuda:7(ca:00.0) NUMA1 CPU84-95 rdmap198s0(c6:00.0) rdmap199s0(c7:00.0) rdmap200s0(c8:00.0) rdmap201s0(c9:00.0)
Run client with the following command:
./build/13_shard 8 32 fe80000000000000088c03fffecfda9500000000fa6bac5a0000000000000000 [page_size num_pages]
Registered MR from cuda:0 cuda:1 cuda:2 cuda:3 cuda:4 cuda:5 cuda:6 cuda:7
------
Received CONNECT message from client: num_gpus=8, num_nets=32, num_mr=64
Received RandomFill request from client:
remote_context: 0x00000123
seed: 0xb584035fabe6ce9b
page_size: 65536
num_pages: 1000
total_repeat: 2000
Generating random data................
Started RDMA WRITE to the remote GPU memory.
[11.019s] WRITE: 100%, ops=32000000/32000000, posted=32000000(100%), bytes=2097152000000/2097152000000, bw=1522.567Gbps(47.6%), 2.904Mpps
Finished all RDMA WRITEs to the remote GPU memory.
------
^C
client$ ./build/13_shard 8 32 fe80000000000000088c03fffecfda9500000000fa6bac5a0000000000000000
GPUs: 8, NICs: 32, Total Bandwidth: 3200 Gbps
PCIe Topology:
cuda:0(53:00.0) NUMA0 CPU 0-11 rdmap79s0 (4f:00.0) rdmap80s0 (50:00.0) rdmap81s0 (51:00.0) rdmap82s0 (52:00.0)
cuda:1(64:00.0) NUMA0 CPU12-23 rdmap96s0 (60:00.0) rdmap97s0 (61:00.0) rdmap98s0 (62:00.0) rdmap99s0 (63:00.0)
cuda:2(75:00.0) NUMA0 CPU24-35 rdmap113s0(71:00.0) rdmap114s0(72:00.0) rdmap115s0(73:00.0) rdmap116s0(74:00.0)
cuda:3(86:00.0) NUMA0 CPU36-47 rdmap130s0(82:00.0) rdmap131s0(83:00.0) rdmap132s0(84:00.0) rdmap133s0(85:00.0)
cuda:4(97:00.0) NUMA1 CPU48-59 rdmap147s0(93:00.0) rdmap148s0(94:00.0) rdmap149s0(95:00.0) rdmap150s0(96:00.0)
cuda:5(a8:00.0) NUMA1 CPU60-71 rdmap164s0(a4:00.0) rdmap165s0(a5:00.0) rdmap166s0(a6:00.0) rdmap167s0(a7:00.0)
cuda:6(b9:00.0) NUMA1 CPU72-83 rdmap181s0(b5:00.0) rdmap182s0(b6:00.0) rdmap183s0(b7:00.0) rdmap184s0(b8:00.0)
cuda:7(ca:00.0) NUMA1 CPU84-95 rdmap198s0(c6:00.0) rdmap199s0(c7:00.0) rdmap200s0(c8:00.0) rdmap201s0(c9:00.0)
Registered MR from cuda:0 cuda:1 cuda:2 cuda:3 cuda:4 cuda:5 cuda:6 cuda:7
Sent CONNECT message to server. SEND latency: 19562.466us
Sent RandomFillRequest to server. page_size: 65536, num_pages: 1000, SEND latency: 1218.370us
Received RDMA WRITE to local GPU memory.
Verifying................
Data is correct
*/
// clang-format on
#include <algorithm>
#include <array>
#include <atomic>
#include <chrono>
#include <cuda.h>
#include <cuda_runtime.h>
#include <deque>
#include <filesystem>
#include <fstream>
#include <functional>
#include <immintrin.h>
#include <inttypes.h>
#include <memory>
#include <netdb.h>
#include <pthread.h>
#include <random>
#include <rdma/fabric.h>
#include <rdma/fi_cm.h>
#include <rdma/fi_domain.h>
#include <rdma/fi_endpoint.h>
#include <rdma/fi_errno.h>
#include <rdma/fi_ext.h>
#include <rdma/fi_rma.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <string_view>
#include <thread>
#include <time.h>
#include <type_traits>
#include <unistd.h>
#include <vector>
#define CHECK(stmt) \
do { \
if (!(stmt)) { \
fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, #stmt); \
std::exit(1); \
} \
} while (0)
#define FI_CHECK(stmt) \
do { \
int rc = (stmt); \
if (rc) { \
fprintf(stderr, "%s:%d %s failed with %d (%s)\n", __FILE__, __LINE__, \
#stmt, rc, fi_strerror(-rc)); \
std::exit(1); \
} \
} while (0)
#define CUDA_CHECK(stmt) \
do { \
cudaError_t rc = (stmt); \
if (rc != cudaSuccess) { \
fprintf(stderr, "%s:%d %s failed with %d (%s)\n", __FILE__, __LINE__, \
#stmt, rc, cudaGetErrorString(rc)); \
std::exit(1); \
} \
} while (0)
#define CU_CHECK(stmt) \
do { \
CUresult rc = (stmt); \
if (rc != CUDA_SUCCESS) { \
const char *err_str; \
cuGetErrorString(rc, &err_str); \
fprintf(stderr, "%s:%d %s failed with %d (%s)\n", __FILE__, __LINE__, \
#stmt, rc, err_str); \
std::exit(1); \
} \
} while (0)
constexpr size_t kBufAlign = 128; // EFA alignment requirement
constexpr size_t kMessageBufferSize = 1 << 20;
constexpr size_t kCompletionQueueReadCount = 16;
constexpr size_t kMemoryRegionSize = 1UL << 30;
constexpr size_t kEfaImmDataSize = 4;
constexpr size_t kMaxNetworksPerGroup = 4;
struct Buffer;
struct Network;
struct PciAddress {
uint16_t domain : 16;
uint8_t bus : 8;
uint8_t device : 5;
uint8_t function : 3;
static PciAddress Parse(std::string_view addr) {
CHECK(addr.size() == 12);
uint16_t domain;
uint8_t bus, device, function;
CHECK(sscanf(addr.data(), "%hx:%hhx:%hhx.%hhx", &domain, &bus, &device,
&function) == 4);
return PciAddress{domain, bus, device, function};
}
uint32_t AsU32() const { return *(uint32_t *)this; }
friend bool operator==(const PciAddress &lhs, const PciAddress &rhs) {
return lhs.AsU32() == rhs.AsU32();
}
};
static_assert(sizeof(PciAddress) == 4);
namespace std {
template <> struct hash<PciAddress> {
size_t operator()(const PciAddress &addr) const {
return hash<uint32_t>()(addr.AsU32());
}
};
} // namespace std
struct TopologyGroup {
int cuda_device;
int numa;
std::vector<struct fi_info *> fi_infos;
std::vector<int> cpus;
};
std::vector<TopologyGroup> DetectTopo(struct fi_info *info) {
char buf[256];
int num_gpus = 0;
CUDA_CHECK(cudaGetDeviceCount(&num_gpus));
std::vector<TopologyGroup> topo_groups(num_gpus);
int num_cpus = 0;
std::vector<std::vector<int>> numa_cpus;
for (const auto &entry : std::filesystem::recursive_directory_iterator(
"/sys/devices/system/node/")) {
if (entry.path().filename().string().rfind("node", 0) != 0) {
continue;
}
numa_cpus.emplace_back();
}
int hardware_concurrency = std::thread::hardware_concurrency();
for (size_t node_id = 0; node_id < numa_cpus.size(); ++node_id) {
for (int cpu = 0; cpu < hardware_concurrency; ++cpu) {
snprintf(buf, sizeof(buf),
"/sys/devices/system/node/node%zu/cpu%d/"
"topology/thread_siblings_list",
node_id, cpu);
// Filter out hyperthreads
std::ifstream f(buf);
std::string sibling_list;
if (f >> sibling_list) {
int first_sibling;
try {
first_sibling = std::stoi(sibling_list);
} catch (std::invalid_argument &e) {
continue;
}
if (first_sibling == cpu) {
numa_cpus[node_id].push_back(cpu);
}
}
}
std::sort(numa_cpus[node_id].begin(), numa_cpus[node_id].end());
num_cpus += numa_cpus[node_id].size();
}
int cpus_per_gpu = num_cpus / num_gpus;
std::unordered_map<PciAddress, PciAddress> pci_parent_map;
for (const auto &entry :
std::filesystem::recursive_directory_iterator("/sys/bus/pci/devices")) {
if (!entry.is_symlink()) {
continue;
}
auto target = std::filesystem::read_symlink(entry.path());
auto addr_str = target.filename().string();
auto parent_addr_str = target.parent_path().filename().string();
CHECK(addr_str.size() == 12); // 0000:51:00.0
if (parent_addr_str.size() != 12) { // 0000:46:01.2
continue; // pci0000:cc
}
auto addr = PciAddress::Parse(addr_str);
auto parent_bus = PciAddress::Parse(parent_addr_str);
parent_bus.device = 0;
parent_bus.function = 0;
pci_parent_map[addr] = parent_bus;
}
std::vector<int> numa_gpu_count(numa_cpus.size());
std::unordered_map<PciAddress, int> bus_cuda_map;
for (int i = 0; i < num_gpus; ++i) {
cudaDeviceProp prop;
CUDA_CHECK(cudaGetDeviceProperties(&prop, i));
auto pci_addr =
PciAddress{(uint16_t)prop.pciDomainID, (uint8_t)prop.pciBusID,
(uint8_t)prop.pciDeviceID, 0};
auto parent_bus = pci_parent_map.at(pci_addr);
bus_cuda_map[parent_bus] = i;
topo_groups[i].cuda_device = i;
snprintf(buf, sizeof(buf),
"/sys/bus/pci/devices/%04x:%02x:%02x.0/numa_node",
prop.pciDomainID, prop.pciBusID, prop.pciDeviceID);
std::ifstream f(buf);
CHECK(f >> topo_groups[i].numa);
int numa_gpu_idx = numa_gpu_count[topo_groups[i].numa]++;
auto &cpus = numa_cpus[topo_groups[i].numa];
int cpu_start = cpus_per_gpu * numa_gpu_idx;
CHECK(cpu_start + cpus_per_gpu <= (int)cpus.size());
topo_groups[i].cpus.assign(cpus.begin() + cpu_start,
cpus.begin() + cpu_start + cpus_per_gpu);
}
for (auto *fi = info; fi; fi = fi->next) {
auto &pci = fi->nic->bus_attr->attr.pci;
auto pci_addr =
PciAddress{pci.domain_id, pci.bus_id, pci.device_id, pci.function_id};
auto parent_bus = pci_parent_map.at(pci_addr);
auto cuda_device = bus_cuda_map.at(parent_bus);
topo_groups[cuda_device].fi_infos.push_back(fi);
}
return topo_groups;
}
void PrintTopologyGroups(const std::vector<TopologyGroup> &topo_groups) {
printf("PCIe Topology:\n");
for (const auto &topo : topo_groups) {
cudaDeviceProp prop;
CUDA_CHECK(cudaGetDeviceProperties(&prop, topo.cuda_device));
printf(" cuda:%d(%02x:%02x.0)", topo.cuda_device, prop.pciBusID,
prop.pciDeviceID);
printf(" NUMA%d", topo.numa);
printf(" CPU%2d-%2d", topo.cpus.front(), topo.cpus.back());
for (auto *fi : topo.fi_infos) {
printf(" %-10s(%02x:%02x.%d)", fi->nic->device_attr->name,
fi->nic->bus_attr->attr.pci.bus_id,
fi->nic->bus_attr->attr.pci.device_id,
fi->nic->bus_attr->attr.pci.function_id);
}
printf("\n");
}
}
void TrimTopo(std::vector<TopologyGroup> &groups, int num_gpus, int num_nets) {
CHECK(num_gpus <= (int)groups.size());
CHECK(num_nets % num_gpus == 0);
int nets_per_gpu = num_nets / num_gpus;
for (const auto &group : groups) {
CHECK(nets_per_gpu <= (int)group.fi_infos.size());
}
while ((int)groups.size() > num_gpus) {
groups.pop_back();
}
for (int i = 0; i < num_gpus; ++i) {
while ((int)groups[i].fi_infos.size() > nets_per_gpu) {
groups[i].fi_infos.pop_back();
}
}
}
struct EfaAddress {
uint8_t bytes[32];
explicit EfaAddress(uint8_t bytes[32]) { memcpy(this->bytes, bytes, 32); }
std::string ToString() const {
char buf[65];
for (size_t i = 0; i < 32; i++) {
snprintf(buf + 2 * i, 3, "%02x", bytes[i]);
}
return std::string(buf, 64);
}
static EfaAddress Parse(const std::string &str) {
if (str.size() != 64) {
fprintf(stderr, "Unexpected address length %zu\n", str.size());
std::exit(1);
}
uint8_t bytes[32];
for (size_t i = 0; i < 32; i++) {
sscanf(str.c_str() + 2 * i, "%02hhx", &bytes[i]);
}
return EfaAddress(bytes);
}
};
enum class RdmaOpType : uint8_t {
kRecv = 0,
kSend = 1,
kWrite = 2,
kRemoteWrite = 3,
};
struct RdmaRecvOp {
Buffer *buf;
fi_addr_t src_addr; // Set after completion
size_t recv_size; // Set after completion
};
static_assert(std::is_pod_v<RdmaRecvOp>);
struct RdmaSendOp {
Buffer *buf;
size_t len;
fi_addr_t dest_addr;
};
static_assert(std::is_pod_v<RdmaSendOp>);
struct RdmaWriteOp {
Buffer *buf;
size_t offset;
size_t len;
uint32_t imm_data;
uint64_t dest_ptr;
fi_addr_t dest_addr;
uint64_t dest_key;
};
static_assert(std::is_pod_v<RdmaWriteOp>);
struct RdmaRemoteWriteOp {
uint32_t op_id;
};
static_assert(std::is_pod_v<RdmaRemoteWriteOp>);
static_assert(sizeof(RdmaRemoteWriteOp) <= kEfaImmDataSize);
struct RdmaOp {
RdmaOpType type;
union {
RdmaRecvOp recv;
RdmaSendOp send;
RdmaWriteOp write;
RdmaRemoteWriteOp remote_write;
};
std::function<void(Network &, RdmaOp &)> callback;
};
struct Network {
struct fi_info *fi;
struct fid_fabric *fabric;
struct fid_domain *domain;
struct fid_cq *cq;
struct fid_av *av;
struct fid_ep *ep;
EfaAddress addr;
int cuda_device;
std::deque<RdmaOp *> pending_ops;
std::unordered_map<void *, struct fid_mr *> mr;
std::unordered_map<uint32_t, RdmaOp *> remote_write_ops;
static Network Open(struct fi_info *fi, int cuda_device);
fi_addr_t AddPeerAddress(const EfaAddress &peer_addr);
void RegisterMemory(Buffer &buf);
struct fid_mr *GetMR(const Buffer &buf);
void PollCompletion();
void ProgressPendingOps();
void PostRecv(Buffer &buf,
std::function<void(Network &, RdmaOp &)> &&callback);
void PostSend(fi_addr_t addr, Buffer &buf, size_t len,
std::function<void(Network &, RdmaOp &)> &&callback);
void PostWrite(RdmaWriteOp &&write,
std::function<void(Network &, RdmaOp &)> &&callback);
void AddRemoteWrite(uint32_t id,
std::function<void(Network &, RdmaOp &)> &&callback);
Network(const Network &) = delete;
Network(Network &&other)
: fi(other.fi), fabric(other.fabric), domain(other.domain), cq(other.cq),
av(other.av), ep(other.ep), addr(other.addr),
cuda_device(other.cuda_device) {
other.fi = nullptr;
other.fabric = nullptr;
other.domain = nullptr;
other.cq = nullptr;
other.av = nullptr;
other.ep = nullptr;
}
~Network() {
for (const auto &[_, mr] : mr) {
FI_CHECK(fi_close(&mr->fid));
}
if (ep)
FI_CHECK(fi_close(&ep->fid));
if (av)
FI_CHECK(fi_close(&av->fid));
if (cq)
FI_CHECK(fi_close(&cq->fid));
if (domain)
FI_CHECK(fi_close(&domain->fid));
if (fabric)
FI_CHECK(fi_close(&fabric->fid));
}
private:
Network(struct fi_info *fi, struct fid_fabric *fabric,
struct fid_domain *domain, struct fid_cq *cq, struct fid_av *av,
struct fid_ep *ep, EfaAddress addr, int cuda_device)
: fi(fi), fabric(fabric), domain(domain), cq(cq), av(av), ep(ep),
addr(addr), cuda_device(cuda_device) {}
};
struct NetworkGroup {
std::vector<Network *> nets;
uint8_t rr_mask;
uint8_t rr_idx = 0;
NetworkGroup(std::vector<Network *> &&nets) {
CHECK(nets.size() <= kMaxNetworksPerGroup);
CHECK((nets.size() & (nets.size() - 1)) == 0); // power of 2
this->rr_mask = nets.size() - 1;
this->nets = std::move(nets);
}
NetworkGroup(const NetworkGroup &) = delete;
NetworkGroup(NetworkGroup &&) = default;
uint8_t GetNext() {
rr_idx = (rr_idx + 1) & rr_mask;
return rr_idx;
}
};
void *align_up(void *ptr, size_t align) {
uintptr_t addr = (uintptr_t)ptr;
return (void *)((addr + align - 1) & ~(align - 1));
}
struct Buffer {
void *data;
size_t size;
int cuda_device;
int dmabuf_fd;
static Buffer Alloc(size_t size, size_t align) {
void *raw_data = malloc(size);
CHECK(raw_data != nullptr);
return Buffer(raw_data, size, align, -1, -1);
}
static Buffer AllocCuda(size_t size, size_t align) {
void *raw_data;
struct cudaPointerAttributes attrs = {};
CUDA_CHECK(cudaMalloc(&raw_data, size));
CUDA_CHECK(cudaPointerGetAttributes(&attrs, raw_data));
CHECK(attrs.type == cudaMemoryTypeDevice);
int cuda_device = attrs.device;
int fd = -1;
CU_CHECK(cuMemGetHandleForAddressRange(
&fd, (CUdeviceptr)align_up(raw_data, align), size,
CU_MEM_RANGE_HANDLE_TYPE_DMA_BUF_FD, 0));
return Buffer(raw_data, size, align, cuda_device, fd);
}
bool is_cuda() const { return cuda_device >= 0; }
Buffer(Buffer &&other)
: data(other.data), size(other.size), cuda_device(other.cuda_device),
dmabuf_fd(other.dmabuf_fd), raw_data(other.raw_data) {
other.data = nullptr;
other.raw_data = nullptr;
other.size = 0;
other.cuda_device = -1;
other.dmabuf_fd = -1;
}
~Buffer() {
if (is_cuda()) {
CUDA_CHECK(cudaFree(raw_data));
} else {
free(raw_data);
}
}
private:
void *raw_data;
Buffer(void *raw_data, size_t raw_size, size_t align, int cuda_device,
int dmabuf_fd) {
this->raw_data = raw_data;
this->data = align_up(raw_data, align);
this->size = (size_t)((uintptr_t)raw_data + raw_size - (uintptr_t)data);
this->cuda_device = cuda_device;
this->dmabuf_fd = dmabuf_fd;
}
Buffer(const Buffer &) = delete;
};
struct fi_info *GetInfo() {
struct fi_info *hints, *info;
hints = fi_allocinfo();
hints->caps = FI_MSG | FI_RMA | FI_HMEM | FI_LOCAL_COMM | FI_REMOTE_COMM;
hints->ep_attr->type = FI_EP_RDM;
hints->fabric_attr->prov_name = strdup("efa");
hints->domain_attr->mr_mode = FI_MR_LOCAL | FI_MR_HMEM | FI_MR_VIRT_ADDR |
FI_MR_ALLOCATED | FI_MR_PROV_KEY;
hints->domain_attr->threading = FI_THREAD_SAFE;
FI_CHECK(fi_getinfo(FI_VERSION(2, 0), nullptr, nullptr, 0, hints, &info));
fi_freeinfo(hints);
return info;
}
Network Network::Open(struct fi_info *fi, int cuda_device) {
struct fid_fabric *fabric;
FI_CHECK(fi_fabric(fi->fabric_attr, &fabric, nullptr));
struct fid_domain *domain;
FI_CHECK(fi_domain(fabric, fi, &domain, nullptr));
struct fid_cq *cq;
struct fi_cq_attr cq_attr = {};
cq_attr.format = FI_CQ_FORMAT_DATA;
FI_CHECK(fi_cq_open(domain, &cq_attr, &cq, nullptr));
struct fid_av *av;
struct fi_av_attr av_attr = {};
FI_CHECK(fi_av_open(domain, &av_attr, &av, nullptr));
struct fid_ep *ep;
FI_CHECK(fi_endpoint(domain, fi, &ep, nullptr));
FI_CHECK(fi_ep_bind(ep, &cq->fid, FI_SEND | FI_RECV));
FI_CHECK(fi_ep_bind(ep, &av->fid, 0));
FI_CHECK(fi_enable(ep));
uint8_t addrbuf[64];
size_t addrlen = sizeof(addrbuf);
FI_CHECK(fi_getname(&ep->fid, addrbuf, &addrlen));
if (addrlen != 32) {
fprintf(stderr, "Unexpected address length %zu\n", addrlen);
std::exit(1);
}
auto addr = EfaAddress(addrbuf);
return Network(fi, fabric, domain, cq, av, ep, addr, cuda_device);
}
fi_addr_t Network::AddPeerAddress(const EfaAddress &peer_addr) {
fi_addr_t addr = FI_ADDR_UNSPEC;
int ret = fi_av_insert(av, peer_addr.bytes, 1, &addr, 0, nullptr);
if (ret != 1) {
fprintf(stderr, "fi_av_insert failed: %d\n", ret);
std::exit(1);
}
return addr;
}
void Network::RegisterMemory(Buffer &buf) {
struct fid_mr *mr;
struct fi_mr_attr mr_attr = {
.iov_count = 1,
.access = FI_SEND | FI_RECV | FI_REMOTE_WRITE | FI_REMOTE_READ |
FI_WRITE | FI_READ,
};
struct iovec iov = {.iov_base = buf.data, .iov_len = buf.size};
struct fi_mr_dmabuf dmabuf = {
.fd = buf.dmabuf_fd, .offset = 0, .len = buf.size, .base_addr = buf.data};
uint64_t flags = 0;
if (buf.is_cuda()) {
CHECK(buf.cuda_device == cuda_device);
mr_attr.iface = FI_HMEM_CUDA;
mr_attr.device.cuda = buf.cuda_device;
if (buf.dmabuf_fd != -1) {
mr_attr.dmabuf = &dmabuf;
flags = FI_MR_DMABUF;
} else {
mr_attr.mr_iov = &iov;
}
} else {
mr_attr.mr_iov = &iov;
}
FI_CHECK(fi_mr_regattr(domain, &mr_attr, flags, &mr));
this->mr[buf.data] = mr;
}
struct fid_mr *Network::GetMR(const Buffer &buf) {
auto it = mr.find(buf.data);
CHECK(it != mr.end());
return it->second;
}
void Network::PostRecv(Buffer &buf,
std::function<void(Network &, RdmaOp &)> &&callback) {
auto *op = new RdmaOp{
.type = RdmaOpType::kRecv,
.recv =
RdmaRecvOp{.buf = &buf, .src_addr = FI_ADDR_UNSPEC, .recv_size = 0},
.callback = std::move(callback),
};
pending_ops.push_back(op);
ProgressPendingOps();
}
void Network::PostSend(fi_addr_t addr, Buffer &buf, size_t len,
std::function<void(Network &, RdmaOp &)> &&callback) {
CHECK(len <= buf.size);
auto *op = new RdmaOp{
.type = RdmaOpType::kSend,
.send = RdmaSendOp{.buf = &buf, .len = len, .dest_addr = addr},
.callback = std::move(callback),
};
pending_ops.push_back(op);
ProgressPendingOps();
}
void Network::PostWrite(RdmaWriteOp &&write,
std::function<void(Network &, RdmaOp &)> &&callback) {
auto *op = new RdmaOp{
.type = RdmaOpType::kWrite,
.write = std::move(write),
.callback = std::move(callback),
};
pending_ops.push_back(op);
ProgressPendingOps();
}
void Network::AddRemoteWrite(
uint32_t id, std::function<void(Network &, RdmaOp &)> &&callback) {
CHECK(remote_write_ops.count(id) == 0);
auto *op = new RdmaOp{
.type = RdmaOpType::kRemoteWrite,
.remote_write = RdmaRemoteWriteOp{.op_id = id},
.callback = std::move(callback),
};
remote_write_ops[id] = op;
}
void Network::ProgressPendingOps() {
while (!pending_ops.empty()) {
auto *op = pending_ops.front();
pending_ops.pop_front();
const char *op_name = nullptr;
ssize_t ret = 0;
switch (op->type) {
case RdmaOpType::kRecv: {
op_name = "fi_recv";
auto &recv = op->recv;
struct iovec iov = {
.iov_base = recv.buf->data,
.iov_len = recv.buf->size,
};
struct fi_msg msg = {
.msg_iov = &iov,
.desc = &GetMR(*recv.buf)->mem_desc,
.iov_count = 1,
.addr = FI_ADDR_UNSPEC,
.context = op,
};
ret = fi_recvmsg(ep, &msg, 0);
break;
}
case RdmaOpType::kSend: {
op_name = "fi_send";
auto &send = op->send;
struct iovec iov = {
.iov_base = send.buf->data,
.iov_len = send.len,
};
struct fi_msg msg = {
.msg_iov = &iov,
.desc = &GetMR(*send.buf)->mem_desc,
.iov_count = 1,
.addr = send.dest_addr,
.context = op,
};
ret = fi_sendmsg(ep, &msg, 0);
break;
}
case RdmaOpType::kWrite: {
op_name = "fi_writemsg";
auto &write = op->write;
struct iovec iov = {
.iov_base = (uint8_t *)write.buf->data + write.offset,
.iov_len = write.len,
};
struct fi_rma_iov rma_iov = {
.addr = write.dest_ptr,
.len = write.len,
.key = write.dest_key,
};
struct fi_msg_rma msg = {
.msg_iov = &iov,
.desc = &GetMR(*write.buf)->mem_desc,
.iov_count = 1,
.addr = write.dest_addr,
.rma_iov = &rma_iov,
.rma_iov_count = 1,
.context = op,
.data = write.imm_data,
};
uint64_t flags = 0;
if (write.imm_data) {
flags |= FI_REMOTE_CQ_DATA;
}
ret = fi_writemsg(ep, &msg, flags);
break;
}
case RdmaOpType::kRemoteWrite: {
CHECK(false); // Unreachable
break;
}
}
if (ret == -FI_EAGAIN) {
// Put it back to the front of the queue
pending_ops.push_front(op);
break;
}
if (ret) {
// Unexpected error. Don't put it back.
// Delete the op since it's not going to be in the completion queue.
delete op;
fprintf(stderr, "Failed to ProgressPendingOps. %s() returned %ld (%s)\n",
op_name, ret, fi_strerror(-ret));
fflush(stderr);
break;
}
}
}
void HandleCompletion(Network &net, const struct fi_cq_data_entry &cqe) {
RdmaOp *op = nullptr;
if (cqe.flags & FI_REMOTE_WRITE) {
// REMOTE WRITE does not have op_context
// NOTE(lequn): EFA only supports 4 bytes of immediate data.
uint32_t op_id = cqe.data;
if (!op_id)
return;
auto it = net.remote_write_ops.find(op_id);
if (it == net.remote_write_ops.end())
return;
op = it->second;
net.remote_write_ops.erase(it);
} else {
// RECV / SEND / WRITE
op = (RdmaOp *)cqe.op_context;
if (!op)
return;
if (cqe.flags & FI_RECV) {
op->recv.recv_size = cqe.len;
} else if (cqe.flags & FI_SEND) {
// Nothing special
} else if (cqe.flags & FI_WRITE) {
// Nothing special
} else {
fprintf(stderr, "Unhandled completion type. cqe.flags=%lx\n", cqe.flags);
std::exit(1);
}
}
if (op->callback)
op->callback(net, *op);
delete op;
}
void Network::PollCompletion() {
// Process completions
struct fi_cq_data_entry cqe[kCompletionQueueReadCount];
for (;;) {
auto ret = fi_cq_read(cq, cqe, kCompletionQueueReadCount);
if (ret > 0) {
for (ssize_t i = 0; i < ret; i++) {
HandleCompletion(*this, cqe[i]);
}
} else if (ret == -FI_EAVAIL) {
struct fi_cq_err_entry err_entry;
ret = fi_cq_readerr(cq, &err_entry, 0);
if (ret < 0) {
fprintf(stderr, "fi_cq_readerr error: %zd (%s)\n", ret,
fi_strerror(-ret));
std::exit(1);
} else if (ret > 0) {
fprintf(stderr, "Failed libfabric operation: %s\n",
fi_cq_strerror(cq, err_entry.prov_errno, err_entry.err_data,
nullptr, 0));
} else {
fprintf(stderr, "fi_cq_readerr returned 0 unexpectedly.\n");
std::exit(1);
}
} else if (ret == -FI_EAGAIN) {
// No more completions
break;
} else {
fprintf(stderr, "fi_cq_read error: %zd (%s)\n", ret, fi_strerror(-ret));
std::exit(1);
}
}
// Try to make progress.
ProgressPendingOps();
}
enum class AppMessageType : uint8_t {
kConnect = 0,
kRandomFill = 1,
};
struct AppMessageBase {
AppMessageType type;
};
struct AppConnectMessage {
struct MemoryRegion {
uint64_t addr;
uint64_t size;
uint64_t rkey;
};
AppMessageBase base;
size_t num_gpus;
size_t num_nets;
size_t num_mr;
EfaAddress &net_addr(size_t index) {
CHECK(index < num_nets);
return ((EfaAddress *)((uintptr_t)&base + sizeof(*this)))[index];
}
MemoryRegion &mr(size_t index) {
CHECK(index < num_mr);
return ((MemoryRegion *)((uintptr_t)&base + sizeof(*this) +
num_nets * sizeof(EfaAddress)))[index];
}
size_t MessageBytes() const {
return sizeof(*this) + num_nets * sizeof(EfaAddress) +
num_mr * sizeof(MemoryRegion);
}
};
struct AppRandomFillMessage {
AppMessageBase base;
uint32_t remote_context;
uint64_t seed;
size_t page_size;
size_t num_pages;
uint32_t &page_idx(size_t index) {
CHECK(index < num_pages);
return ((uint32_t *)((uintptr_t)&base + sizeof(*this)))[index];
}
size_t MessageBytes() const {
return sizeof(*this) + num_pages * sizeof(uint32_t);
}
};
std::vector<uint8_t> RandomBytes(uint64_t seed, size_t size) {
CHECK(size % sizeof(uint64_t) == 0);
std::vector<uint8_t> buf(size);
std::mt19937_64 gen(seed);
std::uniform_int_distribution<uint64_t> dist;
for (size_t i = 0; i < size; i += sizeof(uint64_t)) {
*(uint64_t *)(buf.data() + i) = dist(gen);
}
return buf;
}
uint64_t SumU64x8AVX2(const std::array<uint64_t, 8> &arr) {
// https://www.perplexity.ai/search/c-sum-over-a-std-array-uint64-p8.MLN_mQeOwoik79acOxg
__m256i sum_vec = _mm256_loadu_si256((const __m256i *)arr.data());
sum_vec = _mm256_add_epi64(
sum_vec, _mm256_loadu_si256((const __m256i *)(arr.data() + 4)));
__m128i sum_128 = _mm_add_epi64(_mm256_extracti128_si256(sum_vec, 0),
_mm256_extracti128_si256(sum_vec, 1));
return _mm_extract_epi64(sum_128, 0) + _mm_extract_epi64(sum_128, 1);
}
long TimeDeltaNanos(
const std::chrono::time_point<std::chrono::high_resolution_clock> &start,
const std::chrono::time_point<std::chrono::high_resolution_clock> &end) {
return std::chrono::duration_cast<std::chrono::nanoseconds>(end - start)
.count();
}
struct RandomFillRequestState {
enum class State {
kWaitRequest,
kPostWarmup,
kWaitWarmup,
kWrite,
kDone,
};
struct WriteState {
bool warmup_posted = false;
size_t i_repeat = 0;
size_t i_buf = 0;
size_t i_page = 0;
};
std::vector<Network> *nets;
std::vector<NetworkGroup> *net_groups;
std::vector<Buffer> *cuda_bufs;
size_t total_bw = 0;
std::atomic<State> state = State::kWaitRequest;
AppConnectMessage *connect_msg = nullptr;
AppRandomFillMessage *request_msg = nullptr;
size_t total_repeat = 0;
size_t nets_per_gpu = 0;
size_t buf_per_gpu = 0;
std::vector<std::array<fi_addr_t, kMaxNetworksPerGroup>> remote_addrs;
std::vector<WriteState> write_states;
std::atomic<size_t> posted_warmups = 0;
std::atomic<size_t> cnt_warmups = 0;
size_t total_write_ops_per_gpu = 0;
size_t total_write_ops = 0;
size_t write_op_size = 0;
std::array<uint64_t, 8> posted_write_ops;
std::array<uint64_t, 8> finished_write_ops;
std::atomic<size_t> cnt_finished_gpus = 0;
std::chrono::time_point<std::chrono::high_resolution_clock> write_start_at;
RandomFillRequestState(std::vector<Network> *nets,
std::vector<NetworkGroup> *net_groups,
std::vector<Buffer> *cuda_bufs)
: nets(nets), net_groups(net_groups), cuda_bufs(cuda_bufs) {
for (auto &net : *nets) {
total_bw += net.fi->nic->link_attr->speed;
}
}
void OnRecv(Network &net, RdmaOp &op) {
if (!connect_msg) {
HandleConnect(net, op);
} else {
HandleRequest(net, op);
}
}
void HandleConnect(Network &net, RdmaOp &op) {
auto *base_msg = (AppMessageBase *)op.recv.buf->data;
CHECK(base_msg->type == AppMessageType::kConnect);
CHECK(op.recv.recv_size >= sizeof(AppConnectMessage));
auto &msg = *(AppConnectMessage *)base_msg;
CHECK(op.recv.recv_size == msg.MessageBytes());
CHECK(msg.num_mr > 0);
printf("Received CONNECT message from client: num_gpus=%zu, "
"num_nets=%zu, num_mr=%zu\n",
msg.num_gpus, msg.num_nets, msg.num_mr);
// Save the message. Note that we don't reuse the buffer.
connect_msg = &msg;
// Assuming remote has the same number of GPUs and NICs.
CHECK(msg.num_gpus == cuda_bufs->size());
CHECK(msg.num_nets == nets->size());