File: GraphBench.cpp

package info (click to toggle)
rccl 5.4.3-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 4,332 kB
  • sloc: cpp: 33,357; ansic: 6,717; xml: 5,265; makefile: 508; sh: 365; awk: 243; python: 85
file content (202 lines) | stat: -rw-r--r-- 7,264 bytes parent folder | download
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
/*
Copyright (c) 2022 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 <iostream>
#include <cstdio>
#include <string>
#include <chrono>
#include <hip/hip_runtime.h>
#include <rccl/rccl.h>

#define HIP_CALL(cmd)                                                 \
  do {                                                                \
    hipError_t error = (cmd);                                         \
    if (error != hipSuccess)                                          \
    {                                                                   \
      std::cout << "Encountered HIP error (" << hipGetErrorString(error) << ") at line " \
                << __LINE__ << " in file " << __FILE__ << "\n";         \
      exit(-1);                                                         \
    }                                                                   \
  } while (0)

#define NCCL_CALL(cmd) \
  do { \
    ncclResult_t error = (cmd);                 \
    if (error != ncclSuccess)                   \
    {                                           \
      std::cout << "Encountered NCCL error (" << ncclGetErrorString(error) << ") at line " \
                << __LINE__ << " in file " << __FILE__ << "\n";         \
      exit(-1);                                                         \
    }                                                                   \
  } while (0)

int main(int argc, char **argv)
{
  int nranks;
  HIP_CALL(hipGetDeviceCount(&nranks));

  // Initialize communicators for each rank
  ncclComm_t comm[nranks];
  NCCL_CALL(ncclCommInitAll(comm, nranks, NULL));

  // Allocate GPU resources
  hipStream_t stream[nranks];
  int* iputCpu[nranks];
  int* iputGpu[nranks];
  int* oputGpu[nranks];
  int* oputCpu[nranks];
  int* pattern;
  int* expected;

  int maxN = (1<<24);

  expected = (int*)calloc(maxN, sizeof(int));
  for (int r = 0; r < nranks; r++)
  {
    HIP_CALL(hipSetDevice(r));
    HIP_CALL(hipStreamCreate(&stream[r]));
    HIP_CALL(hipMalloc((void **)&iputGpu[r], maxN * sizeof(int)));
    HIP_CALL(hipMalloc((void **)&oputGpu[r], maxN * sizeof(int)));

    iputCpu[r] = (int*) malloc(maxN * sizeof(int));
    oputCpu[r] = (int*) malloc(maxN * sizeof(int));
    pattern    = (int*) malloc(maxN * sizeof(int));

    for (int i = 0; i < maxN; i++)
    {
      iputCpu[r][i] = (r * 235 + i) % 2057;
      oputCpu[r][i] = 0;
      expected[i] += iputCpu[r][i];

      pattern[i] = -1 - i;
    }

    HIP_CALL(hipMemcpy(iputGpu[r], iputCpu[r], maxN * sizeof(int), hipMemcpyHostToDevice));
    HIP_CALL(hipMemcpy(oputGpu[r], oputCpu[r], maxN * sizeof(int), hipMemcpyHostToDevice));
  }

  int numWarmups    = 3;
  int numIterations = 5;

  hipGraph_t graphs[nranks];
  hipGraphExec_t graphExec[nranks];

  printf("%12s", "NumBytes");
  for (int usingGraphs = 0; usingGraphs <= 1; usingGraphs++)
  {
    printf("%12s", "Setup");
    for (int i = 1; i <= numIterations; ++i)
      printf("%11s%d", usingGraphs ? "Graph" : "NoGraph", i);
    printf("%12s", "Avg");
  }
  printf("%12s\n", "Speedup");

  for (int N = 1; N <= maxN; N *= 2)
  {
    printf("%12lu", N * sizeof(int));

    double average[2] = {};
    for (int usingGraphs = 0; usingGraphs <= 1; usingGraphs++)
    {
      auto setupStart = std::chrono::high_resolution_clock::now();
      if (usingGraphs)
      {
        for (int r = 0; r < nranks; ++r)
          HIP_CALL(hipStreamBeginCapture(stream[r], hipStreamCaptureModeThreadLocal));

        NCCL_CALL(ncclGroupStart());
        for (int r = 0; r < nranks; ++r)
        {
          HIP_CALL(hipSetDevice(r));
          NCCL_CALL(ncclAllReduce(iputGpu[r], oputGpu[r], N, ncclInt, ncclSum, comm[r], stream[r]));
        }
        NCCL_CALL(ncclGroupEnd());

        for (int r = 0; r < nranks; ++r)
          HIP_CALL(hipStreamEndCapture(stream[r], &graphs[r]));

        // Instantiating graphs
        for (int r = 0; r < nranks; ++r)
          HIP_CALL(hipGraphInstantiate(&graphExec[r], graphs[r], NULL, NULL, 0));
      }
      auto setupDelta = std::chrono::high_resolution_clock::now() - setupStart;
      double setupTime = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(setupDelta).count();
      printf("%12.3f", setupTime);

      // Perform iterations
      average[usingGraphs] = 0;
      for (int iteration = -numWarmups; iteration < numIterations; ++iteration)
      {
        auto cpuStart = std::chrono::high_resolution_clock::now();
        if (usingGraphs)
        {
          for (int r = 0; r < nranks; r++)
            HIP_CALL(hipGraphLaunch(graphExec[r], stream[r]));
        }
        else
        {
          NCCL_CALL(ncclGroupStart());
          for (int r = 0; r < nranks; ++r)
          {
            HIP_CALL(hipSetDevice(r));
            NCCL_CALL(ncclAllReduce(iputGpu[r], oputGpu[r], N, ncclInt, ncclSum, comm[r], stream[r]));
          }
          NCCL_CALL(ncclGroupEnd());
        }
        for (int r = 0; r < nranks; r++)
          HIP_CALL(hipStreamSynchronize(stream[r]));

        auto cpuDelta = std::chrono::high_resolution_clock::now() - cpuStart;
        double iterationTime = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(cpuDelta).count();

        // Check result and reset
        bool isCorrect = true;
        for (int r = 0; r < nranks && isCorrect; r++)
        {
          HIP_CALL(hipMemcpy(oputCpu[r], oputGpu[r], N * sizeof(int), hipMemcpyDeviceToHost));
          for (int i = 0; i < N; i++)
          {
            if (oputCpu[r][i] != expected[i])
            {
              isCorrect = false;
              printf("ERROR: Expected: %d Output %d at Index %d\n", expected[i], oputCpu[r][i], i);
              exit(1);
            }
          }
          // Fill output with input for testing reasons
          HIP_CALL(hipMemcpy(oputGpu[r], pattern, N * sizeof(int), hipMemcpyHostToDevice));
        }

        if (iteration >= 0)
        {
          printf("%12.3f", iterationTime); fflush(stdout);
          average[usingGraphs] += iterationTime;
        }
      }
      average[usingGraphs] /= numIterations;
      printf("%12.3f", average[usingGraphs]);
    }
    printf("%12.3f\n", average[0] / average[1]);
    fflush(stdout);
  }
  return 0;
}