AllToAllSweep.hpp 9.31 KB
Newer Older
gilbertlee-amd's avatar
gilbertlee-amd committed
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
/*
Copyright (c) 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.
*/

#include "EnvVars.hpp"

void AllToAllSweepPreset(EnvVars&           ev,
                         size_t      const  numBytesPerTransfer,
                         std::string const  presetName)
{
  enum
  {
    A2A_COPY       = 0,
    A2A_READ_ONLY  = 1,
    A2A_WRITE_ONLY = 2,
    A2A_CUSTOM     = 3,
  };
  char a2aModeStr[4][20] = {"Copy", "Read-Only", "Write-Only", "Custom"};

  // Force single-stream mode for all-to-all benchmark
  ev.useSingleStream = 1;

  int numDetectedGpus = TransferBench::GetNumExecutors(EXE_GPU_GFX);

  // Collect env vars for this preset
  int a2aDirect     = EnvVars::GetEnvVar("A2A_DIRECT"     , 1);
  int a2aLocal      = EnvVars::GetEnvVar("A2A_LOCAL"      , 0);
  int numGpus       = EnvVars::GetEnvVar("NUM_GPU_DEVICES", numDetectedGpus);
gilbertlee-amd's avatar
gilbertlee-amd committed
47
  int showMinOnly   = EnvVars::GetEnvVar("SHOW_MIN_ONLY",   1);
gilbertlee-amd's avatar
gilbertlee-amd committed
48
49
50
51
52
  int useFineGrain  = EnvVars::GetEnvVar("USE_FINE_GRAIN" , 1);
  int useRemoteRead = EnvVars::GetEnvVar("USE_REMOTE_READ", 0);
  int useSpray      = EnvVars::GetEnvVar("USE_SPRAY",       0);
  int verbose       = EnvVars::GetEnvVar("VERBOSE",         0);

gilbertlee-amd's avatar
gilbertlee-amd committed
53
  std::vector<int> blockList  = EnvVars::GetEnvVarArray("BLOCKSIZES", {256});
gilbertlee-amd's avatar
gilbertlee-amd committed
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
  std::vector<int> unrollList = EnvVars::GetEnvVarArray("UNROLLS", {1,2,3,4,6,8});
  std::vector<int> numCusList = EnvVars::GetEnvVarArray("NUM_CUS", {4,8,12,16,24,32});

  // A2A_MODE may be 0,1,2 or else custom numSrcs:numDsts
  int numSrcs, numDsts;
  int a2aMode = 0;
  if (getenv("A2A_MODE") && sscanf(getenv("A2A_MODE"), "%d:%d", &numSrcs, &numDsts) == 2) {
    a2aMode = A2A_CUSTOM;
  } else {
    a2aMode = EnvVars::GetEnvVar("A2A_MODE", 0);
    if (a2aMode < 0 || a2aMode > 2) {
      printf("[ERROR] a2aMode must be between 0 and 2, or else numSrcs:numDsts\n");
      exit(1);
    }
    numSrcs = (a2aMode == A2A_WRITE_ONLY ? 0 : 1);
    numDsts = (a2aMode == A2A_READ_ONLY  ? 0 : 1);
  }

  // Print off environment variables
  ev.DisplayEnvVars();
  if (!ev.hideEnv) {
    if (!ev.outputToCsv) printf("[AllToAll Related]\n");
    ev.Print("A2A_DIRECT"     , a2aDirect        , a2aDirect ? "Only using direct links" : "Full all-to-all");
    ev.Print("A2A_LOCAL"      , a2aLocal         , "%s local transfers", a2aLocal ? "Include" : "Exclude");
    ev.Print("A2A_MODE"       , (a2aMode == A2A_CUSTOM) ?  std::to_string(numSrcs) + ":" + std::to_string(numDsts) : std::to_string(a2aMode),
                                (a2aMode == A2A_CUSTOM) ? (std::to_string(numSrcs) + " read(s) " +
                                                           std::to_string(numDsts) + " write(s)").c_str(): a2aModeStr[a2aMode]);
gilbertlee-amd's avatar
gilbertlee-amd committed
81
    ev.Print("BLOCKSIZES"     , blockList.size() , EnvVars::ToStr(blockList).c_str());
gilbertlee-amd's avatar
gilbertlee-amd committed
82
    ev.Print("SHOW_MIN_ONLY"  , showMinOnly      , showMinOnly ? "Showing only slowest GPU results" : "Showing slowest and fastest GPU results");
gilbertlee-amd's avatar
gilbertlee-amd committed
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
    ev.Print("NUM_CUS"        , numCusList.size(), EnvVars::ToStr(numCusList).c_str());
    ev.Print("NUM_GPU_DEVICES", numGpus          , "Using %d GPUs", numGpus);
    ev.Print("UNROLLS"        , unrollList.size(), EnvVars::ToStr(unrollList).c_str());
    ev.Print("USE_FINE_GRAIN" , useFineGrain     , "Using %s-grained memory", useFineGrain ? "fine" : "coarse");
    ev.Print("USE_REMOTE_READ", useRemoteRead    , "Using %s as executor", useRemoteRead ? "DST" : "SRC");
    ev.Print("USE_SPRAY"      , useSpray         , "%s per CU", useSpray ? "All targets" : "One target");
    ev.Print("VERBOSE"        , verbose          , verbose ? "Display test results" : "Display summary only");
    printf("\n");
  }

  // Validate env vars
  if (numGpus < 0 || numGpus > numDetectedGpus) {
    printf("[ERROR] Cannot use %d GPUs.  Detected %d GPUs\n", numGpus, numDetectedGpus);
    exit(1);
  }

  if (useSpray && numDsts > 1) {
    printf("[ERROR] Cannot use USE_SPRAY with multiple destination buffers\n");
    exit(1);
  }

  // Collect the number of GPU devices to use
  MemType memType = useFineGrain ? MEM_GPU_FINE : MEM_GPU;
  ExeType exeType = EXE_GPU_GFX;

  std::vector<Transfer> transfers;

  int targetCount = 0;
  if (!useSpray) {
    // Each CU will work on just one target
    for (int i = 0; i < numGpus; i++) {
      targetCount = 0;
      for (int j = 0; j < numGpus; j++) {
        // Check whether or not to execute this pair
        if (i == j) {
          if (!a2aLocal) continue;
        } else if (a2aDirect) {
#if !defined(__NVCC__)
          uint32_t linkType, hopCount;
          HIP_CALL(hipExtGetLinkTypeAndHopCount(i, j, &linkType, &hopCount));
          if (hopCount != 1) continue;
#endif
        }

        // Build Transfer and add it to list
        TransferBench::Transfer transfer;
        targetCount++;
        transfer.numBytes = numBytesPerTransfer;
        for (int x = 0; x < numSrcs; x++) transfer.srcs.push_back({memType, i});

        // When using multiple destinations, the additional destinations are "local"
        if (numDsts) transfer.dsts.push_back({memType, j});
        for (int x = 1; x < numDsts; x++) transfer.dsts.push_back({memType, i});
        transfer.exeDevice = {exeType, (useRemoteRead ? j : i)};
        transfer.exeSubIndex = -1;
        transfers.push_back(transfer);
      }
    }
  } else {
    // Each CU will work on all targets
    for (int i = 0; i < numGpus; i++) {
      TransferBench::Transfer transfer;
      transfer.numBytes = numBytesPerTransfer;
      transfer.exeDevice = {exeType, i};
      transfer.exeSubIndex = -1;
      targetCount = 0;
      for (int j = 0; j < numGpus; j++) {
        // Check whether or not to transfer to this GPU
        if (i == j) {
          if (!a2aLocal) continue;
        } else if (a2aDirect) {
#if !defined(__NVCC__)
          uint32_t linkType, hopCount;
          HIP_CALL(hipExtGetLinkTypeAndHopCount(i, j, &linkType, &hopCount));
          if (hopCount != 1) continue;
#endif
        }
        targetCount++;
        for (int x = 0; x < numSrcs; x++) transfer.srcs.push_back({memType, useRemoteRead ? j : i});

        if (numDsts) transfer.dsts.push_back({memType, j});
        for (int x = 1; x < numDsts; x++) transfer.dsts.push_back({memType, i});
      }
      transfers.push_back(transfer);
    }
  }

  printf("GPU-GFX All-To-All Sweep benchmark:\n");
  printf("==========================\n");
  printf("- Copying %lu bytes between %s pairs of GPUs\n", numBytesPerTransfer, a2aDirect ? "directly connected" : "all");
  if (transfers.size() == 0) {
    printf("[WARN} No transfers requested. Try adjusting A2A_DIRECT or A2A_LOCAL\n");
    return;
  }

  // Execute Transfers
  TransferBench::ConfigOptions cfg = ev.ToConfigOptions();

  // Run tests
  std::map<std::pair<int, int>, TransferBench::TestResults> results;

  // Display summary
gilbertlee-amd's avatar
gilbertlee-amd committed
185
186
187
188
189
  for (int blockSize : blockList) {
    printf("Blocksize: %d\n", blockSize);
    ev.gfxBlockSize = cfg.gfx.blockSize = blockSize;

    printf("#CUs\\Unroll");
gilbertlee-amd's avatar
gilbertlee-amd committed
190
    for (int u : unrollList) {
gilbertlee-amd's avatar
gilbertlee-amd committed
191
192
      printf("  %d(Min) ", u);
      if (!showMinOnly) printf("  %d(Max) ", u);
gilbertlee-amd's avatar
gilbertlee-amd committed
193
    }
gilbertlee-amd's avatar
gilbertlee-amd committed
194
    printf("\n");
gilbertlee-amd's avatar
gilbertlee-amd committed
195
    for (int c : numCusList) {
gilbertlee-amd's avatar
gilbertlee-amd committed
196
      printf("   %5d   ", c);  fflush(stdout);
gilbertlee-amd's avatar
gilbertlee-amd committed
197
      for (int u : unrollList) {
gilbertlee-amd's avatar
gilbertlee-amd committed
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
        ev.gfxUnroll = cfg.gfx.unrollFactor = u;
        for (auto& transfer : transfers)
          transfer.numSubExecs = useSpray ? (c * targetCount) : c;

        double minBandwidth = std::numeric_limits<double>::max();
        double maxBandwidth = std::numeric_limits<double>::min();
        TransferBench::TestResults result;
        if (TransferBench::RunTransfers(cfg, transfers, result)) {
          for (auto const& exeResult : result.exeResults) {
            minBandwidth = std::min(minBandwidth, exeResult.second.avgBandwidthGbPerSec);
            maxBandwidth = std::max(maxBandwidth, exeResult.second.avgBandwidthGbPerSec);
          }
          if (useSpray) {
            minBandwidth *= targetCount;
            maxBandwidth *= targetCount;
          }
          results[std::make_pair(c,u)] = result;
        } else {
          minBandwidth = 0.0;
        }
        printf(" %7.2f ", minBandwidth);
        if (!showMinOnly) printf(" %7.2f ", maxBandwidth);
        fflush(stdout);
      }
      printf("\n"); fflush(stdout);
    }

    if (verbose) {
      int testNum = 0;
      for (int c : numCusList) {
        for (int u : unrollList) {
          printf("CUs: %d Unroll %d\n", c, u);
          PrintResults(ev, ++testNum, transfers, results[std::make_pair(c,u)]);
        }
gilbertlee-amd's avatar
gilbertlee-amd committed
232
233
234
235
      }
    }
  }
}