forked from HabanaAI/hccl_demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hccl_demo.cpp
829 lines (703 loc) · 34.5 KB
/
hccl_demo.cpp
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
/******************************************************************************
# Copyright (c) 2022 Habana Labs, Ltd.
# SPDX-License-Identifier: Apache-2.0
******************************************************************************/
// C++ Standard Libraries
#include <iostream>
#include <exception>
#include <vector>
#include <cstdlib> // for getenv, mkstemp
#include <chrono> // for Bandwidth calculation
#include <iomanip> // for setprecision
#include <unistd.h>
#include <functional>
#include <algorithm>
#include <sstream>
#include <numeric>
#include <fstream>
// HCCL :: Habana Collective Communications Library
#include <hccl.h>
// Synapse :: Habana Synapse training API
#include <synapse_api.h>
#if AFFINITY_ENABLED
#include "affinity.h"
#endif
#define DATA_ELEMENTS_MAX 13
#define DEFAULT_TEST_SIZE 33554432
#define DEFAULT_TEST_LOOP 10
#define DEFAULT_BOX_SIZE 8
#define NUMBER_OF_WARMUPS 100
using namespace std;
using Clock = chrono::high_resolution_clock;
#define CHECK_HCCL_STATUS(x) \
{ \
const auto _res = (x); \
if (_res != hcclSuccess) \
throw runtime_error {"In function " + string {__FUNCTION__} + \
"(): " #x " failed: " + hcclGetErrorString(_res)}; \
}
#define CHECK_SYNAPSE_STATUS(x) \
{ \
const auto _res = (x); \
if (_res != synSuccess) \
throw runtime_error {"In function " + string {__FUNCTION__} + \
"(): " #x " failed with synapse error: " + to_string((_res))}; \
}
struct hccl_demo_data
{
synDeviceId device_handle;
synStreamHandle device_to_host_stream;
synStreamHandle host_to_device_stream;
synStreamHandle collective_stream;
size_t nranks;
hcclComm_t hccl_comm;
size_t num_iters;
};
struct hccl_demo_stats
{
float avg_duration_in_sec;
float rank_duration_in_sec;
size_t num_iters;
};
ostream& log()
{
return cout;
}
hcclResult_t get_avg_duration(hccl_demo_data& demo_data, hccl_demo_stats& stat)
{
float& rank_duration = stat.rank_duration_in_sec;
float& avg_duration = stat.avg_duration_in_sec;
uint64_t data_size = sizeof(stat.rank_duration_in_sec);
uint64_t count = data_size / sizeof(float);
const void* input_host_data_ptr = reinterpret_cast<void*>(&rank_duration);
uint64_t input_dev_ptr {};
uint64_t output_dev_ptr {};
CHECK_SYNAPSE_STATUS(synDeviceMalloc(demo_data.device_handle, data_size, 0, 0, &input_dev_ptr));
CHECK_SYNAPSE_STATUS(synDeviceMalloc(demo_data.device_handle, data_size, 0, 0, &output_dev_ptr));
CHECK_SYNAPSE_STATUS(synHostMap(demo_data.device_handle, data_size, input_host_data_ptr));
CHECK_SYNAPSE_STATUS(synMemCopyAsync(demo_data.host_to_device_stream,
(uint64_t) input_host_data_ptr,
data_size,
input_dev_ptr,
HOST_TO_DRAM));
CHECK_SYNAPSE_STATUS(synStreamSynchronize(demo_data.host_to_device_stream));
CHECK_HCCL_STATUS(hcclAllReduce((const void*) input_dev_ptr,
(void*) output_dev_ptr,
count,
hcclFloat32,
hcclSum,
demo_data.hccl_comm,
demo_data.collective_stream));
CHECK_SYNAPSE_STATUS(synStreamSynchronize(demo_data.collective_stream));
const void* output_host_data_ptr = reinterpret_cast<void*>(&avg_duration);
CHECK_SYNAPSE_STATUS(synHostMap(demo_data.device_handle, data_size, output_host_data_ptr));
CHECK_SYNAPSE_STATUS(synMemCopyAsync(demo_data.device_to_host_stream,
output_dev_ptr,
data_size,
(uint64_t) output_host_data_ptr,
DRAM_TO_HOST));
CHECK_SYNAPSE_STATUS(synStreamSynchronize(demo_data.device_to_host_stream));
avg_duration = avg_duration / demo_data.nranks;
return hcclSuccess;
}
hccl_demo_stats benchmark(hccl_demo_data& demo_data, const function<void()>& fn)
{
// Warmup run
hccl_demo_stats stat;
auto num_warmup_iters = size_t {NUMBER_OF_WARMUPS};
for (size_t iter = 0; iter < num_warmup_iters; ++iter)
{
fn();
}
CHECK_SYNAPSE_STATUS(synStreamSynchronize(demo_data.collective_stream));
// Actual iterations
auto start_time = Clock::now();
for (size_t iter = 0; iter < demo_data.num_iters; ++iter)
{
fn();
}
CHECK_SYNAPSE_STATUS(synStreamSynchronize(demo_data.collective_stream));
auto duration = Clock::now() - start_time;
stat.rank_duration_in_sec = chrono::duration_cast<chrono::duration<double>>(duration).count();
stat.rank_duration_in_sec = stat.rank_duration_in_sec / demo_data.num_iters;
CHECK_HCCL_STATUS(get_avg_duration(demo_data, stat));
return stat;
}
bool should_report_stat(int rank)
{
return rank == 0;
}
inline string format_bw(const double bytes_per_sec)
{
stringstream ss;
ss << fixed << setprecision(3) << bytes_per_sec / 1e6 << " MB/s";
return ss.str();
}
string get_print_delimiter(size_t length, char delimiter)
{
stringstream ss;
for (size_t i = 0; i < length; i++)
{
ss << delimiter;
}
return ss.str();
}
string get_demo_test_type()
{
static bool is_cached = false;
static auto test_type = string {"broadcast"};
if (!is_cached)
{
char* env_value = getenv("HCCL_DEMO_TEST");
test_type = (env_value != nullptr) ? string(env_value) : test_type;
is_cached = true;
}
return test_type;
}
int get_demo_box_size()
{
static bool is_cached = false;
static auto box_size = DEFAULT_BOX_SIZE;
if (!is_cached)
{
char* env_value = getenv("HCCL_BOX_SIZE");
box_size = (env_value != nullptr) ? atoi(env_value) : box_size;
is_cached = true;
}
return box_size;
}
int get_demo_test_root()
{
static bool is_cached = false;
static auto test_root = 0;
if (!is_cached)
{
char* env_value = getenv("HCCL_DEMO_TEST_ROOT");
test_root = (env_value != nullptr) ? atoi(env_value) : test_root;
is_cached = true;
}
return test_root;
}
int get_demo_test_size()
{
static bool is_cached = false;
static auto test_size = DEFAULT_TEST_SIZE;
if (!is_cached)
{
char* env_value = getenv("HCCL_DEMO_TEST_SIZE");
test_size = (env_value != nullptr) ? atoi(env_value) : test_size;
is_cached = true;
}
return test_size;
}
int get_demo_test_loop()
{
static bool is_cached = false;
static auto test_loop = DEFAULT_TEST_LOOP;
if (!is_cached)
{
char* env_value = getenv("HCCL_DEMO_TEST_LOOP");
test_loop = (env_value != nullptr) ? atoi(env_value) : test_loop;
is_cached = true;
}
return test_loop;
}
string get_demo_csv_path()
{
static bool is_cached = false;
static auto csv_path = string {""};
if (!is_cached)
{
char* env_value = getenv("HCCL_DEMO_CSV_PATH");
csv_path = (env_value != nullptr) ? string(env_value) : csv_path;
is_cached = true;
}
return csv_path;
}
int get_nranks()
{
static bool is_cached = false;
static auto test_nranks = 0;
if (!is_cached)
{
char* env_value = getenv("HCCL_NRANKS");
test_nranks = (env_value != nullptr) ? atoi(env_value) : test_nranks;
is_cached = true;
}
return test_nranks;
}
int get_hccl_rank()
{
static bool is_cached = false;
static auto test_rank = -1;
if (!is_cached)
{
char* env_value = getenv("HCCL_RANK");
test_rank = (env_value != nullptr) ? atoi(env_value) : test_rank;
is_cached = true;
}
return test_rank;
}
void describe_stat(const string& stat_name,
const hccl_demo_stats& stats,
size_t data_size,
double factor,
int hccl_rank,
int loop,
const string& test_type,
const string& dtype)
{
auto avg_bandwidth = (double) data_size / stats.avg_duration_in_sec;
avg_bandwidth = avg_bandwidth * factor;
auto rank_bandwith = (double) data_size / stats.rank_duration_in_sec;
rank_bandwith = rank_bandwith * factor;
if (should_report_stat(hccl_rank))
{
stringstream ss;
sleep(1);
size_t delimiter_size = stat_name.length() + string {"[BENCHMARK]"}.length() + 1;
ss << get_print_delimiter(delimiter_size, '#') << '\n';
ss << "[BENCHMARK] " << stat_name << '\n';
ss << "[BENCHMARK] Bandwidth : " << format_bw(avg_bandwidth);
ss << '\n' << get_print_delimiter(delimiter_size, '#') << '\n';
log() << ss.str();
}
// Write results to csv file
auto csv_path = get_demo_csv_path();
if (!csv_path.empty())
{
ofstream output;
output.open(csv_path, ofstream::out | ofstream::app);
output << test_type << "," << hccl_rank << "," << dtype << "," << data_size << "," << loop << ","
<< format_bw(rank_bandwith) << endl;
output.close();
}
}
hcclResult_t send_recv_test(
void* out_dev_ptr, const void* input_dev_ptr, size_t count, hcclComm_t comm, hcclStream_t stream, int peerRank)
{
hcclGroupStart();
CHECK_HCCL_STATUS(hcclSend((const void*) input_dev_ptr, count, hcclFloat32, peerRank, comm, stream));
CHECK_HCCL_STATUS(hcclRecv((void*) out_dev_ptr, count, hcclFloat32, peerRank, comm, stream));
hcclGroupEnd();
return hcclSuccess;
}
int main()
{
try
{
log() << "Running HCCL Demo :: A simple program demonstrating HCCL usage from C++" << endl;
hccl_demo_data demo_data;
demo_data.nranks = get_nranks();
demo_data.num_iters = get_demo_test_loop();
int hccl_rank = get_hccl_rank();
// Initialize Synapse API context
CHECK_SYNAPSE_STATUS(synInitialize());
// Acquire device
const synModuleId device_module_id = hccl_rank % get_demo_box_size();
CHECK_SYNAPSE_STATUS(synDeviceAcquireByModuleId(&demo_data.device_handle, device_module_id));
#if AFFINITY_ENABLED
setupAffinity(device_module_id);
#endif
// Create Streams
CHECK_SYNAPSE_STATUS(
synStreamCreate(&demo_data.collective_stream, demo_data.device_handle, STREAM_TYPE_NETWORK_COLLECTIVE, 0));
CHECK_SYNAPSE_STATUS(synStreamCreate(&demo_data.device_to_host_stream,
demo_data.device_handle,
STREAM_TYPE_COPY_DEVICE_TO_HOST,
0));
CHECK_SYNAPSE_STATUS(synStreamCreate(&demo_data.host_to_device_stream,
demo_data.device_handle,
STREAM_TYPE_COPY_HOST_TO_DEVICE,
0));
// Generate unique id
hcclUniqueId unique_id {};
constexpr int master_mpi_rank = 0;
if (hccl_rank == master_mpi_rank)
{
CHECK_HCCL_STATUS(hcclGetUniqueId(&unique_id));
}
// Create new HCCL communicator
CHECK_HCCL_STATUS(hcclCommInitRank(&demo_data.hccl_comm, demo_data.nranks, unique_id, hccl_rank));
uint64_t input_dev_ptr {};
uint64_t output_dev_ptr {};
// Allocate buffers on the HPU device
uint64_t data_size = get_demo_test_size();
uint64_t count = data_size / sizeof(float);
auto input_host_data = vector<float>(count, hccl_rank + 1);
const void* input_host_data_ptr = reinterpret_cast<void*>(input_host_data.data());
CHECK_SYNAPSE_STATUS(synDeviceMalloc(demo_data.device_handle, data_size, 0, 0, &input_dev_ptr));
CHECK_SYNAPSE_STATUS(synDeviceMalloc(demo_data.device_handle, data_size, 0, 0, &output_dev_ptr));
CHECK_SYNAPSE_STATUS(synHostMap(demo_data.device_handle, data_size, input_host_data_ptr));
CHECK_SYNAPSE_STATUS(synMemCopyAsync(demo_data.host_to_device_stream,
(uint64_t) input_host_data_ptr,
data_size,
input_dev_ptr,
HOST_TO_DRAM));
CHECK_SYNAPSE_STATUS(synStreamSynchronize(demo_data.host_to_device_stream));
string test_type = get_demo_test_type();
if (test_type == "broadcast")
{
double broadcast_factor = 1;
int root = get_demo_test_root();
for (uint64_t i = 0; i < count; ++i)
{
input_host_data[i] = i + hccl_rank;
}
// Copy from input_host_data_ptr to input_dev_ptr (to be used in benchmark)
CHECK_SYNAPSE_STATUS(synMemCopyAsync(demo_data.host_to_device_stream,
(uint64_t) input_host_data_ptr,
data_size,
input_dev_ptr,
HOST_TO_DRAM));
CHECK_SYNAPSE_STATUS(synStreamSynchronize(demo_data.host_to_device_stream));
// Run HCCL Broadcast collective
auto stat = benchmark(demo_data, [&]() {
CHECK_HCCL_STATUS(hcclBroadcast((const void*) input_dev_ptr,
(void*) output_dev_ptr,
input_host_data.size(),
hcclFloat32,
root,
demo_data.hccl_comm,
demo_data.collective_stream));
});
// Correctness check
bool is_ok = true;
auto output_host_data = vector<float>(input_host_data.size());
const void* output_host_data_ptr = reinterpret_cast<void*>(output_host_data.data());
CHECK_SYNAPSE_STATUS(synHostMap(demo_data.device_handle, data_size, output_host_data_ptr));
CHECK_SYNAPSE_STATUS(synMemCopyAsync(demo_data.device_to_host_stream,
output_dev_ptr,
data_size,
(uint64_t) output_host_data_ptr,
DRAM_TO_HOST));
CHECK_SYNAPSE_STATUS(synStreamSynchronize(demo_data.device_to_host_stream));
for (size_t i = 0; i < input_host_data.size(); ++i)
{
if (abs(output_host_data[i] - (float) (i + root)) != 0)
{
is_ok = false;
}
}
log() << "Broadcast hccl_rank=" << hccl_rank << " root=" << root << " size=" << data_size << " <float>"
<< " Input Buffer [" << input_host_data[0] << " " << input_host_data[1] << " " << input_host_data[2]
<< " " << input_host_data[3] << " ...]"
<< " Output Buffer [" << output_host_data[0] << " " << output_host_data[1] << " "
<< output_host_data[2] << " " << output_host_data[3] << " ...]"
<< " which is " << (is_ok ? "fine." : "bad.") << endl;
CHECK_SYNAPSE_STATUS(synHostUnmap(demo_data.device_handle, input_host_data_ptr));
CHECK_SYNAPSE_STATUS(synHostUnmap(demo_data.device_handle, output_host_data_ptr));
// End of correctness check
describe_stat("Broadcast(count=" + to_string(input_host_data.size()) +
", dtype=fp32, iterations=" + to_string(demo_data.num_iters) + ")",
stat,
data_size,
broadcast_factor,
hccl_rank,
demo_data.num_iters,
test_type,
"float");
}
else if (test_type == "all_reduce")
{
double allreduce_factor = ((double) (2 * (demo_data.nranks - 1))) / ((double) demo_data.nranks);
// Fill input data, example:
// Input | Output
// G0 G1 G2 G3 G0 G1 G2 G3
// 0 1 2 3 => 6 6 6 6
// 4 5 6 7 22 22 22 22
// 8 9 10 11 38 38 38 38
// 12 13 14 15 54 54 54 54
for (uint64_t i = 0; i < count; ++i)
{
// We want to make sure we use different values on each cell and between ranks,
// but we don't want the summation to get too big, that is why we modulo by DATA_ELEMENTS_MAX.
input_host_data[i] = hccl_rank + (demo_data.nranks * (i % DATA_ELEMENTS_MAX));
}
//Copy from input_host_data_ptr to input_dev_ptr (to be used in benchmark)
CHECK_SYNAPSE_STATUS(synMemCopyAsync(demo_data.host_to_device_stream,
(uint64_t) input_host_data_ptr,
data_size,
input_dev_ptr,
HOST_TO_DRAM));
CHECK_SYNAPSE_STATUS(synStreamSynchronize(demo_data.host_to_device_stream));
// Run HCCL AllReduce collective
auto stat = benchmark(demo_data, [&]() {
CHECK_HCCL_STATUS(hcclAllReduce((const void*) input_dev_ptr,
(void*) output_dev_ptr,
input_host_data.size(),
hcclFloat32,
hcclSum,
demo_data.hccl_comm,
demo_data.collective_stream));
});
// Correctness check
bool is_ok = true;
auto output_host_data = vector<float>(input_host_data.size());
const void* output_host_data_ptr = reinterpret_cast<void*>(output_host_data.data());
CHECK_SYNAPSE_STATUS(synHostMap(demo_data.device_handle, data_size, output_host_data_ptr));
CHECK_SYNAPSE_STATUS(synMemCopyAsync(demo_data.device_to_host_stream,
output_dev_ptr,
data_size,
(uint64_t) output_host_data_ptr,
DRAM_TO_HOST));
CHECK_SYNAPSE_STATUS(synStreamSynchronize(demo_data.device_to_host_stream));
int start = 0;
int end = demo_data.nranks - 1;
int expected;
int addCommSize;
for (size_t i = 0; i < input_host_data.size(); ++i)
{
addCommSize = demo_data.nranks * (i % DATA_ELEMENTS_MAX);
// Arithmetic progression
expected = ((start + addCommSize) + (end + addCommSize)) * demo_data.nranks / 2;
if (abs(output_host_data[i] - expected) != 0)
{
is_ok = false;
}
}
log() << "Allreduce hccl_rank=" << hccl_rank << " size=" << data_size << " <float>"
<< " Input Buffer [" << input_host_data[0] << " " << input_host_data[1] << " " << input_host_data[2]
<< " " << input_host_data[3] << " ...]"
<< " reduced to Output Buffer [" << output_host_data[0] << " " << output_host_data[1] << " "
<< output_host_data[2] << " " << output_host_data[3] << " ...]"
<< " which is " << (is_ok ? "fine." : "bad.") << endl;
CHECK_SYNAPSE_STATUS(synHostUnmap(demo_data.device_handle, input_host_data_ptr));
CHECK_SYNAPSE_STATUS(synHostUnmap(demo_data.device_handle, output_host_data_ptr));
// End of correctness check
describe_stat("hcclAllReduce(src!=dst, count=" + to_string(input_host_data.size()) +
", dtype=fp32, iterations=" + to_string(demo_data.num_iters) + ")",
stat,
data_size,
allreduce_factor,
hccl_rank,
demo_data.num_iters,
test_type,
"float");
}
else if (test_type == "reduce_scatter")
{
double reduce_scatter_factor = ((double) (demo_data.nranks - 1)) / ((double) demo_data.nranks);
// Fill input data, example:
// Input | Output
// G0 G1 G2 G3 G0 G1 G2 G3
// 0 1 2 3 => 6 22 38 54
// 4 5 6 7
// 8 9 10 11
// 12 13 14 15
for (uint64_t i = 0; i < count; ++i)
{
// We want to make sure we use different values on each cell and between ranks,
// but we don't want the summation to get too big, that is why we modulo by DATA_ELEMENTS_MAX.
input_host_data[i] = hccl_rank + (demo_data.nranks * (i % DATA_ELEMENTS_MAX));
}
//Copy from input_host_data_ptr to input_dev_ptr (to be used in benchmark)
CHECK_SYNAPSE_STATUS(synMemCopyAsync(demo_data.host_to_device_stream,
(uint64_t) input_host_data_ptr,
data_size,
input_dev_ptr,
HOST_TO_DRAM));
CHECK_SYNAPSE_STATUS(synStreamSynchronize(demo_data.host_to_device_stream));
// Run HCCL ReduceScatter collective
auto stat = benchmark(demo_data, [&]() {
CHECK_HCCL_STATUS(hcclReduceScatter((const void*) input_dev_ptr,
(void*) output_dev_ptr,
input_host_data.size() / demo_data.nranks,
hcclFloat32,
hcclSum,
demo_data.hccl_comm,
demo_data.collective_stream));
});
// Correctness check
bool is_ok = true;
auto output_host_data = vector<float>(input_host_data.size() / demo_data.nranks);
const void* output_host_data_ptr = reinterpret_cast<void*>(output_host_data.data());
CHECK_SYNAPSE_STATUS(
synHostMap(demo_data.device_handle, data_size / demo_data.nranks, output_host_data_ptr));
CHECK_SYNAPSE_STATUS(synMemCopyAsync(demo_data.device_to_host_stream,
output_dev_ptr,
data_size / demo_data.nranks,
(uint64_t) output_host_data_ptr,
DRAM_TO_HOST));
CHECK_SYNAPSE_STATUS(synStreamSynchronize(demo_data.device_to_host_stream));
int start;
int end;
int expected = 0;
for (size_t i = 0; i < output_host_data.size(); ++i)
{
start = (hccl_rank * output_host_data.size()) % DATA_ELEMENTS_MAX * demo_data.nranks;
end = start + (demo_data.nranks - 1);
// Arithmetic progression
expected = (((start + demo_data.nranks * i) % (demo_data.nranks * DATA_ELEMENTS_MAX)) +
((end + demo_data.nranks * i) % (demo_data.nranks * DATA_ELEMENTS_MAX))) *
demo_data.nranks / 2;
if (abs(output_host_data[i] - expected) != 0)
{
is_ok = false;
}
}
log() << "ReduceScatter hccl_rank=" << hccl_rank << " size=" << data_size << " <float>"
<< " Input Buffer [" << input_host_data[0] << " " << input_host_data[1] << " " << input_host_data[2]
<< " " << input_host_data[3] << " ...]"
<< " reduced to Output Buffer [" << output_host_data[0] << " " << output_host_data[1] << " "
<< output_host_data[2] << " " << output_host_data[3] << " ...]"
<< " which is " << (is_ok ? "fine." : "bad.") << endl;
CHECK_SYNAPSE_STATUS(synHostUnmap(demo_data.device_handle, input_host_data_ptr));
CHECK_SYNAPSE_STATUS(synHostUnmap(demo_data.device_handle, output_host_data_ptr));
// End of correctness
describe_stat("hcclReduceScatter(src!=dst, count=" + to_string(input_host_data.size()) +
", dtype=fp32, iterations=" + to_string(demo_data.num_iters) + ")",
stat,
data_size,
reduce_scatter_factor,
hccl_rank,
demo_data.num_iters,
test_type,
"float");
}
else if (test_type == "all_gather")
{
double all_gather_factor = ((double) (demo_data.nranks - 1));
CHECK_SYNAPSE_STATUS(
synDeviceMalloc(demo_data.device_handle, data_size * demo_data.nranks, 0, 0, &output_dev_ptr));
// Fill input data, example:
// Input | Output
// G0 G1 G2 G3 G0 G1 G2 G3
// 0 2 4 6 => 0 0 0 0
// 1 3 5 7 1 1 1 1
// 2 2 2 2
// 3 3 3 3
// 4 4 4 4
// 5 5 5 5
// 6 6 6 6
// 7 7 7 7
for (uint64_t i = 0; i < count; ++i)
{
input_host_data[i] = hccl_rank * count + i;
}
//Copy from input_host_data_ptr to input_dev_ptr (to be used in benchmark)
CHECK_SYNAPSE_STATUS(synMemCopyAsync(demo_data.host_to_device_stream,
(uint64_t) input_host_data_ptr,
data_size,
input_dev_ptr,
HOST_TO_DRAM));
CHECK_SYNAPSE_STATUS(synStreamSynchronize(demo_data.host_to_device_stream));
// Run HCCL AllGather collective
auto stat = benchmark(demo_data, [&]() {
CHECK_HCCL_STATUS(hcclAllGather((const void*) input_dev_ptr,
(void*) output_dev_ptr,
input_host_data.size(),
hcclFloat32,
demo_data.hccl_comm,
demo_data.collective_stream));
});
// Correctness check
bool is_ok = true;
auto output_host_data = vector<float>(input_host_data.size() * demo_data.nranks);
const void* output_host_data_ptr = reinterpret_cast<void*>(output_host_data.data());
CHECK_SYNAPSE_STATUS(
synHostMap(demo_data.device_handle, data_size * demo_data.nranks, output_host_data_ptr));
CHECK_SYNAPSE_STATUS(synMemCopyAsync(demo_data.device_to_host_stream,
output_dev_ptr,
data_size * demo_data.nranks,
(uint64_t) output_host_data_ptr,
DRAM_TO_HOST));
CHECK_SYNAPSE_STATUS(synStreamSynchronize(demo_data.device_to_host_stream));
for (size_t i = 0; i < output_host_data.size(); ++i)
{
if (output_host_data[i] != i)
{
is_ok = false;
}
}
log() << "AllGather hccl_rank=" << hccl_rank << " size=" << data_size << " <float>"
<< " Input Buffer [" << input_host_data[0] << " " << input_host_data[1] << " " << input_host_data[2]
<< " " << input_host_data[3] << " ...]"
<< " gathered to Output Buffer [" << output_host_data[0] << " " << output_host_data[1] << " "
<< output_host_data[2] << " " << output_host_data[3] << " ...]"
<< " which is " << (is_ok ? "fine." : "bad.") << endl;
CHECK_SYNAPSE_STATUS(synHostUnmap(demo_data.device_handle, input_host_data_ptr));
CHECK_SYNAPSE_STATUS(synHostUnmap(demo_data.device_handle, output_host_data_ptr));
// End of correctness check
describe_stat("hcclAllGather(src!=dst, count=" + to_string(input_host_data.size()) +
", dtype=fp32, iterations=" + to_string(demo_data.num_iters) + ")",
stat,
data_size,
all_gather_factor,
hccl_rank,
demo_data.num_iters,
test_type,
"float");
}
else if (test_type == "send_recv")
{
double send_recv_factor = 1;
int peerRank = get_hccl_rank() % 2 ? get_hccl_rank() - 1 : get_hccl_rank() + 1;
auto stat = benchmark(demo_data, [&]() {
CHECK_HCCL_STATUS(send_recv_test((void*) output_dev_ptr,
(const void*) input_dev_ptr,
(uint64_t) input_host_data.size(),
demo_data.hccl_comm,
demo_data.collective_stream,
peerRank));
});
// Correctness check
bool is_ok = true;
auto output_host_data = vector<float>(input_host_data.size());
const void* output_host_data_ptr = reinterpret_cast<void*>(output_host_data.data());
CHECK_SYNAPSE_STATUS(synHostMap(demo_data.device_handle, data_size, output_host_data_ptr));
CHECK_SYNAPSE_STATUS(synMemCopyAsync(demo_data.device_to_host_stream,
output_dev_ptr,
data_size,
(uint64_t) output_host_data_ptr,
DRAM_TO_HOST));
CHECK_SYNAPSE_STATUS(synStreamSynchronize(demo_data.device_to_host_stream));
for (size_t i = 0; i < input_host_data.size(); ++i)
{
if (abs(output_host_data[i] - (float) (peerRank + 1)) != 0)
{
is_ok = false;
}
}
log() << "SendRecv hccl_rank=" << hccl_rank << " peerRank=" << peerRank << " size=" << data_size
<< " <float>"
<< " Input Buffer [" << input_host_data[0] << " " << input_host_data[1] << " " << input_host_data[2]
<< " " << input_host_data[3] << " ...]"
<< " Output Buffer [" << output_host_data[0] << " " << output_host_data[1] << " "
<< output_host_data[2] << " " << output_host_data[3] << " ...]"
<< " which is " << (is_ok ? "fine." : "bad.") << endl;
CHECK_SYNAPSE_STATUS(synHostUnmap(demo_data.device_handle, input_host_data_ptr));
CHECK_SYNAPSE_STATUS(synHostUnmap(demo_data.device_handle, output_host_data_ptr));
// End of correctness check
describe_stat("hcclSendRecv(src!=dst, count=" + to_string(input_host_data.size()) +
", dtype=fp32, iterations=" + to_string(demo_data.num_iters) + ")",
stat,
data_size,
send_recv_factor,
hccl_rank,
demo_data.num_iters,
test_type,
"float");
}
else
{
log() << "Unknown test type (" << test_type << ")" << endl;
return -1;
}
CHECK_SYNAPSE_STATUS(synStreamSynchronize(demo_data.collective_stream));
// Destroy HCCL communicator
CHECK_HCCL_STATUS(hcclCommDestroy(demo_data.hccl_comm));
CHECK_SYNAPSE_STATUS(synDeviceFree(demo_data.device_handle, input_dev_ptr, 0));
CHECK_SYNAPSE_STATUS(synDeviceFree(demo_data.device_handle, output_dev_ptr, 0));
// Clean up HCCL
CHECK_SYNAPSE_STATUS(synDeviceRelease(demo_data.device_handle));
// Destroy synapse api context
CHECK_SYNAPSE_STATUS(synDestroy());
}
catch (const exception& ex)
{
log() << "error: " << ex.what() << endl;
return -1;
}
return 0;
}