-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhccl_demo.cpp
874 lines (774 loc) · 30.8 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
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
/******************************************************************************
# Copyright (c) 2022 Habana Labs, Ltd.
# SPDX-License-Identifier: Apache-2.0
******************************************************************************/
#include "common.h"
#include <algorithm> // for std::min_element, std::max_element
#include <chrono> // for Bandwidth calculation
#include <cmath> // for pow
#include <cstddef> // for std::to_string
#include <fstream> // for file stream
#include <numeric> // for std::accumulate
#include <stdexcept> // for std::runtime_error
#include <unistd.h> // for sleep
// Constants
static constexpr size_t MAX_PRINTED_BUFFER_ELEMENTS = 4;
double benchmark(const EnvData& envData,
const DeviceResources& resources,
const std::function<void(uint64_t)>& fn,
const std::function<void()>& fnCorrectness)
{
float rankDurationInSec;
// Run warmup iterations to sync all the gaudis on the device.
size_t iterations = 1;
char default_gdr_input[128];
synConfigurationGet("HCCL_GAUDI_DIRECT", default_gdr_input, sizeof(default_gdr_input));
if (default_gdr_input[0] == '1')
{
iterations = MAX_BUFFER_COUNT;
}
for (size_t warmup_iter = 0; warmup_iter < iterations; ++warmup_iter)
{
fn(warmup_iter);
}
CHECK_SYNAPSE_STATUS(synStreamSynchronize(resources.collectiveStream));
// Actual iterations
auto startTime = std::chrono::high_resolution_clock::now();
for (size_t iter = 1; iter < envData.numIters; ++iter)
{
fn(iter);
}
// Correctness run on separate device output buffer
fnCorrectness();
CHECK_SYNAPSE_STATUS(synStreamSynchronize(resources.collectiveStream));
const auto duration = std::chrono::high_resolution_clock::now() - startTime;
rankDurationInSec = std::chrono::duration_cast<std::chrono::duration<double>>(duration).count();
rankDurationInSec = rankDurationInSec / envData.numIters;
return rankDurationInSec;
}
static void initMPI()
{
#if MPI_ENABLED
// Initialize the Open MPI execution context.
CHECK_MPI_STATUS(MPI_Init(NULL, NULL));
#endif // MPI_ENABLED
}
static HCL_Rank handleCustomComm(EnvData& envData, DeviceResources& resources)
{
// Custom comm is not supported for send_recv and scale_validation
if (envData.testType == "send_recv" || envData.testType == "scale_validation")
{
throw std::runtime_error {"Custom comm is not supported for this test type"};
}
std::vector<HCL_Rank> peers = envData.customComm;
// Choosing new root rank if it is not part of the custom comm.
std::vector<HCL_Rank>::iterator rootIt = find(peers.begin(), peers.end(), envData.root);
if (rootIt == peers.end())
{
rootIt = peers.begin();
envData.root = *peers.begin();
if (isRoot(envData))
{
log() << "While building a new custom communicator, the root rank is automatically set to "
<< *peers.begin() << "." << std::endl;
}
}
// Check if the current rank is part of the custom comm
std::vector<HCL_Rank>::iterator rankIt = find(peers.begin(), peers.end(), envData.rank);
if (rankIt == peers.end())
{
log() << "HCCL demo process id (" << envData.rank << ") will not participate in the custom communicator"
<< std::endl;
#if MPI_ENABLED
hcclUniqueId uniqueID {};
CHECK_MPI_STATUS(MPI_Bcast(&uniqueID, sizeof(uniqueID), MPI_BYTE, envData.root, MPI_COMM_WORLD));
CHECK_MPI_STATUS(MPI_Finalize());
#endif
exit(0);
}
// In the custom comm - override params to match new custom comm
envData.nranks = peers.size();
resources.commRoot = distance(peers.begin(), rootIt);
return distance(peers.begin(), rankIt);
}
static void initDevice(EnvData& envData, DeviceResources& resources)
{
HCL_Rank commRank = envData.rank;
if (envData.customComm.size() == 0)
{
// Generate HCCL comm world
resources.commRoot = envData.root;
for (HCL_Rank i = 0; i < envData.nranks; i++)
{
envData.customComm.push_back(i);
}
}
else
{
commRank = handleCustomComm(envData, resources);
}
// Initialize Synapse API context
CHECK_SYNAPSE_STATUS(synInitialize());
// Acquire device
synModuleId deviceModuleID = envData.rank % envData.ranksPerNode;
synStatus rc = synDeviceAcquireByModuleId(&resources.deviceHandle, deviceModuleID);
if (rc != synSuccess)
{
deviceModuleID = INVALID_MODULE_ID;
CHECK_SYNAPSE_STATUS(synDeviceAcquire(&resources.deviceHandle, nullptr));
}
#if AFFINITY_ENABLED
if (setupAffinity(deviceModuleID) != 0)
{
throw std::runtime_error {"Affinity setting for HCCL demo failed."};
}
#endif
// Generate unique id
hcclUniqueId uniqueID {};
if (isRoot(envData))
{
CHECK_HCCL_STATUS(hcclGetUniqueId(&uniqueID));
}
#if MPI_ENABLED
CHECK_MPI_STATUS(MPI_Bcast(&uniqueID, sizeof(uniqueID), MPI_BYTE, envData.root, MPI_COMM_WORLD));
#endif // MPI_ENABLED
// Create new HCCL communicator
CHECK_HCCL_STATUS(hcclCommInitRank(&resources.comm, envData.nranks, uniqueID, commRank));
// Create Streams
CHECK_SYNAPSE_STATUS(synStreamCreateGeneric(&resources.collectiveStream, resources.deviceHandle, 0));
CHECK_SYNAPSE_STATUS(synStreamCreateGeneric(&resources.deviceToHostStream, resources.deviceHandle, 0));
CHECK_SYNAPSE_STATUS(synStreamCreateGeneric(&resources.hostToDeviceStream, resources.deviceHandle, 0));
}
static void destroyDevice(DeviceResources& resources)
{
// destroy streams
CHECK_SYNAPSE_STATUS(synStreamDestroy(resources.collectiveStream));
CHECK_SYNAPSE_STATUS(synStreamDestroy(resources.deviceToHostStream));
CHECK_SYNAPSE_STATUS(synStreamDestroy(resources.hostToDeviceStream));
// Destroy HCCL communicator
CHECK_HCCL_STATUS(hcclCommDestroy(resources.comm));
// Clean up HCCL
CHECK_SYNAPSE_STATUS(synDeviceRelease(resources.deviceHandle));
// Destroy synapse api context
CHECK_SYNAPSE_STATUS(synDestroy());
}
template<class T>
static void createCSVReport(const EnvData& envData, const std::string& type, std::vector<T> data, int count)
{
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream datetime;
datetime << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d_%H%M");
auto csvPath = envData.dataCSVPath + "/" + "HCCL_demo_" + envData.testType + "_" + type + "_" +
std::to_string(envData.rank) + "_" + datetime.str() + ".csv";
std::fstream fout;
fout.open(csvPath, std::ios::out | std::ios::app);
for (int i = 0; i < count; i++)
{
const std::string space_char = i % 32 == 0 && i != 0 ? "\n" : "\t";
if (isBfloat16)
{
fout << bf16ToFloat(data[i]) << space_char;
}
else
{
fout << data[i] << space_char;
}
}
fout.close();
}
static float calcExpectedReduction(std::vector<float>& args, hcclRedOp_t redop)
{
switch (redop)
{
case hcclSum:
return std::accumulate(args.cbegin(), args.cend(), 0);
case hcclMin:
return *std::min_element(args.cbegin(), args.cend());
case hcclMax:
return *std::max_element(args.cbegin(), args.cend());
default:
throw std::runtime_error {" Unknown reduction op."};
}
}
static void getExpectedOutputs(const EnvData& envData, const Buffers& buffers, std::vector<float>& expectedOutputs)
{
size_t inputCount = buffers.inputSize / getDataTypeSize(envData);
size_t outputCount = buffers.outputSize / getDataTypeSize(envData);
HCL_Rank inputRank = 0;
size_t inputIdx = 0;
for (size_t i = 0; i < outputCount; i++)
{
if (envData.testType == "broadcast")
{
// Output is not defined for root rank
if (isRoot(envData))
{
return;
}
// Every rank gets root input
inputRank = envData.root;
inputIdx = i;
}
else if (envData.testType == "all_reduce")
{
// 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
inputIdx = i;
}
else if (envData.testType == "reduce_scatter")
{
// 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
inputIdx = i + envData.rank * (inputCount / envData.nranks);
}
else if (envData.testType == "all_gather")
{
// 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
inputRank = i / inputCount;
inputIdx = i % inputCount;
}
else if (envData.testType == "all2all")
{
// Fill input data, example:
// Input | Output
// G0 G1 G2 G3 G0 G1 G2 G3
// 0 2 4 6 => 0 4 8 12
// 1 3 5 7 1 5 9 13
// 4 5 8 10 2 6 10 14
// 5 6 9 11 3 7 11 15
// 8 10 12 14 4 8 12 16
// 9 11 13 15 5 9 13 17
// 12 14 16 18 6 10 14 18
// 13 15 17 19 7 11 15 19
inputRank = i / (inputCount / envData.nranks);
inputIdx = i % (inputCount / envData.nranks) + envData.rank * (inputCount / envData.nranks);
}
else if (envData.testType == "reduce")
{
// Output is not defined for non-root ranks
if (envData.rank != envData.root)
{
return;
}
// Fill input data, example:
// root = G1
// Input | Output
// G0 G1 G2 G3 G0 G1 G2 G3
// 0 1 2 3 => 6
// 4 5 6 7 22
// 8 9 10 11 38
// 12 13 14 15 54
inputIdx = i;
}
if (envData.testType.find("reduce") != std::string::npos)
{
std::vector<float> inputs(envData.nranks);
for (HCL_Rank rank = 0; rank < envData.nranks; rank++)
{
inputs[rank] = getInput(envData.customComm[rank], envData.nranks, inputIdx);
}
expectedOutputs.push_back(calcExpectedReduction(inputs, getReductionOp(envData)));
}
else
{
expectedOutputs.push_back(getInput(envData.customComm[inputRank], envData.nranks, inputIdx));
}
}
}
static void
prepareBuffers(const EnvData& envData, const DeviceResources& resources, const uint64_t size, Buffers& buffers)
{
// Calculate buffers sizes
buffers.inputSize = size;
if (envData.testType == "all_gather")
{
buffers.outputSize = size * envData.nranks;
}
else if (envData.testType == "reduce_scatter")
{
buffers.outputSize = size / envData.nranks;
}
else
{
buffers.outputSize = size;
}
// Validate calculated buffer size.
if (buffers.inputSize < getDataTypeSize(envData) || buffers.outputSize < getDataTypeSize(envData))
{
throw std::runtime_error {"Invalid buffer size"};
}
// Calculate number of buffers
const uint64_t maxBufferSize = std::max(buffers.inputSize, buffers.outputSize);
uint64_t numberOfBuffers = 2;
if (maxBufferSize <= ALLOCATED_HBM_SIZE)
{
numberOfBuffers = (ALLOCATED_HBM_SIZE / maxBufferSize) <= AMOUNT_JUMBO_BUFFERS
? AMOUNT_JUMBO_BUFFERS
: ALLOCATED_HBM_SIZE / maxBufferSize;
}
numberOfBuffers = std::min(numberOfBuffers, MAX_BUFFER_COUNT);
// Allocate buffers on the device
uint64_t inputDevPtr = 0;
uint64_t outputDevPtr = 0;
buffers.correctnessDevPtr = 0;
CHECK_SYNAPSE_STATUS(
synDeviceMalloc(resources.deviceHandle, buffers.inputSize * numberOfBuffers, 0, 0, &inputDevPtr));
CHECK_SYNAPSE_STATUS(
synDeviceMalloc(resources.deviceHandle, buffers.outputSize * numberOfBuffers, 0, 0, &outputDevPtr));
for (uint64_t index = 0; index < numberOfBuffers; index++)
{
buffers.inputDevPtrs.push_back(inputDevPtr + (index * buffers.inputSize));
buffers.outputDevPtrs.push_back(outputDevPtr + (index * buffers.outputSize));
}
// Set default correctness buffer on the device
buffers.correctnessDevPtr = buffers.outputDevPtrs[0];
}
template<class T>
static void
generateInputs(const EnvData& envData, const DeviceResources& resources, const uint64_t size, Buffers& buffers)
{
// Allocate correctness buffer on the device
CHECK_SYNAPSE_STATUS(synDeviceMalloc(resources.deviceHandle, buffers.outputSize, 0, 0, &buffers.correctnessDevPtr));
// Allocate temp host buffer
std::vector<T> inputHostData(buffers.inputSize / getDataTypeSize(envData));
void* inputHostDataPtr = reinterpret_cast<void*>(inputHostData.data());
// Create inputs
for (size_t i = 0; i < inputHostData.size(); i++)
{
float value = getInput(envData.rank, envData.nranks, i);
if (isBfloat16(envData))
{
inputHostData[i] = floatToBf16(value);
}
else
{
inputHostData[i] = value;
}
}
// Copy from inputHostDataPtr to inputDevPtr (to be used in benchmark)
CHECK_SYNAPSE_STATUS(synHostMap(resources.deviceHandle, buffers.inputSize, inputHostDataPtr));
CHECK_SYNAPSE_STATUS(synMemCopyAsync(resources.hostToDeviceStream,
(uint64_t) inputHostDataPtr,
buffers.inputSize,
buffers.inputDevPtrs[0],
HOST_TO_DRAM));
CHECK_SYNAPSE_STATUS(synStreamSynchronize(resources.hostToDeviceStream));
CHECK_SYNAPSE_STATUS(synHostUnmap(resources.deviceHandle, inputHostDataPtr));
// Save the input data to a CSV file if requested
if (!envData.dataCSVPath.empty())
{
createCSVReport(envData, "input", inputHostData, buffers.inputSize / getDataTypeSize(envData));
}
}
static void clearBuffers(const DeviceResources& resources, Buffers& buffers)
{
CHECK_SYNAPSE_STATUS(synDeviceFree(resources.deviceHandle, buffers.inputDevPtrs[0], 0));
CHECK_SYNAPSE_STATUS(synDeviceFree(resources.deviceHandle, buffers.outputDevPtrs[0], 0));
if (buffers.correctnessDevPtr != buffers.outputDevPtrs[0])
{
CHECK_SYNAPSE_STATUS(synDeviceFree(resources.deviceHandle, buffers.correctnessDevPtr, 0));
}
}
static bool compareBf16(EnvData envData, float expected, float outValue, int i)
{
const float accuracyCoefficient = bf16AccuracyCoefficient(envData.nranks);
const float tolerance = fabs(outValue) * accuracyCoefficient;
const float difference = fabs(outValue - expected);
if (difference > tolerance)
{
log() << "index=" << i << ", expectedValue=" << expected << ", value=" << outValue
<< ", thisRankId=" << envData.rank << ", tolerance=" << tolerance << ", difference=" << difference
<< ", accuracyCoefficient=" << accuracyCoefficient << ", m_numberOfRanks=" << envData.nranks << std::endl;
return false;
}
return true;
}
template<class T>
static bool checkCorrectness(const EnvData& envData,
const DeviceResources& resources,
const Buffers& buffers,
std::vector<float>& expectedOutputs)
{
bool isOK = true;
auto outputHostData = std::vector<T>(buffers.outputSize / getDataTypeSize(envData));
const void* outputHostDataPtr = reinterpret_cast<void*>(outputHostData.data());
CHECK_SYNAPSE_STATUS(synHostMap(resources.deviceHandle, buffers.outputSize, outputHostDataPtr));
CHECK_SYNAPSE_STATUS(synMemCopyAsync(resources.deviceToHostStream,
buffers.correctnessDevPtr,
buffers.outputSize,
(uint64_t) outputHostDataPtr,
DRAM_TO_HOST));
CHECK_SYNAPSE_STATUS(synStreamSynchronize(resources.deviceToHostStream));
CHECK_SYNAPSE_STATUS(synHostUnmap(resources.deviceHandle, outputHostDataPtr));
for (size_t i = 0; i < outputHostData.size(); i++)
{
if (i < expectedOutputs.size())
{
if (isBfloat16(envData))
{
isOK &= compareBf16(envData, expectedOutputs[i], bf16ToFloat(outputHostData[i]), i);
}
else
{
if (std::abs((float) outputHostData[i] - expectedOutputs[i]) != 0)
{
isOK = false;
}
}
}
}
if (expectedOutputs.size() > 0)
{
log() << "rank=" << envData.rank << " size=" << buffers.inputSize << " <" << envData.dataType << ">";
// Print input buffer.
log() << " Input Buffer [";
for (size_t i = 0; i < std::min(MAX_PRINTED_BUFFER_ELEMENTS, buffers.inputSize / getDataTypeSize(envData)); i++)
{
log() << getInput(envData.rank, envData.nranks, i) << " ";
}
if (buffers.inputSize / getDataTypeSize(envData) > MAX_PRINTED_BUFFER_ELEMENTS)
{
log() << "...";
}
log() << "]";
// Print output buffer.
log() << " Output Buffer [";
for (size_t i = 0; i < std::min(MAX_PRINTED_BUFFER_ELEMENTS, outputHostData.size()); i++)
{
log() << getFloat(outputHostData[i]) << " ";
}
if (outputHostData.size() > MAX_PRINTED_BUFFER_ELEMENTS)
{
log() << "...";
}
log() << "]";
// Print correctness check result.
log() << " which is " << (isOK ? "fine." : "bad.") << std::endl;
}
if (!envData.dataCSVPath.empty())
{
createCSVReport(envData, "output", outputHostData, buffers.outputSize / getDataTypeSize(envData));
}
return isOK;
}
static void
describeStat(const EnvData& envData, const Buffers& buffers, const Stats& stats, std::vector<ReportEntry>& reportVec)
{
auto algoBW = (double) buffers.inputSize / stats.rankDurationInSec;
auto nwBW = algoBW * stats.factor;
if (envData.sizeMax > envData.sizeMin)
{
log() << "Processing data_size " << envData.rank << " is done with " << envData.testType << " test."
<< std::endl;
ReportEntry report_entry = {buffers.inputSize,
(uint64_t) (buffers.inputSize / getDataTypeSize(envData)),
stats.rankDurationInSec,
algoBW,
nwBW};
reportVec.push_back(report_entry);
}
else
{
// Sleep in order to describe stats after all correctness logs.
sleep(1);
std::string statHeadline = stats.statName + "(dataSize=" + std::to_string(buffers.inputSize) +
", count=" + std::to_string(buffers.inputSize / getDataTypeSize(envData)) +
", dtype=" + envData.dataType + ", iterations=" + std::to_string(envData.numIters) +
")";
size_t delimiterSize = statHeadline.length() + std::string {"[BENCHMARK]"}.length() + 1;
log() << getPrintDelimiter(delimiterSize, '#') << '\n';
log() << "[BENCHMARK] " << statHeadline << '\n';
log() << "[BENCHMARK] NW Bandwidth : " << formatBW(nwBW) << '\n';
log() << "[BENCHMARK] Algo Bandwidth : " << formatBW(algoBW);
log() << '\n' << getPrintDelimiter(delimiterSize, '#') << '\n';
}
// Write results to csv file
auto csvPath = envData.resultsCSVPath;
if (!csvPath.empty())
{
std::ofstream output;
output.open(csvPath, std::ofstream::out | std::ofstream::app);
output << envData.testType << "," << envData.rank << "," << envData.dataType << "," << buffers.inputSize << ","
<< envData.numIters << "," << formatBW(nwBW) << std::endl;
output.close();
}
}
using CollectiveWrapper = std::function<void(
const EnvData& envData, const DeviceResources& resources, const void* sendbuff, void* recvbuff, size_t recvcount)>;
static void hcclBroadcastWrapper(
const EnvData& envData, const DeviceResources& resources, const void* sendbuff, void* recvbuff, size_t recvcount)
{
CHECK_HCCL_STATUS(hcclBroadcast(sendbuff,
recvbuff,
recvcount,
getDataType(envData),
resources.commRoot,
resources.comm,
resources.collectiveStream));
}
static void hcclAllReduceWrapper(
const EnvData& envData, const DeviceResources& resources, const void* sendbuff, void* recvbuff, size_t recvcount)
{
CHECK_HCCL_STATUS(hcclAllReduce(sendbuff,
recvbuff,
recvcount,
getDataType(envData),
getReductionOp(envData),
resources.comm,
resources.collectiveStream));
}
static void hcclReduceScatterWrapper(
const EnvData& envData, const DeviceResources& resources, const void* sendbuff, void* recvbuff, size_t recvcount)
{
CHECK_HCCL_STATUS(hcclReduceScatter(sendbuff,
recvbuff,
recvcount / envData.nranks,
getDataType(envData),
getReductionOp(envData),
resources.comm,
resources.collectiveStream));
}
static void hcclAllGatherWrapper(
const EnvData& envData, const DeviceResources& resources, const void* sendbuff, void* recvbuff, size_t recvcount)
{
CHECK_HCCL_STATUS(
hcclAllGather(sendbuff, recvbuff, recvcount, getDataType(envData), resources.comm, resources.collectiveStream));
}
static void hcclAlltoAllWrapper(
const EnvData& envData, const DeviceResources& resources, const void* sendbuff, void* recvbuff, size_t recvcount)
{
CHECK_HCCL_STATUS(
hcclAlltoAll(sendbuff, recvbuff, recvcount, getDataType(envData), resources.comm, resources.collectiveStream));
}
static void hcclReduceWrapper(
const EnvData& envData, const DeviceResources& resources, const void* sendbuff, void* recvbuff, size_t recvcount)
{
CHECK_HCCL_STATUS(hcclReduce(sendbuff,
recvbuff,
recvcount,
getDataType(envData),
getReductionOp(envData),
resources.commRoot,
resources.comm,
resources.collectiveStream));
}
static void collectiveTestDriver(
const EnvData& envData, const DeviceResources& resources, const Buffers& buffers, const uint64_t size, Stats& stats)
{
CollectiveWrapper collective;
if (envData.testType == "broadcast")
{
stats.statName = "hcclBroadcast";
stats.factor = 1;
collective = hcclBroadcastWrapper;
}
else if (envData.testType == "all_reduce")
{
stats.statName = "hcclAllReduce";
stats.factor = ((double) (2 * (envData.nranks - 1))) / ((double) envData.nranks);
collective = hcclAllReduceWrapper;
}
else if (envData.testType == "reduce_scatter")
{
stats.statName = "hcclReduceScatter";
stats.factor = ((double) (envData.nranks - 1)) / ((double) envData.nranks);
collective = hcclReduceScatterWrapper;
}
else if (envData.testType == "all_gather")
{
stats.statName = "hcclAllGather";
stats.factor = ((double) (envData.nranks - 1));
collective = hcclAllGatherWrapper;
}
else if (envData.testType == "all2all")
{
stats.statName = "hcclAlltoAll";
stats.factor = ((double) (envData.nranks - 1)) / ((double) envData.nranks);
collective = hcclAlltoAllWrapper;
}
else if (envData.testType == "reduce")
{
stats.statName = "hcclReduce";
stats.factor = 1;
collective = hcclReduceWrapper;
}
else
{
log() << "Unknown test type (" << envData.testType << ")" << std::endl;
return;
}
// Run HCCL collective
stats.rankDurationInSec = benchmark(
envData,
resources,
[&](uint64_t iter) {
uint64_t index = iter % buffers.inputDevPtrs.size();
collective(envData,
resources,
(const void*) buffers.inputDevPtrs[index],
(void*) buffers.outputDevPtrs[index],
buffers.inputSize / getDataTypeSize(envData));
},
[&]() {
collective(envData,
resources,
(const void*) buffers.inputDevPtrs[0],
(void*) buffers.correctnessDevPtr,
buffers.inputSize / getDataTypeSize(envData));
});
if (envData.shouldCheckCorrectness)
{
getExpectedOutputs(envData, buffers, stats.expectedOutputs);
}
if (isRoot(envData))
{
stats.isDescribing = true;
}
}
static void printReport(const EnvData& envData, const std::vector<ReportEntry>& reportVec)
{
constexpr size_t columnWidth = 14;
const static std::vector<std::string> header = {"size", "count", "type", "redop", "time", "algoBW", "nw_bw"};
const static std::vector<std::string> units = {"(B)", "(elements)", "", "", "(ms)", "(GB/s)", "(GB/s)"};
std::stringstream ss;
const std::string summary = "[SUMMARY REPORT]";
const std::string statName =
"(src!=dst, collective=" + envData.testType + ", iterations=" + std::to_string(envData.numIters) + ")";
size_t delimiterSize = statName.length() + 1;
ss << '\n' << getPrintDelimiter(delimiterSize, '#') << std::endl;
ss << summary << '\n' << statName << '\n' << std::endl;
ss << std::left;
// print header
for (size_t i = 0; i < header.size(); ++i)
{
if (header[i] == "redop" && envData.redop == "") continue;
ss << std::setw(columnWidth) << header[i];
}
ss << std::endl;
// print units
for (size_t i = 0; i < units.size(); ++i)
{
if (header[i] == "redop" && envData.redop == "") continue;
ss << std::setw(columnWidth) << units[i];
}
ss << std::endl;
// print stats for each data size
for (const auto& entry : reportVec)
{
ss << std::setw(columnWidth) << std::to_string(entry.size) << std::setw(columnWidth)
<< std::to_string(entry.count) << std::setw(columnWidth) << envData.dataType;
if (envData.redop != "")
{
ss << std::setw(columnWidth) << envData.redop;
}
ss << std::setw(columnWidth) << std::fixed << std::setprecision(3) << entry.time * 1000
<< std::setw(columnWidth) << std::fixed << std::setprecision(6) << entry.algoBW / 1e9
<< std::setw(columnWidth) << std::fixed << std::setprecision(6) << entry.avgBW / 1e9 << std::endl;
}
log() << ss.str();
}
template<class T>
static void runTest(EnvData& envData, const DeviceResources& resources)
{
bool isOK = true;
std::vector<ReportEntry> reportVec;
for (double size = envData.sizeMin; size <= envData.sizeMax; size *= pow(2, envData.sizeInc))
{
Buffers buffers;
prepareBuffers(envData, resources, size, buffers);
if (envData.shouldCheckCorrectness)
{
generateInputs<T>(envData, resources, size, buffers);
}
// Perform test
Stats stats;
if (envData.testType == "send_recv")
{
sendRecvTestDriver(envData, resources, buffers, static_cast<uint64_t>(size), stats);
}
else if (envData.testType == "scale_validation")
{
#ifdef MPI_ENABLED
// since there is no correctness check here the data can be random (no need to initialize)
scaleValidationTestDriver(envData, resources, buffers, static_cast<uint64_t>(size));
#else
throw std::runtime_error {"MPI must be enabled for scale validation test"};
#endif //MPI_ENABLED
}
else
{
collectiveTestDriver(envData, resources, buffers, static_cast<uint64_t>(size), stats);
}
if (envData.shouldCheckCorrectness && !checkCorrectness<T>(envData, resources, buffers, stats.expectedOutputs))
{
isOK = false;
}
if (stats.isDescribing)
{
describeStat(envData, buffers, stats, reportVec);
}
clearBuffers(resources, buffers);
}
if (reportVec.size() > 0)
{
printReport(envData, reportVec);
}
if (!isOK) throw std::runtime_error {"Collective operation has failed on correctness."};
}
int main()
{
try
{
initMPI();
auto envData = getenvData();
if (isRoot(envData))
{
#if MPI_ENABLED
log() << "MPI enabled. Make sure that HCCL demo is launched with mpirun." << std::endl;
#endif // MPI_ENABLED
log() << "Running HCCL Demo :: A simple program demonstrating HCCL usage from C++" << std::endl;
}
// Init device
DeviceResources resources;
initDevice(envData, resources);
if (isBfloat16(envData))
{
runTest<uint16_t>(envData, resources);
}
else
{
runTest<float>(envData, resources);
}
destroyDevice(resources);
#if MPI_ENABLED
CHECK_MPI_STATUS(MPI_Finalize());
#endif // MPI_ENABLED
}
catch (const std::exception& ex)
{
log() << "HCCL demo error: " << ex.what() << std::endl;
return -1;
}
return 0;
}