Files
rocm-systems/projects/rccl/src/init.cc
T

1076 lines
41 KiB
C++
Raw Normal View History

2018-09-24 16:06:59 -07:00
/*************************************************************************
2021-04-12 16:00:11 -07:00
* Copyright (c) 2015-2021, NVIDIA CORPORATION. All rights reserved.
2018-09-24 16:06:59 -07:00
*
* See LICENSE.txt for license information
************************************************************************/
#include "nccl.h"
2018-12-13 15:56:12 -08:00
#include "channel.h"
2018-09-24 16:06:59 -07:00
#include "nvmlwrap.h"
2021-04-12 16:00:11 -07:00
#include "gdrwrap.h"
2018-09-24 16:06:59 -07:00
#include "bootstrap.h"
#include "transport.h"
#include "group.h"
#include "net.h"
2020-01-16 16:02:42 -08:00
#include "coll_net.h"
2018-12-13 15:56:12 -08:00
#include "enqueue.h"
2019-11-19 14:57:39 -08:00
#include "graph.h"
#include "argcheck.h"
2018-09-24 16:06:59 -07:00
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
2018-11-13 10:37:20 -08:00
#include <dlfcn.h>
2019-11-19 14:57:39 -08:00
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
2018-09-24 16:06:59 -07:00
2018-11-13 10:37:20 -08:00
#define STR2(v) #v
#define STR(v) STR2(v)
2018-09-24 16:06:59 -07:00
#ifdef ENABLE_TRACE
std::chrono::high_resolution_clock::time_point ncclEpoch;
#endif
2019-03-14 19:39:20 -07:00
#if CUDART_VERSION >= 9020
2018-09-24 16:06:59 -07:00
#define NCCL_GROUP_CUDA_STREAM 0 // CGMD: CUDA 9.2,10.X Don't need to use an internal CUDA stream
#else
#define NCCL_GROUP_CUDA_STREAM 1 // CGMD: CUDA 9.0,9.1 Need to use an internal CUDA stream
#endif
2020-05-12 14:40:18 -07:00
const char* ncclFuncStr[NCCL_NUM_FUNCTIONS] = { "Broadcast", "Reduce", "AllGather", "ReduceScatter", "AllReduce" };
const char* ncclAlgoStr[NCCL_NUM_ALGORITHMS] = { "Tree", "Ring", "CollNet" };
const char* ncclProtoStr[NCCL_NUM_PROTOCOLS] = { "LL", "LL128", "Simple" };
2018-09-24 16:06:59 -07:00
NCCL_PARAM(GroupCudaStream, "GROUP_CUDA_STREAM", NCCL_GROUP_CUDA_STREAM);
NCCL_PARAM(CheckPointers, "CHECK_POINTERS", 0);
ncclNet_t* ncclNet = NULL;
2020-01-16 16:02:42 -08:00
ncclCollNet_t* ncclCollNet = NULL;
2018-09-24 16:06:59 -07:00
2018-12-04 14:47:41 -08:00
// Returns ncclInternalError if anything fails, causing that network to be ignored.
2018-11-13 10:37:20 -08:00
ncclResult_t initNet(ncclNet_t* net) {
int ndev;
2018-12-04 14:47:41 -08:00
if (net->init(ncclDebugLog) != ncclSuccess) return ncclInternalError;
if (net->devices(&ndev) != ncclSuccess) return ncclInternalError;
2018-12-13 15:56:12 -08:00
if (ndev <= 0) return ncclSystemError;
2018-11-13 10:37:20 -08:00
return ncclSuccess;
}
2020-01-16 16:02:42 -08:00
ncclResult_t initCollNet(ncclCollNet_t* collnet) {
int ndev;
if (collnet->init(ncclDebugLog) != ncclSuccess) return ncclInternalError;
if (collnet->devices(&ndev) != ncclSuccess) return ncclInternalError;
if (ndev <= 0) return ncclSystemError;
return ncclSuccess;
}
ncclResult_t initNetPlugin(ncclNet_t** net, ncclCollNet_t** collnet) {
2018-11-13 10:37:20 -08:00
void* netPluginLib = dlopen("libnccl-net.so", RTLD_NOW | RTLD_LOCAL);
if (netPluginLib == NULL) {
// dlopen does not guarantee to set errno, but dlerror only gives us a
// string, so checking errno doesn't hurt to try to provide a better
// error message
if (errno == ENOENT) {
2019-11-19 14:57:39 -08:00
INFO(NCCL_INIT|NCCL_NET, "NET/Plugin : No plugin found (libnccl-net.so), using internal implementation");
} else {
2018-12-13 15:56:12 -08:00
INFO(NCCL_INIT|NCCL_NET, "NET/Plugin : Plugin load returned %d : %s.", errno, dlerror());
}
2018-11-13 10:37:20 -08:00
return ncclSuccess;
}
2021-07-08 14:12:04 -07:00
*net = (ncclNet_t*) dlsym(netPluginLib, STR(NCCL_PLUGIN_SYMBOL));
if (*net == NULL) {
2018-12-13 15:56:12 -08:00
INFO(NCCL_INIT|NCCL_NET, "NET/Plugin: Failed to find " STR(NCCL_PLUGIN_SYMBOL) " symbol.");
2021-07-08 14:12:04 -07:00
if (netPluginLib != NULL) dlclose(netPluginLib);
2018-11-13 10:37:20 -08:00
return ncclSuccess;
}
2021-07-08 14:12:04 -07:00
// Check for CollNet
*collnet = (ncclCollNet_t*) dlsym(netPluginLib, STR(NCCL_COLLNET_PLUGIN_SYMBOL));
if (*collnet == NULL) {
INFO(NCCL_INIT|NCCL_NET, "NET/Plugin: Failed to find " STR(NCCL_COLLNET_PLUGIN_SYMBOL) " symbol.");
}
2018-11-13 10:37:20 -08:00
return ncclSuccess;
}
ncclResult_t initNet() {
2019-06-25 13:22:47 -07:00
// Always initialize bootstrap network
NCCLCHECK(bootstrapNetInit());
2018-11-13 10:37:20 -08:00
2021-07-08 14:12:04 -07:00
// Initialize main communication network
ncclNet_t* nets[3] = { NULL, &ncclNetIb, &ncclNetSocket };
ncclCollNet_t* collNets[3] = { NULL, NULL, NULL };
NCCLCHECK(initNetPlugin(nets+0, collNets+0));
char* netName = getenv("NCCL_NET");
for (int i=0; i<3; i++) {
if (nets[i] == NULL) continue;
if (netName && strcmp(netName, nets[i]->name) != 0) continue;
// net plugin is already initialized
if (initNet(nets[i]) != ncclSuccess) continue;
ncclNet = nets[i];
if (collNets[i] && initCollNet(collNets[i]) == ncclSuccess) {
ncclCollNet = collNets[i];
}
break;
}
if (ncclNet == NULL) {
WARN("Error: network %s not found.", netName ? netName : "");
return ncclInvalidUsage;
2018-09-24 16:06:59 -07:00
}
2018-11-13 10:37:20 -08:00
return ncclSuccess;
2018-09-24 16:06:59 -07:00
}
2021-04-12 16:00:11 -07:00
// GDRCOPY support: Off by default
NCCL_PARAM(GdrCopyEnable, "GDRCOPY_ENABLE", 0);
// GDRCOPY support
gdr_t ncclGdrCopy = NULL;
ncclResult_t initGdrCopy() {
if (ncclParamGdrCopyEnable() == 1) {
ncclGdrCopy = ncclGdrInit();
}
return ncclSuccess;
}
2020-01-16 16:02:42 -08:00
NCCL_PARAM(CollNetEnable, "COLLNET_ENABLE", 0);
2018-09-24 16:06:59 -07:00
pthread_mutex_t initLock = PTHREAD_MUTEX_INITIALIZER;
static bool initialized = false;
2021-04-12 16:00:11 -07:00
static size_t maxLocalSizeBytes = 0;
2018-09-24 16:06:59 -07:00
static ncclResult_t ncclInit() {
if (initialized) return ncclSuccess;
pthread_mutex_lock(&initLock);
if (!initialized) {
initEnv();
2021-04-12 16:00:11 -07:00
initGdrCopy();
maxLocalSizeBytes = ncclKernMaxLocalSize();
2020-05-12 14:40:18 -07:00
NCCLCHECK(initNet());
2020-01-16 16:02:42 -08:00
INFO(NCCL_INIT, "Using network %s", ncclNetName());
2018-09-24 16:06:59 -07:00
initialized = true;
}
pthread_mutex_unlock(&initLock);
return ncclSuccess;
}
NCCL_API(ncclResult_t, ncclGetVersion, int* version);
ncclResult_t ncclGetVersion(int* version) {
if (version == NULL) return ncclInvalidArgument;
*version = NCCL_VERSION_CODE;
return ncclSuccess;
}
NCCL_API(ncclResult_t, ncclGetUniqueId, ncclUniqueId* out);
ncclResult_t ncclGetUniqueId(ncclUniqueId* out) {
NCCLCHECK(ncclInit());
NCCLCHECK(PtrCheck(out, "GetUniqueId", "out"));
return bootstrapGetUniqueId(out);
}
2019-03-14 19:39:20 -07:00
// Prevent compiler from optimizing out these operations
2019-12-06 18:14:55 +01:00
#ifdef __clang__
2019-12-09 18:31:13 +01:00
#define NCCL_NO_OPTIMIZE __attribute__((optnone))
2019-12-06 18:14:55 +01:00
#else
#define NCCL_NO_OPTIMIZE __attribute__((optimize("O0")))
#endif
void NCCL_NO_OPTIMIZE commPoison(ncclComm_t comm) {
2019-11-19 14:57:39 -08:00
comm->rank = comm->cudaDev = comm->busId = comm->nRanks = -1;
2019-03-14 19:39:20 -07:00
}
2019-12-06 18:14:55 +01:00
#undef NCCL_NO_OPTIMIZE
2018-09-24 16:06:59 -07:00
static ncclResult_t commFree(ncclComm_t comm) {
if (comm == NULL)
return ncclSuccess;
2020-09-04 14:35:05 -07:00
free(comm->connectSend);
free(comm->connectRecv);
2021-07-08 14:12:04 -07:00
for (int peer=0; peer<comm->nRanks; peer++) {
delete comm->p2pSends[peer];
delete comm->p2pRecvs[peer];
}
2020-09-04 14:35:05 -07:00
free(comm->p2pSends);
free(comm->p2pRecvs);
free(comm->asyncOps);
2018-09-24 16:06:59 -07:00
2018-12-13 15:56:12 -08:00
free(comm->peerInfo);
2019-11-19 14:57:39 -08:00
ncclTopoFree(comm->topo);
2018-12-13 15:56:12 -08:00
if (comm->bootstrap)
NCCLCHECK(bootstrapClose(comm->bootstrap));
2021-07-08 14:12:04 -07:00
CUDACHECK(cudaFree((ncclDevCommAndChannels*)comm->devComm));
2018-09-24 16:06:59 -07:00
2020-05-12 14:40:18 -07:00
for (int channel=0; channel<MAXCHANNELS; channel++)
2018-12-13 15:56:12 -08:00
NCCLCHECK(freeChannel(comm->channels+channel, comm->nRanks));
2018-09-24 16:06:59 -07:00
if (comm->doneEvent != NULL)
CUDACHECK(cudaEventDestroy(comm->doneEvent));
2021-04-12 16:00:11 -07:00
if (comm->intDoneEvent != NULL)
CUDACHECK(cudaEventDestroy(comm->intDoneEvent));
2018-09-24 16:06:59 -07:00
if (comm->launchMode == ncclComm::GROUP) {
CUDACHECK(cudaStreamDestroy(comm->groupStream));
}
2021-04-12 16:00:11 -07:00
ncclDestroyQueueInfo(comm->enqueueInfo);
2018-09-24 16:06:59 -07:00
// Last rank frees shared resources between threads
int isLast;
NCCLCHECK(ncclCpuBarrierIn(comm, &isLast));
if (isLast) {
free(comm->intraBarrier);
free(comm->intraParams);
free(comm->intraCudaDevs);
free(comm->intraCGMode);
free(comm->intraCC);
}
2020-09-04 14:35:05 -07:00
NCCLCHECK(ncclCudaHostFree((void *)comm->abortFlag));
2018-09-24 16:06:59 -07:00
2019-03-14 19:39:20 -07:00
// Poison comm to try and catch a double free
commPoison(comm);
2018-09-24 16:06:59 -07:00
free(comm);
return ncclSuccess;
}
2021-07-08 14:12:04 -07:00
NCCL_PARAM(AggChannelSize, "AGG_CHANNEL_SIZE", -2);
2018-09-24 16:06:59 -07:00
static ncclResult_t commAlloc(ncclComm_t* comret, int ndev, int rank) {
if (ndev < 1) {
WARN("invalid device count (%d) requested", ndev);
return ncclInvalidArgument;
}
if (rank >= ndev || rank < 0) {
WARN("rank %d exceeds ndev=%d", rank, ndev);
return ncclInvalidArgument;
}
// Try to create a CUDA object right away. If there is something wrong with
// the device we're on (failure cause #1) , better know it early.
cudaEvent_t doneEvent;
CUDACHECK(cudaEventCreateWithFlags(&doneEvent, cudaEventDisableTiming));
2021-04-12 16:00:11 -07:00
cudaEvent_t intDoneEvent;
CUDACHECK(cudaEventCreateWithFlags(&intDoneEvent, cudaEventDisableTiming));
2018-09-24 16:06:59 -07:00
struct ncclComm* comm;
NCCLCHECK(ncclCalloc(&comm, 1));
2020-09-04 14:35:05 -07:00
comm->rank = comm->hostDevComm.rank = rank;
2019-03-14 19:39:20 -07:00
comm->nRanks = comm->hostDevComm.nRanks = ndev;
2018-09-24 16:06:59 -07:00
cudaGetDevice(&comm->cudaDev);
2019-11-19 14:57:39 -08:00
NCCLCHECK(getBusId(comm->cudaDev, &comm->busId));
2021-05-11 18:16:30 -07:00
TRACE(NCCL_INIT,"comm %p rank %d nranks %d cudaDev %d busId %lx", comm, rank, ndev, comm->cudaDev, comm->busId);
2018-12-13 15:56:12 -08:00
2018-09-24 16:06:59 -07:00
comm->doneEvent = doneEvent;
2021-04-12 16:00:11 -07:00
comm->intDoneEvent = intDoneEvent;
2018-09-24 16:06:59 -07:00
comm->checkPointers = ncclParamCheckPointers() == 1 ? true : false;
2019-03-14 19:39:20 -07:00
#if CUDART_VERSION >= 9020
2018-09-24 16:06:59 -07:00
comm->groupCudaStream = ncclParamGroupCudaStream();
#else
// Don't allow the user to overload the default setting in older CUDA builds
comm->groupCudaStream = NCCL_GROUP_CUDA_STREAM;
#endif
2018-12-13 15:56:12 -08:00
comm->fatalError = ncclSuccess;
2020-05-12 14:40:18 -07:00
NCCLCHECK(ncclCudaHostCalloc((uint32_t**)&comm->abortFlag, 1));
comm->hostDevComm.abortFlag = comm->abortFlag;
2018-12-13 15:56:12 -08:00
*comm->abortFlag = 0;
2018-09-24 16:06:59 -07:00
comm->argsptr = &comm->args;
2020-01-16 16:02:42 -08:00
comm->collNetSupport = 0;
2020-09-04 14:35:05 -07:00
NCCLCHECK(ncclCalloc(&comm->asyncOps, NCCL_MAX_OPS));
comm->asyncOpCount = 0;
comm->asyncTotalSize = 0;
2021-07-08 14:12:04 -07:00
comm->channelSize = ncclParamAggChannelSize();
comm->asyncAllocMode = ncclComm::SHORTEST_QUEUE;
char* str = getenv("NCCL_AGG_ALLOC_MODE");
if (str) INFO(NCCL_ENV, "NCCL_AGG_ALLOC_MODE set by environment to %s", str);
if (str && strcmp(str, "ROUND_ROBIN") == 0) {
comm->asyncAllocMode = ncclComm::ROUND_ROBIN;
}
2020-09-04 14:35:05 -07:00
2021-07-08 14:12:04 -07:00
NCCLCHECK(ncclCreateQueueInfo(&comm->enqueueInfo, comm));
2021-04-12 16:00:11 -07:00
comm->lastSetupNode = NULL;
comm->lastCudaGraphId = -1;
2021-05-11 18:16:30 -07:00
CUDACHECK(cudaDriverGetVersion(&comm->driverVersion));
2020-09-04 14:35:05 -07:00
static_assert(MAXCHANNELS <= sizeof(*comm->connectSend)*8, "comm->connectSend must have enough bits for all channels");
static_assert(MAXCHANNELS <= sizeof(*comm->connectRecv)*8, "comm->connectRecv must have enough bits for all channels");
NCCLCHECK(ncclCalloc(&comm->connectSend, comm->nRanks));
NCCLCHECK(ncclCalloc(&comm->connectRecv, comm->nRanks));
comm->p2pSendCount = comm->p2pRecvCount = 0;
NCCLCHECK(ncclCalloc(&comm->p2pSends, comm->nRanks));
NCCLCHECK(ncclCalloc(&comm->p2pRecvs, comm->nRanks));
2020-05-12 14:40:18 -07:00
// Mark channels as non initialized.
for (int c=0; c<MAXCHANNELS; c++) comm->channels[c].id = -1;
2018-09-24 16:06:59 -07:00
*comret = comm;
return ncclSuccess;
}
static ncclResult_t devCommSetup(ncclComm_t comm) {
2021-07-08 14:12:04 -07:00
ncclDevCommAndChannels *devCommAndChans;
NCCLCHECK(ncclCudaCalloc(&devCommAndChans, 1));
comm->devComm = &devCommAndChans->comm;
comm->hostDevComm.channels = devCommAndChans->channels;
2019-03-14 19:39:20 -07:00
// Duplicate the channels on the device
2021-05-11 18:16:30 -07:00
int nChannels = std::max(comm->nChannels, comm->p2pnChannels);
NCCLCHECK(ncclCudaMemcpy(comm->hostDevComm.channels, comm->channels, nChannels));
2019-03-14 19:39:20 -07:00
// Copy userRanks and peers
2021-05-11 18:16:30 -07:00
for (int r=0; r<comm->nChannels; r++) {
2018-12-13 15:56:12 -08:00
NCCLCHECK(ncclCudaMemcpy(comm->channels[r].ring.devUserRanks, comm->channels[r].ring.userRanks, comm->nRanks));
2018-09-24 16:06:59 -07:00
}
2019-03-14 19:39:20 -07:00
// Duplicate the dev comm on the device
NCCLCHECK(ncclCudaMemcpy(comm->devComm, &comm->hostDevComm, 1));
2018-09-24 16:06:59 -07:00
return ncclSuccess;
}
// Pre-process the string so that running "strings" on the lib can quickly reveal the version.
#define VERSION_STRING "NCCL version " STR(NCCL_MAJOR) "." STR(NCCL_MINOR) "." STR(NCCL_PATCH) NCCL_SUFFIX "+cuda" STR(CUDA_MAJOR) "." STR(CUDA_MINOR)
static void showVersion() {
static int shown = 0;
2018-11-13 10:37:20 -08:00
if (shown == 0 && ncclDebugLevel >= NCCL_LOG_VERSION) {
2018-09-24 16:06:59 -07:00
printf("%s\n", VERSION_STRING);
fflush(stdout);
if (ncclDebugFile != stdout)
2018-11-13 10:37:20 -08:00
INFO(NCCL_ALL,"%s", VERSION_STRING); // Also log NCCL version in one of the files
2018-09-24 16:06:59 -07:00
shown = 1;
}
}
2019-11-19 14:57:39 -08:00
static ncclResult_t fillInfo(struct ncclComm* comm, struct ncclPeerInfo* info, uint64_t commHash) {
info->rank = comm->rank;
2018-12-13 15:56:12 -08:00
CUDACHECK(cudaGetDevice(&info->cudaDev));
info->hostHash=getHostHash()+commHash;
info->pidHash=getPidHash()+commHash;
2018-12-13 15:56:12 -08:00
2019-11-19 14:57:39 -08:00
// Get the device MAJOR:MINOR of /dev/shm so we can use that
// information to decide whether we can use SHM for inter-process
// communication in a container environment
struct stat statbuf;
SYSCHECK(stat("/dev/shm", &statbuf), "stat");
info->shmDev = statbuf.st_dev;
info->busId = comm->busId;
2020-01-16 16:02:42 -08:00
NCCLCHECK(ncclGpuGdrSupport(&info->gdrSupport));
2018-09-24 16:06:59 -07:00
return ncclSuccess;
}
2019-11-19 14:57:39 -08:00
static ncclResult_t setupChannel(struct ncclComm* comm, int channelId, int rank, int nranks, int* ringRanks) {
2018-12-13 15:56:12 -08:00
TRACE(NCCL_INIT, "rank %d nranks %d", rank, nranks);
NCCLCHECK(initChannel(comm, channelId));
2019-11-19 14:57:39 -08:00
struct ncclRing* ring = &comm->channels[channelId].ring;
2021-07-08 14:12:04 -07:00
// Find our ring-distance from rank zero and reorganize ranks to start with rank.
int ixZero=0, ixRank=0;
for (int i=0; i < nranks; i++) {
if (ringRanks[i] == 0) ixZero = i;
if (ringRanks[i] == rank) ixRank = i;
2018-09-24 16:06:59 -07:00
}
2021-07-08 14:12:04 -07:00
ring->index = (ixRank-ixZero + nranks)%nranks;
2018-09-24 16:06:59 -07:00
for (int i=0; i<nranks; i++) {
2021-07-08 14:12:04 -07:00
ring->userRanks[i] = ringRanks[(i+ixRank)%nranks];
2018-09-24 16:06:59 -07:00
}
return ncclSuccess;
}
void* waitForNonNullPtr(void* p) {
volatile void** ptr = (volatile void**) p;
while (*ptr == NULL) sched_yield();
return (void*)*ptr;
}
ncclResult_t initParams(struct ncclComm* comm) {
struct cudaLaunchParams* params = comm->myParams = comm->intraParams+comm->intraRank;
params->args = &comm->argsptr;
params->stream = NULL;
params->sharedMem = 0;
params->blockDim.x = 0; params->blockDim.y = params->blockDim.z = 1;
params->gridDim.x = 0; params->gridDim.y = params->gridDim.z = 1;
return ncclSuccess;
}
// Allocate/Set Intra Process Structures and set CG options
2021-07-08 14:12:04 -07:00
ncclResult_t ncclCommSetIntraProc(struct ncclComm* comm, int rank, int ranks, struct ncclComm* comm0) {
2018-09-24 16:06:59 -07:00
comm->intraRank = rank;
comm->intraRanks = ranks;
comm->intraPhase = 0;
// Alloc shared structures
if (rank == 0) {
assert(comm == comm0);
int* bar;
NCCLCHECK(ncclCalloc(&bar, 2));
bar[0] = bar[1] = 0;
comm->intraBarrier = bar;
NCCLCHECK(ncclCalloc(&comm->intraParams, comm->intraRanks));
NCCLCHECK(ncclCalloc(&comm->intraCudaDevs, comm->intraRanks));
int* CGMode;
NCCLCHECK(ncclCalloc(&CGMode, 1));
*CGMode = 0x11;
comm->intraCGMode = CGMode;
int* CC;
NCCLCHECK(ncclCalloc(&CC, 1));
2019-11-19 14:57:39 -08:00
*CC = ncclCudaCompCap();
2018-09-24 16:06:59 -07:00
comm->intraCC = CC;
} else {
comm->intraBarrier = (int*)waitForNonNullPtr(&comm0->intraBarrier);
comm->intraParams = (struct cudaLaunchParams*)waitForNonNullPtr(&comm0->intraParams);
comm->intraCudaDevs = (int*)waitForNonNullPtr(&comm0->intraCudaDevs);
comm->intraCGMode = (int*)waitForNonNullPtr(&comm0->intraCGMode);
comm->intraCC = (int*)waitForNonNullPtr(&comm0->intraCC);
}
comm->intraCudaDevs[comm->intraRank] = comm->cudaDev;
NCCLCHECK(initParams(comm));
int cgMdLaunch = 0;
// Set CG Mode
2021-04-12 16:00:11 -07:00
comm->launchMode = ncclComm::PARALLEL;
2018-09-24 16:06:59 -07:00
char* str = getenv("NCCL_LAUNCH_MODE");
2020-05-12 14:40:18 -07:00
if (str) INFO(NCCL_ENV, "NCCL_LAUNCH_MODE set by environment to %s", str);
2021-04-12 16:00:11 -07:00
if (str && strcmp(str, "GROUP") == 0) {
comm->launchMode = ncclComm::GROUP;
2018-09-24 16:06:59 -07:00
}
if (comm->launchMode == ncclComm::GROUP) {
CUDACHECK(cudaStreamCreateWithFlags(&comm->groupStream, cudaStreamNonBlocking));
2018-12-13 15:22:17 -08:00
#if CUDART_VERSION >= 9000
2019-11-19 14:57:39 -08:00
if (*comm->intraCC && (ncclCudaCompCap() == *comm->intraCC)) {
2018-09-24 16:06:59 -07:00
// Check whether the GPU supports Cooperative Group Multi Device Launch
(void) cudaDeviceGetAttribute(&cgMdLaunch, cudaDevAttrCooperativeMultiDeviceLaunch, comm->cudaDev);
}
#endif
}
// Disable cgMdLaunch if any rank does not support it
if (cgMdLaunch == 0) {
*comm->intraCGMode = 0x10;
}
return ncclSuccess;
}
2020-05-12 14:40:18 -07:00
#define DEFAULT_LL_BUFFSIZE (NCCL_LL_LINES_PER_THREAD*NCCL_LL_MAX_NTHREADS*NCCL_STEPS*sizeof(union ncclLLFifoLine))
#define DEFAULT_LL128_BUFFSIZE (NCCL_LL128_ELEMS_PER_THREAD*NCCL_LL128_MAX_NTHREADS*NCCL_STEPS*sizeof(uint64_t))
2020-09-04 14:35:05 -07:00
#define DEFAULT_BUFFSIZE (1 << 22) /* 4MiB */
#define DEFAULT_BUFFSIZE_ARM (1 << 20) /* 1MiB */
2020-05-12 14:40:18 -07:00
NCCL_PARAM(BuffSize, "BUFFSIZE", -2);
NCCL_PARAM(LlBuffSize, "LL_BUFFSIZE", -2);
NCCL_PARAM(Ll128BuffSize, "LL128_BUFFSIZE", -2);
static ncclResult_t computeBuffSizes(struct ncclComm* comm) {
int cpuArch, cpuVendor, cpuModel;
NCCLCHECK(ncclTopoCpuType(comm->topo, &cpuArch, &cpuVendor, &cpuModel));
int64_t envs[NCCL_NUM_PROTOCOLS] = { ncclParamLlBuffSize(), ncclParamLl128BuffSize(), ncclParamBuffSize() };
int defaults[NCCL_NUM_PROTOCOLS] = { DEFAULT_LL_BUFFSIZE, DEFAULT_LL128_BUFFSIZE, DEFAULT_BUFFSIZE };
if (cpuArch == NCCL_TOPO_CPU_ARCH_ARM) defaults[NCCL_PROTO_SIMPLE] = DEFAULT_BUFFSIZE_ARM;
for (int p=0; p<NCCL_NUM_PROTOCOLS; p++) {
comm->buffSizes[p] = comm->hostDevComm.buffSizes[p] = envs[p] != -2 ? envs[p] : defaults[p];
}
2018-12-13 15:56:12 -08:00
return ncclSuccess;
}
2019-11-19 14:57:39 -08:00
NCCL_PARAM(CrossNic, "CROSS_NIC", 2);
2020-01-16 16:02:42 -08:00
NCCL_PARAM(GraphDumpFileRank, "GRAPH_DUMP_FILE_RANK", 0);
2021-05-11 18:16:30 -07:00
NCCL_PARAM(CollNetNodeThreshold, "COLLNET_NODE_THRESHOLD", 2);
NCCL_PARAM(NvbPreconnect, "NVB_PRECONNECT", 1);
2019-11-19 14:57:39 -08:00
2018-09-24 16:06:59 -07:00
static ncclResult_t initTransportsRank(struct ncclComm* comm, ncclUniqueId* commId) {
2020-09-04 14:35:05 -07:00
// We use 2 AllGathers
// 1. { peerInfo, comm, compCap}
// 2. { nChannels, graphInfo, topoRanks }
2018-12-13 15:56:12 -08:00
2018-09-24 16:06:59 -07:00
int rank = comm->rank;
int nranks = comm->nRanks;
uint64_t commHash = getHash(commId->internal, NCCL_UNIQUE_ID_BYTES);
TRACE(NCCL_INIT, "comm %p, commHash %lx, rank %d nranks %d - BEGIN", comm, commHash, rank, nranks);
2018-12-13 15:56:12 -08:00
NCCLCHECK(bootstrapInit(commId, rank, nranks, &comm->bootstrap));
2018-09-24 16:06:59 -07:00
2018-12-13 15:56:12 -08:00
// AllGather1 - begin
struct {
struct ncclPeerInfo peerInfo;
struct ncclComm* comm;
2020-09-04 14:35:05 -07:00
int cudaCompCap;
2018-12-13 15:56:12 -08:00
} *allGather1Data;
NCCLCHECK(ncclCalloc(&allGather1Data, nranks));
allGather1Data[rank].comm = comm;
2020-09-04 14:35:05 -07:00
allGather1Data[rank].cudaCompCap = ncclCudaCompCap();
2019-11-19 14:57:39 -08:00
struct ncclPeerInfo* myInfo = &allGather1Data[rank].peerInfo;
NCCLCHECK(fillInfo(comm, myInfo, commHash));
2018-12-13 15:56:12 -08:00
NCCLCHECK(bootstrapAllGather(comm->bootstrap, allGather1Data, sizeof(*allGather1Data)));
2020-01-16 16:02:42 -08:00
NCCLCHECK(ncclCalloc(&comm->peerInfo, nranks+1)); // Extra rank to represent CollNet root
2018-12-13 15:56:12 -08:00
for (int i = 0; i < nranks; i++) {
memcpy(comm->peerInfo+i, &allGather1Data[i].peerInfo, sizeof(struct ncclPeerInfo));
2019-11-19 14:57:39 -08:00
if ((i != rank) && (comm->peerInfo[i].hostHash == myInfo->hostHash) && (comm->peerInfo[i].busId == myInfo->busId)) {
2021-02-09 15:34:08 -08:00
WARN("Duplicate GPU detected : rank %d and rank %d both on CUDA device %lx", rank, i, myInfo->busId);
2019-11-19 14:57:39 -08:00
return ncclInvalidUsage;
}
2018-12-13 15:56:12 -08:00
}
2020-09-04 14:35:05 -07:00
// Compute intra ranks and minimum CUDA Compute capabilities of intra-node GPUs and all GPUs
2021-07-08 14:12:04 -07:00
int intraProcRank0 = -1, intraProcRank = -1, intraProcRanks = 0;
int intraNodeRank0 = -1, intraNodeRank = -1, intraNodeRanks = 0;
2020-09-04 14:35:05 -07:00
int myCompCap = allGather1Data[rank].cudaCompCap;
int minCompCap = myCompCap, maxCompCap = myCompCap;
2021-07-08 14:12:04 -07:00
int intraNodeGlobalRanks[256];
2020-09-04 14:35:05 -07:00
for (int i = 0; i < nranks; i++) {
if (allGather1Data[i].peerInfo.hostHash == allGather1Data[rank].peerInfo.hostHash) {
2021-07-08 14:12:04 -07:00
// Rank is on same node
if (intraNodeRanks == 0) intraNodeRank0 = i;
if (i == rank) intraNodeRank = intraNodeRanks;
intraNodeGlobalRanks[intraNodeRanks++] = i;
2020-09-04 14:35:05 -07:00
if (allGather1Data[i].peerInfo.pidHash == allGather1Data[rank].peerInfo.pidHash) {
2021-07-08 14:12:04 -07:00
// Rank is in same process
if (intraProcRanks == 0) intraProcRank0 = i;
if (i == rank) intraProcRank = intraProcRanks;
intraProcRanks++;
2020-09-04 14:35:05 -07:00
}
}
minCompCap = std::min(allGather1Data[i].cudaCompCap, minCompCap);
maxCompCap = std::max(allGather1Data[i].cudaCompCap, maxCompCap);
}
2021-07-08 14:12:04 -07:00
TRACE(NCCL_INIT,"hostHash[%d] %lx intraNodeRank %d intraNodeRanks %d intraNodeRank0 %d",
rank, allGather1Data[rank].peerInfo.hostHash, intraNodeRank, intraNodeRanks, intraNodeRank0);
TRACE(NCCL_INIT,"pidHash[%d] %lx intraProcRank %d intraProcRanks %d intraProcRank0 %d",
rank, allGather1Data[rank].peerInfo.pidHash, intraProcRank, intraProcRanks, intraProcRank0);
if (intraProcRank == -1 || intraProcRank0 == -1 || allGather1Data[intraProcRank0].comm == NULL) {
WARN("Failed to determine intra proc ranks rank %d hostHash %lx pidHash %lx intraProcRank %d intraProcRanks %d intraProcRank0 %d",
rank, allGather1Data[rank].peerInfo.hostHash, allGather1Data[rank].peerInfo.pidHash,
intraProcRank, intraProcRanks, intraProcRank0);
2020-09-04 14:35:05 -07:00
return ncclInternalError;
}
2021-07-08 14:12:04 -07:00
if (intraNodeRank == -1 || intraNodeRank0 == -1 || intraNodeRanks == 0) {
WARN("Failed to determine intra node ranks rank %d hostHash %lx pidHash %lx intraNodeRank %d intraNodeRanks %d intraNodeRank0 %d",
rank, allGather1Data[rank].peerInfo.hostHash, allGather1Data[rank].peerInfo.pidHash,
intraNodeRank, intraNodeRanks, intraNodeRank0);
return ncclInternalError;
}
struct ncclComm* intraProcRank0Comm = allGather1Data[intraProcRank0].comm;
uint64_t intraNodeRank0pidHash = allGather1Data[intraNodeRank0].peerInfo.pidHash;
2020-09-04 14:35:05 -07:00
free(allGather1Data);
2018-12-13 15:56:12 -08:00
// AllGather1 - end
2019-11-19 14:57:39 -08:00
// Topo detection / System graph creation
NCCLCHECK(ncclTopoGetSystem(comm, &comm->topo));
// Compute paths between GPUs and NICs
NCCLCHECK(ncclTopoComputePaths(comm->topo, comm->peerInfo));
// Remove inaccessible GPUs and unused NICs
NCCLCHECK(ncclTopoTrimSystem(comm->topo, comm));
// Recompute paths after trimming
NCCLCHECK(ncclTopoComputePaths(comm->topo, comm->peerInfo));
2020-01-16 16:02:42 -08:00
// Init search
NCCLCHECK(ncclTopoSearchInit(comm->topo));
2019-11-19 14:57:39 -08:00
// Print final topology
NCCLCHECK(ncclTopoPrint(comm->topo));
// Get rings and trees
struct ncclTopoGraph ringGraph;
2020-01-16 16:02:42 -08:00
ringGraph.id = 0;
2019-11-19 14:57:39 -08:00
ringGraph.pattern = NCCL_TOPO_PATTERN_RING;
ringGraph.crossNic = ncclParamCrossNic();
2020-01-16 16:02:42 -08:00
ringGraph.collNet = 0;
ringGraph.minChannels = 1;
ringGraph.maxChannels = MAXCHANNELS/2;
2019-11-19 14:57:39 -08:00
NCCLCHECK(ncclTopoCompute(comm->topo, &ringGraph));
NCCLCHECK(ncclTopoPrintGraph(comm->topo, &ringGraph));
2018-09-24 16:06:59 -07:00
2020-01-16 16:02:42 -08:00
struct ncclTopoGraph treeGraph;
treeGraph.id = 1;
2021-07-08 14:12:04 -07:00
treeGraph.pattern = NCCL_TOPO_PATTERN_BALANCED_TREE;
2020-01-16 16:02:42 -08:00
treeGraph.crossNic = ncclParamCrossNic();
treeGraph.collNet = 0;
treeGraph.minChannels = 1;
treeGraph.maxChannels = ringGraph.nChannels;
NCCLCHECK(ncclTopoCompute(comm->topo, &treeGraph));
NCCLCHECK(ncclTopoPrintGraph(comm->topo, &treeGraph));
struct ncclTopoGraph collNetGraph;
collNetGraph.id = 2;
collNetGraph.pattern = NCCL_TOPO_PATTERN_TREE;
collNetGraph.collNet = 1;
collNetGraph.crossNic = ncclParamCrossNic();
collNetGraph.minChannels = collNetGraph.maxChannels = ringGraph.nChannels;
NCCLCHECK(ncclTopoCompute(comm->topo, &collNetGraph));
NCCLCHECK(ncclTopoPrintGraph(comm->topo, &collNetGraph));
if (comm->rank == ncclParamGraphDumpFileRank()) {
struct ncclTopoGraph* graphs[3] = { &ringGraph, &treeGraph, &collNetGraph };
NCCLCHECK(ncclTopoDumpGraphs(comm->topo, 3, graphs));
}
2021-05-11 18:16:30 -07:00
// Determine local CollNet support before all-gather
2021-07-08 14:12:04 -07:00
if (ncclParamCollNetEnable() == 1 && collNetSupport() == 1 && collNetGraph.nChannels > 0) comm->collNetSupport = 1;
if (intraNodeRanks > 8) {
2021-04-12 16:00:11 -07:00
if (comm->collNetSupport == 1) WARN("CollNet currently only supports up to 8 GPUs per node");
comm->collNetSupport = 0;
}
2018-12-13 15:56:12 -08:00
// AllGather3 - begin
2020-01-16 16:02:42 -08:00
struct ncclGraphInfo {
2020-09-04 14:35:05 -07:00
int pattern;
2021-04-12 16:00:11 -07:00
int nChannels;
2020-01-16 16:02:42 -08:00
int sameChannels;
float speedIntra;
float speedInter;
int typeIntra;
2020-09-04 14:35:05 -07:00
int typeInter;
2020-01-16 16:02:42 -08:00
};
2019-11-19 14:57:39 -08:00
2018-12-13 15:56:12 -08:00
struct {
2021-04-12 16:00:11 -07:00
int collNetSupport;
2020-01-16 16:02:42 -08:00
struct ncclGraphInfo tree;
struct ncclGraphInfo ring;
struct ncclGraphInfo collNet;
2019-11-19 14:57:39 -08:00
struct ncclTopoRanks topoRanks;
2018-12-13 15:56:12 -08:00
} *allGather3Data;
NCCLCHECK(ncclCalloc(&allGather3Data, nranks));
2020-09-04 14:35:05 -07:00
allGather3Data[rank].tree.pattern = treeGraph.pattern;
2021-04-12 16:00:11 -07:00
allGather3Data[rank].tree.nChannels = treeGraph.nChannels;
2019-11-19 14:57:39 -08:00
allGather3Data[rank].tree.sameChannels = treeGraph.sameChannels;
allGather3Data[rank].tree.speedIntra = treeGraph.speedIntra;
allGather3Data[rank].tree.speedInter = treeGraph.speedInter;
2020-01-16 16:02:42 -08:00
allGather3Data[rank].tree.typeIntra = treeGraph.typeIntra;
2020-09-04 14:35:05 -07:00
allGather3Data[rank].tree.typeInter = treeGraph.typeInter;
allGather3Data[rank].ring.pattern = ringGraph.pattern;
2021-04-12 16:00:11 -07:00
allGather3Data[rank].ring.nChannels = ringGraph.nChannels;
2019-11-19 14:57:39 -08:00
allGather3Data[rank].ring.sameChannels = ringGraph.sameChannels;
allGather3Data[rank].ring.speedIntra = ringGraph.speedIntra;
allGather3Data[rank].ring.speedInter = ringGraph.speedInter;
2020-01-16 16:02:42 -08:00
allGather3Data[rank].ring.typeIntra = ringGraph.typeIntra;
2020-09-04 14:35:05 -07:00
allGather3Data[rank].ring.typeInter = ringGraph.typeInter;
allGather3Data[rank].collNet.pattern = collNetGraph.pattern;
2021-04-12 16:00:11 -07:00
allGather3Data[rank].collNet.nChannels = collNetGraph.nChannels;
2020-01-16 16:02:42 -08:00
allGather3Data[rank].collNet.sameChannels = collNetGraph.sameChannels;
allGather3Data[rank].collNet.speedIntra = collNetGraph.speedIntra;
allGather3Data[rank].collNet.speedInter = collNetGraph.speedInter;
allGather3Data[rank].collNet.typeIntra = collNetGraph.typeIntra;
2020-09-04 14:35:05 -07:00
allGather3Data[rank].collNet.typeInter = collNetGraph.typeInter;
2021-04-12 16:00:11 -07:00
allGather3Data[rank].collNetSupport = comm->collNetSupport;
2019-11-19 14:57:39 -08:00
2021-04-12 16:00:11 -07:00
comm->nChannels = std::min(treeGraph.nChannels, ringGraph.nChannels);
NCCLCHECK(ncclTopoPreset(comm, &treeGraph, &ringGraph, &allGather3Data[rank].topoRanks));
2019-11-19 14:57:39 -08:00
2018-12-13 15:56:12 -08:00
NCCLCHECK(bootstrapAllGather(comm->bootstrap, allGather3Data, sizeof(*allGather3Data)));
2019-11-19 14:57:39 -08:00
// Determine nNodes, firstRanks, ...
2020-09-04 14:35:05 -07:00
int *nodesFirstRank, *nodesTreePatterns;
2019-11-19 14:57:39 -08:00
NCCLCHECK(ncclCalloc(&nodesFirstRank, nranks));
2020-09-04 14:35:05 -07:00
NCCLCHECK(ncclCalloc(&nodesTreePatterns, nranks));
2019-11-19 14:57:39 -08:00
for (int i=0; i<nranks; i++) {
int node = -1;
int firstRank = allGather3Data[i].topoRanks.ringRecv[0];
for (int n=0; n<comm->nNodes; n++) {
if (nodesFirstRank[n] == firstRank) node = n;
}
if (node == -1) {
node = comm->nNodes++;
nodesFirstRank[node] = firstRank;
2020-09-04 14:35:05 -07:00
// Record tree pattern of each node as they can be different depending on sm arch
nodesTreePatterns[node] = allGather3Data[i].tree.pattern;
2019-11-19 14:57:39 -08:00
}
if (i == comm->rank) comm->node = node;
}
2018-09-24 16:06:59 -07:00
2019-11-19 14:57:39 -08:00
int nChannelsOrig = comm->nChannels;
struct ncclTopoRanks** allTopoRanks;
NCCLCHECK(ncclCalloc(&allTopoRanks, comm->nRanks));
for (int i=0; i<nranks; i++) {
allTopoRanks[i] = &allGather3Data[i].topoRanks;
// Make sure we align all ranks so that the tuning is consistent across ranks
2021-04-12 16:00:11 -07:00
treeGraph.nChannels = std::min(allGather3Data[i].tree.nChannels, treeGraph.nChannels);
2019-11-19 14:57:39 -08:00
treeGraph.sameChannels = std::min(allGather3Data[i].tree.sameChannels, treeGraph.sameChannels);
treeGraph.speedIntra = std::min(allGather3Data[i].tree.speedIntra, treeGraph.speedIntra);
treeGraph.speedInter = std::min(allGather3Data[i].tree.speedInter, treeGraph.speedInter);
2020-01-16 16:02:42 -08:00
treeGraph.typeIntra = std::min(allGather3Data[i].tree.typeIntra, treeGraph.typeIntra);
2020-09-04 14:35:05 -07:00
treeGraph.typeInter = std::min(allGather3Data[i].tree.typeInter, treeGraph.typeInter);
2021-04-12 16:00:11 -07:00
ringGraph.nChannels = std::min(allGather3Data[i].ring.nChannels, ringGraph.nChannels);
2019-11-19 14:57:39 -08:00
ringGraph.sameChannels = std::min(allGather3Data[i].ring.sameChannels, ringGraph.sameChannels);
ringGraph.speedIntra = std::min(allGather3Data[i].ring.speedIntra, ringGraph.speedIntra);
ringGraph.speedInter = std::min(allGather3Data[i].ring.speedInter, ringGraph.speedInter);
2020-01-16 16:02:42 -08:00
ringGraph.typeIntra = std::min(allGather3Data[i].ring.typeIntra, ringGraph.typeIntra);
2020-09-04 14:35:05 -07:00
ringGraph.typeInter = std::min(allGather3Data[i].ring.typeInter, ringGraph.typeInter);
2021-04-12 16:00:11 -07:00
collNetGraph.nChannels = std::min(allGather3Data[i].collNet.nChannels, collNetGraph.nChannels);
2020-01-16 16:02:42 -08:00
collNetGraph.sameChannels = std::min(allGather3Data[i].collNet.sameChannels, collNetGraph.sameChannels);
collNetGraph.speedIntra = std::min(allGather3Data[i].collNet.speedIntra, collNetGraph.speedIntra);
collNetGraph.speedInter = std::min(allGather3Data[i].collNet.speedInter, collNetGraph.speedInter);
collNetGraph.typeIntra = std::min(allGather3Data[i].collNet.typeIntra, collNetGraph.typeIntra);
2020-09-04 14:35:05 -07:00
collNetGraph.typeInter = std::min(allGather3Data[i].collNet.typeInter, collNetGraph.typeInter);
2021-04-12 16:00:11 -07:00
comm->collNetSupport = std::min(allGather3Data[i].collNetSupport, comm->collNetSupport);
2019-11-19 14:57:39 -08:00
}
2018-12-13 15:56:12 -08:00
2021-04-12 16:00:11 -07:00
comm->nChannels = treeGraph.nChannels = ringGraph.nChannels = std::min(treeGraph.nChannels, ringGraph.nChannels);
2019-11-19 14:57:39 -08:00
if (comm->nChannels < nChannelsOrig) {
// We started duplicating channels during Preset(), so we need to move the
// duplicated channels since we have removed some.
for (int i=0; i<comm->nChannels; i++) memcpy(comm->channels+comm->nChannels+i, comm->channels+nChannelsOrig+i, sizeof(struct ncclChannel));
2018-09-24 16:06:59 -07:00
}
2018-12-13 15:56:12 -08:00
2021-05-11 18:16:30 -07:00
// Determine CollNet support after all-gather now that we know nNodes
int collNetNodeThreshold = ncclParamCollNetNodeThreshold();
if (comm->nNodes < collNetNodeThreshold) {
if (comm->collNetSupport == 1)
INFO(NCCL_INIT, "Communicator has %d nodes which is less than CollNet node threshold %d, disabling CollNet", comm->nNodes, collNetNodeThreshold);
comm->collNetSupport = 0;
}
2018-09-24 16:06:59 -07:00
int *rings;
2018-12-13 15:56:12 -08:00
NCCLCHECK(ncclCalloc(&rings, nranks*MAXCHANNELS));
2021-04-12 16:00:11 -07:00
NCCLCHECK(ncclTopoPostset(comm, nodesFirstRank, nodesTreePatterns, allTopoRanks, rings, &collNetGraph));
2019-11-19 14:57:39 -08:00
free(allTopoRanks);
2020-09-04 14:35:05 -07:00
free(nodesTreePatterns);
2019-11-19 14:57:39 -08:00
free(nodesFirstRank);
free(allGather3Data);
// AllGather3 - end
TRACE(NCCL_INIT, "rank %d nranks %d - BUILT %d TREES/RINGS", rank, nranks, comm->nChannels);
char line[1024];
line[0]='\0';
for (int c=0; c<comm->nChannels; c++) {
2020-09-04 14:35:05 -07:00
struct ncclTree* tree = &comm->channels[c].tree;
snprintf(line+strlen(line), 1023-strlen(line), " [%d] %d/%d/%d->%d->%d",
c, tree->down[0], tree->down[1], tree->down[2], rank, tree->up);
2021-07-08 14:12:04 -07:00
INFO(NCCL_GRAPH, "Ring %02d : %d -> %d -> %d", c, comm->channels[c].ring.prev, comm->rank, comm->channels[c].ring.next);
2019-11-19 14:57:39 -08:00
}
line[1023] = '\0';
INFO(NCCL_INIT, "Trees%s", line);
2018-09-24 16:06:59 -07:00
2020-01-16 16:02:42 -08:00
// Set Affinity to a CPU local the our GPU, so that all memory we allocate
// on the host is local.
2021-07-08 14:12:04 -07:00
NCCLCHECK(ncclTopoGetCpuAffinity(comm->topo, comm->rank, &comm->cpuAffinity));
2020-01-16 16:02:42 -08:00
cpu_set_t affinitySave;
2021-07-08 14:12:04 -07:00
if (CPU_COUNT(&comm->cpuAffinity)) {
sched_getaffinity(0, sizeof(cpu_set_t), &affinitySave);
sched_setaffinity(0, sizeof(cpu_set_t), &comm->cpuAffinity);
}
2020-01-16 16:02:42 -08:00
ncclResult_t ret;
2020-05-12 14:40:18 -07:00
NCCLCHECK(computeBuffSizes(comm));
2018-09-24 16:06:59 -07:00
// Connect with prev/next for each ring
2019-11-19 14:57:39 -08:00
for (int c=0; c<comm->nChannels; c++) {
struct ncclChannel* channel = comm->channels+c;
2020-01-16 16:02:42 -08:00
NCCLCHECKGOTO(setupChannel(comm, c, rank, nranks, rings+c*nranks), ret, affinity_restore);
2019-11-19 14:57:39 -08:00
if (comm->nRanks == 1) continue;
2021-04-12 16:00:11 -07:00
NCCLCHECKGOTO(ncclTransportP2pConnect(comm, channel, 1, &channel->ring.prev, 1, &channel->ring.next, 0), ret, affinity_restore);
2020-01-16 16:02:42 -08:00
}
2021-04-12 16:00:11 -07:00
NCCLCHECKGOTO(ncclTransportP2pSetup(comm, &ringGraph, 0), ret, affinity_restore);
2021-05-11 18:16:30 -07:00
free(rings);
2020-09-04 14:35:05 -07:00
INFO(NCCL_INIT, "Connected all rings");
// Connect Trees
for (int c=0; c<comm->nChannels; c++) {
struct ncclChannel* channel = comm->channels+c;
if (comm->nRanks == 1) continue;
2021-04-12 16:00:11 -07:00
NCCLCHECKGOTO(ncclTransportP2pConnect(comm, channel, NCCL_MAX_TREE_ARITY, channel->tree.down, 1, &channel->tree.up, 0), ret, affinity_restore);
NCCLCHECKGOTO(ncclTransportP2pConnect(comm, channel, 1, &channel->tree.up, NCCL_MAX_TREE_ARITY, channel->tree.down, 0), ret, affinity_restore);
2020-09-04 14:35:05 -07:00
}
2021-04-12 16:00:11 -07:00
NCCLCHECKGOTO(ncclTransportP2pSetup(comm, &treeGraph, 0), ret, affinity_restore);
2020-09-04 14:35:05 -07:00
INFO(NCCL_INIT, "Connected all trees");
2020-01-16 16:02:42 -08:00
// Check if we can setup CollNet
2021-04-12 16:00:11 -07:00
if (comm->collNetSupport > 0) {
2020-01-16 16:02:42 -08:00
int collNetSetupFail = 0;
2021-04-12 16:00:11 -07:00
// Find all head ranks
int nHeads = collNetGraph.nChannels;
int *heads;
NCCLCHECK(ncclCalloc(&heads, nHeads));
// Head GPU index is always 0
for (int c=0; c<nHeads; c++) {
heads[c] = collNetGraph.intra[c*comm->localRanks+0];
}
for (int c=0; c<comm->nChannels; c++) {
struct ncclChannel* channel = comm->channels+c;
for (int h=0; h<nHeads; h++) {
const int head = heads[h];
2021-07-08 14:12:04 -07:00
collNetSetupFail = ncclTransportCollNetSetup(comm, &collNetGraph, channel, head, head, h, collNetRecv);
if (!collNetSetupFail) collNetSetupFail = ncclTransportCollNetSetup(comm, &collNetGraph, channel, head, head, h, collNetSend);
2021-04-12 16:00:11 -07:00
}
2021-05-11 18:16:30 -07:00
// Verify CollNet setup across ranks after trying the first channel
if (c == 0) {
NCCLCHECKGOTO(ncclTransportCollNetCheck(comm, collNetSetupFail), ret, collnet_cleanup);
}
}
// Verify CollNet setup across ranks after trying all channels
NCCLCHECKGOTO(ncclTransportCollNetCheck(comm, collNetSetupFail), ret, collnet_cleanup);
TRACE(NCCL_INIT, "rank %d Connected inter-node CollNet", rank);
// Connect intra-node CollNet
for (int c=0; c<comm->nChannels; c++) {
struct ncclChannel* channelRecv = comm->channels+c;
NCCLCHECKGOTO(ncclTransportP2pConnect(comm, channelRecv, NCCL_MAX_DIRECT_ARITY, channelRecv->collTree.up, NCCL_MAX_DIRECT_ARITY, channelRecv->collTree.down, 0), ret, collnet_cleanup);
}
NCCLCHECKGOTO(ncclTransportP2pSetup(comm, &collNetGraph, 0), ret, collnet_cleanup);
for (int c=0; c<comm->nChannels; c++) {
struct ncclChannel* channelSend = comm->channels+c;
NCCLCHECKGOTO(ncclTransportP2pConnect(comm, channelSend, NCCL_MAX_DIRECT_ARITY, channelSend->collTree.down, NCCL_MAX_DIRECT_ARITY, channelSend->collTree.up, 1), ret, collnet_cleanup);
2020-01-16 16:02:42 -08:00
}
2021-05-11 18:16:30 -07:00
NCCLCHECKGOTO(ncclTransportP2pSetup(comm, &collNetGraph, 1), ret, collnet_cleanup);
INFO(NCCL_INIT, "rank %d Connected CollNet", rank);
collnet_cleanup:
2021-04-26 14:24:50 -07:00
free(heads);
2021-05-11 18:16:30 -07:00
if (ret != ncclSuccess) {
NCCLCHECK(ncclTransportCollNetFree(comm));
comm->collNetSupport = 0;
ret = ncclSuccess;
2021-04-12 16:00:11 -07:00
}
2019-11-19 14:57:39 -08:00
}
TRACE(NCCL_INIT, "rank %d nranks %d - CONNECTED %d RINGS AND TREES", rank, nranks, comm->nChannels);
2018-09-24 16:06:59 -07:00
2020-09-04 14:35:05 -07:00
// Compute time models for algorithm and protocol combinations
NCCLCHECK(ncclTopoTuneModel(comm, minCompCap, maxCompCap, &treeGraph, &ringGraph, &collNetGraph));
2020-05-12 14:40:18 -07:00
// Compute nChannels per peer for p2p
NCCLCHECK(ncclTopoComputeP2pChannels(comm));
2021-05-11 18:16:30 -07:00
if (ncclParamNvbPreconnect()) {
// Connect p2p when using NVB path
int nvbNpeers;
int* nvbPeers;
NCCLCHECK(ncclTopoGetNvbGpus(comm->topo, comm->rank, &nvbNpeers, &nvbPeers));
for (int r=0; r<nvbNpeers; r++) {
int peer = nvbPeers[r];
int delta = (comm->nRanks + (comm->rank-peer)) % comm->nRanks;
for (int c=0; c<comm->p2pnChannelsPerPeer; c++) {
int channelId = (delta+comm->p2pChannels[c]) % comm->p2pnChannels;
if (comm->channels[channelId].peers[peer].recv[0].connected == 0) { // P2P uses only 1 connector
comm->connectRecv[peer] |= (1<<channelId);
}
}
delta = (comm->nRanks - (comm->rank-peer)) % comm->nRanks;
for (int c=0; c<comm->p2pnChannelsPerPeer; c++) {
int channelId = (delta+comm->p2pChannels[c]) % comm->p2pnChannels;
if (comm->channels[channelId].peers[peer].send[0].connected == 0) { // P2P uses only 1 connector
comm->connectSend[peer] |= (1<<channelId);
}
}
}
NCCLCHECK(ncclTransportP2pSetup(comm, NULL, 0));
free(nvbPeers);
}
2021-07-08 14:12:04 -07:00
NCCLCHECK(ncclCommSetIntraProc(comm, intraProcRank, intraProcRanks, intraProcRank0Comm));
/* Local intra-node barrier */
NCCLCHECK(bootstrapBarrier(comm->bootstrap, intraNodeGlobalRanks, (int)intraNodeRank0pidHash, intraNodeRank, intraNodeRanks));
2018-10-24 14:44:59 -07:00
2020-05-12 14:40:18 -07:00
if (comm->nNodes) NCCLCHECK(ncclProxyCreate(comm));
2018-12-13 15:56:12 -08:00
// We should have allocated all buffers, collective fifos, ... we can
// restore the affinity.
affinity_restore:
2021-07-08 14:12:04 -07:00
if (CPU_COUNT(&comm->cpuAffinity)) sched_setaffinity(0, sizeof(cpu_set_t), &affinitySave);
if (ret != ncclSuccess) return ret;
2018-12-13 15:56:12 -08:00
TRACE(NCCL_INIT, "rank %d nranks %d - DONE", rank, nranks);
2018-09-24 16:06:59 -07:00
return ncclSuccess;
}
2021-04-12 16:00:11 -07:00
NCCL_PARAM(SetStackSize, "SET_STACK_SIZE", 0);
2019-11-19 14:57:39 -08:00
ncclResult_t ncclCommInitRankSync(ncclComm_t* newcomm, int nranks, ncclUniqueId commId, int myrank, int cudaDev) {
2018-09-24 16:06:59 -07:00
ncclResult_t res;
2020-01-16 16:02:42 -08:00
CUDACHECK(cudaSetDevice(cudaDev));
2021-04-12 16:00:11 -07:00
// Set the maximum kernel stack size of all kernels to avoid
// a CUDA memory reconfig on load (c.f. NVSHMEM issue)
if (maxLocalSizeBytes > 0 && ncclParamSetStackSize() == 1) {
TRACE(NCCL_INIT, "Setting cudaLimitStackSize to %zi", maxLocalSizeBytes);
CUDACHECKIGNORE(cudaDeviceSetLimit(cudaLimitStackSize, maxLocalSizeBytes));
}
2018-10-24 14:44:59 -07:00
NCCLCHECKGOTO(commAlloc(newcomm, nranks, myrank), res, cleanup);
2018-09-24 16:06:59 -07:00
NCCLCHECKGOTO(initTransportsRank(*newcomm, &commId), res, cleanup);
NCCLCHECKGOTO(devCommSetup(*newcomm), res, cleanup);
2021-02-09 15:34:08 -08:00
INFO(NCCL_INIT,"comm %p rank %d nranks %d cudaDev %d busId %lx - Init COMPLETE", *newcomm, myrank, nranks, (*newcomm)->cudaDev, (*newcomm)->busId);
2018-10-24 14:44:59 -07:00
2018-09-24 16:06:59 -07:00
return ncclSuccess;
cleanup:
2019-11-19 14:57:39 -08:00
if ((*newcomm) && (*newcomm)->bootstrap) bootstrapAbort((*newcomm)->bootstrap);
2018-09-24 16:06:59 -07:00
*newcomm = NULL;
return res;
}
2019-11-19 14:57:39 -08:00
static ncclResult_t ncclCommInitRankDev(ncclComm_t* newcomm, int nranks, ncclUniqueId commId, int myrank, int cudaDev) {
ncclResult_t res;
2018-09-24 16:06:59 -07:00
char* env = getenv("NCCL_COMM_ID");
if (env && myrank == 0) {
2020-05-12 14:40:18 -07:00
INFO(NCCL_ENV, "NCCL_COMM_ID set by environment to %s", env);
2019-11-19 14:57:39 -08:00
NCCLCHECKGOTO(bootstrapCreateRoot(&commId, true), res, end);
2018-09-24 16:06:59 -07:00
}
2019-11-19 14:57:39 -08:00
NCCLCHECKGOTO(ncclInit(), res, end);
2018-09-24 16:06:59 -07:00
if (myrank == 0) showVersion();
// Make sure the CUDA runtime is initialized.
2019-11-19 14:57:39 -08:00
CUDACHECKGOTO(cudaFree(NULL), res, end);
2018-09-24 16:06:59 -07:00
2019-11-19 14:57:39 -08:00
NCCLCHECKGOTO(PtrCheck(newcomm, "CommInitRank", "newcomm"), res, end);
2018-09-24 16:06:59 -07:00
if (nranks < 1 || myrank < 0 || myrank >= nranks) {
WARN("Invalid rank requested : %d/%d", myrank, nranks);
2019-11-19 14:57:39 -08:00
res = ncclInvalidArgument;
goto end;
2018-09-24 16:06:59 -07:00
}
if (ncclAsyncMode()) {
2019-11-19 14:57:39 -08:00
NCCLCHECKGOTO(ncclAsyncInit(ncclCommInitRankSync, newcomm, nranks, commId, myrank, cudaDev), res, end);
2018-09-24 16:06:59 -07:00
} else {
2019-11-19 14:57:39 -08:00
NCCLCHECKGOTO(ncclCommInitRankSync(newcomm, nranks, commId, myrank, cudaDev), res, end);
2018-09-24 16:06:59 -07:00
}
2021-04-12 16:00:11 -07:00
2019-11-19 14:57:39 -08:00
end:
if (ncclAsyncMode()) return ncclAsyncErrCheck(res);
else return res;
2018-09-24 16:06:59 -07:00
}
2019-11-19 14:57:39 -08:00
NCCL_API(ncclResult_t, ncclCommInitRank, ncclComm_t* newcomm, int nranks, ncclUniqueId commId, int myrank);
ncclResult_t ncclCommInitRank(ncclComm_t* newcomm, int nranks, ncclUniqueId commId, int myrank) {
2020-09-04 14:35:05 -07:00
NVTX3_FUNC_RANGE_IN(nccl_domain);
2019-11-19 14:57:39 -08:00
int cudaDev;
CUDACHECK(cudaGetDevice(&cudaDev));
NCCLCHECK(ncclCommInitRankDev(newcomm, nranks, commId, myrank, cudaDev));
2018-09-24 16:06:59 -07:00
return ncclSuccess;
}
NCCL_API(ncclResult_t, ncclCommInitAll, ncclComm_t* comms, int ndev, const int* devlist);
ncclResult_t ncclCommInitAll(ncclComm_t* comms, int ndev, const int* devlist) {
2020-09-04 14:35:05 -07:00
NVTX3_FUNC_RANGE_IN(nccl_domain);
2018-09-24 16:06:59 -07:00
NCCLCHECK(PtrCheck(comms, "CommInitAll", "comms"));
2019-11-19 14:57:39 -08:00
if (ndev < 0) {
2018-09-24 16:06:59 -07:00
WARN("Invalid device count requested : %d", ndev);
return ncclInvalidArgument;
}
2019-11-19 14:57:39 -08:00
ncclUniqueId uniqueId;
NCCLCHECK(ncclGetUniqueId(&uniqueId));
NCCLCHECK(ncclGroupStart());
2018-09-24 16:06:59 -07:00
for (int i=0; i<ndev; i++) {
2019-11-19 14:57:39 -08:00
// Ignore return codes .. we need to call ncclGroupEnd to clean up anyway
ncclCommInitRankDev(comms+i, ndev, uniqueId, i, devlist ? devlist[i] : i);
2018-09-24 16:06:59 -07:00
}
2019-11-19 14:57:39 -08:00
NCCLCHECK(ncclGroupEnd());
return ncclSuccess;
2018-09-24 16:06:59 -07:00
}
2018-12-13 15:56:12 -08:00
static ncclResult_t commDestroy(ncclComm_t comm) {
2018-09-24 16:06:59 -07:00
int savedDevice;
CUDACHECK(cudaGetDevice(&savedDevice));
int commDevice = comm->cudaDev;
if (savedDevice != commDevice) {
CUDACHECK(cudaSetDevice(commDevice));
}
2020-09-04 14:35:05 -07:00
TRACE(NCCL_INIT, "Destroying comm %p rank %d abortFlag %d fatalError %d", comm, comm->rank, *comm->abortFlag, comm->fatalError);
2018-12-13 15:56:12 -08:00
CUDACHECK(cudaStreamSynchronize(comm->groupStream));
2020-05-12 14:40:18 -07:00
NCCLCHECK(ncclProxyDestroy(comm));
2018-09-24 16:06:59 -07:00
NCCLCHECK(commFree(comm));
if (savedDevice != commDevice)
CUDACHECK(cudaSetDevice(savedDevice));
2020-09-04 14:35:05 -07:00
TRACE(NCCL_INIT, "Destroyed comm %p rank %d", comm, comm->rank);
2018-12-13 15:56:12 -08:00
2018-09-24 16:06:59 -07:00
return ncclSuccess;
}
2018-12-13 15:56:12 -08:00
NCCL_API(ncclResult_t, ncclCommDestroy, ncclComm_t comm);
ncclResult_t ncclCommDestroy(ncclComm_t comm) {
2020-09-04 14:35:05 -07:00
NVTX3_FUNC_RANGE_IN(nccl_domain);
2018-12-13 15:56:12 -08:00
if (comm == NULL)
return ncclSuccess;
2021-05-11 18:16:30 -07:00
TRACE(NCCL_INIT, "comm %p rank %d nRanks %d cudaDev %d busId %lx", comm, comm->rank, comm->nRanks, comm->cudaDev, comm->busId);
2019-03-14 19:39:20 -07:00
// Try and prevent a double free of the comm struct (user error)
2019-11-19 14:57:39 -08:00
if (comm->rank == -1 || comm->nRanks <= 0 || comm->cudaDev == -1 || comm->busId == -1) {
2019-03-14 19:39:20 -07:00
WARN("comm %p has already been destroyed", comm);
return ncclInvalidArgument;
}
2018-12-13 15:56:12 -08:00
return commDestroy(comm);
}
NCCL_API(ncclResult_t, ncclCommAbort, ncclComm_t comm);
ncclResult_t ncclCommAbort(ncclComm_t comm) {
2020-09-04 14:35:05 -07:00
NVTX3_FUNC_RANGE_IN(nccl_domain);
2018-12-13 15:56:12 -08:00
if (comm == NULL)
return ncclSuccess;
// Ask anything that might still be running on the device to quit
*comm->abortFlag = 1;
return commDestroy(comm);
}
2018-09-24 16:06:59 -07:00
NCCL_API(const char*, ncclGetErrorString, ncclResult_t code);
const char* ncclGetErrorString(ncclResult_t code) {
switch (code) {
case ncclSuccess : return "no error";
case ncclUnhandledCudaError : return "unhandled cuda error";
case ncclSystemError : return "unhandled system error";
case ncclInternalError : return "internal error";
case ncclInvalidArgument : return "invalid argument";
case ncclInvalidUsage : return "invalid usage";
default : return "unknown result code";
}
}
2018-12-13 15:56:12 -08:00
NCCL_API(ncclResult_t, ncclCommGetAsyncError, ncclComm_t comm, ncclResult_t *asyncError);
ncclResult_t ncclCommGetAsyncError(ncclComm_t comm, ncclResult_t *asyncError) {
NCCLCHECK(PtrCheck(comm, "ncclGetAsyncError", "comm"));
NCCLCHECK(PtrCheck(asyncError, "ncclGetAsyncError", "asyncError"));
*asyncError = comm->fatalError;
return ncclSuccess;
}
2018-09-24 16:06:59 -07:00
NCCL_API(ncclResult_t, ncclCommCount, const ncclComm_t comm, int* count);
ncclResult_t ncclCommCount(const ncclComm_t comm, int* count) {
2020-09-04 14:35:05 -07:00
NVTX3_FUNC_RANGE_IN(nccl_domain);
2018-09-24 16:06:59 -07:00
NCCLCHECK(PtrCheck(comm, "CommCount", "comm"));
NCCLCHECK(PtrCheck(count, "CommCount", "count"));
*count = comm->nRanks;
return ncclSuccess;
}
NCCL_API(ncclResult_t, ncclCommCuDevice, const ncclComm_t comm, int* devid);
ncclResult_t ncclCommCuDevice(const ncclComm_t comm, int* devid) {
2020-09-04 14:35:05 -07:00
NVTX3_FUNC_RANGE_IN(nccl_domain);
2018-09-24 16:06:59 -07:00
NCCLCHECK(PtrCheck(comm, "CommCuDevice", "comm"));
NCCLCHECK(PtrCheck(devid, "CommCuDevice", "devid"));
*devid = comm->cudaDev;
return ncclSuccess;
}
NCCL_API(ncclResult_t, ncclCommUserRank, const ncclComm_t comm, int* rank);
ncclResult_t ncclCommUserRank(const ncclComm_t comm, int* rank) {
2020-09-04 14:35:05 -07:00
NVTX3_FUNC_RANGE_IN(nccl_domain);
2018-09-24 16:06:59 -07:00
NCCLCHECK(PtrCheck(comm, "CommUserRank", "comm"));
NCCLCHECK(PtrCheck(rank, "CommUserRank", "rank"));
*rank = comm->rank;
return ncclSuccess;
}