Client.cpp 12.5 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
/*
Copyright (c) 2019-2024 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 "Client.hpp"
#include "Presets.hpp"
#include "Topology.hpp"
#include <fstream>

int main(int argc, char **argv) {

  // Collect environment variables
  EnvVars ev;

  // Display usage instructions and detected topology
  if (argc <= 1) {
    if (!ev.outputToCsv) {
      DisplayUsage(argv[0]);
      DisplayPresets();
    }
    DisplayTopology(ev.outputToCsv);
    exit(0);
  }

  // Determine number of bytes to run per Transfer
  size_t numBytesPerTransfer = argc > 2 ? atoll(argv[2]) : DEFAULT_BYTES_PER_TRANSFER;
  if (argc > 2) {
    // Adjust bytes if unit specified
    char units = argv[2][strlen(argv[2])-1];
    switch (units) {
    case 'G': case 'g': numBytesPerTransfer *= 1024;
    case 'M': case 'm': numBytesPerTransfer *= 1024;
    case 'K': case 'k': numBytesPerTransfer *= 1024;
    }
  }
  if (numBytesPerTransfer % 4) {
    printf("[ERROR] numBytesPerTransfer (%lu) must be a multiple of 4\n", numBytesPerTransfer);
    exit(1);
  }

  // Run preset benchmark if requested
  if (RunPreset(ev, numBytesPerTransfer, argc, argv)) exit(0);

  // Read input from command line or configuration file
  std::vector<std::string> lines;
  {
    std::string line;
    if (!strcmp(argv[1], "cmdline")) {
      for (int i = 3; i < argc; i++)
        line += std::string(argv[i]) + " ";
      lines.push_back(line);
    } else {
      std::ifstream cfgFile(argv[1]);
      if (!cfgFile.is_open()) {
        printf("[ERROR] Unable to open transfer configuration file: [%s]\n", argv[1]);
        exit(1);
      }
      while (std::getline(cfgFile, line))
        lines.push_back(line);
      cfgFile.close();
    }
  }

  // Print environment variables and CSV header
  ev.DisplayEnvVars();
  if (ev.outputToCsv)
    printf("Test#,Transfer#,NumBytes,Src,Exe,Dst,CUs,BW(GB/s),Time(ms),SrcAddr,DstAddr\n");

  TransferBench::ConfigOptions cfgOptions = ev.ToConfigOptions();
  TransferBench::TestResults results;
  std::vector<ErrResult> errors;

  // Process each line as a Test
  int testNum = 0;
  for (std::string const &line : lines) {
    // Check if line is a comment to be echoed to output (starts with ##)
    if (!ev.outputToCsv && line[0] == '#' && line[1] == '#') printf("%s\n", line.c_str());

    // Parse set of parallel Transfers to execute
    std::vector<Transfer> transfers;
    CheckForError(TransferBench::ParseTransfers(line, transfers));
    if (transfers.empty()) continue;

    // Check for variable sub-executors Transfers
    int numVariableTransfers = 0;
    int maxVarCount = 0;
    {
      std::map<ExeDevice, int> varTransferCount;
      for (auto const& t : transfers) {
        if (t.numSubExecs == 0) {
          if (t.exeDevice.exeType != EXE_GPU_GFX) {
            printf("[ERROR] Variable number of subexecutors is only supported on GFX executors\n");
            exit(1);
          }
          numVariableTransfers++;
          varTransferCount[t.exeDevice]++;
          maxVarCount = max(maxVarCount, varTransferCount[t.exeDevice]);
        }
      }
      if (numVariableTransfers > 0 && numVariableTransfers != transfers.size()) {
        printf("[ERROR] All or none of the Transfers in the Test must use variable number of Subexecutors\n");
        exit(1);
      }
    }

124
125
126
127
128
129
130
131
    // Track which transfers have already numBytes specified
    std::vector<bool> bytesSpecified(transfers.size());
    int hasUnspecified = false;
    for (int i = 0; i < transfers.size(); i++) {
      bytesSpecified[i] = (transfers[i].numBytes != 0);
      if (transfers[i].numBytes == 0) hasUnspecified = true;
    }

132
133
134
135
136
    // Run the specified numbers of bytes otherwise generate a range of values
    for (size_t bytes = (1<<10); bytes <= (1<<29); bytes *= 2) {
      size_t deltaBytes = std::max(1UL, bytes / ev.samplingFactor);
      size_t currBytes = (numBytesPerTransfer == 0) ? bytes : numBytesPerTransfer;
      do {
137
138
139
140
        for (int i = 0; i < transfers.size(); i++) {
          if (!bytesSpecified[i])
            transfers[i].numBytes = currBytes;
        }
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

        if (maxVarCount == 0) {
          if (TransferBench::RunTransfers(cfgOptions, transfers, results)) {
            PrintResults(ev, ++testNum, transfers, results);
          }
          PrintErrors(results.errResults);
        } else {
          // Variable subexecutors - Determine how many subexecutors to sweep up to
          int maxNumVarSubExec = ev.maxNumVarSubExec;
          if (maxNumVarSubExec == 0) {
            maxNumVarSubExec = TransferBench::GetNumSubExecutors({EXE_GPU_GFX, 0}) / maxVarCount;
          }

          TransferBench::TestResults bestResults;
          std::vector<Transfer> bestTransfers;
          for (int numSubExecs = ev.minNumVarSubExec; numSubExecs <= maxNumVarSubExec; numSubExecs++) {
            std::vector<Transfer> tempTransfers = transfers;
            for (auto& t : tempTransfers) {
              if (t.numSubExecs == 0) t.numSubExecs = numSubExecs;
            }

            TransferBench::TestResults tempResults;
            if (!TransferBench::RunTransfers(cfgOptions, tempTransfers, tempResults)) {
              PrintErrors(tempResults.errResults);
            } else {
              if (tempResults.avgTotalBandwidthGbPerSec > bestResults.avgTotalBandwidthGbPerSec) {
                bestResults = tempResults;
                bestTransfers = tempTransfers;
              }
            }
          }
          PrintResults(ev, ++testNum, bestTransfers, bestResults);
          PrintErrors(bestResults.errResults);
        }
175
        if (numBytesPerTransfer != 0 || !hasUnspecified) break;
176
177
        currBytes += deltaBytes;
      } while (currBytes < bytes * 2);
178
      if (numBytesPerTransfer != 0 || !hasUnspecified) break;
179
180
181
182
183
184
    }
  }
}

void DisplayUsage(char const* cmdName)
{
185
186
187
188
189
  std::string nicSupport = "";
#if NIC_EXEC_ENABLED
  nicSupport = " (with NIC support)";
#endif
  printf("TransferBench v%s.%s%s\n", TransferBench::VERSION, CLIENT_VERSION, nicSupport.c_str());
190
191
  printf("========================================\n");

192
  if (numa_available() == -1) {
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
    printf("[ERROR] NUMA library not supported. Check to see if libnuma has been installed on this system\n");
    exit(1);
  }

  printf("Usage: %s config <N>\n", cmdName);
  printf("  config: Either:\n");
  printf("          - Filename of configFile containing Transfers to execute (see example.cfg for format)\n");
  printf("          - Name of preset config:\n");
  printf("  N     : (Optional) Number of bytes to copy per Transfer.\n");
  printf("          If not specified, defaults to %lu bytes. Must be a multiple of 4 bytes\n",
         DEFAULT_BYTES_PER_TRANSFER);
  printf("          If 0 is specified, a range of Ns will be benchmarked\n");
  printf("          May append a suffix ('K', 'M', 'G') for kilobytes / megabytes / gigabytes\n");
  printf("\n");

  EnvVars::DisplayUsage();
}

std::string MemDevicesToStr(std::vector<MemDevice> const& memDevices) {
  if (memDevices.empty()) return "N";
  std::stringstream ss;
  for (auto const& m : memDevices)
    ss << TransferBench::MemTypeStr[m.memType] << m.memIndex;
  return ss.str();
}

void PrintResults(EnvVars const& ev, int const testNum,
                  std::vector<Transfer> const& transfers,
                  TransferBench::TestResults const& results)
{
  char sep = ev.outputToCsv ? ',' : '|';
  size_t numTimedIterations = results.numTimedIterations;

  if (!ev.outputToCsv) printf("Test %d:\n", testNum);

  // Loop over each executor
  for (auto exeInfoPair : results.exeResults) {
    ExeDevice const& exeDevice = exeInfoPair.first;
    ExeResult const& exeResult = exeInfoPair.second;
    ExeType const    exeType   = exeDevice.exeType;
    int32_t const    exeIndex  = exeDevice.exeIndex;

235
    printf(" Executor: %3s %02d %c %8.3f GB/s %c %8.3f ms %c %12lu bytes %c %-7.3f GB/s (sum)\n",
236
237
238
239
240
241
242
243
244
245
246
           ExeTypeName[exeType], exeIndex, sep, exeResult.avgBandwidthGbPerSec, sep,
           exeResult.avgDurationMsec, sep, exeResult.numBytes, sep, exeResult.sumBandwidthGbPerSec);

    // Loop over each executor
    for (int idx : exeResult.transferIdx) {
      Transfer const& t = transfers[idx];
      TransferResult const& r = results.tfrResults[idx];

      char exeSubIndexStr[32] = "";
      if (t.exeSubIndex != -1)
        sprintf(exeSubIndexStr, ".%d", t.exeSubIndex);
247
      printf("     Transfer %02d  %c %8.3f GB/s %c %8.3f ms %c %12lu bytes %c %s -> %c%03d%s:%03d -> %s\n",
248
249
250
251
             idx,                    sep,
             r.avgBandwidthGbPerSec, sep,
             r.avgDurationMsec,      sep,
             r.numBytes,             sep,
252
253
254
255
             MemDevicesToStr(t.srcs).c_str(),
             TransferBench::ExeTypeStr[t.exeDevice.exeType], t.exeDevice.exeIndex,
             exeSubIndexStr, t.numSubExecs,
             MemDevicesToStr(t.dsts).c_str());
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

      // Show per-iteration timing information
      if (ev.showIterations) {

        // Check that per-iteration information exists
        if (r.perIterMsec.size() != numTimedIterations) {
          printf("[ERROR] Per iteration timing data unavailable: Expected %lu data points, but have %lu\n",
                 numTimedIterations, r.perIterMsec.size());
          exit(1);
        }

        // Compute standard deviation and track iterations by speed
        std::set<std::pair<double, int>> times;
        double stdDevTime = 0;
        double stdDevBw = 0;
        for (int i = 0; i < numTimedIterations; i++) {
          times.insert(std::make_pair(r.perIterMsec[i], i+1));
          double const varTime = fabs(r.avgDurationMsec - r.perIterMsec[i]);
          stdDevTime += varTime * varTime;

          double iterBandwidthGbs = (t.numBytes / 1.0E9) / r.perIterMsec[i] * 1000.0f;
          double const varBw = fabs(iterBandwidthGbs - r.avgBandwidthGbPerSec);
          stdDevBw += varBw * varBw;
        }
        stdDevTime = sqrt(stdDevTime / numTimedIterations);
        stdDevBw = sqrt(stdDevBw / numTimedIterations);

        // Loop over iterations (fastest to slowest)
        for (auto& time : times) {
          double iterDurationMsec = time.first;
          double iterBandwidthGbs = (t.numBytes / 1.0E9) / iterDurationMsec * 1000.0f;
287
          printf("      Iter %03d    %c %8.3f GB/s %c %8.3f ms %c", time.second, sep, iterBandwidthGbs, sep, iterDurationMsec, sep);
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302

          std::set<int> usedXccs;
          if (time.second - 1 < r.perIterCUs.size()) {
            printf(" CUs:");
            for (auto x : r.perIterCUs[time.second - 1]) {
              printf(" %02d:%02d", x.first, x.second);
              usedXccs.insert(x.first);
            }
          }

          printf(" XCCs:");
          for (auto x : usedXccs)
            printf(" %02d", x);
          printf("\n");
        }
303
        printf("      StandardDev %c %8.3f GB/s %c %8.3f ms %c\n", sep, stdDevBw, sep, stdDevTime, sep);
304
305
306
      }
    }
  }
307
  printf(" Aggregate (CPU)  %c %8.3f GB/s %c %8.3f ms %c %12lu bytes %c Overhead: %.3f ms\n",
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
         sep, results.avgTotalBandwidthGbPerSec,
         sep, results.avgTotalDurationMsec,
         sep, results.totalBytesTransferred,
         sep, results.overheadMsec);
}

void CheckForError(ErrResult const& error)
{
  switch (error.errType) {
  case ERR_NONE: return;
  case ERR_WARN:
    printf("[WARN] %s\n", error.errMsg.c_str());
    return;
  case ERR_FATAL:
    printf("[ERROR] %s\n", error.errMsg.c_str());
    exit(1);
  default:
    break;
  }
}

void PrintErrors(std::vector<ErrResult> const& errors)
{
  bool isFatal = false;
  for (auto const& err : errors) {
    printf("[%s] %s\n", err.errType == ERR_FATAL ? "ERROR" : "WARN", err.errMsg.c_str());
    isFatal |= (err.errType == ERR_FATAL);
  }
  if (isFatal) exit(1);
}