Files
rocm-systems/projects/rccl/src/graph/rings.cc
T

64 lines
2.2 KiB
C++
Raw Normal View History

2019-11-19 14:57:39 -08:00
/*************************************************************************
* Copyright (c) 2016-2019, NVIDIA CORPORATION. All rights reserved.
*
* See LICENSE.txt for license information
************************************************************************/
#include "core.h"
void dumpLine(int* values, int nranks, const char* prefix) {
2024-09-10 05:57:10 -07:00
constexpr int line_length = 128;
char line[line_length];
int num_width = snprintf(nullptr, 0, "%d", nranks-1); // safe as per "man snprintf"
int n = snprintf(line, line_length, "%s", prefix);
for (int i = 0; i < nranks && n < line_length-1; i++) {
n += snprintf(line + n, line_length - n, " %*d", num_width, values[i]);
// At this point n may be more than line_length-1, so don't use it
// for indexing into "line".
}
if (n >= line_length) {
// Sprintf wanted to write more than would fit in the buffer. Assume
// line_length is at least 4 and replace the end with "..." to
// indicate that it was truncated.
snprintf(line+line_length-4, 4, "...");
}
INFO(NCCL_INIT, "%s", line);
2019-11-19 14:57:39 -08:00
}
ncclResult_t ncclBuildRings(int nrings, int* rings, int rank, int nranks, int* prev, int* next) {
for (int r=0; r<nrings; r++) {
2020-09-04 14:35:05 -07:00
char prefix[40];
2019-11-19 14:57:39 -08:00
/*sprintf(prefix, "[%d] Channel %d Prev : ", rank, r);
dumpLine(prev+r*nranks, nranks, prefix);
sprintf(prefix, "[%d] Channel %d Next : ", rank, r);
dumpLine(next+r*nranks, nranks, prefix);*/
int current = rank;
for (int i=0; i<nranks; i++) {
rings[r*nranks+i] = current;
current = next[r*nranks+current];
}
2024-09-10 05:57:10 -07:00
snprintf(prefix, sizeof(prefix), "Channel %02d/%02d :", r, nrings);
2019-11-19 14:57:39 -08:00
if (rank == 0) dumpLine(rings+r*nranks, nranks, prefix);
if (current != rank) {
WARN("Error : ring %d does not loop back to start (%d != %d)", r, current, rank);
return ncclInternalError;
}
// Check that all ranks are there
for (int i=0; i<nranks; i++) {
int found = 0;
for (int j=0; j<nranks; j++) {
if (rings[r*nranks+j] == i) {
found = 1;
break;
}
}
if (found == 0) {
WARN("Error : ring %d does not contain rank %d", r, i);
return ncclInternalError;
}
}
}
return ncclSuccess;
}