Topology.hpp 11.5 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

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.
*/

#pragma once

#include "TransferBench.hpp"
26
#include "Utilities.hpp"
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

static int RemappedCpuIndex(int origIdx)
{
  static std::vector<int> remappingCpu;

  // Build CPU remapping on first use
  // Skip numa nodes that are not configured
  if (remappingCpu.empty()) {
    for (int node = 0; node <= numa_max_node(); node++)
      if (numa_bitmask_isbitset(numa_get_mems_allowed(), node))
        remappingCpu.push_back(node);
  }
  return remappingCpu[origIdx];
}

gilbertlee-amd's avatar
gilbertlee-amd committed
42
43
44
static void PrintNicToGPUTopo(bool outputToCsv)
{
#ifdef NIC_EXEC_ENABLED
gilbertlee-amd's avatar
gilbertlee-amd committed
45
  printf(" NIC | Device Name | Active | PCIe Bus ID  | NUMA | Closest GPU(s) | GID Index | GID Descriptor\n");
gilbertlee-amd's avatar
gilbertlee-amd committed
46
  if(!outputToCsv)
gilbertlee-amd's avatar
gilbertlee-amd committed
47
    printf("-----+-------------+--------+--------------+------+----------------+-----------+-------------------\n");
gilbertlee-amd's avatar
gilbertlee-amd committed
48
49
50
51
52
53
54
55
56
57
58
59
60

  int numGpus = TransferBench::GetNumExecutors(EXE_GPU_GFX);
  auto const& ibvDeviceList = GetIbvDeviceList();
  for (int i = 0; i < ibvDeviceList.size(); i++) {

    std::string closestGpusStr = "";
    for (int j = 0; j < numGpus; j++) {
      if (TransferBench::GetClosestNicToGpu(j) == i) {
        if (closestGpusStr != "") closestGpusStr += ",";
        closestGpusStr += std::to_string(j);
      }
    }

gilbertlee-amd's avatar
gilbertlee-amd committed
61
    printf(" %-3d | %-11s | %-6s | %-12s | %-4d | %-14s | %-9s | %-20s\n",
gilbertlee-amd's avatar
gilbertlee-amd committed
62
63
64
65
           i, ibvDeviceList[i].name.c_str(),
           ibvDeviceList[i].hasActivePort ? "Yes" : "No",
           ibvDeviceList[i].busId.c_str(),
           ibvDeviceList[i].numaNode,
gilbertlee-amd's avatar
gilbertlee-amd committed
66
           closestGpusStr.c_str(),
67
68
           ibvDeviceList[i].isRoce && ibvDeviceList[i].hasActivePort ? std::to_string(ibvDeviceList[i].gidIndex).c_str() : "N/A",
           ibvDeviceList[i].isRoce && ibvDeviceList[i].hasActivePort ? ibvDeviceList[i].gidDescriptor.c_str() : "N/A"
gilbertlee-amd's avatar
gilbertlee-amd committed
69
          );
gilbertlee-amd's avatar
gilbertlee-amd committed
70
71
72
73
74
  }
  printf("\n");
#endif
}

75
void DisplaySingleRankTopology(bool outputToCsv)
76
77
78
{
  int numCpus = TransferBench::GetNumExecutors(EXE_CPU);
  int numGpus = TransferBench::GetNumExecutors(EXE_GPU_GFX);
gilbertlee-amd's avatar
gilbertlee-amd committed
79
  int numNics = TransferBench::GetNumExecutors(EXE_NIC);
80
81
82
83
84
  char sep = (outputToCsv ? ',' : '|');

  if (outputToCsv) {
    printf("NumCpus,%d\n", numCpus);
    printf("NumGpus,%d\n", numGpus);
gilbertlee-amd's avatar
gilbertlee-amd committed
85
    printf("NumNics,%d\n", numNics);
86
87
88
89
90
  } else {
    printf("\nDetected Topology:\n");
    printf("==================\n");
    printf("  %d configured CPU NUMA node(s) [%d total]\n", numCpus, numa_max_node() + 1);
    printf("  %d GPU device(s)\n", numGpus);
gilbertlee-amd's avatar
gilbertlee-amd committed
91
    printf("  %d Supported NIC device(s)\n", numNics);
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
  }

  // Print out detected CPU topology
  printf("\n            %c", sep);
  for (int j = 0; j < numCpus; j++)
    printf("NUMA %02d%c", j, sep);
  printf(" #Cpus %c Closest GPU(s)\n", sep);

  if (!outputToCsv) {
    printf("------------+");
    for (int j = 0; j <= numCpus; j++)
      printf("-------+");
    printf("---------------\n");
  }

  for (int i = 0; i < numCpus; i++) {
    int nodeI = RemappedCpuIndex(i);
    printf("NUMA %02d (%02d)%c", i, nodeI, sep);
    for (int j = 0; j < numCpus; j++) {
      int nodeJ = RemappedCpuIndex(j);
      int numaDist = numa_distance(nodeI, nodeJ);
      printf(" %5d %c", numaDist, sep);
    }

    int numCpuCores = 0;
    for (int j = 0; j < numa_num_configured_cpus(); j++)
      if (numa_node_of_cpu(j) == nodeI) numCpuCores++;
    printf(" %5d %c", numCpuCores, sep);

    for (int j = 0; j < numGpus; j++) {
      if (TransferBench::GetClosestCpuNumaToGpu(j) == nodeI) {
        printf(" %d", j);
      }
    }
    printf("\n");
  }
  printf("\n");

gilbertlee-amd's avatar
gilbertlee-amd committed
130
131
  // Print out detected NIC topology
  PrintNicToGPUTopo(outputToCsv);
132

gilbertlee-amd's avatar
gilbertlee-amd committed
133
  // Print out detected GPU topology
134
135
136
137
138
139
140
141
142
143
#if defined(__NVCC__)
  for (int i = 0; i < numGpus; i++) {
    hipDeviceProp_t prop;
    HIP_CALL(hipGetDeviceProperties(&prop, i));
    printf(" GPU %02d | %s\n", i, prop.name);
  }
  // No further topology detection done for NVIDIA platforms
  return;
#else
  // Print headers
144
145
146
147
148
149
150
151
152
153
154
  if (numGpus > 0) {
    if (!outputToCsv) {
      printf("        |");
      for (int j = 0; j < numGpus; j++) {
        hipDeviceProp_t prop;
        HIP_CALL(hipGetDeviceProperties(&prop, j));
        std::string fullName = prop.gcnArchName;
        std::string archName = fullName.substr(0, fullName.find(':'));
        printf(" %6s |", archName.c_str());
      }
      printf("\n");
155
156
    }

157
158
159
160
    printf("        %c", sep);
    for (int j = 0; j < numGpus; j++)
      printf(" GPU %02d %c", j, sep);
    printf(" PCIe Bus ID  %c #CUs %c NUMA %c #DMA %c #XCC %c NIC\n", sep, sep, sep, sep, sep);
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
    if (!outputToCsv) {
      for (int j = 0; j <= numGpus; j++)
        printf("--------+");
      printf("--------------+------+------+------+------+------\n");
    }

    // Loop over each GPU device
    for (int i = 0; i < numGpus; i++) {
      printf(" GPU %02d %c", i, sep);

      // Print off link information
      for (int j = 0; j < numGpus; j++) {
        if (i == j) {
          printf("    N/A %c", sep);
        } else {
          uint32_t linkType, hopCount;
          HIP_CALL(hipExtGetLinkTypeAndHopCount(i, j, &linkType, &hopCount));
          printf(" %s-%d %c",
                 linkType == HSA_AMD_LINK_INFO_TYPE_HYPERTRANSPORT ? "  HT" :
                 linkType == HSA_AMD_LINK_INFO_TYPE_QPI            ? " QPI" :
                 linkType == HSA_AMD_LINK_INFO_TYPE_PCIE           ? "PCIE" :
                 linkType == HSA_AMD_LINK_INFO_TYPE_INFINBAND      ? "INFB" :
                 linkType == HSA_AMD_LINK_INFO_TYPE_XGMI           ? "XGMI" : "????",
                 hopCount, sep);
        }
      }

      char pciBusId[20];
      HIP_CALL(hipDeviceGetPCIBusId(pciBusId, 20, i));
      printf(" %-11s %c %-4d %c %-4d %c %-4d %c %-4d %c %-4d\n",
             pciBusId, sep,
             TransferBench::GetNumSubExecutors({EXE_GPU_GFX, i}), sep,
             TransferBench::GetClosestCpuNumaToGpu(i), sep,
             TransferBench::GetNumExecutorSubIndices({EXE_GPU_DMA, i}), sep,
             TransferBench::GetNumExecutorSubIndices({EXE_GPU_GFX, i}), sep,
             TransferBench::GetClosestNicToGpu(i));
    }
199
  }
200
201
#endif
}
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
void DisplayMultiRankTopology(bool outputToCsv, bool showBorders)
{
  Utils::RankGroupMap& groups = Utils::GetRankGroupMap();

  printf("%d rank(s) in %lu homogeneous group(s)\n", TransferBench::GetNumRanks(), groups.size());
  printf("\n");

  // Print off each group
  int groupNum = 1;

  for (auto const& group : groups) {
    Utils::GroupKey const& key    = group.first;
    std::vector<int> const& hosts = group.second;

    std::string              ppodId        = std::get<0>(key);
    int                      vpodId        = std::get<1>(key);
    std::vector<std::string> cpuNames      = std::get<2>(key);
    std::vector<int>         cpuSubExecs   = std::get<3>(key);
    std::vector<std::string> gpuNames      = std::get<4>(key);
    std::vector<int>         gpuSubExecs   = std::get<5>(key);
    std::vector<int>         gpuClosestCpu = std::get<6>(key);
    std::vector<std::string> nicNames      = std::get<7>(key);
    std::vector<int>         nicClosestCpu = std::get<8>(key);
    std::vector<int>         nicClosestGpu = std::get<9>(key);
    std::vector<int>         nicIsActive   = std::get<10>(key);

    int numRanks = hosts.size();
    int numCpus  = cpuNames.size();
    int numGpus  = gpuNames.size();
    int numNics  = nicNames.size();
    int numExecutors = numCpus + numGpus + numNics;
    int numActiveNics = 0;
    for (auto x : nicIsActive) numActiveNics += x;

    if (groupNum > 1) printf("\n");
    printf("Group %03d: %d rank(s) %d CPU(s) %d GPU(s) %d NIC(s) (%d active NICs)\n",
           groupNum++, numRanks, numCpus, numGpus, numNics, numActiveNics);

    // Determine size of table
    int numCols = 7;
    int numRows = 1 + std::max(numRanks, numExecutors);
    TransferBench::Utils::TableHelper table(numRows, numCols);

    // Table borders / alignment
    for (int col = 0; col <= numCols; col++) {
      table.DrawColBorder(col);
      table.SetColAlignment(col, TransferBench::Utils::TableHelper::ALIGN_LEFT);
    }
    table.DrawRowBorder(0);
    table.DrawRowBorder(1);
    table.DrawRowBorder(numRows);

    // Table header
    table.Set(0, 0, " Rank ");
    table.Set(0, 1, " Hostname ");
    table.Set(0, 2, " POD ");
    table.Set(0, 3, " VID ");
    table.Set(0, 4, " Executor ");
    table.Set(0, 5, " Executor Name ");
    table.Set(0, 6, " #SE ");

    // Fill in ranks / hosts
    for (int i = 0; i < numRanks; i++) {
      int rank = hosts[i];
      table.Set(1 + i, 0, " %04d ", rank);
      table.Set(1 + i, 1, " %s ", TransferBench::GetHostname(rank).c_str());
    }

    // Fill in PPOD and VPOD
    table.Set(1, 2, " %s ", ppodId.c_str());
    table.Set(1, 3, " %d ", vpodId);

    // Fill in Executor information
    int rowIdx = 1;
    for (int cpuIndex = 0; cpuIndex < numCpus; cpuIndex++) {
      table.Set(rowIdx, 4, " CPU %02d ", cpuIndex);
      table.Set(rowIdx, 5, " %s ",       cpuNames[cpuIndex].c_str());
      table.Set(rowIdx, 6, " %d ",       cpuSubExecs[cpuIndex]);
      rowIdx++;

      // Loop over each GPU closest to this CPU executor
      for (int gpuIndex = 0; gpuIndex < numGpus; gpuIndex++) {
        if (gpuClosestCpu[gpuIndex] != cpuIndex) continue;
        table.Set(rowIdx, 4, " - GPU %02d ", gpuIndex);
        table.Set(rowIdx, 5, " - %s ",         gpuNames[gpuIndex].c_str());
        table.Set(rowIdx, 6, " %d ",         gpuSubExecs[gpuIndex]);
        rowIdx++;

        //  Loop over each NIC closest to this GPU
        for (int nicIndex = 0; nicIndex < numNics; nicIndex++) {
          if (nicClosestGpu[nicIndex] != gpuIndex) continue;
          table.Set(rowIdx, 4, "   - NIC %02d ", nicIndex);
          table.Set(rowIdx, 5, "   - %s", nicNames[nicIndex].c_str());
          table.Set(rowIdx, 6, " %s ", nicIsActive[nicIndex] ? "ON" : "OFF");
          rowIdx++;
        }
      }

      // Loop over remaining NICs not associated with GPU but associated with this CPU
      for (int nicIndex = 0; nicIndex < numNics; nicIndex++) {
        if (nicClosestGpu[nicIndex] != -1 || nicClosestCpu[nicIndex] != cpuIndex) continue;
        table.Set(rowIdx, 4, " - NIC %02d ", nicIndex);
        table.Set(rowIdx, 5, " - %s ", nicNames[nicIndex].c_str());
        table.Set(rowIdx, 6, " %s ", nicIsActive[nicIndex] ? "ON" : "OFF");
        rowIdx++;
309
310
      }
    }
311
312
    table.PrintTable(outputToCsv, showBorders);
  }
313

314
315
  if (Utils::HasDuplicateHostname()) {
    printf("[WARN] It is recommended to run TransferBench with one rank per host to avoid potential aliasing of executors\n");
316
  }
317
318
319
320
321
322
323
324
}

void DisplayTopology(bool outputToCsv, bool showBorders)
{
  if (GetNumRanks() > 1)
    DisplayMultiRankTopology(outputToCsv, showBorders);
  else
    DisplaySingleRankTopology(outputToCsv);
325
}