-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathproxy.cpp
More file actions
1557 lines (1423 loc) · 51.4 KB
/
Copy pathproxy.cpp
File metadata and controls
1557 lines (1423 loc) · 51.4 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 "proxy.hpp"
#include "bench_utils.hpp"
#include "d2h_queue_host.hpp"
#include "ep_util.hpp"
#include "rdma.hpp"
#include "rdma_util.hpp"
#include "util/util.h"
#include <arpa/inet.h> // for htonl, ntohl
#include <chrono>
#include <cstdlib>
#include <thread>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#ifndef USE_SUBSET_BARRIER
static std::string shm_name_for_barrier(std::string const& ip,
bool use_normal_mode, int thread_idx) {
// Include UID to avoid cross-user collisions on /dev/shm which cause EACCES
// when a leftover object is owned by a different user.
// Include mode ("ht" for high-throughput vs "ll" for low-latency) so that
// two coexisting Buffers on the same node (e.g. one HT + one LL) get
// distinct barrier namespaces; otherwise their proxy threads (sharing
// thread_idx 0..N-1) would collide on /dev/shm and corrupt each other's
// LocalBarrier counters.
uid_t uid = getuid();
return "/uccl_barrier_" + ip + "_uid" + std::to_string(uid) +
(use_normal_mode ? "_ht" : "_ll") + "_th" + std::to_string(thread_idx);
}
LocalBarrier* map_local_barrier_shm(std::string const& name, bool* out_owner) {
*out_owner = false;
size_t const kSize = sizeof(LocalBarrier);
mode_t const kMode = 0600;
int fd = shm_open(name.c_str(), O_RDWR | O_CREAT | O_EXCL, kMode);
if (fd >= 0) {
*out_owner = true;
if (ftruncate(fd, kSize) != 0) {
perror("ftruncate(LocalBarrier)");
close(fd);
shm_unlink(name.c_str());
return nullptr;
}
} else {
if (errno != EEXIST) {
perror("shm_open");
return nullptr;
}
fd = shm_open(name.c_str(), O_RDWR, kMode);
if (fd < 0) {
perror("shm_open(existing)");
return nullptr;
}
struct stat st {};
int tries = 1000;
while (tries-- > 0) {
if (fstat(fd, &st) == 0 && static_cast<size_t>(st.st_size) >= kSize)
break;
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
if (tries < 0) {
fprintf(stderr,
"map_local_barrier_shm: existing shm '%s' never sized to %zu\n",
name.c_str(), kSize);
close(fd);
return nullptr;
}
}
void* p = mmap(nullptr, kSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
int saved_errno = errno;
close(fd);
if (p == MAP_FAILED) {
errno = saved_errno;
perror("mmap(LocalBarrier)");
return nullptr;
}
return reinterpret_cast<LocalBarrier*>(p);
}
void unmap_local_barrier_shm(std::string const& name, LocalBarrier* lb,
bool owner) {
if (lb) munmap(lb, sizeof(LocalBarrier));
if (owner) shm_unlink(name.c_str());
}
#endif
Proxy::Proxy(Config const& cfg) : cfg_(cfg) {
// Initialize state tracking for each ring buffer
listen_port_ = uccl::create_listen_socket(&listen_fd_);
#ifndef USE_MSCCLPP_FIFO_BACKEND
ring_tails_.resize(cfg_.d2h_queues.size(), 0);
ring_seen_.resize(cfg_.d2h_queues.size(), 0);
#endif
}
double Proxy::avg_rdma_write_us() const {
if (kIterations == 0) return 0.0;
return total_rdma_write_durations_.count() / static_cast<double>(kIterations);
}
double Proxy::avg_wr_latency_us() const {
if (completion_count_ <= kWarmupOps) return 0.0;
return static_cast<double>(wr_time_total_us_) /
static_cast<double>(completion_count_ - kWarmupOps);
}
uint64_t Proxy::completed_wr() const { return completion_count_; }
void Proxy::pin_thread_to_cpu_wrapper() {
if (cfg_.pin_thread) {
// TODO(MaoZiming): improves pinning.
// Offset LL-mode proxies onto a separate CPU range so a high-throughput
// Buffer and an LL-mode Buffer running in the same process don't fight
// for the same cores. Range size = kNumProxyThs * UCCL_MAX_LOCAL_RANKS.
int const mode_offset =
cfg_.use_normal_mode ? 0 : (kNumProxyThs * UCCL_MAX_LOCAL_RANKS);
pin_thread_to_cpu(mode_offset + cfg_.thread_idx +
cfg_.local_rank * kNumProxyThs);
int cpu = sched_getcpu();
if (cpu == -1) {
perror("sched_getcpu");
} else {
printf(
"Local CPU thread pinned to core %d, thread_idx: %d, "
"local_rank: %d, mode: %s\n",
cpu, cfg_.thread_idx, cfg_.local_rank,
cfg_.use_normal_mode ? "high_throughput" : "low_latency");
}
}
}
void Proxy::pin_thread_to_numa_wrapper() {
if (cfg_.pin_thread) {
assert(ctx_.numa_node != -1);
pin_thread_unique(ctx_.numa_node, cfg_.local_rank, cfg_.thread_idx,
kNumProxyThs);
// Get the actual CPU this thread is running on
int cpu = sched_getcpu();
// Get the affinity mask (optional but useful)
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
printf(
"Local CPU thread pinned to NUMA node %d, thread_idx: %d, local_rank: "
"%d, "
"running on CPU %d.\n",
ctx_.numa_node, cfg_.thread_idx, cfg_.local_rank, cpu);
}
}
void Proxy::set_peers_meta(std::vector<PeerMeta> const& peers) {
peers_.clear();
peers_.reserve(peers.size());
for (auto const& p : peers) {
peers_.push_back(p);
}
ctxs_for_all_ranks_.clear();
ctxs_for_all_ranks_.resize(peers.size());
for (size_t i = 0; i < peers.size(); ++i) {
ctxs_for_all_ranks_[i] = std::make_unique<ProxyCtx>();
ctxs_for_all_ranks_[i]->udp_sport_base = get_udp_sport_base_from_env();
}
}
void Proxy::set_bench_d2h_channel_addrs(std::vector<uintptr_t> const& addrs) {
#ifndef USE_MSCCLPP_FIFO_BACKEND
ring_tails_.clear();
ring_seen_.clear();
ring_tails_.resize(addrs.size(), 0);
ring_seen_.resize(addrs.size(), 0);
#endif
cfg_.d2h_queues.clear();
cfg_.d2h_queues.reserve(addrs.size());
for (auto addr : addrs) {
d2hq::HostD2HHandle h{};
d2hq::init_from_addr(h, addr); // unified initialization
cfg_.d2h_queues.push_back(h);
}
}
void Proxy::init_common() {
int const my_rank = cfg_.rank;
per_thread_rdma_init(ctx_, cfg_.gpu_buffer, cfg_.total_size, my_rank,
cfg_.thread_idx, cfg_.local_rank);
pin_thread_to_numa_wrapper();
if (!get_cq(ctx_)) {
(void)create_per_thread_comp_channel(ctx_);
(void)create_per_thread_cq(ctx_);
}
// Register atomic_buffer_ptr as a separate RDMA memory region if it was set
// This must be done after PD is initialized by per_thread_rdma_init
// TODO(MaoZiming): Skip registering for EFA.
if (atomic_buffer_ptr_ && !ctx_.atomic_buffer_mr) {
ctx_.atomic_buffer_mr =
ibv_reg_mr(ctx_.pd, atomic_buffer_ptr_, kAtomicBufferSize,
IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE |
#ifdef EFA
IBV_ACCESS_REMOTE_READ
#else
IBV_ACCESS_REMOTE_READ | IBV_ACCESS_REMOTE_ATOMIC
#endif
);
if (!ctx_.atomic_buffer_mr) {
perror("Failed to register atomic_buffer_ptr MR");
std::abort();
}
fprintf(stderr,
"[Proxy] Registered atomic_buffer_ptr MR: addr=0x%llx, len=%zu, "
"rkey=0x%x\n",
(unsigned long long)ctx_.atomic_buffer_mr->addr,
(size_t)ctx_.atomic_buffer_mr->length, ctx_.atomic_buffer_mr->rkey);
}
if (ctxs_for_all_ranks_.empty()) {
fprintf(stderr,
"Error: peers metadata not set before init_common (peers_.size() "
"=%zu)\n",
peers_.size());
std::abort();
}
// Allocate/register local scratch buffer for native RDMA atomics.
// NOTE: This must NOT alias cfg_.gpu_buffer (which is used for other
// layouts). For IBV_WR_ATOMIC_FETCH_AND_ADD the NIC DMA-writes the old value
// here.
if (!ctx_.atomic_old_values_buf || !ctx_.atomic_old_values_mr) {
size_t const atomic_buf_size = ProxyCtx::kMaxAtomicOps * sizeof(uint64_t);
void* p = nullptr;
int rc = posix_memalign(&p, /*alignment=*/8, atomic_buf_size);
if (rc != 0 || !p) {
fprintf(stderr,
"posix_memalign failed for atomic_old_values_buf (rc=%d)\n", rc);
std::abort();
}
std::memset(p, 0, atomic_buf_size);
ctx_.atomic_old_values_buf = static_cast<uint64_t*>(p);
ctx_.atomic_old_values_mr =
ibv_reg_mr(ctx_.pd, ctx_.atomic_old_values_buf, atomic_buf_size,
IBV_ACCESS_LOCAL_WRITE);
if (!ctx_.atomic_old_values_mr) {
perror("Failed to register atomic_old_values_buf MR");
std::abort();
}
}
int num_ranks = ctxs_for_all_ranks_.size();
local_infos_.assign(num_ranks, RDMAConnectionInfo{});
remote_infos_.assign(num_ranks, RDMAConnectionInfo{});
#ifdef EFA
// SRD is connectionless: create one set of QPs on ctx_ and alias them into
// each peer ctx; per-peer dst_ah selects the destination per WR.
RDMAConnectionInfo template_local_info{};
if (!ctx_.qp) {
create_per_thread_qp(
ctx_, cfg_.gpu_buffer, cfg_.total_size, &template_local_info, cfg_.rank,
cfg_.d2h_queues.size(), cfg_.use_normal_mode, atomic_buffer_ptr_);
// Pre-post recv WRs once on the shared recv_ack_qp, sized for all peers.
int num_active_peers = 0;
for (int p = 0; p < num_ranks; ++p) {
if (p == my_rank) continue;
if (peers_[p].ip == peers_[my_rank].ip) continue;
if (cfg_.use_normal_mode && std::abs(p - my_rank) % MAX_NUM_GPUS != 0)
continue;
++num_active_peers;
}
int const ack_depth =
std::min(static_cast<int>(kMaxOutstandingRecvs),
kSenderAckQueueDepth * std::max(1, num_active_peers));
local_post_ack_buf(ctx_, ack_depth);
}
#endif
uint32_t next_tag = 1;
ctx_by_tag_.clear();
ctx_by_tag_.resize(ctxs_for_all_ranks_.size() + 1, nullptr);
// Per peer QP initialization
for (int peer = 0; peer < num_ranks; ++peer) {
auto& c = *ctxs_for_all_ranks_[peer];
c.tag = next_tag++;
if (c.tag >= ctx_by_tag_.size()) ctx_by_tag_.resize(c.tag + 1, nullptr);
ctx_by_tag_[c.tag] = &c;
c.context = ctx_.context;
c.pd = ctx_.pd;
c.mr = ctx_.mr;
c.rkey = ctx_.rkey;
#ifdef USE_DMABUF
c.gpu_mr_chunks = ctx_.gpu_mr_chunks;
#endif
// Share cq/pd/mr; non-EFA QPs are per-peer, EFA QPs are aliased below.
c.cq = ctx_.cq;
c.cq_ex = ctx_.cq_ex;
// Share the atomic buffer MR with peer contexts
c.atomic_buffer_mr = ctx_.atomic_buffer_mr;
// Share local atomic scratch buffer MR (used for native atomics)
c.atomic_old_values_buf = ctx_.atomic_old_values_buf;
c.atomic_old_values_mr = ctx_.atomic_old_values_mr;
if (peer == my_rank) continue;
// Skip rdma connection for intra-node.
if (peers_[peer].ip == peers_[my_rank].ip) continue;
if (cfg_.use_normal_mode && std::abs(peer - my_rank) % MAX_NUM_GPUS != 0)
continue;
#ifdef EFA
// Alias the shared SRD QPs from ctx_; dst_ah/dst_qpn (set later in
// modify_qp_to_rtr) routes per WR via ibv_wr_set_ud_addr.
c.qp = ctx_.qp;
c.ack_qp = ctx_.ack_qp;
c.recv_ack_qp = ctx_.recv_ack_qp;
c.data_qps_by_channel = ctx_.data_qps_by_channel;
c.gid_index = ctx_.gid_index;
c.qps_are_shared = true;
// Advertised local info is identical for all peers (shared QPs/MR/GID).
local_infos_[peer] = template_local_info;
#else
create_per_thread_qp(c, cfg_.gpu_buffer, cfg_.total_size,
&local_infos_[peer], my_rank, cfg_.d2h_queues.size(),
cfg_.use_normal_mode, atomic_buffer_ptr_);
#endif
modify_qp_to_init(c);
}
usleep(50 * 1000);
// Out-of-band exchange info per pair: start receiver thread first
std::thread receiver_thread([this, num_ranks, my_rank]() {
for (int peer = 0; peer < num_ranks; ++peer) {
// Skip rdma connection for intra-node.
if (peer == my_rank || peers_[peer].ip == peers_[my_rank].ip ||
(cfg_.use_normal_mode &&
std::abs(peer - my_rank) % MAX_NUM_GPUS != 0))
continue;
int actual_peer;
recv_connection_info_as_server(my_rank, &actual_peer, listen_fd_,
remote_infos_.data());
}
});
// Then send our info to all peers
for (int peer = 0; peer < num_ranks; ++peer) {
if (peer == my_rank || peers_[peer].ip == peers_[my_rank].ip ||
(cfg_.use_normal_mode && std::abs(peer - my_rank) % MAX_NUM_GPUS != 0))
continue;
char const* peer_ip = peers_[peer].ip.c_str();
int const peer_listen_port = peers_[peer].listen_ports[cfg_.thread_idx];
send_connection_info_as_client(my_rank, peer, peer_ip, peer_listen_port,
&local_infos_[peer]);
}
// Wait for receiver thread to finish
receiver_thread.join();
// Verify remote info correctness
for (int peer = 0; peer < num_ranks; ++peer) {
if (peer == my_rank || peers_[peer].ip == peers_[my_rank].ip ||
(cfg_.use_normal_mode && std::abs(peer - my_rank) % MAX_NUM_GPUS != 0))
continue;
if (remote_infos_[peer].addr != peers_[peer].ptr) {
fprintf(stderr,
"Rank %d thread %d: Warning: remote addr mismatch for peer %d: "
"got 0x%lx, expected 0x%lx\n",
my_rank, cfg_.thread_idx, peer, remote_infos_[peer].addr,
peers_[peer].ptr);
std::abort();
}
}
// Bring each per-peer QP to RTR/RTS
for (int peer = 0; peer < num_ranks; ++peer) {
if (peer == my_rank) continue;
// Skip rdma connection for intra-node.
if (peers_[peer].ip == peers_[my_rank].ip) continue;
if (cfg_.use_normal_mode && std::abs(peer - my_rank) % MAX_NUM_GPUS != 0)
continue;
auto& c = *ctxs_for_all_ranks_[peer];
// qp is different from each rank.
modify_qp_to_rtr(c, &remote_infos_[peer], cfg_.use_normal_mode);
modify_qp_to_rts(c, &local_infos_[peer]);
c.remote_addr = remote_infos_[peer].addr;
c.remote_rkey = remote_infos_[peer].rkey;
c.remote_len = remote_infos_[peer].len;
#ifdef USE_DMABUF
// Populate remote MR chunks from exchanged connection info.
if (remote_infos_[peer].num_mr_chunks > 0) {
c.remote_mr_chunks.resize(remote_infos_[peer].num_mr_chunks);
for (uint32_t ci = 0; ci < remote_infos_[peer].num_mr_chunks; ++ci) {
c.remote_mr_chunks[ci] = {remote_infos_[peer].mr_chunk_info[ci].addr,
remote_infos_[peer].mr_chunk_info[ci].len,
remote_infos_[peer].mr_chunk_info[ci].rkey};
}
if (cfg_.thread_idx == 0) {
fprintf(stderr, "[Proxy] Received %u remote MR chunks from peer %d:\n",
remote_infos_[peer].num_mr_chunks, peer);
for (uint32_t ci = 0; ci < remote_infos_[peer].num_mr_chunks; ++ci) {
fprintf(stderr, " [%u] base=0x%llx len=%zu rkey=0x%x\n", ci,
(unsigned long long)c.remote_mr_chunks[ci].base,
c.remote_mr_chunks[ci].len, c.remote_mr_chunks[ci].rkey);
}
}
} else {
if (cfg_.thread_idx == 0) {
fprintf(stderr,
"[Proxy] Peer %d has num_mr_chunks=0 (single MR: rkey=0x%x)\n",
peer, remote_infos_[peer].rkey);
}
}
#endif
// Set remote atomic buffer info from exchanged connection info
c.remote_atomic_buffer_addr = remote_infos_[peer].atomic_buffer_addr;
c.remote_atomic_buffer_len = remote_infos_[peer].atomic_buffer_len;
c.remote_atomic_buffer_rkey = remote_infos_[peer].atomic_buffer_rkey;
if (c.remote_atomic_buffer_addr == 0) {
fprintf(
stderr,
"[Proxy] WARNING: Remote atomic buffer not registered for peer %d "
"(local atomic_buffer_ptr=%p, local atomic_buffer_mr=%p)\n",
peer, atomic_buffer_ptr_, (void*)ctx_.atomic_buffer_mr);
} else {
fprintf(stderr,
"[Proxy] Remote atomic buffer info for peer %d: addr=0x%llx, "
"len=%zu, rkey=0x%x\n",
peer, (unsigned long long)c.remote_atomic_buffer_addr,
(size_t)c.remote_atomic_buffer_len, c.remote_atomic_buffer_rkey);
}
}
usleep(50 * 1000);
if (cfg_.use_normal_mode) {
// if (cfg_.thread_idx != 0) {
// return;
// }
// Discover local ranks (same IP as me)
std::string const my_ip = peers_[cfg_.rank].ip;
std::vector<int> local_ranks;
local_ranks.reserve(ctxs_for_all_ranks_.size());
int leader_rank = cfg_.rank;
for (int r = 0; r < (int)peers_.size(); ++r) {
if (peers_[r].ip == my_ip) {
local_ranks.push_back(r);
if (r < leader_rank) leader_rank = r;
}
}
ctx_.num_local_ranks = (int)local_ranks.size();
ctx_.node_leader_rank = leader_rank;
ctx_.local_rank = cfg_.local_rank;
ctx_.thread_idx = cfg_.thread_idx;
if (ctx_.num_local_ranks > UCCL_MAX_LOCAL_RANKS) {
fprintf(stderr, "num_local_ranks=%d exceeds UCCL_MAX_LOCAL_RANKS=%d\n",
ctx_.num_local_ranks, (int)UCCL_MAX_LOCAL_RANKS);
std::abort();
}
#ifndef USE_SUBSET_BARRIER
std::string const shm_name =
shm_name_for_barrier(my_ip, cfg_.use_normal_mode, cfg_.thread_idx);
ctx_.lb = map_local_barrier_shm(shm_name, &ctx_.lb_owner);
if (!ctx_.lb) {
fprintf(stderr, "Failed to map local barrier shm: %s\n",
shm_name.c_str());
std::abort();
}
if (ctx_.lb_owner) {
ctx_.lb->full_mask = (ctx_.num_local_ranks >= 64)
? ~0ULL
: ((1ULL << ctx_.num_local_ranks) - 1ULL);
for (int i = 0; i < ctx_.num_local_ranks; ++i) {
ctx_.lb->arrive_seq[i].store(0, std::memory_order_relaxed);
ctx_.lb->release_seq[i].store(0, std::memory_order_relaxed);
}
} else {
while (ctx_.lb->full_mask == 0ULL) cpu_relax();
}
#endif
}
#ifdef USE_MSCCLPP_FIFO_BACKEND
fifo_seq_.assign(cfg_.d2h_queues.size(), 0);
fifo_pending_.assign(cfg_.d2h_queues.size(),
std::deque<std::pair<uint64_t, size_t>>{});
#endif
}
void Proxy::init_sender() {
init_common();
assert(cfg_.rank == 0);
auto& ctx_ptr = ctxs_for_all_ranks_[1];
#ifndef EFA
local_post_ack_buf(*ctx_ptr, kSenderAckQueueDepth);
#else
// EFA: posted once on the shared recv_ack_qp in init_common.
(void)ctx_ptr;
#endif
}
void Proxy::init_remote() {
init_common();
assert(cfg_.rank == 1);
auto& ctx_ptr = ctxs_for_all_ranks_[0];
#ifndef EFA
local_post_ack_buf(*ctx_ptr, kSenderAckQueueDepth);
#endif
remote_reg_ack_buf(ctx_ptr->pd, ring.ack_buf, ring.ack_mr);
ring.ack_qp = ctx_ptr->ack_qp;
#ifndef EFA
post_receive_buffer_for_imm(*ctx_ptr);
#endif
}
void Proxy::run_sender() {
printf("CPU sender thread %d started\n", cfg_.thread_idx);
init_sender();
size_t seen = 0;
uint64_t my_tail = 0;
while (ctx_.progress_run.load(std::memory_order_acquire)) {
local_poll_completions(ctx_, acked_wrs_, cfg_.thread_idx, ctx_by_tag_);
notify_gpu_completion(my_tail);
post_gpu_command(my_tail, seen);
}
}
void Proxy::run_remote() {
printf("Remote CPU thread %d started\n", cfg_.thread_idx);
init_remote();
std::set<PendingUpdate> pending_atomic_updates;
while (ctx_.progress_run.load(std::memory_order_acquire)) {
remote_poll_completions(ctx_, cfg_.thread_idx, ring, ctx_by_tag_,
atomic_buffer_ptr_, cfg_.num_ranks,
cfg_.num_experts, pending_atomic_updates, cfg_.rank,
cfg_.num_nodes, cfg_.use_normal_mode);
#ifdef USE_RECEIVER_BARRIER
if (!cfg_.use_normal_mode) {
apply_pending_updates(ctx_, pending_atomic_updates, atomic_buffer_ptr_,
cfg_.num_experts, cfg_.num_ranks);
}
#endif
}
}
void Proxy::run_dual() {
init_common();
for (int peer = 0; peer < (int)ctxs_for_all_ranks_.size(); ++peer) {
if (peer == cfg_.rank) continue;
if (peers_[peer].ip == peers_[cfg_.rank].ip) continue;
if (cfg_.use_normal_mode && std::abs(peer - cfg_.rank) % MAX_NUM_GPUS != 0)
continue;
auto& ctx_ptr = ctxs_for_all_ranks_[peer];
if (!ctx_ptr) continue;
#ifndef EFA
// EFA: posted once on the shared recv_ack_qp in init_common.
local_post_ack_buf(*ctx_ptr, kSenderAckQueueDepth);
#endif
remote_reg_ack_buf(ctx_ptr->pd, ring.ack_buf, ring.ack_mr);
ring.ack_qp = ctx_ptr->ack_qp;
#ifndef EFA
post_receive_buffer_for_imm(*ctx_ptr);
#endif
}
uint64_t my_tail = 0;
size_t seen = 0;
std::set<PendingUpdate> pending_atomic_updates;
adaptive_sleeper_.init_timer();
while (ctx_.progress_run.load(std::memory_order_acquire)) {
adaptive_sleeper_.maybe_sleep(ctx_);
poll_cq_dual(ctx_, acked_wrs_, cfg_.thread_idx, ring, ctx_by_tag_,
atomic_buffer_ptr_, cfg_.num_ranks, cfg_.num_experts,
pending_atomic_updates, cfg_.rank, cfg_.num_nodes,
cfg_.use_normal_mode);
notify_gpu_completion(my_tail);
post_gpu_command(my_tail, seen);
#ifdef USE_RECEIVER_BARRIER
if (!cfg_.use_normal_mode) {
apply_pending_updates(ctx_, pending_atomic_updates, atomic_buffer_ptr_,
cfg_.num_experts, cfg_.num_ranks);
}
#endif
#ifdef USE_SENDER_BARRIER
if (!cfg_.use_normal_mode) {
auto postponed_wr_ids = postponed_wr_ids_;
auto postponed_atomics = postponed_atomics_;
postponed_wr_ids_.clear();
postponed_atomics_.clear();
assert(postponed_wr_ids.size() == postponed_atomics.size());
assert(postponed_wr_ids_.size() == 0);
post_gpu_commands_mixed(postponed_wr_ids, postponed_atomics);
}
#endif
if (cfg_.use_normal_mode) {
barrier_check();
}
}
}
void Proxy::notify_gpu_completion(uint64_t& my_tail) {
if (acked_wrs_.empty()) return;
// Mark all acked command slots in each ring's bitmask
#ifdef USE_MSCCLPP_FIFO_BACKEND
// FIFO path: pop in order using the pending deque and the completion set.
for (size_t rb_idx = 0; rb_idx < cfg_.d2h_queues.size(); ++rb_idx) {
auto* fifo = cfg_.d2h_queues[rb_idx].fifo;
if (!fifo) continue;
auto& pend = fifo_pending_[rb_idx];
while (!pend.empty()) {
auto [front_wr, front_bytes] = pend.front();
if (acked_wrs_.find(front_wr) == acked_wrs_.end()) break;
acked_wrs_.erase(front_wr); // consume this completion
pend.pop_front(); // retire pending entry
if (front_bytes) {
current_inflight_bytes.fetch_sub(front_bytes,
std::memory_order_release);
}
if (ctx_.quiet_wr != -1 && front_wr == (uint64_t)ctx_.quiet_wr) {
ctx_.quiet_inflight = false;
ctx_.quiet_wr = -1;
fifo->pop();
}
if (ctx_.barrier_wr != -1 && front_wr == (uint64_t)ctx_.barrier_wr) {
ctx_.barrier_inflight = false;
ctx_.barrier_wr = -1;
fifo->pop();
}
}
}
#else
for (auto wr_id : acked_wrs_) {
size_t const rb_idx = (wr_id >> 32) & 0xFFFFFFFF;
size_t const cmd_idx = wr_id & 0xFFFFFFFF;
if (rb_idx >= cfg_.d2h_queues.size()) {
fprintf(stderr, "Invalid rb_idx %zu in acked_wrs_\n", rb_idx);
continue;
}
d2hq::HostD2HHandle* h = &cfg_.d2h_queues[rb_idx];
h->volatile_clear_cmd_type(cmd_idx);
h->mark_acked(cmd_idx % h->capacity());
}
acked_wrs_.clear();
// Advance tails for each ring buffer based on contiguous acked bits
for (size_t rb_idx = 0; rb_idx < cfg_.d2h_queues.size(); ++rb_idx) {
d2hq::HostD2HHandle* h = &cfg_.d2h_queues[rb_idx];
ring_tails_[rb_idx] = h->advance_tail_from_mask();
}
#endif
}
void Proxy::post_gpu_command(uint64_t& my_tail, size_t& seen) {
// Multi-ring buffer processing: collect commands from all ring buffers
// Process each ring buffer (similar to test_multi_ring_throughput.cu)
for (size_t rb_idx = 0; rb_idx < cfg_.d2h_queues.size(); rb_idx++) {
d2hq::HostD2HHandle* h = &cfg_.d2h_queues[rb_idx];
#ifdef USE_MSCCLPP_FIFO_BACKEND
assert(h && "h is empty!\n");
assert(h->fifo && "h->fifo is empty!\n");
// FIFO path: one trigger == one command. Do NOT pop yet.
auto* fifo = h->fifo;
if (!fifo) continue;
// Available budget for this FIFO, constrainted by the 32bit imm.
size_t pending = fifo_pending_[rb_idx].size();
size_t kMaxInflight = cfg_.use_normal_mode ? get_max_inflight_normal()
: get_max_inflight_low_latency();
size_t budget = (kMaxInflight > pending) ? (kMaxInflight - pending) : 0;
size_t max_inflight_bytes = get_max_inflight_bytes() / kNumProxyThs;
// Pop commands from the FIFO until the budget is reached or the max
// inflight bytes is reached.
for (size_t take = 0; take < budget; ++take) {
if (current_inflight_bytes.load(std::memory_order_acquire) >=
max_inflight_bytes) {
break;
}
auto trig = fifo->poll();
if (trig.fst == 0) break;
TransferCmd cmd = d2hq::decode_from_trigger(trig);
/* For some reason, this is important for correctness */
/* It cannot be if (ctx_.barrier_inflight) */
if (get_base_cmd(cmd.cmd_type) == CmdType::BARRIER &&
ctx_.barrier_inflight) {
break;
}
if (get_base_cmd(cmd.cmd_type) == CmdType::QUIET && ctx_.quiet_inflight) {
break;
}
uint64_t unique_wr_id = (static_cast<uint64_t>(rb_idx) << 32) |
(fifo_seq_[rb_idx]++ & 0xFFFFFFFFULL);
wrs_to_post.push_back(unique_wr_id);
cmds_to_post.push_back(cmd);
fifo_pending_[rb_idx].push_back(
std::make_pair(unique_wr_id, static_cast<size_t>(cmd.bytes)));
if (get_base_cmd(cmd.cmd_type) == CmdType::WRITE && cmd.bytes > 0) {
current_inflight_bytes.fetch_add(static_cast<size_t>(cmd.bytes),
std::memory_order_release);
}
if (get_base_cmd(cmd.cmd_type) == CmdType::BARRIER ||
get_base_cmd(cmd.cmd_type) == CmdType::QUIET) {
if (get_base_cmd(cmd.cmd_type) == CmdType::BARRIER) {
assert(!ctx_.barrier_inflight);
assert(ctx_.barrier_wr == -1);
ctx_.barrier_inflight = true;
} else if (get_base_cmd(cmd.cmd_type) == CmdType::QUIET) {
assert(!ctx_.quiet_inflight);
assert(ctx_.quiet_wr == -1);
ctx_.quiet_inflight = true;
}
break;
} else {
// Other operations just pop.
fifo->pop();
}
}
#else
uint64_t& ring_tail = ring_tails_[rb_idx];
size_t& ring_seen = ring_seen_[rb_idx];
// Force load head from DRAM (like original code)
uint64_t cur_head = h->volatile_head();
if (cur_head == ring_tail) {
continue; // No new work in this ring
}
// Batch processing for this ring buffer
size_t batch_size = cur_head - ring_seen;
if (batch_size == 0) continue;
// Reserve space for new commands
size_t current_size = wrs_to_post.size();
wrs_to_post.reserve(current_size + batch_size);
cmds_to_post.reserve(current_size + batch_size);
// Collect batch of commands from this ring buffer
for (size_t i = ring_seen; i < cur_head; ++i) {
CmdType cmd = h->volatile_load_cmd_type(i);
// NOTE(MaoZiming): Non-blocking. prevent local and remote both while
// loop.
if (cmd == CmdType::EMPTY) break;
TransferCmd& cmd_entry = h->load_cmd_entry(i);
if (cmd_entry.cmd_type == CmdType::EMPTY) break;
if (get_base_cmd(cmd_entry.cmd_type) == CmdType::WRITE ||
get_base_cmd(cmd_entry.cmd_type) == CmdType::ATOMIC) {
if (static_cast<int>(cmd_entry.dst_rank) == cfg_.rank) {
fprintf(stderr,
"[ERROR] Local command!, cmd.dst_rank: %d, cfg_.rank: %d, "
"cmd_entry.cmd_type: %d, cur_head: %lu, i: %lu\n",
cmd_entry.dst_rank, cfg_.rank,
static_cast<int>(cmd_entry.cmd_type), cur_head, i);
cudaDeviceSynchronize();
std::abort();
}
if (peers_[cmd_entry.dst_rank].ip == peers_[cfg_.rank].ip) {
fprintf(
stderr,
"[ERROR] Intra-node command!, cmd.dst_rank: %d, cfg_.rank: %d, "
"cmd_entry.cmd_type: %d, cur_head: %lu, i: %lu\n",
cmd_entry.dst_rank, cfg_.rank,
static_cast<int>(cmd_entry.cmd_type), cur_head, i);
cudaDeviceSynchronize();
std::abort();
}
}
// Use a unique ID combining ring buffer index and command index
uint64_t unique_wr_id = (rb_idx << 32) | i;
wrs_to_post.push_back(unique_wr_id);
cmds_to_post.push_back(cmd_entry);
#ifdef MEASURE_PER_VERB_LATENCY
wr_id_to_start_time_[unique_wr_id] =
std::chrono::high_resolution_clock::now();
#endif
ring_seen = i + 1;
}
#endif
}
// Process all collected commands in batch
if (!wrs_to_post.empty()) {
#ifdef MEASURE_PER_OP_LATENCY
auto start = std::chrono::high_resolution_clock::now();
#endif
// Post all commands: writes, atomics, barriers, quiets
post_gpu_commands_mixed(wrs_to_post, cmds_to_post);
#ifdef MEASURE_PER_OP_LATENCY
auto end = std::chrono::high_resolution_clock::now();
total_rdma_write_durations_ +=
std::chrono::duration_cast<std::chrono::microseconds>(end - start);
#endif
}
// This does not deallocate the memory, saving malloc overhead.
wrs_to_post.clear();
cmds_to_post.clear();
}
void Proxy::run_local() {
pin_thread_to_cpu_wrapper();
printf("Local CPU thread %d started with %zu ring buffers\n", cfg_.thread_idx,
cfg_.d2h_queues.size());
if (cfg_.d2h_queues.empty()) {
printf("Error: No ring buffers available for local mode\n");
return;
}
int total_seen = 0;
while (true) {
if (!ctx_.progress_run.load(std::memory_order_acquire)) {
printf("Local thread %d stopping early at total_seen=%d\n",
cfg_.thread_idx, total_seen);
return;
}
bool found_work = false;
// Multi-ring buffer polling (consistent with other modes)
for (size_t rb_idx = 0; rb_idx < cfg_.d2h_queues.size(); rb_idx++) {
d2hq::HostD2HHandle* h = &cfg_.d2h_queues[rb_idx];
#ifdef USE_MSCCLPP_FIFO_BACKEND
auto* fifo = h->fifo;
if (!fifo) continue;
auto trig = fifo->poll();
if (trig.fst != 0) {
d2hq::decode_from_trigger(trig);
fifo->pop();
total_seen++;
found_work = true;
}
#else
uint64_t& ring_tail = ring_tails_[rb_idx];
// Check for new work in this ring buffer
uint64_t cur_head = h->volatile_head();
if (cur_head == ring_tail) {
continue; // No new work in this ring
}
// Process commands from this ring buffer
while (ring_tail < cur_head) {
uint64_t const idx = ring_tail & kQueueMask;
CmdType cmd;
auto last_print = std::chrono::steady_clock::now();
size_t spin_count = 0;
do {
cmd = h->volatile_load_cmd_type(idx);
cpu_relax();
auto now = std::chrono::steady_clock::now();
if (now - last_print > std::chrono::seconds(10)) {
printf(
"Still waiting at thread %d, ring %zu, total_seen=%d, "
"spin_count=%zu, ring_tail=%lu, cmd: %d\n",
cfg_.thread_idx, rb_idx, total_seen, spin_count, ring_tail,
static_cast<int>(cmd));
last_print = now;
spin_count++;
}
if (!ctx_.progress_run.load(std::memory_order_acquire)) {
printf("Local thread %d stopping early at total_seen=%d\n",
cfg_.thread_idx, total_seen);
return;
}
} while (cmd == CmdType::EMPTY);
#ifdef DEBUG_PRINT
printf(
"Local thread %d, ring %zu, total_seen=%d head=%lu tail=%lu "
"consuming cmd=%llu\n",
cfg_.thread_idx, rb_idx, total_seen, h->head, ring_tail,
static_cast<unsigned long long>(cmd));
#endif
std::atomic_thread_fence(std::memory_order_acquire);
// Mark command as processed
h->volatile_clear_cmd_type(idx);
ring_tail++;
h->cpu_volatile_store_tail(ring_tail);
total_seen++;
found_work = true;
// Break to check other ring buffers and progress_run flag
break;
}
#endif
}
// If no work found across all ring buffers, relax CPU
if (!found_work) {
cpu_relax();
}
}
printf("Local thread %d finished %d commands across %zu ring buffers\n",
cfg_.thread_idx, total_seen, cfg_.d2h_queues.size());
}
void Proxy::post_gpu_commands_mixed(
std::vector<uint64_t> const& wrs_to_post,
std::vector<TransferCmd> const& cmds_to_post) {
// Separate atomic operations from regular RDMA writes
std::vector<uint64_t> rdma_wrs, atomic_wrs, quiet_wrs, barrier_wrs;
std::vector<TransferCmd> rdma_cmds, atomic_cmds, quiet_cmds, barrier_cmds;
for (size_t i = 0; i < cmds_to_post.size(); ++i) {
switch (get_base_cmd(cmds_to_post[i].cmd_type)) {
case (CmdType::ATOMIC): {
#ifdef USE_SENDER_BARRIER
if (!cfg_.use_normal_mode) {
int value = cmds_to_post[i].value;
uint32_t offset = static_cast<int64_t>(cmds_to_post[i].req_rptr);
uint32_t new_offset =
offset - get_low_latency(cmds_to_post[i].cmd_type) *
align<size_t>(cfg_.num_experts * sizeof(int), 128);
size_t new_index = new_offset / sizeof(int);
int expected_value;
int expert_idx;
if (get_is_combine(cmds_to_post[i].cmd_type)) {
expert_idx = new_index;
expected_value = ctx_.combine_sent_counter.Get(
{get_low_latency(cmds_to_post[i].cmd_type), expert_idx,
cmds_to_post[i].dst_rank});
} else {
expert_idx = new_index / cfg_.num_ranks;
expected_value = ctx_.dispatch_sent_counter.Get(
{get_low_latency(cmds_to_post[i].cmd_type), expert_idx,
cmds_to_post[i].dst_rank});
value = -value - 1;
}
if (value != expected_value) {
postponed_atomics_.push_back(cmds_to_post[i]);
postponed_wr_ids_.push_back(wrs_to_post[i]);
assert(postponed_atomics_.size() == postponed_wr_ids_.size());
continue;
}
}
#endif
atomic_wrs.push_back(wrs_to_post[i]);
atomic_cmds.push_back(cmds_to_post[i]);
#ifdef USE_SENDER_BARRIER
if (!cfg_.use_normal_mode) {
uint32_t offset = static_cast<int64_t>(cmds_to_post[i].req_rptr);
uint32_t new_offset =
offset - get_low_latency(cmds_to_post[i].cmd_type) *
align<size_t>(cfg_.num_experts * sizeof(int), 128);
size_t new_index = new_offset / sizeof(int);
int expert_idx;
if (get_is_combine(cmds_to_post[i].cmd_type)) {
expert_idx = new_index;
ctx_.combine_sent_counter.Reset(
{get_low_latency(cmds_to_post[i].cmd_type), expert_idx,
cmds_to_post[i].dst_rank});
} else {
expert_idx = new_index / cfg_.num_ranks;
ctx_.dispatch_sent_counter.Reset(
{get_low_latency(cmds_to_post[i].cmd_type), expert_idx,
cmds_to_post[i].dst_rank});
}
}
#endif
break;
}
case (CmdType::WRITE): {
rdma_wrs.push_back(wrs_to_post[i]);
rdma_cmds.push_back(cmds_to_post[i]);
break;
}