-
Notifications
You must be signed in to change notification settings - Fork 26
/
ccl_offload_control.c
executable file
·1416 lines (1282 loc) · 49.8 KB
/
ccl_offload_control.c
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
/*******************************************************************************
# Copyright (C) 2021 Xilinx, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# *******************************************************************************/
#include <stdbool.h>
#include "ccl_offload_control.h"
#ifndef MB_FW_EMULATION
#include "xparameters.h"
#include "mb_interface.h"
#endif
static volatile int use_tcp = 1;
static volatile unsigned int timeout = 1 << 28;
static volatile unsigned int max_segment_size = DMA_MAX_BTT;
static datapath_arith_config arcfg;
static communicator world;
static bool comm_cached = false;
static bool comm_cache_adr;
#ifdef MB_FW_EMULATION
//uint32_t sim_cfgmem[END_OF_EXCHMEM/4];
uint32_t sim_cfgmem[(GPIO_BASEADDR+0x1000)/4];
uint32_t *cfgmem = sim_cfgmem;
hlslib::Stream<ap_axiu<32,0,0,0>, 512> cmd_fifos[4];
hlslib::Stream<ap_axiu<32,0,0,0>, 512> sts_fifos[4];
#else
uint32_t *cfgmem = (uint32_t *)(EXCHMEM_BASEADDR);
#endif
//utility functions
/*Function to find minimum of x and y*/
inline int min(int x, int y){
return y + ((x - y) & ((x - y) >> 31));
}
/*Function to find maximum of x and y*/
inline int max(int x, int y){
return x - ((x - y) & ((x - y) >> 31));
}
//retrieves all the communicator
static inline communicator find_comm(unsigned int adr){
communicator ret;
ret.size = Xil_In32(adr);
ret.local_rank = Xil_In32(adr+4);
if(ret.size != 0 && ret.local_rank < ret.size){
ret.ranks = (comm_rank*)(cfgmem+adr/4+2);
} else {
ret.size = 0;
ret.local_rank = 0;
ret.ranks = NULL;
}
return ret;
}
//Packetizer/Depacketizer
static inline void start_packetizer(unsigned int max_pktsize) {
//get number of DATAPATH_WIDTH_BYTES transfers corresponding to max_pktsize (rounded down)
unsigned int max_pkt_transfers = max_pktsize/DATAPATH_WIDTH_BYTES;
Xil_Out32(NET_TXPKT_BASEADDR+0x10, max_pkt_transfers);
SET(NET_TXPKT_BASEADDR, CONTROL_REPEAT_MASK | CONTROL_START_MASK);
}
static inline void start_depacketizer() {
SET(NET_RXPKT_BASEADDR, CONTROL_REPEAT_MASK | CONTROL_START_MASK );
}
static inline void start_offload_engines(){
//start rxbuf enqueue
Xil_Out32(RX_ENQUEUE_BASEADDR+0x10, EXCHMEM_BASEADDR);
SET(RX_ENQUEUE_BASEADDR, CONTROL_REPEAT_MASK | CONTROL_START_MASK);
//start rxbuf dequeue
Xil_Out32(RX_DEQUEUE_BASEADDR+0x10, EXCHMEM_BASEADDR);
SET(RX_DEQUEUE_BASEADDR, CONTROL_REPEAT_MASK | CONTROL_START_MASK);
//start rxbuf seek
Xil_Out32(RX_SEEK_BASEADDR+0x10, EXCHMEM_BASEADDR);
SET(RX_SEEK_BASEADDR, CONTROL_REPEAT_MASK | CONTROL_START_MASK);
}
//connection management
//establish connection with every other rank in the communicator
int openCon()
{
unsigned int session = 0;
unsigned int dst_ip = 0;
unsigned int dst_port = 0;
unsigned int success = 0;
int ret = 0;
unsigned int cur_rank_ip = 0;
unsigned int cur_rank_port = 0;
unsigned int size = world.size;
unsigned int local_rank = world.local_rank;
//open connection to all the ranks except for the local rank
ret = NO_ERROR;
for (int i = 0; i < size; i++)
{
if (i != local_rank)
{
cur_rank_ip = world.ranks[i].ip;
cur_rank_port = world.ranks[i].port;
//send open connection request to the packetizer
putd(CMD_NET_CON, cur_rank_ip);
putd(CMD_NET_CON, cur_rank_port);
//wait until the connections status is returned
session = getd(STS_NET_CON);
dst_ip = getd(STS_NET_CON);
dst_port = getd(STS_NET_CON);
success = getd(STS_NET_CON);
if(success){
world.ranks[i].session = session;
} else {
ret = OPEN_CON_NOT_SUCCEEDED;
}
}
}
return ret;
}
//close connection with every other rank in the communicator
int closeCon()
{
unsigned int cur_rank_sess_id;
int ret = NO_ERROR;
unsigned int size = world.size;
unsigned int local_rank = world.local_rank;
//close connection to all the ranks except for the local rank
for (int i = 0; i < size; i++)
{
if (i != local_rank)
{
cur_rank_sess_id = world.ranks[i].session;
//send close connection request to the packetizer
putd(CMD_NET_CON, 0);
putd(CMD_NET_CON, cur_rank_sess_id);
}
}
return ret;
}
//open local port for listening
int openPort()
{
int success = 0;
//open port with only the local rank
putd(CMD_NET_PORT, world.ranks[world.local_rank].port);
success = getd(STS_NET_PORT);
if (success)
return NO_ERROR;
else
return OPEN_PORT_NOT_SUCCEEDED;
}
static inline unsigned int segment(unsigned int number_of_bytes,unsigned int segment_size){
return (number_of_bytes + segment_size - 1) / segment_size;
}
//configure datapath before calling this method
//instructs the data plane to move data
//use MOVE_IMMEDIATE
void start_move(
uint32_t op0_opcode,
uint32_t op1_opcode,
uint32_t res_opcode,
uint32_t compression_flags,
uint32_t remote_flags,
uint32_t func_id,
uint32_t count,
uint32_t comm_offset,
uint32_t arcfg_offset,
uint64_t op0_addr,
uint64_t op1_addr,
uint64_t res_addr,
int32_t op0_stride,
int32_t op1_stride,
int32_t res_stride,
uint32_t rx_src_rank,
uint32_t rx_tag,
uint32_t tx_dst_rank,
uint32_t tx_tag
) {
//TODO: mask everything to the correct bitwidth
uint32_t opcode = 0;
opcode |= op0_opcode;
opcode |= op1_opcode << 3;
opcode |= res_opcode << 6;
opcode |= remote_flags << 9;
bool res_is_remote = (remote_flags == RES_REMOTE);
opcode |= (compression_flags & 0x7) << 10;//mask is to prevent ETH_COMPRESSED flag leaking into func_id
opcode |= func_id << 13;
putd(CMD_DMA_MOVE, opcode);
putd(CMD_DMA_MOVE, count);
//arith config offset, as an offset in a uint32_t array
putd(CMD_DMA_MOVE, arcfg_offset/4);
//get addr for op0, or equivalents
if(op0_opcode == MOVE_IMMEDIATE){
putd(CMD_DMA_MOVE, (uint32_t)op0_addr);
putd(CMD_DMA_MOVE, (uint32_t)(op0_addr>>32));
} else if(op0_opcode == MOVE_STRIDE){
putd(CMD_DMA_MOVE, (uint32_t)op0_stride);
}
//get addr for op1, or equivalents
if(op1_opcode == MOVE_IMMEDIATE){
putd(CMD_DMA_MOVE, (uint32_t)op1_addr);
putd(CMD_DMA_MOVE, (uint32_t)(op1_addr>>32));
} else if(op1_opcode == MOVE_ON_RECV){
putd(CMD_DMA_MOVE, rx_src_rank);
putd(CMD_DMA_MOVE, rx_tag);
} else if(op1_opcode == MOVE_STRIDE){
putd(CMD_DMA_MOVE, (uint32_t)op1_stride);
}
//get addr for res, or equivalents
if(res_opcode == MOVE_IMMEDIATE){
putd(CMD_DMA_MOVE, (uint32_t)res_addr);
putd(CMD_DMA_MOVE, (uint32_t)(res_addr>>32));
} else if(res_opcode == MOVE_STRIDE){
putd(CMD_DMA_MOVE, (uint32_t)res_stride);
}
//get send related stuff, if result is remote or stream
if(res_is_remote || res_opcode == MOVE_STREAM){
putd(CMD_DMA_MOVE, tx_tag);
}
if(res_is_remote || op1_opcode == MOVE_ON_RECV){
putd(CMD_DMA_MOVE, comm_offset/4);
}
if(res_is_remote){
putd(CMD_DMA_MOVE, tx_dst_rank);
}
}
inline int end_move(){
return getd(STS_DMA_MOVE);
}
int move(
uint32_t op0_opcode,
uint32_t op1_opcode,
uint32_t res_opcode,
uint32_t compression_flags,
uint32_t remote_flags,
uint32_t func_id,
uint32_t count,
uint32_t comm_offset,
uint32_t arcfg_offset,
uint64_t op0_addr,
uint64_t op1_addr,
uint64_t res_addr,
int32_t op0_stride,
int32_t op1_stride,
int32_t res_stride,
uint32_t rx_src_rank,
uint32_t rx_tag,
uint32_t tx_dst_rank,
uint32_t tx_tag
){
start_move(
op0_opcode, op1_opcode, res_opcode,
compression_flags, remote_flags,
func_id, count,
comm_offset, arcfg_offset,
op0_addr, op1_addr, res_addr,
op0_stride, op1_stride, res_stride,
rx_src_rank, rx_tag,
tx_dst_rank, tx_tag
);
return end_move();
}
//performs a copy using DMA0. DMA0 rx reads while DMA1 tx overwrites
//use MOVE_IMMEDIATE
static inline int copy( unsigned int count,
uint64_t src_addr,
uint64_t dst_addr,
unsigned int arcfg_offset,
unsigned int compression,
unsigned int stream) {
return move(
(stream & OP0_STREAM) ? MOVE_STREAM : MOVE_IMMEDIATE,
MOVE_NONE,
(stream & RES_STREAM) ? MOVE_STREAM : MOVE_IMMEDIATE,
compression, RES_LOCAL, 0,
count, 0, arcfg_offset, src_addr, 0, dst_addr, 0, 0, 0,
0, 0, 0, 0
);
}
//performs an accumulate using DMA1 and DMA0. DMA0 rx reads op1 DMA1 rx reads op2 while DMA1 tx back to dst buffer
//use MOVE_IMMEDIATE
int combine(unsigned int count,
unsigned int function,
uint64_t op0_addr,
uint64_t op1_addr,
uint64_t res_addr,
unsigned int arcfg_offset,
unsigned int compression,
unsigned int stream) {
return move(
(stream & OP0_STREAM) ? MOVE_STREAM : MOVE_IMMEDIATE,
MOVE_IMMEDIATE,
(stream & RES_STREAM) ? MOVE_STREAM : MOVE_IMMEDIATE,
compression, RES_LOCAL, function,
count, 0, arcfg_offset, op0_addr, op1_addr, res_addr, 0, 0, 0,
0, 0, 0, 0
);
}
//transmits a buffer to a rank of the world communicator
//use MOVE_IMMEDIATE
int send(
unsigned int dst_rank,
unsigned int count,
uint64_t src_addr,
unsigned int comm_offset,
unsigned int arcfg_offset,
unsigned int dst_tag,
unsigned int compression,
unsigned int stream
){
//if ETH_COMPRESSED is set, also set RES_COMPRESSED
compression |= (compression & ETH_COMPRESSED) >> 1;
//TODO: when doing a one-sided send to a remote stream, check:
//dst_tag is < 247 (for correct routing on remote side)
//destination compression == Ethernet compression
//(since data can't be decompressed on remote side)
return move(
(stream & OP0_STREAM) ? MOVE_STREAM : MOVE_IMMEDIATE,
MOVE_NONE,
(stream & RES_STREAM) ? MOVE_STREAM : MOVE_IMMEDIATE,
compression, (dst_rank == world.local_rank) ? RES_LOCAL : RES_REMOTE, 0,
count, comm_offset, arcfg_offset, src_addr, 0, 0, 0, 0, 0,
0, 0, dst_rank, dst_tag
);
}
//waits for a messages to come and move their contents in a buffer
//use MOVE_ON_RECV
int recv( unsigned int src_rank,
unsigned int count,
uint64_t dst_addr,
unsigned int comm_offset,
unsigned int arcfg_offset,
unsigned int src_tag,
unsigned int compression,
unsigned int stream){
//if ETH_COMPRESSED is set, also set OP1_COMPRESSED
compression |= (compression & ETH_COMPRESSED) >> 2;
return move(
MOVE_NONE,
MOVE_ON_RECV,
(stream & RES_STREAM) ? MOVE_STREAM : MOVE_IMMEDIATE,
compression, RES_LOCAL, 0,
count, comm_offset, arcfg_offset, 0, 0, dst_addr, 0, 0, 0,
src_rank, src_tag, 0, (stream & RES_STREAM) ? src_tag : 0
);
}
//iterates over rx buffers until match is found or a timeout expires
//matches count, src and tag if tag is not ANY
//returns the index of the spare_buffer or -1 if not found
int seek_rx_buffer(
unsigned int src_rank,
unsigned int count,
unsigned int src_tag
){
unsigned int seq_num; //src_port TODO: use this variable to choose depending on session id or port
int i;
seq_num = world.ranks[src_rank].inbound_seq + 1;
//TODO: use a list to store recent message received to avoid scanning in the entire spare_buffer_struct.
//parse rx buffers until match is found
//matches count, src
//matches tag or tag is ANY
//return buffer index
unsigned int nbufs = Xil_In32(RX_BUFFER_COUNT_OFFSET);
rx_buffer *rx_buf_list = (rx_buffer*)(cfgmem+RX_BUFFER_COUNT_OFFSET/4+1);
for(i=0; i<nbufs; i++){
if(rx_buf_list[i].status == STATUS_RESERVED)
{
if((rx_buf_list[i].rx_src == src_rank) && (rx_buf_list[i].rx_len == count))
{
if(((rx_buf_list[i].rx_tag == src_tag) || (src_tag == TAG_ANY)) && (rx_buf_list[i].sequence_number == seq_num) )
{
//only now advance sequence number
world.ranks[src_rank].inbound_seq++;
return i;
}
}
}
}
return -1;
}
//iterates over rx buffers until match is found or a timeout expires
//matches count, src and tag if tag is not ANY
//returns the index of the spare_buffer
//timeout is jumps to exception handler
//useful as infrastructure for [I]MProbe/[I]MRecv
int wait_on_rx(
unsigned int src_rank,
unsigned int count,
unsigned int src_tag
){
int idx, i;
for(i = 0; timeout == 0 || i < timeout; i++){
idx = seek_rx_buffer(src_rank, count, src_tag);
if(idx >= 0) return idx;
}
return -1;
}
//1) receives from a rank
//2) sums with a a buffer
//3) the result is saved in (possibly another) local buffer
//use MOVE_ON_RECV
int fused_recv_reduce(
unsigned int src_rank,
unsigned int count,
unsigned int func,
uint64_t op0_addr,
uint64_t dst_addr,
unsigned int comm_offset,
unsigned int arcfg_offset,
unsigned int src_tag,
unsigned int compression,
unsigned int stream)
{
unsigned int err = NO_ERROR;
//figure out compression
//if ETH_COMPRESSED flag is set, then we need to set OP1_COMPRESSED (for the recv)
//keep OP0_COMPRESSED and RES_COMPRESSED as-is
compression = compression | ((compression & ETH_COMPRESSED) >> 2);
err |= move(
(stream & OP0_STREAM) ? MOVE_STREAM : MOVE_IMMEDIATE,
MOVE_ON_RECV,
(stream & RES_STREAM) ? MOVE_STREAM : MOVE_IMMEDIATE,
compression, RES_LOCAL, func,
count,
comm_offset, arcfg_offset,
op0_addr, 0, dst_addr, 0, 0, 0,
src_rank, src_tag, 0, 0
);
return err;
}
//1) receives from a rank
//2) sums with a a buffer
//3) result is sent to another rank
//use MOVE_ON_RECV
int fused_recv_reduce_send(
unsigned int src_rank,
unsigned int dst_rank,
unsigned int count,
unsigned int func,
uint64_t op0_addr,
unsigned int comm_offset,
unsigned int arcfg_offset,
unsigned int mpi_tag,
unsigned int compression,
unsigned int stream)
{
unsigned int err = NO_ERROR;
//figure out compression
//if ETH_COMPRESSED flag is set, then we need to set OP1_COMPRESSED (for the recv)
//and RES_COMPRESSED (for the send)
//keep OP0_COMPRESSED as-is
compression = (compression & OP0_COMPRESSED) | ((compression & ETH_COMPRESSED) >> 1) | ((compression & ETH_COMPRESSED) >> 2);
err |= move(
(stream & OP0_STREAM) ? MOVE_STREAM : MOVE_IMMEDIATE,
MOVE_ON_RECV,
MOVE_IMMEDIATE,
compression, RES_REMOTE, func,
count,
comm_offset, arcfg_offset,
op0_addr, 0, 0, 0, 0, 0,
src_rank, mpi_tag, dst_rank, mpi_tag
);
return err;
}
//COLLECTIVES
//root reads multiple times the same segment and send it to each rank before
//moving to the next part of the buffer to be transmitted.
//use MOVE_IMMEDIATE and MOVE_INCREMENTING and MOVE_REPEATING in sequence
int broadcast( unsigned int count,
unsigned int src_rank,
uint64_t buf_addr,
unsigned int comm_offset,
unsigned int arcfg_offset,
unsigned int compression,
unsigned int stream){
int err = NO_ERROR;
unsigned int max_seg_count;
int elems_remaining = count;
//convert max segment size to max segment count
//if pulling from a stream, segment size is irrelevant and we use the
//count directly because streams can't be read losslessly
if(stream & OP0_STREAM){
max_seg_count = count;
} else{
//compute count from uncompressed elem bytes in aright config
//instead of Xil_In32 we could use:
//(datapath_arith_config*)(arcfg_offset)->uncompressed_elem_bytes;
max_seg_count = max_segment_size / Xil_In32(arcfg_offset);
}
while(elems_remaining > 0){
//determine if we're sending or receiving
if(src_rank == world.local_rank){
//on the root we only care about ETH_COMPRESSED and OP0_COMPRESSED
//so replace RES_COMPRESSED with ETH_COMPRESSED
compression = compression | ((compression & ETH_COMPRESSED) >> 1);
//send a segment to each of the ranks (excluding self)
for(int i=0; i < world.size; i++){
start_move(
(i==0) ? ((elems_remaining == count) ? MOVE_IMMEDIATE : MOVE_INCREMENT) : MOVE_REPEAT,
MOVE_NONE,
MOVE_IMMEDIATE,
compression, RES_REMOTE, 0,
(i == src_rank) ? 0 : min(max_seg_count, elems_remaining),
comm_offset, arcfg_offset,
buf_addr, 0, 0, 0, 0, 0,
0, 0, i, TAG_ANY
);
}
for(int i=0; i < world.size; i++){
err |= end_move();
}
} else{
//on non-root nodes we only care about ETH_COMPRESSED and RES_COMPRESSED
//so replace OP1_COMPRESSED with the value of ETH_COMPRESSED
compression = compression | ((compression & ETH_COMPRESSED) >> 2);
err |= move(
MOVE_NONE,
MOVE_ON_RECV,
(elems_remaining == count) ? MOVE_IMMEDIATE : MOVE_INCREMENT,
compression, RES_LOCAL, 0,
min(max_seg_count, elems_remaining),
comm_offset, arcfg_offset,
0, 0, buf_addr, 0, 0, 0,
src_rank, TAG_ANY, 0, 0
);
}
elems_remaining -= max_seg_count;
}
return err;
}
//scatter segment at a time. root sends each rank a segment in a round robin fashion
//use MOVE_IMMEDIATE and MOVE_INCREMENTING and MOVE_STRIDE in sequence
int scatter(unsigned int count,
unsigned int src_rank,
uint64_t src_buf_addr,
uint64_t dst_buf_addr,
unsigned int comm_offset,
unsigned int arcfg_offset,
unsigned int compression,
unsigned int stream){
//TODO: implement segmentation
//TODO: scattering from/to stream
int err = NO_ERROR;
//determine if we're sending or receiving
if(src_rank == world.local_rank){
//on the root we only care about ETH_COMPRESSED and OP0_COMPRESSED
//so replace RES_COMPRESSED with ETH_COMPRESSED
compression = compression | ((compression & ETH_COMPRESSED) >> 1);
for(int i=0; i < world.size; i++){
start_move(
(i==0) ? MOVE_IMMEDIATE : MOVE_INCREMENT,
MOVE_NONE,
MOVE_IMMEDIATE,
compression, (i==src_rank) ? RES_LOCAL : RES_REMOTE, 0,
count,
comm_offset, arcfg_offset,
src_buf_addr, 0, dst_buf_addr, 0, 0, 0,
0, 0, i, TAG_ANY
);
}
for(int i=0; i < world.size; i++){
err |= end_move();
}
} else{
//on non-root odes we only care about ETH_COMPRESSED and RES_COMPRESSED
//so replace OP0_COMPRESSED with the value of ETH_COMPRESSED
compression = compression | ((compression & ETH_COMPRESSED) >> 2);
err |= move(
MOVE_NONE,
MOVE_ON_RECV,
MOVE_IMMEDIATE,
compression, RES_LOCAL, 0,
count,
comm_offset, arcfg_offset,
0, 0, dst_buf_addr, 0, 0, 0,
src_rank, TAG_ANY, 0, 0
);
}
return err;
}
//ring gather: non root relay data to the root. root copies segments in dst buffer as they come.
//on root, SEND_ON_RECV
//elsewhere MOVE_STRIDE then SEND_ON_RECV for relay
int gather( unsigned int count,
unsigned int root_rank,
uint64_t src_buf_addr,
uint64_t dst_buf_addr,
unsigned int comm_offset,
unsigned int arcfg_offset,
unsigned int compression,
unsigned int stream){
uint64_t tmp_buf_addr;
unsigned int i, curr_pos, next_in_ring, prev_in_ring, number_of_shift;
int err = NO_ERROR;
next_in_ring = (world.local_rank + 1) % world.size;
prev_in_ring = (world.local_rank + world.size - 1) % world.size;
//TODO: compute compression
if(root_rank == world.local_rank){ //root ranks mainly receives
//we need to compute correct compression schemes from the input compression flags
//copy to self: keep all flags except ETH_COMPRESSED, which should be reset for safety
//recv: keep RES_COMPRESSED and replace OP0_COMPRESSED with value of ETH_COMPRESSED
unsigned int copy_compression = compression & ~ETH_COMPRESSED;
unsigned int recv_compression = compression | ((compression & ETH_COMPRESSED) >> 2);
//initialize destination address in offload core
start_move(
MOVE_NONE,
MOVE_NONE,
MOVE_IMMEDIATE,
NO_COMPRESSION, RES_LOCAL, NO_STREAM,
0,
comm_offset, arcfg_offset,
0, 0, dst_buf_addr, 0, 0, 0,
0, 0, 0, 0
);
//receive from all members of the communicator
curr_pos = world.local_rank;
for(i=0; i<world.size; i++){
start_move(
(i==0) ? MOVE_IMMEDIATE : MOVE_NONE,
(i==0) ? MOVE_NONE : MOVE_ON_RECV,
MOVE_STRIDE,
(i==0) ? copy_compression : recv_compression, RES_LOCAL, NO_STREAM,
count,
comm_offset, arcfg_offset,
src_buf_addr, 0, 0, 0, 0, count*((i==0) ? curr_pos : ((curr_pos==(world.size-1)) ? (world.size-1) : -1)),
prev_in_ring, TAG_ANY, 0, 0
);
//update current position
curr_pos = (curr_pos + world.size - 1) % world.size;
}
for(i=0; i<=world.size; i++){
err |= end_move();
}
}else{
//non root ranks sends their data + relay others data to the next rank in sequence
// as a daisy chain
//we need to compute correct compression schemes from the input compression flags
//send: keep all flags except RES_COMPRESSED, which should be replaced by ETH_COMPRESSED
//relay: keep ETH_COMPRESSED, reset everything else
unsigned int send_compression = (compression & ~RES_COMPRESSED) | ((compression & ETH_COMPRESSED) >> 1);
unsigned int relay_compression = ((compression & ETH_COMPRESSED) >> 1) | ((compression & ETH_COMPRESSED) >> 2);
//first send our own data
start_move(
MOVE_IMMEDIATE,
MOVE_NONE,
MOVE_IMMEDIATE,
send_compression, RES_REMOTE, NO_STREAM,
count,
comm_offset, arcfg_offset,
src_buf_addr, 0, 0, 0, 0, 0,
0, 0, next_in_ring, TAG_ANY
);
//next relay a number of times depending on our position in the ring
number_of_shift = ((world.size+world.local_rank-root_rank)%world.size) - 1 ; //distance to the root
for (i=0; i<number_of_shift; i++){
start_move(
MOVE_NONE,
MOVE_ON_RECV,
MOVE_IMMEDIATE,
relay_compression, RES_REMOTE, NO_STREAM,
count,
comm_offset, arcfg_offset,
0, 0, 0, 0, 0, 0,
prev_in_ring, TAG_ANY, next_in_ring, TAG_ANY
);
}
for(i=0; i<=number_of_shift; i++){
err |= end_move();
}
}
return err;
}
//naive fused: 1) receive a segment 2) move in the dest buffer 3) relay to next rank
int allgather(
unsigned int count,
uint64_t src_buf_addr,
uint64_t dst_buf_addr,
unsigned int comm_offset,
unsigned int arcfg_offset,
unsigned int compression,
unsigned int stream
){
int i, curr_pos, rel_stride, abs_stride, next_in_ring, prev_in_ring;
int err = NO_ERROR;
//compression is tricky for the relay: we've already received into the destination buffer
//with associated flag RES_COMPRESSED; this buffer becomes the source for a send
//so if RES_COMPRESSED is set, OP0_COMPRESSED must be set for the send, and RES_COMPRESSED reset
unsigned int relay_compression = (compression & RES_COMPRESSED) ? (compression | OP0_COMPRESSED) : compression;
relay_compression &= ~(RES_COMPRESSED);
next_in_ring = (world.local_rank + 1) % world.size;
prev_in_ring = (world.local_rank + world.size - 1) % world.size;
//prime the address slot for the destination, so we can subsequently stride against it
start_move(
MOVE_NONE, MOVE_NONE, MOVE_IMMEDIATE,
NO_COMPRESSION, RES_LOCAL, 0,
0,
0, arcfg_offset,
src_buf_addr, 0, dst_buf_addr, 0, 0, 0,
0, 0, 0, 0
);
//copy our local data into the appropriate destination slot
start_move(
MOVE_IMMEDIATE, MOVE_NONE, MOVE_STRIDE,
compression, RES_LOCAL, 0,
count,
0, arcfg_offset,
src_buf_addr, 0, 0, 0, 0, count*world.local_rank,
0, 0, 0, 0
);
//send to next in ring
//ETH_COMPRESSED flag overwrites RES_COMPRESSED
start_move(
MOVE_IMMEDIATE, MOVE_NONE, MOVE_IMMEDIATE,
compression | ((compression & ETH_COMPRESSED) >> 1), RES_REMOTE, 0,
count,
comm_offset, arcfg_offset,
src_buf_addr, 0, 0, 0, 0, 0,
0, 0, next_in_ring, TAG_ANY
);
err |= end_move();
err |= end_move();
err |= end_move();
//receive and forward from all other members of the communicator
curr_pos = world.local_rank;
for(i=0; i<world.size-1; i++){
rel_stride = count*((curr_pos == 0) ? (world.size-1) : -1);
abs_stride = count*((curr_pos == 0) ? (world.size-1) : curr_pos-1);
//we use a blocking move here, because we want to avoid a race condition with the relay below
//TODO: avoid this; we either need to solve the RAW dependency in hardware (the generic approach),
//or a way to reuse a rx buffer (solves this problem in particular but does not extend to e.g. reduces)
//e.g. MOVE_ON_RECV_KEEP which would keep the RX buffer in the pending state
// on compression: ETH_COMPRESSED flag overwrites OP1_COMPRESSED
err |= move(
MOVE_NONE, MOVE_ON_RECV, MOVE_STRIDE,
compression | ((compression & ETH_COMPRESSED) >> 2), RES_LOCAL, 0,
count,
comm_offset, arcfg_offset,
0, 0, 0, 0, 0, rel_stride,
prev_in_ring, TAG_ANY, 0, 0
);
if(i < world.size-2){ //if not the last data, relay to the next in ring
//first prime the address
start_move(
MOVE_NONE, MOVE_IMMEDIATE, MOVE_NONE,
NO_COMPRESSION, RES_REMOTE, 0,
0,
comm_offset, arcfg_offset,
0, dst_buf_addr, 0, 0, 0, 0,
0, 0, next_in_ring, TAG_ANY
);
//send
//here we're copying from the result buffer back into the network, so
//RES_COMPRESSED flag overwrites OP0_COMPRESSED, and
//ETH_COMPRESSED flag overwrites RES_COMPRESSED
start_move(
MOVE_NONE, MOVE_STRIDE, MOVE_IMMEDIATE,
((compression & RES_COMPRESSED) >> 2) | ((compression & ETH_COMPRESSED) >> 1), RES_REMOTE, 0,
count,
comm_offset, arcfg_offset,
0, 0, 0, 0, abs_stride, 0,
0, 0, next_in_ring, TAG_ANY
);
err |= end_move();
err |= end_move();
}
curr_pos = (curr_pos + world.size - 1) % world.size;
}
return err;
}
//every rank receives a buffer it reduces its own buffer and forwards to next rank in the ring
int reduce( unsigned int count,
unsigned int func,
unsigned int root_rank,
uint64_t src_addr,
uint64_t dst_addr,
unsigned int comm_offset,
unsigned int arcfg_offset,
unsigned int compression,
unsigned int stream){
unsigned int next_in_ring = (world.local_rank + 1) % world.size;
unsigned int prev_in_ring = (world.local_rank + world.size-1) % world.size;
if(world.size == 1){
//corner-case copy for when running a single-node reduction
return copy(count, src_addr, dst_addr, arcfg_offset, compression, stream);
}else if( prev_in_ring == root_rank){
//non root ranks immediately after the root sends; only OP0_STREAM flag is relevant here
return send(next_in_ring, count, src_addr, comm_offset, arcfg_offset, TAG_ANY, compression, (stream & OP0_STREAM));
}else if (world.local_rank != root_rank){
//non root ranks sends their data + data received from previous rank to the next rank in sequence as a daisy chain; only OP0_STREAM flag is relevant here
return fused_recv_reduce_send(prev_in_ring, next_in_ring, count, func, src_addr, comm_offset, arcfg_offset, TAG_ANY, compression, (stream & OP0_STREAM));
}else{
//root only receive from previous node in the ring, add its local buffer and save in destination buffer
return fused_recv_reduce(prev_in_ring, count, func, src_addr, dst_addr, comm_offset, arcfg_offset, TAG_ANY, compression, stream);
}
}
//reduce_scatter: (a,b,c), (1,2,3), (X,Y,Z) -> (a+1+X,,) (,b+2+Y,) (,,c+3+Z)
//count == size of chunks
int reduce_scatter(
unsigned int count,
unsigned int func,
uint64_t src_buf_addr,
uint64_t dst_buf_addr,
unsigned int comm_offset,
unsigned int arcfg_offset,
unsigned int compression,
unsigned int stream
){
int i, curr_pos, rel_stride, abs_stride, next_in_ring, prev_in_ring;
int err = NO_ERROR;
unsigned int tmp_compression = NO_COMPRESSION;
if(world.size == 1){
//corner-case copy for when running a single-node reduction
return copy(count, src_buf_addr, dst_buf_addr, arcfg_offset, compression, stream);
}
next_in_ring = (world.local_rank + 1) % world.size;
prev_in_ring = (world.local_rank + world.size - 1) % world.size;
//preamble: send our data to next in ring
//prime the address slot for the source, so we can subsequently stride against it
start_move(
MOVE_IMMEDIATE, MOVE_NONE, MOVE_NONE,
NO_COMPRESSION, RES_LOCAL, 0,
0,
0, arcfg_offset,
src_buf_addr, 0, 0, 0, 0, 0,
0, 0, 0, 0
);
curr_pos = prev_in_ring;
//send local chunk to next in ring
//send: keep OP0_COMPRESSED, replace RES_COMPRESSED by ETH_COMPRESSED
tmp_compression = (compression & OP0_COMPRESSED) | ((compression & ETH_COMPRESSED) >> 1);
start_move(
MOVE_STRIDE, MOVE_NONE, MOVE_IMMEDIATE,
tmp_compression, RES_REMOTE, 0,
count,
comm_offset, arcfg_offset,
0, 0, 0, count*curr_pos, 0, 0,
0, 0, next_in_ring, TAG_ANY
);
//receive and reduce+forward from all other members of the communicator
for(i=0; i<world.size-1; i++){
rel_stride = count*((curr_pos == 0) ? (world.size-1) : -1);
//simultaneous receive, reduce and send for the received chunk,
//unless it is the last step, in which case we don't send, but save locally
if(i < world.size-2){
tmp_compression = (compression & OP0_COMPRESSED) | ((compression & ETH_COMPRESSED) >> 2) | ((compression & ETH_COMPRESSED) >> 1);
start_move(
MOVE_STRIDE, MOVE_ON_RECV, MOVE_IMMEDIATE,
tmp_compression, RES_REMOTE, func,
count,
comm_offset, arcfg_offset,
0, 0, 0, rel_stride, 0, 0,
prev_in_ring, TAG_ANY, next_in_ring, TAG_ANY
);
} else{
tmp_compression = compression | ((compression & ETH_COMPRESSED) >> 2);
start_move(
MOVE_STRIDE, MOVE_ON_RECV, MOVE_IMMEDIATE,
tmp_compression, RES_LOCAL, 0,
count,
comm_offset, arcfg_offset,
0, 0, dst_buf_addr, rel_stride, 0, 0,
prev_in_ring, TAG_ANY, 0, 0
);
}
curr_pos = (curr_pos + world.size - 1) % world.size;
//pop one result here to keep the result FIFO not full
err |= end_move();
}
//pop final two results
err |= end_move();
err |= end_move();
return err;
}
//2 stage allreduce: fused reduce_scatter+all_gather
int allreduce(
unsigned int count,
unsigned int func,
uint64_t src_buf_addr,
uint64_t dst_buf_addr,
unsigned int comm_offset,
unsigned int arcfg_offset,
unsigned int compression,
unsigned int stream
){
int i, curr_pos, curr_count, rel_stride, abs_stride, next_in_ring, prev_in_ring;
int err = NO_ERROR;
unsigned int tmp_compression = NO_COMPRESSION;
if(world.size == 1){
//corner-case copy for when running a single-node reduction
return copy(count, src_buf_addr, dst_buf_addr, arcfg_offset, compression, stream);
}
next_in_ring = (world.local_rank + 1) % world.size;
prev_in_ring = (world.local_rank + world.size - 1) % world.size;
//we need to break the input into world.size chunks of equal size
//if count does not divide by world.size, the chunk with the largest index (tail) will be smaller
unsigned int bulk_count = (count + world.size -1) / world.size;//equivalent to ceil(count/world.size)
unsigned int tail_count = count - bulk_count*(world.size-1);
//preamble: send our data to next in ring
//prime the address slots for the source and destination,
//so we can subsequently stride against them