File: HelloRccl.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 (257 lines) | stat: -rw-r--r-- 8,363 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
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
/*
Copyright (c) 2020 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 <sys/socket.h>
#include <ifaddrs.h>
#include <netdb.h>
#include <unistd.h>
#include <cstdio>
#include <string>
#include <chrono>
#include <hip/hip_runtime.h>
#include <rccl.h>
#include "HelloRccl.hpp"


void Usage(char *argv0);
void ExecuteTest(int numIntraRank, int intraRankStartId, int numTotalRanks, ncclComm_t* comm);

int main(int argc, char **argv)
{
  if (getenv("NCCL_COMM_ID") && argc == 3) // Run in multi-process mode
  {
    int nranks   = atoi(argv[1]);
    int rank     = atoi(argv[2]);
    if (rank == 0) printf("Running in multi-process mode\n");

    // Create communicator for this rank
    ncclUniqueId commId;
    NCCL_CALL(ncclGetUniqueId(&commId));

    // Initialize communicator
    ncclComm_t comm;
    HIP_CALL(hipSetDevice(rank));
    NCCL_CALL(ncclCommInitRank(&comm, nranks, commId, rank));

    // Run the test
    ExecuteTest(1, rank, nranks, &comm);
  }
  else if (argc == 2) // Run in single-process mode
  {
    printf("Running in single-process mode\n");

    int nranks   = atoi(argv[1]);

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

    // Run the test
    ExecuteTest(nranks, 0, nranks, comm);
  }
  else
  {
    Usage(argv[0]);
    return 1;
  }
  return 0;
}

void ExecuteTest(int numIntraRank, int intraRankStartId, int numTotalRanks, ncclComm_t* comm)
{
  // Test configuration settings
  int minPow        = 10;      // Starting power of 2 input size
  int maxPow        = 28;      // Ending power of 2 input size
  int numWarmups    =  3;      // Number of untimed warmup iterations
  int numIterations = 10;      // Number of timed iterations

  // Allocate GPU resources for this process
  hipStream_t stream[numIntraRank];
  hipEvent_t  startEvent[numIntraRank];
  hipEvent_t  stopEvent[numIntraRank];
  for (int i = 0; i < numIntraRank; i++)
  {
    HIP_CALL(hipSetDevice(intraRankStartId + i));
    HIP_CALL(hipStreamCreate(&stream[i]));
    HIP_CALL(hipEventCreate(&startEvent[i]));
    HIP_CALL(hipEventCreate(&stopEvent[i]));
  }

  if (intraRankStartId == 0)
  {
    printf("AllReduce Performance (sum of floats):\n");
    printf("%10s %10s %10s\n", "Bytes", "CpuTime(ms)", "GpuTime(ms)");
  }

  // Loop over power-of-two input sizes
  for (int power = minPow; power <= maxPow; power++)
  {
    int N = 1 << power;

    // Allocate GPU memory
    float *iputGpu[numIntraRank], *oputGpu[numIntraRank];
    for (int r = 0; r < numIntraRank; r++)
    {
      HIP_CALL(hipSetDevice(intraRankStartId + r));
      HIP_CALL(hipMalloc((void **)&iputGpu[r], N * sizeof(float)));
      HIP_CALL(hipMalloc((void **)&oputGpu[r], N * sizeof(float)));
    }

    // Allocate CPU memory for input/output
    float *iputCpu = (float *)malloc(N * sizeof(float));
    float *oputCpu = (float *)malloc(N * sizeof(float));

    // Fill CPU memory with a simple pattern
    for (int i = 0; i < N; i++)
    {
      iputCpu[i] = 1.0f;
      oputCpu[i] = 0.0f;
    }

    // Copy the input from CPU memory to GPU memory
    for (int r = 0; r < numIntraRank; r++)
    {
      HIP_CALL(hipSetDevice(intraRankStartId + r));
      HIP_CALL(hipMemcpy(iputGpu[r], iputCpu, N * sizeof(float), hipMemcpyHostToDevice));
    }

    // Perform some untimed initial warmup iterations
    for (int iteration = 0; iteration < numWarmups; iteration++)
    {
      NCCL_CALL(ncclGroupStart());
      for (int r = 0; r < numIntraRank; r++)
      {
        HIP_CALL(hipSetDevice(intraRankStartId + r));
        NCCL_CALL(ncclAllReduce(iputGpu[r], oputGpu[r], N, ncclFloat, ncclSum, comm[r], stream[r]));
      }
      NCCL_CALL(ncclGroupEnd());
    }
    for (int r = 0; r < numIntraRank; r++)
      HIP_CALL(hipStreamSynchronize(stream[r]));

    // Perform timed iterations
    auto cpuStart = std::chrono::high_resolution_clock::now();
    for (int r = 0; r < numIntraRank; r++)
      HIP_CALL(hipEventRecord(startEvent[r], stream[r]));

    for (int iteration = 0; iteration < numIterations; iteration++)
    {
      NCCL_CALL(ncclGroupStart());
      for (int r = 0; r < numIntraRank; r++)
      {
        HIP_CALL(hipSetDevice(intraRankStartId + r));
        NCCL_CALL(ncclAllReduce(iputGpu[r], oputGpu[r], N, ncclFloat, ncclSum, comm[r], stream[r]));
      }
      NCCL_CALL(ncclGroupEnd());
    }

    for (int r = 0; r < numIntraRank; r++)
      HIP_CALL(hipEventRecord(stopEvent[r], stream[r]));

    for (int r = 0; r < numIntraRank; r++)
      HIP_CALL(hipStreamSynchronize(stream[r]));

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

    float totalGpuTime;
    HIP_CALL(hipEventElapsedTime(&totalGpuTime, startEvent[0], stopEvent[0]));

    if (intraRankStartId == 0) printf("%10lu %10.3f %10.3f\n", N * sizeof(float), (totalCpuTime / numIterations), (totalGpuTime / numIterations));

    // Validate results
    for (int r = 0; r < numIntraRank; r++)
    {
      HIP_CALL(hipMemcpy(oputCpu, oputGpu[r], N * sizeof(float), hipMemcpyDeviceToHost));
      bool isOK = true;
      int expected = numTotalRanks;
      for (int i = 0; i < N; i++)
      {
        isOK &= (oputCpu[i] == expected);
      }
      if (!isOK)
      {
        printf("[ERROR] Rank %d Incorrect results for N = %d\n", intraRankStartId + r, N);
        NCCL_CALL(ncclCommDestroy(comm[r]));
        exit(1);
      }
    }

    // Release GPU resources
    for (int r = 0; r < numIntraRank; r++)
    {
      HIP_CALL(hipFree(oputGpu[r]));
      HIP_CALL(hipFree(iputGpu[r]));
    }
    free(iputCpu);
    free(oputCpu);
  }

  for (int r = 0; r < numIntraRank; r++)
  {
    HIP_CALL(hipStreamDestroy(stream[r]));
    HIP_CALL(hipEventDestroy(startEvent[r]));
    HIP_CALL(hipEventDestroy(stopEvent[r]));
    NCCL_CALL(ncclCommDestroy(comm[r]));
  }
}

void Usage(char *argv0)
{
  printf("Single Process Usage: %s numRanks\n", argv0);
  printf("\n");
  printf("Multi Process Usage: %s numRanks rank\n", argv0);
  printf(" - NCCL_COMM_ID must be set in order to use this\n\n");
  printf(" - To use this process as the root process you may use any of the following:\n");

  char hostname[256];
  gethostname(hostname, 256);
  printf("    export NCCL_COMM_ID=%s:12345\n", hostname);

  // Loop over linked list of interfaces
  struct ifaddrs *ifaddr;
  getifaddrs(&ifaddr);
  for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
  {
    // Skip anything not based on IPv4 / IPv6
    int family = ifa->ifa_addr->sa_family;
    if (family != AF_INET && family != AF_INET6) continue;

    // Skip iPv6 loopback interface
    if (family == AF_INET6)
    {
      struct sockaddr_in6* sa = (struct sockaddr_in6*)(ifa->ifa_addr);
      if (IN6_IS_ADDR_LOOPBACK(&sa->sin6_addr)) continue;
    }

    socklen_t saLen = (family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6));
    char host[NI_MAXHOST];
    char service[NI_MAXSERV];

    getnameinfo(ifa->ifa_addr, saLen, host, NI_MAXHOST, service, NI_MAXSERV,
                NI_NUMERICHOST|NI_NUMERICSERV);

    std::string result = std::string(host) + ":12345";
    printf("    export NCCL_COMM_ID=%s\n", result.c_str());
  }
  freeifaddrs(ifaddr);
}