You need to sign in or sign up before continuing.
Client.cpp 9.73 KB
Newer Older
1
/*
2
Copyright (c) Advanced Micro Devices, Inc. All rights reserved.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

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 "Presets.hpp"
#include "Topology.hpp"
#include <fstream>

27
28
void DisplayVersion();
void DisplayUsage(char const* cmdName);
29

30
31
32
33
34
35
36
using namespace TransferBench;
using namespace TransferBench::Utils;

size_t constexpr DEFAULT_BYTES_PER_TRANSFER = (1<<28);

int main(int argc, char **argv)
{
37
38
39
40
41
  // Collect environment variables
  EnvVars ev;

  // Display usage instructions and detected topology
  if (argc <= 1) {
42
43
44
45
46
47
48
    if (RankDoesOutput()) {
      if (!ev.outputToCsv) {
        DisplayVersion();
        DisplayUsage(argv[0]);
        DisplayPresets();
      }
      DisplayTopology(ev.outputToCsv, ev.showBorders);
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    }
    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) {
65
    Print("[ERROR] numBytesPerTransfer (%lu) must be a multiple of 4\n", numBytesPerTransfer);
66
67
68
    exit(1);
  }

69
70
71
  // Display TransferBench version and build configuration
  DisplayVersion();

72
  // Run preset benchmark if requested
73
74
  int retCode = 0;
  if (RunPreset(ev, numBytesPerTransfer, argc, argv, retCode)) return retCode;
75
76

  // Read input from command line or configuration file
77
  bool isDryRun = !strcmp(argv[1], "dryrun");
78
79
80
  std::vector<std::string> lines;
  {
    std::string line;
81
    if (!strcmp(argv[1], "cmdline") || isDryRun) {
82
83
84
85
86
87
      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()) {
88
        Print("[ERROR] Unable to open transfer configuration file: [%s]\n", argv[1]);
89
        exit(1);
90
     }
91
92
93
94
95
96
      while (std::getline(cfgFile, line))
        lines.push_back(line);
      cfgFile.close();
    }
  }

97
98
  ConfigOptions cfgOptions = ev.ToConfigOptions();
  TestResults results;
99
100
  std::vector<ErrResult> errors;

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
  // Dry run prints off transfers (and errors)
  if (isDryRun) {
    Print("Transfers to be executed (dry-run):\n");
    Print("================================================================================\n");
    std::vector<Transfer> transfers;
    CheckForError(ParseTransfers(lines[0], transfers));
    if (transfers.empty()) {
      Print("<none>\n");
    } else {
      bool isMultiNode = GetNumRanks() > 1;
      for (size_t i = 0; i < transfers.size(); i++) {
        Transfer const& t = transfers[i];
        Print("Transfer %5lu: (%s->", i, MemDevicesToStr(t.srcs).c_str());
        if (isMultiNode) Print("R%d", t.exeDevice.exeRank);
        Print("%c%d", ExeTypeStr[t.exeDevice.exeType], t.exeDevice.exeIndex);
        if (t.exeDevice.exeSlot) Print("%c", 'A' + t.exeDevice.exeSlot);
        if (t.exeSubIndex != -1) Print(".%d", t.exeSubIndex);
        if (t.exeSubSlot != 0) Print("%c", 'A' + t.exeSubSlot);
        Print("->%s)\n", MemDevicesToStr(t.dsts).c_str());
      }
    }
    return 0;
  }

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

132
133
134
135
136
137
138
139
  // 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;
140
    CheckForError(ParseTransfers(line, transfers));
141
142
143
144
145
146
147
148
149
150
    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) {
151
            Print("[ERROR] Variable number of subexecutors is only supported on GFX executors\n");
152
153
154
155
156
157
158
159
            exit(1);
          }
          numVariableTransfers++;
          varTransferCount[t.exeDevice]++;
          maxVarCount = max(maxVarCount, varTransferCount[t.exeDevice]);
        }
      }
      if (numVariableTransfers > 0 && numVariableTransfers != transfers.size()) {
160
        Print("[ERROR] All or none of the Transfers in the Test must use variable number of Subexecutors\n");
161
162
163
164
        exit(1);
      }
    }

gilbertlee-amd's avatar
gilbertlee-amd committed
165
166
167
168
169
170
171
172
    // 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;
    }

173
174
175
176
177
    // 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 {
gilbertlee-amd's avatar
gilbertlee-amd committed
178
179
180
181
        for (int i = 0; i < transfers.size(); i++) {
          if (!bytesSpecified[i])
            transfers[i].numBytes = currBytes;
        }
182
183

        if (maxVarCount == 0) {
184
          if (RunTransfers(cfgOptions, transfers, results)) {
185
186
            PrintResults(ev, ++testNum, transfers, results);
          }
187
188
189
          if (RankDoesOutput()) {
            PrintErrors(results.errResults);
          }
190
191
192
193
        } else {
          // Variable subexecutors - Determine how many subexecutors to sweep up to
          int maxNumVarSubExec = ev.maxNumVarSubExec;
          if (maxNumVarSubExec == 0) {
194
            maxNumVarSubExec = GetNumSubExecutors({EXE_GPU_GFX, 0}) / maxVarCount;
195
196
          }

197
          TestResults bestResults;
198
199
200
201
202
203
204
          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;
            }

205
206
            TestResults tempResults;
            if (!RunTransfers(cfgOptions, tempTransfers, tempResults)) {
207
208
209
210
211
212
213
214
215
216
217
              PrintErrors(tempResults.errResults);
            } else {
              if (tempResults.avgTotalBandwidthGbPerSec > bestResults.avgTotalBandwidthGbPerSec) {
                bestResults = tempResults;
                bestTransfers = tempTransfers;
              }
            }
          }
          PrintResults(ev, ++testNum, bestTransfers, bestResults);
          PrintErrors(bestResults.errResults);
        }
gilbertlee-amd's avatar
gilbertlee-amd committed
218
        if (numBytesPerTransfer != 0 || !hasUnspecified) break;
219
220
        currBytes += deltaBytes;
      } while (currBytes < bytes * 2);
gilbertlee-amd's avatar
gilbertlee-amd committed
221
      if (numBytesPerTransfer != 0 || !hasUnspecified) break;
222
223
224
225
    }
  }
}

226
void DisplayVersion()
227
{
228
  bool nicSupport = false, mpiSupport = false;
gilbertlee-amd's avatar
gilbertlee-amd committed
229
#if NIC_EXEC_ENABLED
230
231
232
233
  nicSupport = true;
#endif
#if MPI_COMM_ENABLED
  mpiSupport = true;
gilbertlee-amd's avatar
gilbertlee-amd committed
234
#endif
235

236
237
238
239
  std::string support = "";
  if (mpiSupport && nicSupport) support = " (with MPI+NIC support)";
  else if (mpiSupport)          support = " (with MPI support)";
  else if (nicSupport)          support = " (with NIC support)";
240

241
242
243
244
245
  std::string multiNodeMode = "";
  switch (GetCommMode()) {
  case COMM_NONE:   multiNodeMode = " (Single-node mode)";       break;
  case COMM_SOCKET: multiNodeMode = " (Multi-node via sockets)"; break;
  case COMM_MPI:    multiNodeMode = " (Multi-node via MPI)";     break;
246
  }
247
248
249

  Print("TransferBench v%s.%s%s%s\n", VERSION, CLIENT_VERSION, support.c_str(), multiNodeMode.c_str());
  Print("=============================================================================================================\n");
250
251
}

252
void DisplayUsage(char const* cmdName)
253
{
254
255
  if (numa_available() == -1) {
    Print("[ERROR] NUMA library not supported. Check to see if libnuma has been installed on this system\n");
256
257
258
    exit(1);
  }

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

  EnvVars::DisplayUsage();
};