EnvVars.hpp 35.4 KB
Newer Older
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
/*
Copyright (c) 2021-2025 Advanced Micro Devices, Inc. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#ifndef ENVVARS_HPP
#define ENVVARS_HPP

#include "Client.hpp"
#include "TransferBench.hpp"

#include <numa.h>
#include <time.h>

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <random>
#include <utility>


// Helper macro for catching HIP errors
// clang-format off
#define HIP_CALL(cmd)                                                                          \
    do {                                                                                       \
        hipError_t error = (cmd);                                                              \
        if (error != hipSuccess) {                                                             \
            std::cerr << "Encountered HIP error (" << hipGetErrorString(error) << ") at line " \
                      << __LINE__ << " in file " << __FILE__ << "\n";                          \
            exit(-1);                                                                          \
        }                                                                                      \
    } while (0)
// clang-format off

using namespace TransferBench;

// Redefinitions for CUDA compatibility
//==========================================================================================
// clang-format off
#if defined(__NVCC__)
#define hipError_t                  cudaError_t
#define hipGetErrorString           cudaGetErrorString
#define hipDeviceProp_t             cudaDeviceProp
#define hipDeviceGetPCIBusId        cudaDeviceGetPCIBusId
#define hipGetDeviceProperties      cudaGetDeviceProperties
#define hipSuccess                  cudaSuccess
#define gcnArchName                 name
#define hipGetDeviceCount           cudaGetDeviceCount
#endif
// clang-format on

// This class manages environment variable that affect TransferBench
class EnvVars
{
    public:
        // Default configuration values
        int const DEFAULT_SAMPLING_FACTOR = 1;

        // Environment variables
        // General options
        int numIterations;       // Number of timed iterations to perform.  If negative, run for
                                 // -numIterations seconds instead
        int numSubIterations;    // Number of subiterations to perform
        int numWarmups;          // Number of un-timed warmup iterations to perform
        int showIterations;      // Show per-iteration timing info
        int useInteractive;      // Pause for user-input before starting transfer loop

        // Data options
        int alwaysValidate;    // Validate after each iteration instead of once after all iterations
        int blockBytes;    // Each subexecutor, except the last, gets a multiple of this many bytes
                           // to copy
        int byteOffset;    // Byte-offset for memory allocations
        vector<float> fillPattern;    // Pattern of floats used to fill source data
        // Percentages of 64B lines to be filled by random/1B0/2B0/4B0/32B0
        vector<int>fillCompress;
        int validateDirect;    // Validate GPU destination memory directly instead of staging GPU
                               // memory on host
        int validateSource;    // Validate source GPU memory immediately after preparation

        // DMA options
        int useHsaDma;    // Use hsa_amd_async_copy instead of hipMemcpy for non-targetted DMA
                          // executions

        // GFX options
        int gfxBlockOrder;    // How threadblocks for multiple Transfers are ordered 0=sequential
                              // 1=interleaved
        int gfxBlockSize;     // Size of each threadblock (must be multiple of 64)
        vector<uint32_t> cuMask;             // Bit-vector representing the CU mask
        vector<vector<int>> prefXccTable;    // Specifies XCC to use for given exe->dst pair
        int gfxTemporal;        // Non-temporal load/store mode (0=none, 1=load, 2=store, 3=both)
        int gfxUnroll;          // GFX-kernel unroll factor
        int useHipEvents;       // Use HIP events for timing GFX/DMA Executor
        int useSingleStream;    // Use a single stream per GPU GFX executor instead of stream per
                                // Transfer
        int gfxSingleTeam;      // Team all subExecutors across the data array
        int gfxWaveOrder;       // GFX-kernel wavefront ordering
        int gfxWordSize;        // GFX-kernel packed data size (4=DWORDx4, 2=DWORDx2, 1=DWORDx1)

        // Client options
        int hideEnv;             // Skip printing environment variable
        int minNumVarSubExec;    // Minimum # of subexecutors to use for variable subExec Transfers
        int maxNumVarSubExec;    // Maximum # of subexecutors to use for variable subExec Transfers
                                 // (0 to use device limit)
        int outputToCsv;         // Output in CSV format
        int samplingFactor;      // Affects how many different values of N are generated (when N set
                                 // to 0)

        // NIC options
        int ibGidIndex;               // GID Index for RoCE NICs
        int roceVersion;              // RoCE version number
        int ipAddressFamily;          // IP Address Famliy
        uint8_t ibPort;               // NIC port number to be used
        int nicRelaxedOrder;          // Use relaxed ordering for RDMA
        std::string closestNicStr;    // Holds the user-specified list of closest NICs

        // Developer features
        int gpuMaxHwQueues;    // Tracks GPU_MAX_HW_QUEUES environment variable

        // Constructor that collects values
        EnvVars()
        {
            int numDetectedCpus = TransferBench::GetNumExecutors(EXE_CPU);
            int numDetectedGpus = TransferBench::GetNumExecutors(EXE_GPU_GFX);
            int numDeviceCUs    = TransferBench::GetNumSubExecutors({EXE_GPU_GFX, 0});

            hipDeviceProp_t prop;
            HIP_CALL(hipGetDeviceProperties(&prop, 0));
            std::string fullName = prop.gcnArchName;
            std::string archName = fullName.substr(0, fullName.find(':'));

            // Different hardware pick different GPU kernels
            // This performance difference is generally only noticable when executing fewer CUs
            int defaultGfxUnroll = 4;
            if (archName == "gfx906") {
                defaultGfxUnroll = 8;
            } else if (archName == "gfx90a") {
                defaultGfxUnroll = 8;
            } else if (archName == "gfx942") {
                defaultGfxUnroll = 4;
            } else if (archName == "gfx950") {
                defaultGfxUnroll = 4;
            }

            alwaysValidate   = GetEnvVar("ALWAYS_VALIDATE", 0);
            blockBytes       = GetEnvVar("BLOCK_BYTES", 256);
            byteOffset       = GetEnvVar("BYTE_OFFSET", 0);
            fillCompress     = GetEnvVarArray("FILL_COMPRESS", {});
            gfxBlockOrder    = GetEnvVar("GFX_BLOCK_ORDER", 0);
            gfxBlockSize     = GetEnvVar("GFX_BLOCK_SIZE", 256);
            gfxSingleTeam    = GetEnvVar("GFX_SINGLE_TEAM", 1);
            gfxTemporal      = GetEnvVar("GFX_TEMPORAL", 0);
            gfxUnroll        = GetEnvVar("GFX_UNROLL", defaultGfxUnroll);
            gfxWaveOrder     = GetEnvVar("GFX_WAVE_ORDER", 0);
            gfxWordSize      = GetEnvVar("GFX_WORD_SIZE", 4);
            hideEnv          = GetEnvVar("HIDE_ENV", 0);
            minNumVarSubExec = GetEnvVar("MIN_VAR_SUBEXEC", 1);
            maxNumVarSubExec = GetEnvVar("MAX_VAR_SUBEXEC", 0);
            numIterations    = GetEnvVar("NUM_ITERATIONS", 10);
            numSubIterations = GetEnvVar("NUM_SUBITERATIONS", 1);
            numWarmups       = GetEnvVar("NUM_WARMUPS", 3);
            outputToCsv      = GetEnvVar("OUTPUT_TO_CSV", 0);
            samplingFactor   = GetEnvVar("SAMPLING_FACTOR", 1);
            showIterations   = GetEnvVar("SHOW_ITERATIONS", 0);
            useHipEvents     = GetEnvVar("USE_HIP_EVENTS", 1);
            useHsaDma        = GetEnvVar("USE_HSA_DMA", 0);
            useInteractive   = GetEnvVar("USE_INTERACTIVE", 0);
            useSingleStream  = GetEnvVar("USE_SINGLE_STREAM", 1);
            validateDirect   = GetEnvVar("VALIDATE_DIRECT", 0);
            validateSource   = GetEnvVar("VALIDATE_SOURCE", 0);

            ibGidIndex      = GetEnvVar("IB_GID_INDEX", -1);
            ibPort          = GetEnvVar("IB_PORT_NUMBER", 1);
            roceVersion     = GetEnvVar("ROCE_VERSION", 2);
            ipAddressFamily = GetEnvVar("IP_ADDRESS_FAMILY", 4);
            nicRelaxedOrder = GetEnvVar("NIC_RELAX_ORDER", 1);
            closestNicStr   = GetEnvVar("CLOSEST_NIC", "");

            gpuMaxHwQueues = GetEnvVar("GPU_MAX_HW_QUEUES", 4);


            // Check for fill pattern
            char* pattern = getenv("FILL_PATTERN");
            if (pattern != NULL) {
                int patternLen = strlen(pattern);
                if (patternLen % 2) {
                    printf("[ERROR] FILL_PATTERN must contain an even-number of hex digits\n");
                    exit(1);
                }

                // Read in bytes
                std::vector<unsigned char> bytes;
                unsigned char val = 0;
                for (int i = 0; i < patternLen; i++) {
                    if ('0' <= pattern[i] && pattern[i] <= '9') {
                        val += (pattern[i] - '0');
                    } else if ('A' <= pattern[i] && pattern[i] <= 'F') {
                        val += (pattern[i] - 'A' + 10);
                    } else if ('a' <= pattern[i] && pattern[i] <= 'f') {
                        val += (pattern[i] - 'a' + 10);
                    } else {
                        printf(
                            "[ERROR] FILL_PATTERN must contain an even-number of hex digits "
                            "(0-9'/a-f/A-F).  (not %c)\n",
                            pattern[i]);
                        exit(1);
                    }

                    if (i % 2 == 0) {
                        val <<= 4;
                    } else {
                        bytes.push_back(val);
                        val = 0;
                    }
                }

                // Reverse bytes (input is assumed to be given in big-endian)
                std::reverse(bytes.begin(), bytes.end());

                // Figure out how many copies of the pattern are necessary to fill a 4-byte float
                // properly
                int copies;
                switch (patternLen % 8) {
                    case 0: copies = 1; break;
                    case 4: copies = 2; break;
                    default: copies = 4; break;
                }

                // Fill floats
                int numFloats = copies * patternLen / 8;
                fillPattern.resize(numFloats);
                unsigned char* rawData = (unsigned char*)fillPattern.data();
                for (int i = 0; i < numFloats * 4; i++) { rawData[i] = bytes[i % bytes.size()]; }
            } else {
                fillPattern.clear();
            }

            // Check for CU mask
            int numXccs = TransferBench::GetNumExecutorSubIndices({EXE_GPU_GFX, 0});
            cuMask.clear();
            char* cuMaskStr = getenv("CU_MASK");
            if (cuMaskStr != NULL) {
#if defined(__NVCC__)
                printf("[WARN] CU_MASK is not supported in CUDA\n");
#else
                std::vector<std::pair<int, int>> ranges;
                int maxCU   = 0;
                char* token = strtok(cuMaskStr, ",");
                while (token) {
                    int start, end;
                    if (sscanf(token, "%d-%d", &start, &end) == 2) {
                        ranges.push_back(
                            std::make_pair(std::min(start, end), std::max(start, end)));
                        maxCU = std::max(maxCU, std::max(start, end));
                    } else if (sscanf(token, "%d", &start) == 1) {
                        ranges.push_back(std::make_pair(start, start));
                        maxCU = std::max(maxCU, start);
                    } else {
                        printf("[ERROR] Unrecognized token [%s]\n", token);
                        exit(1);
                    }
                    token = strtok(NULL, ",");
                }
                cuMask.resize(2 * numXccs, 0);

                for (auto range : ranges) {
                    for (int i = range.first; i <= range.second; i++) {
                        for (int x = 0; x < numXccs; x++) {
                            int targetBit = i * numXccs + x;
                            cuMask[targetBit / 32] |= (1 << (targetBit % 32));
                        }
                    }
                }
#endif
            }

            // Parse preferred XCC table (if provided)
            char* prefXccStr = getenv("XCC_PREF_TABLE");
            if (prefXccStr) {
                prefXccTable.resize(numDetectedGpus);
                for (int i = 0; i < numDetectedGpus; i++) {
                    prefXccTable[i].resize(numDetectedGpus, -1);
                }
                char* token    = strtok(prefXccStr, ",");
                int tokenCount = 0;
                while (token) {
                    int xccId;
                    if (sscanf(token, "%d", &xccId) == 1) {
                        int src = tokenCount / numDetectedGpus;
                        int dst = tokenCount % numDetectedGpus;
                        if (xccId < 0 || xccId >= numXccs) {
                            printf(
                                "[ERROR] XCC index (%d) out of bounds. Expect value less than %d\n",
                                xccId,
                                numXccs);
                            exit(1);
                        }
                        prefXccTable[src][dst] = xccId;

                        tokenCount++;
                        if (tokenCount == (numDetectedGpus * numDetectedGpus)) { break; }
                    } else {
                        printf("[ERROR] Unrecognized token [%s]\n", token);
                        exit(1);
                    }
                    token = strtok(NULL, ",");
                }
            }
        }

        static std::string ToStr(std::vector<int> const& values)
        {
            std::string result = "";
            bool isFirst       = true;
            for (int v : values) {
                if (isFirst) {
                    isFirst = false;
                } else {
                    result += ",";
                }
                result += std::to_string(v);
            }
            return result;
        }

        // Display info on the env vars that can be used
        static void DisplayUsage()
        {
            printf("Environment variables:\n");
            printf("======================\n");
            printf(
                " ALWAYS_VALIDATE   - Validate after each iteration instead of once after all "
                "iterations\n");
            printf(
                " BLOCK_BYTES       - Controls granularity of how work is divided across "
                "subExecutors\n");
            printf(
                " BYTE_OFFSET       - Initial byte-offset for memory allocations.  Must be "
                "multiple of 4\n");
#if NIC_EXEC_ENABLED
            printf(
                " CLOSEST_NIC       - Comma-separated list of per-GPU closest NIC "
                "(default=auto)\n");
#endif
            printf(
                " CU_MASK           - CU mask for streams. Can specify ranges e.g '5,10-12,14'\n");
            printf(
                " FILL_COMPRESS     - Percentages of 64B lines to be filled by "
                "random/1B0/2B0/4B0/32B0\n");
            printf(
                " FILL_PATTERN      - Big-endian pattern for source data, specified in hex digits. "
                "Must be even # of digits\n");
            printf(
                " GFX_BLOCK_ORDER   - How blocks for transfers are ordered. 0=sequential, "
                "1=interleaved\n");
            printf(" GFX_BLOCK_SIZE    - # of threads per threadblock (Must be multiple of 64)\n");
            printf(
                " GFX_TEMPORAL      - Use of non-temporal loads or stores (0=none 1=loads 2=stores "
                "3=both)\n");
            printf(
                " GFX_UNROLL        - Unroll factor for GFX kernel (0=auto), must be less than "
                "%d\n",
                TransferBench::GetIntAttribute(ATR_GFX_MAX_UNROLL));
            printf(
                " GFX_SINGLE_TEAM   - Have subexecutors work together on full array instead of "
                "working on disjoint subarrays\n");
            printf(
                " GFX_WAVE_ORDER    - Stride pattern for GFX kernel "
                "(0=UWC,1=UCW,2=WUC,3=WCU,4=CUW,5=CWU)\n");
            printf(
                " GFX_WORD_SIZE     - GFX kernel packed data size (4=DWORDx4, 2=DWORDx2, "
                "1=DWORDx1)\n");
            printf(" HIDE_ENV          - Hide environment variable value listing\n");
#if NIC_EXEC_ENABLED
            printf(" IB_GID_INDEX      - Required for RoCE NICs (default=-1/auto)\n");
            printf(" IB_PORT_NUMBER    - RDMA port count for RDMA NIC (default=1)\n");
            printf(" IP_ADDRESS_FAMILY - IP address family (4=v4, 6=v6, default=v4)\n");
#endif
            printf(
                " MIN_VAR_SUBEXEC   - Minumum # of subexecutors to use for variable subExec "
                "Transfers\n");
            printf(
                " MAX_VAR_SUBEXEC   - Maximum # of subexecutors to use for variable subExec "
                "Transfers (0 for device limits)\n");
#if NIC_EXEC_ENABLED
            printf(" NIC_RELAX_ORDER   - Set to non-zero to use relaxed ordering");
#endif
            printf(
                " NUM_ITERATIONS    - # of timed iterations per test. If negative, run for this "
                "many seconds instead\n");
            printf(
                " NUM_SUBITERATIONS - # of sub-iterations to run per iteration. Must be "
                "non-negative\n");
            printf(" NUM_WARMUPS       - # of untimed warmup iterations per test\n");
            printf(" OUTPUT_TO_CSV     - Outputs to CSV format if set\n");
#if NIC_EXEC_ENABLED
            printf(" ROCE_VERSION      - RoCE version (default=2)\n");
#endif
            printf(
                " SAMPLING_FACTOR   - Add this many samples (when possible) between powers of 2 "
                "when auto-generating data sizes\n");
            printf(" SHOW_ITERATIONS   - Show per-iteration timing info\n");
            printf(" USE_HIP_EVENTS    - Use HIP events for GFX executor timing\n");
            printf(
                " USE_HSA_DMA       - Use hsa_amd_async_copy instead of hipMemcpy for non-targeted "
                "DMA execution\n");
            printf(" USE_INTERACTIVE   - Pause for user-input before starting transfer loop\n");
            printf(
                " USE_SINGLE_STREAM - Use a single stream per GPU GFX executor instead of stream "
                "per Transfer\n");
            printf(
                " VALIDATE_DIRECT   - Validate GPU destination memory directly instead of staging "
                "GPU memory on host\n");
            printf(" VALIDATE_SOURCE   - Validate GPU src memory immediately after preparation\n");
        }


        void Print(std::string const& name, int32_t const value, const char* format, ...) const
        {
            printf("%-20s%s%12d%s",
                   name.c_str(),
                   outputToCsv ? "," : " = ",
                   value,
                   outputToCsv ? "," : " : ");
            va_list args;
            va_start(args, format);
            vprintf(format, args);
            va_end(args);
            printf("\n");
        }

        void Print(std::string const& name, std::string const& value, const char* format, ...) const
        {
            printf("%-20s%s%12s%s",
                   name.c_str(),
                   outputToCsv ? "," : " = ",
                   value.c_str(),
                   outputToCsv ? "," : " : ");
            va_list args;
            va_start(args, format);
            vprintf(format, args);
            va_end(args);
            printf("\n");
        }

        // Display env var settings
        void DisplayEnvVars() const
        {
            int numGpuDevices      = TransferBench::GetNumExecutors(EXE_GPU_GFX);
            std::string nicSupport = "";
#if NIC_EXEC_ENABLED
            nicSupport = " (with NIC support)";
#endif
            if (!outputToCsv) {
                printf("TransferBench v%s.(%s)[%s]\n",
                       TransferBench::GetTransferBenchVersion().c_str(),
                       GetClientVersion().c_str(),
                       nicSupport.c_str());
                printf("===============================================================\n");
                if (!hideEnv) {
                    printf(
                        "[Common]                              (Suppress by setting HIDE_ENV=1)\n");
                }
            } else if (!hideEnv) {
                printf("EnvVar,Value,Description,(TransferBench Client v%s, Backend v%s)\n",
                       GetClientVersion().c_str(),
                       TransferBench::GetTransferBenchVersion().c_str());
            }
            if (hideEnv) { return; }

            Print("ALWAYS_VALIDATE",
                  alwaysValidate,
                  "Validating after %s",
                  (alwaysValidate ? "each iteration" : "all iterations"));
            Print("BLOCK_BYTES",
                  blockBytes,
                  "Each CU gets a mulitple of %d bytes to copy",
                  blockBytes);
            Print("BYTE_OFFSET", byteOffset, "Using byte offset of %d", byteOffset);
#if NIC_EXEC_ENABLED
            Print("CLOSEST_NIC",
                  (closestNicStr == "" ? "auto" : "user-input"),
                  "Per-GPU closest NIC is set as %s",
                  (closestNicStr == "" ? "auto" : closestNicStr.c_str()));
#endif
            Print("CU_MASK",
                  getenv("CU_MASK") ? 1 : 0,
                  "%s",
                  (cuMask.size() ? GetCuMaskDesc().c_str() : "All"));
            Print("FILL_COMPRESS",
                  getenv("FILL_COMPRESS") ? 1 : 0,
                  "%s",
                  (fillCompress.size() ? GetStr(fillCompress).c_str() : "Not specified"));
            Print("FILL_PATTERN",
                  getenv("FILL_PATTERN") ? 1 : 0,
                  "%s",
                  (fillPattern.size()
                       ? getenv("FILL_PATTERN")
                       : TransferBench::GetStrAttribute(ATR_SRC_PREP_DESCRIPTION).c_str()));
            Print("GFX_BLOCK_ORDER",
                  gfxBlockOrder,
                  "Thread block ordering: %s",
                  gfxBlockOrder == 0 ? "Sequential" : "Interleaved");
            Print("GFX_BLOCK_SIZE", gfxBlockSize, "Threadblock size of %d", gfxBlockSize);
            Print("GFX_SINGLE_TEAM",
                  gfxSingleTeam,
                  "%s",
                  (gfxSingleTeam ? "Combining CUs to work across entire data array"
                                 : "Each CUs operates on its own disjoint subarray"));
            Print("GFX_TEMPORAL",
                  gfxTemporal,
                  "%s",
                  (gfxTemporal == 0   ? "Not using non-temporal loads/stores"
                   : gfxTemporal == 1 ? "Using non-temporal loads"
                   : gfxTemporal == 2 ? "Using non-temporal stores"
                                      : "Using non-temporal loads and stores"));

            Print("GFX_UNROLL", gfxUnroll, "Using GFX unroll factor of %d", gfxUnroll);
            Print("GFX_WAVE_ORDER",
                  gfxWaveOrder,
                  "Using GFX wave ordering of %s",
                  (gfxWaveOrder == 0   ? "Unroll,Wavefront,CU"
                   : gfxWaveOrder == 1 ? "Unroll,CU,Wavefront"
                   : gfxWaveOrder == 2 ? "Wavefront,Unroll,CU"
                   : gfxWaveOrder == 3 ? "Wavefront,CU,Unroll"
                   : gfxWaveOrder == 4 ? "CU,Unroll,Wavefront"
                                       : "CU,Wavefront,Unroll"));
            Print("GFX_WORD_SIZE",
                  gfxWordSize,
                  "Using GFX word size of %d (DWORDx%d)",
                  gfxWordSize,
                  gfxWordSize);

#if NIC_EXEC_ENABLED
            Print("IP_ADDRESS_FAMILY",
                  ipAddressFamily,
                  "IP address family is set to IPv%d",
                  ipAddressFamily);

            Print("IB_GID_INDEX",
                  ibGidIndex,
                  "RoCE GID index is set to %s",
                  (ibGidIndex < 0 ? "auto" : std::to_string(ibGidIndex).c_str()));
            Print("IB_PORT_NUMBER", ibPort, "IB port number is set to %d", ibPort);
#endif
            Print("MIN_VAR_SUBEXEC",
                  minNumVarSubExec,
                  "Using at least %d subexecutor(s) for variable subExec tranfers",
                  minNumVarSubExec);
            Print("MAX_VAR_SUBEXEC",
                  maxNumVarSubExec,
                  "Using up to %s subexecutors for variable subExec transfers",
                  maxNumVarSubExec ? std::to_string(maxNumVarSubExec).c_str() : "all available");
#if NIC_EXEC_ENABLED
            Print("NIC_RELAX_ORDER",
                  nicRelaxedOrder,
                  "Using %s ordering for NIC RDMA",
                  nicRelaxedOrder ? "relaxed" : "strict");
#endif
            Print("NUM_ITERATIONS",
                  numIterations,
                  (numIterations == 0) ? "Running infinitely" : "Running %d %s",
                  abs(numIterations),
                  (numIterations > 0 ? " timed iteration(s)" : "seconds(s) per Test"));
            Print("NUM_SUBITERATIONS",
                  numSubIterations,
                  "Running %s subiterations",
                  (numSubIterations == 0 ? "infinite" : std::to_string(numSubIterations)).c_str());
            Print("NUM_WARMUPS", numWarmups, "Running %d warmup iteration(s) per Test", numWarmups);
#if NIC_EXEC_ENABLED
            Print("ROCE_VERSION", roceVersion, "RoCE version is set to %d", roceVersion);
#endif
            Print("SHOW_ITERATIONS",
                  showIterations,
                  "%s per-iteration timing",
                  showIterations ? "Showing" : "Hiding");
            Print("USE_HIP_EVENTS",
                  useHipEvents,
                  "Using %s for GFX/DMA Executor timing",
                  useHipEvents ? "HIP events" : "CPU wall time");
            Print("USE_HSA_DMA",
                  useHsaDma,
                  "Using %s for DMA execution",
                  useHsaDma ? "hsa_amd_async_copy" : "hipMemcpyAsync");
            Print("USE_INTERACTIVE",
                  useInteractive,
                  "Running in %s mode",
                  useInteractive ? "interactive" : "non-interactive");
            Print("USE_SINGLE_STREAM",
                  useSingleStream,
                  "Using single stream per GFX %s",
                  useSingleStream ? "device" : "Transfer");
            if (getenv("XCC_PREF_TABLE")) {
                printf("%36s: Preferred XCC Table (XCC_PREF_TABLE)\n", "");
                printf("%36s:         ", "");
                for (int i = 0; i < numGpuDevices; i++) { printf(" %3d", i); }
                printf(" (#XCCs)\n");
                for (int i = 0; i < numGpuDevices; i++) {
                    printf("%36s: GPU %3d ", "", i);
                    for (int j = 0; j < numGpuDevices; j++) { printf(" %3d", prefXccTable[i][j]); }
                    printf(" %3d\n", TransferBench::GetNumExecutorSubIndices({EXE_GPU_GFX, i}));
                }
            }
            Print("VALIDATE_DIRECT",
                  validateDirect,
                  "Validate GPU destination memory %s",
                  validateDirect ? "directly" : "via CPU staging buffer");
            Print("VALIDATE_SOURCE",
                  validateSource,
                  validateSource ? "Validate source after preparation"
                                 : "Do not perform source validation after prep");
            printf("\n");
        };

        // Helper function that gets parses environment variable or sets to default value
        static int GetEnvVar(std::string const& varname, int defaultValue)
        {
            if (getenv(varname.c_str())) { return atoi(getenv(varname.c_str())); }
            return defaultValue;
        }

        static std::vector<int> GetEnvVarArray(std::string const& varname,
                                               std::vector<int> const& defaultValue)
        {
            if (getenv(varname.c_str())) {
                std::vector<int> values;
                char* arrayStr = getenv(varname.c_str());
                char* token    = strtok(arrayStr, ",");
                while (token) {
                    int val;
                    if (sscanf(token, "%d", &val) == 1) {
                        values.push_back(val);
                    } else {
                        printf("[ERROR] Unrecognized token [%s]\n", token);
                        exit(1);
                    }
                    token = strtok(NULL, ",");
                }
                return values;
            }
            return defaultValue;
        }

        static std::vector<int> GetEnvVarRangeArray(std::string const& varname,
                                                    std::vector<int> const& defaultValue)
        {
            if (getenv(varname.c_str())) {
                char* rangeStr = getenv(varname.c_str());
                std::set<int> values;
                char* token = strtok(rangeStr, ",");
                while (token) {
                    int start, end;
                    if (sscanf(token, "%d-%d", &start, &end) == 2) {
                        for (int i = start; i <= end; i++) { values.insert(i); }
                    } else if (sscanf(token, "%d", &start) == 1) {
                        values.insert(start);
                    } else {
                        printf("[ERROR] Unrecognized token [%s]\n", token);
                        exit(1);
                    }
                    token = strtok(NULL, ",");
                }
                std::vector<int> result;
                for (auto v : values) { result.push_back(v); }
                return result;
            }
            return defaultValue;
        }

        static std::string GetEnvVar(std::string const& varname, std::string const& defaultValue)
        {
            if (getenv(varname.c_str())) { return getenv(varname.c_str()); }
            return defaultValue;
        }

        std::string GetStr(std::vector<int> const& varnameList) const
        {
            std::string result = "";
            for (auto i = std::size_t(0); i < varnameList.size(); i++) {
                if (i) { result += ","; }
                result += std::to_string(varnameList[i]);
            }
            return result;
        }

        std::string GetCuMaskDesc() const
        {
            std::vector<std::pair<int, int>> runs;
            int numXccs = TransferBench::GetNumExecutorSubIndices({EXE_GPU_GFX, 0});
            bool inRun  = false;
            std::pair<int, int> curr;
            int used = 0;
            for (int targetBit = 0; static_cast<std::size_t>(targetBit) < (cuMask.size() * 32);
                 targetBit += numXccs) {
                if (cuMask[targetBit / 32] & (1 << (targetBit % 32))) {
                    used++;
                    if (!inRun) {
                        inRun      = true;
                        curr.first = targetBit / numXccs;
                    }
                } else {
                    if (inRun) {
                        inRun       = false;
                        curr.second = targetBit / numXccs - 1;
                        runs.push_back(curr);
                    }
                }
            }
            if (inRun) { curr.second = (cuMask.size() * 32) / numXccs - 1; }

            std::string result = "CUs used: (" + std::to_string(used) + ") ";
            for (auto i = std::size_t(0); i < runs.size(); i++) {
                if (i) { result += ","; }
                if (runs[i].first == runs[i].second) {
                    result += std::to_string(runs[i].first);
                } else {
                    result += std::to_string(runs[i].first) + "-" + std::to_string(runs[i].second);
                }
            }
            return result;
        }

        TransferBench::ConfigOptions ToConfigOptions()
        {
            TransferBench::ConfigOptions cfg;

            cfg.general.numIterations      = numIterations;
            cfg.general.numSubIterations   = numSubIterations;
            cfg.general.numWarmups         = numWarmups;
            cfg.general.recordPerIteration = showIterations;
            cfg.general.useInteractive     = useInteractive;

            cfg.data.alwaysValidate = alwaysValidate;
            cfg.data.blockBytes     = blockBytes;
            cfg.data.byteOffset     = byteOffset;
            cfg.data.fillCompress   = fillCompress;
            cfg.data.fillPattern    = fillPattern;
            cfg.data.validateDirect = validateDirect;
            cfg.data.validateSource = validateSource;

            cfg.dma.useHipEvents = useHipEvents;
            cfg.dma.useHsaCopy   = useHsaDma;

            cfg.gfx.blockOrder     = gfxBlockOrder;
            cfg.gfx.blockSize      = gfxBlockSize;
            cfg.gfx.cuMask         = cuMask;
            cfg.gfx.prefXccTable   = prefXccTable;
            cfg.gfx.unrollFactor   = gfxUnroll;
            cfg.gfx.temporalMode   = gfxTemporal;
            cfg.gfx.useHipEvents   = useHipEvents;
            cfg.gfx.useMultiStream = !useSingleStream;
            cfg.gfx.useSingleTeam  = gfxSingleTeam;
            cfg.gfx.waveOrder      = gfxWaveOrder;
            cfg.gfx.wordSize       = gfxWordSize;

            cfg.nic.ibGidIndex      = ibGidIndex;
            cfg.nic.ibPort          = ibPort;
            cfg.nic.ipAddressFamily = ipAddressFamily;
            cfg.nic.useRelaxedOrder = nicRelaxedOrder;
            cfg.nic.roceVersion     = roceVersion;

            std::vector<int> closestNics;
            if (closestNicStr != "") {
                std::stringstream ss(closestNicStr);
                std::string item;
                while (std::getline(ss, item, ',')) {
                    try {
                        int nic = std::stoi(item);
                        closestNics.push_back(nic);
                    } catch (const std::invalid_argument& e) {
                        printf("[ERROR] Invalid NIC index (%s) by user in %s\n",
                               item.c_str(),
                               closestNicStr.c_str());
                        exit(1);
                    }
                }
                cfg.nic.closestNics = closestNics;
            }
            return cfg;
        }
};


#endif    // ENVVARS_HPP