• NCCL源码解析⑤:路径计算


    a18f9875bc7a55b2967b3869e3e6914c.png

    作者|KIDGINBROOK
    更新|潘丽晨

    上节NCCL完成了对机器PCI系统拓扑的建图,其中建好的图如下所示,其中GPU之间是通过NVLink连接起来的。

    6d47e5be42e8b3219df6857a0ba674a4.png

    为了方便之后的搜索channel,接下来NCCL会先计算GPU和NIC节点到其他任意节点之间的最优路径,以及对应的带宽,即最优路径上所有边的带宽的最小值。

    那么抽象一下,这个问题可以建模为给定一个无向图,每条边有一个权值,给定查询(u, v),求节点u到节点v的路径,使得路径上的最小边的权值最大,类似无向图的最小瓶颈路,可以用生成树+LCA的方法解决;如果查询中的u是固定的,那么也可以使用类似SPFA的方法解决,将松弛方法改一下即可。

    上节忘记介绍图的数据结构,这里补一下。

    图中的边由ncclTopoLink表示,type区分边的类型,比如NVLink,PCI;width表示带宽;remNode表示当前边连接的对端节点。

    最后计算出来节点之间的路径由ncclTopoLinkList表示,路径一共有count条边,这个路径的带宽是width,即count条边中带宽最小为width,list为具体的边。

     
     
    1. struct ncclTopoLink {
    2. int type;
    3. float width;
    4. struct ncclTopoNode* remNode;
    5. };
    6. struct ncclTopoLinkList {
    7. struct ncclTopoLink* list[NCCL_TOPO_MAX_HOPS];
    8. int count;
    9. float width;
    10. int type;
    11. };

    其中type为路径的类型,一共有如下几种枚举值。

     
     
    1. #define PATH_LOC 0
    2. #define PATH_NVL 1
    3. #define PATH_PIX 2
    4. #define PATH_PXB 3
    5. #define PATH_PHB 4
    6. #define PATH_SYS 5
    7. #define PATH_NET 6

    PATH_LOC为节点到自己,PATH_NVL表示路径上的边都是NVLink,PATH_PIX表示经过最多一个PCIe switch,PATH_PXB表示经过了多个PCIe witch,但是没有经过CPU,PATH_PHB表示经过了CPU,PATH_SYS表示不同numa之间的路径。

    每个节点由ncclTopoNode表示,nlinks表示该节点有几条边,links存储了具体连接的边,paths存储了到其他节点的路径,node1中的paths[type][id]就是node1到type类型的第id个node的路径。

    ncclTopoNodeSet表示某种类型的所有节点,比如GPU,PCI,NIC等,ncclTopoSystem存储了全局所有类型的节点。

    每个节点由ncclTopoNode表示,nlinks表示该节点有几条边,links存储了具体连接的边,paths存储了到其他节点的路径,node1中的paths[type][id]就是node1到type类型的第id个node的路径。

    ncclTopoNodeSet表示某种类型的所有节点,比如GPU,PCI,NIC等,ncclTopoSystem存储了全局所有类型的节点。

     
     
    1. struct ncclTopoNodeSet {
    2. int count;
    3. struct ncclTopoNode nodes[NCCL_TOPO_MAX_NODES];
    4. };
    5. struct ncclTopoSystem {
    6. struct ncclTopoNodeSet nodes[NCCL_TOPO_NODE_TYPES];
    7. float maxWidth;
    8. };
    9. struct ncclTopoNode {
    10. int type;
    11. int64_t id;
    12. // Type specific data
    13. union {
    14. struct {
    15. int dev; // NVML dev number
    16. int rank;
    17. int cudaCompCap;
    18. int gdrSupport;
    19. }gpu;
    20. struct {
    21. uint64_t asic;
    22. int port;
    23. float width;
    24. int gdrSupport;
    25. int collSupport;
    26. int maxChannels;
    27. }net;
    28. struct {
    29. int arch;
    30. int vendor;
    31. int model;
    32. cpu_set_t affinity;
    33. }cpu;
    34. };
    35. int nlinks;
    36. struct ncclTopoLink links[NCCL_TOPO_MAX_LINKS];
    37. // Pre-computed paths to GPUs and NICs
    38. struct ncclTopoLinkList* paths[NCCL_TOPO_NODE_TYPES];
    39. // Used during search
    40. uint64_t used;
    41. };


    然后看下NCCL路径计算的过程,主要是这三步。

     
     
    1. NCCLCHECK(ncclTopoComputePaths(comm->topo, comm->peerInfo));
    2.  // Remove inaccessible GPUs and unused NICs
    3.  NCCLCHECK(ncclTopoTrimSystem(comm->topo, comm));
    4.  // Recompute paths after trimming
    5.  NCCLCHECK(ncclTopoComputePaths(comm->topo, comm->peerInfo));

    其中ncclTopoComputePaths就是执行路径的计算,ncclTopoTrimSystem是删除用不到的节点,接下来详细看下。

     
     
    1. ncclResult_t ncclTopoComputePaths(struct ncclTopoSystem* system, struct ncclPeerInfo* peerInfos) {
    2. // Precompute paths between GPUs/NICs.
    3. // Remove everything in case we're re-computing
    4. for (int t=0; t<NCCL_TOPO_NODE_TYPES; t++) ncclTopoRemovePathType(system, t);
    5. // Set direct paths from/to CPUs. We need them in many cases.
    6. for (int c=0; c<system->nodes[CPU].count; c++) {
    7. NCCLCHECK(ncclTopoSetPaths(system->nodes[CPU].nodes+c, system));
    8. }
    9. ...
    10. }

    首先通过ncclTopoRemovePathType将所有node中的paths清空。

    ncclTopoSetPaths作用就是计算出其他所有节点到baseNode的path,这里遍历所有的CPU节点,计算出其他所有节点到所有CPU节点的路径。

    ncclTopoSetPaths实现类似SPFA,由于这个版本的NCCL不允许GPU作为路径的中间节点,所以在SPFA的过程中不会将GPU节点添加到队列中更新其他节点,相当于这个无向图没有环,因此这个场景下的SPFA过程也就相当于BFS。

    这里baseNode就是CPU节点,先分配CPU到CPU path的空间,nodeList和nextNodeList就是队列的作用,先将baseNode入队列。

    getPath函数是获取node中到type为t的第id个节点的路径path。

     
     
    static ncclResult_t getPath(struct ncclTopoSystem* system, struct ncclTopoNode* node, int t, int64_t id, struct ncclTopoLinkList** path);

    通过getPath获取到CPU节点到自己的path,然后设置count为0,带宽为LOC_WIDTH,type为PATH_LOC。

    然后每次从nodeList中拿出一个节点node,获取node到baseNode的路径path,然后用node去更新和node相连的节点,遍历node的边link,获取link对端节点remNode,获取remNode到baseNode的路径remPath,此时需要比较两个路径哪个更优,一个路径是原来的remPath,另一个是path+link这个新路径,新路径的带宽width是path和link的带宽取个min,如果width大于remPath->width,那么remPath更新为path+link。

     
     
    1. static ncclResult_t ncclTopoSetPaths(struct ncclTopoNode* baseNode, struct ncclTopoSystem* system) {
    2. if (baseNode->paths[baseNode->type] == NULL) {
    3. NCCLCHECK(ncclCalloc(baseNode->paths+baseNode->type, system->nodes[baseNode->type].count));
    4. }
    5. // breadth-first search to set all paths to that node in the system
    6. struct ncclTopoNodeList nodeList;
    7. struct ncclTopoNodeList nextNodeList;
    8. nodeList.count = 1; nodeList.list[0] = baseNode;
    9. nextNodeList.count = 0;
    10. struct ncclTopoLinkList* basePath;
    11. NCCLCHECK(getPath(system, baseNode, baseNode->type, baseNode->id, &basePath));
    12. basePath->count = 0;
    13. basePath->width = LOC_WIDTH;
    14. basePath->type = PATH_LOC;
    15. while (nodeList.count) {
    16. nextNodeList.count = 0;
    17. for (int n=0; n
    18. struct ncclTopoNode* node = nodeList.list[n];
    19. struct ncclTopoLinkList* path;
    20. NCCLCHECK(getPath(system, node, baseNode->type, baseNode->id, &path));
    21. for (int l=0; lnlinks; l++) {
    22. struct ncclTopoLink* link = node->links+l;
    23. struct ncclTopoNode* remNode = link->remNode;
    24. if (remNode->paths[baseNode->type] == NULL) {
    25. NCCLCHECK(ncclCalloc(remNode->paths+baseNode->type, system->nodes[baseNode->type].count));
    26. }
    27. struct ncclTopoLinkList* remPath;
    28. NCCLCHECK(getPath(system, remNode, baseNode->type, baseNode->id, &remPath));
    29. float width = std::min(path->width, link->width);
    30. if (remPath->width < width) {
    31. // Find reverse link
    32. for (int l=0; lnlinks; l++) {
    33. if (remNode->links[l].remNode == node) {
    34. remPath->list[0] = remNode->links+l;
    35. break;
    36. }
    37. }
    38. if (remPath->list[0] == NULL) {
    39. WARN("Failed to find reverse path from remNode %d/%lx nlinks %d to node %d/%lx",
    40. remNode->type, remNode->id, remNode->nlinks, node->type, node->id);
    41. return ncclInternalError;
    42. }
    43. // Copy the rest of the path
    44. for (int i=0; icount; i++) remPath->list[i+1] = path->list[i];
    45. remPath->count = path->count + 1;
    46. remPath->width = width;
    47. // Start with path type = link type. PATH and LINK types are supposed to match.
    48. // Don't consider LINK_NET as we only care about the NIC->GPU path.
    49. int type = link->type == LINK_NET ? 0 : link->type;
    50. // Differentiate between one and multiple PCI switches
    51. if (type == PATH_PIX && (node->type == PCI || link->remNode->type == PCI) && remPath->count > 3) type = PATH_PXB;
    52. // Consider a path going through the CPU as PATH_PHB
    53. if (link->type == LINK_PCI && (node->type == CPU || link->remNode->type == CPU)) type = PATH_PHB;
    54. // Ignore Power CPU in an NVLink path
    55. if (path->type == PATH_NVL && type == PATH_SYS && link->remNode->type == CPU &&
    56. link->remNode->cpu.arch == NCCL_TOPO_CPU_ARCH_POWER) type = 0;
    57. remPath->type = std::max(path->type, type);
    58. // Add to the list for the next iteration if not already in the list
    59. // Disallow GPUs as intermediate steps for now
    60. if (remNode->type != GPU) {
    61. int i;
    62. for (i=0; iif (nextNodeList.list[i] == remNode) break;
    63. if (i == nextNodeList.count) nextNodeList.list[nextNodeList.count++] = remNode;
    64. }
    65. }
    66. }
    67. }
    68. memcpy(&nodeList, &nextNodeList, sizeof(nodeList));
    69. }
    70. return ncclSuccess;
    71. }

    路径更新后需要计算remPath的type,这里有个取巧的地方是上节设置边type和本节设置路径type是对应的,比如LINK_PCI等于PATH_PIX,然后可以看到之前说的各种路径的type是怎么计算出来的。

    首先计算当前link作为一条路径的type,初始化为link的type,比如这个边是LINK_PCI,那么就是LINK_PIX,如果remPath的count大于3的话type就会更新为PATH_PXB(但是这里有个疑问是大于3可能也跨过了两个PCIe switch),如果link有一端是CPU,那么type进一步更新为PATH_PHB,最后取个max,remPath->type = std::max(path->type, type)。

     
     
    1. #define LINK_LOC 0
    2. #define LINK_NVL 1
    3. #define LINK_PCI 2
    4. // Skipping 3 for PATH_PXB
    5. // Skipping 4 for PATH_PHB
    6. #define LINK_SYS 5
    7. #define LINK_NET 6
    8. #define PATH_LOC 0
    9. #define PATH_NVL 1
    10. #define PATH_PIX 2
    11. #define PATH_PXB 3
    12. #define PATH_PHB 4
    13. #define PATH_SYS 5
    14. #define PATH_NET 6

    如果remNode不是GPU,那么将remNode添加到nextNodeList,等nodeList遍历完之后,将nextNodeList赋给nodeList继续遍历。

    然后回到ncclTopoComputePaths,还是使用ncclTopoSetPaths计算GPU节点到其他所有节点的距离。

     
     
    1. ncclResult_t ncclTopoComputePaths(struct ncclTopoSystem* system, struct ncclPeerInfo* peerInfos) {
    2. ...
    3. // Set direct paths from/to GPUs.
    4. for (int g=0; gnodes[GPU].count; g++) {
    5. // Compute paths to GPU g
    6. NCCLCHECK(ncclTopoSetPaths(system->nodes[GPU].nodes+g, system));
    7. // Update path when we don't want to / can't use GPU Direct P2P
    8. for (int p=0; pnodes[GPU].count; p++) {
    9. int p2p, read;
    10. NCCLCHECK(ncclTopoCheckP2p(system, system->nodes[GPU].nodes[p].id, system->nodes[GPU].nodes[g].id, &p2p, &read));
    11. if (p2p == 0) {
    12. // Divert all traffic through the CPU
    13. int cpu;
    14. NCCLCHECK(getLocalCpu(system, g, &cpu));
    15. NCCLCHECK(addCpuStep(system, cpu, GPU, p, GPU, g));
    16. }
    17. }
    18. if (peerInfos == NULL) continue;
    19. // Remove GPUs we can't talk to because of containers.
    20. struct ncclPeerInfo* dstInfo = peerInfos+system->nodes[GPU].nodes[g].gpu.rank;
    21. for (int p=0; pnodes[GPU].count; p++) {
    22. if (p == g) continue;
    23. struct ncclPeerInfo* srcInfo = peerInfos+system->nodes[GPU].nodes[p].gpu.rank;
    24. int shm;
    25. NCCLCHECK(ncclTransports[TRANSPORT_SHM].canConnect(&shm, system, NULL, srcInfo, dstInfo));
    26. if (shm == 0) {
    27. // Mark this peer as inaccessible. We'll trim it later.
    28. system->nodes[GPU].nodes[p].paths[GPU][g].count = 0;
    29. }
    30. }
    31. }
    32. // Set direct paths from/to NICs.
    33. for (int n=0; nnodes[NET].count; n++) {
    34. struct ncclTopoNode* netNode = system->nodes[NET].nodes+n;
    35. NCCLCHECK(ncclTopoSetPaths(netNode, system));
    36. for (int g=0; gnodes[GPU].count; g++) {
    37. // Update path when we dont want to / can't use GPU Direct RDMA.
    38. int gdr;
    39. NCCLCHECK(ncclTopoCheckGdr(system, system->nodes[GPU].nodes[g].id, netNode->id, 0, &gdr));
    40. if (gdr == 0) {
    41. // We cannot use GPU Direct RDMA, divert all traffic through the CPU local to the GPU
    42. int localCpu;
    43. NCCLCHECK(getLocalCpu(system, g, &localCpu));
    44. NCCLCHECK(addCpuStep(system, localCpu, NET, n, GPU, g));
    45. NCCLCHECK(addCpuStep(system, localCpu, GPU, g, NET, n));
    46. }
    47. }
    48. }
    49. return ncclSuccess;
    50. }

    然后通过ncclTopoCheckP2p检查当前GPU节点和其他所有的GPU节点之间是否可以使用p2p通信,其实就是判断gpu1到gpu2的路径type是否满足p2pLevel的限制,默认p2pLevel是PATH_SYS,如果用户没有通过环境变量设置的话就相当于没有限制,任意gpu之间都是支持p2p通信,另外如果路径类型为PATH_NVL的话,那么还支持p2p read。

     
     
    1. ncclResult_t ncclTopoCheckP2p(struct ncclTopoSystem* system, int64_t id1, int64_t id2, int* p2p, int *read) {
    2. *p2p = 0;
    3. *read = 0;
    4. // Get GPUs from topology
    5. int g1, g2;
    6. NCCLCHECK(ncclTopoIdToIndex(system, GPU, id1, &g1));
    7. struct ncclTopoNode* gpu1 = system->nodes[GPU].nodes+g1;
    8. if (ncclTopoIdToIndex(system, GPU, id2, &g2) == ncclInternalError) {
    9. // GPU not found, we can't use p2p.
    10. return ncclSuccess;
    11. }
    12. struct ncclTopoLinkList* path = gpu1->paths[GPU]+g2;
    13. // In general, use P2P whenever we can.
    14. int p2pLevel = PATH_SYS;
    15. // User override
    16. if (ncclTopoUserP2pLevel == -1)
    17. NCCLCHECK(ncclGetLevel(&ncclTopoUserP2pLevel, "NCCL_P2P_DISABLE", "NCCL_P2P_LEVEL"));
    18. if (ncclTopoUserP2pLevel != -2) {
    19. p2pLevel = ncclTopoUserP2pLevel;
    20. goto compare;
    21. }
    22. // Don't use P2P through ARM CPUs
    23. int arch, vendor, model;
    24. NCCLCHECK(ncclTopoCpuType(system, &arch, &vendor, &model));
    25. if (arch == NCCL_TOPO_CPU_ARCH_ARM) p2pLevel = PATH_PXB;
    26. if (arch == NCCL_TOPO_CPU_ARCH_X86 && vendor == NCCL_TOPO_CPU_VENDOR_INTEL) {
    27. if (model == NCCL_TOPO_CPU_TYPE_BDW) p2pLevel = PATH_PXB;
    28. else p2pLevel = PATH_PHB;
    29. }
    30. compare:
    31. // Compute the PCI distance and compare with the p2pLevel.
    32. if (path->type <= p2pLevel) *p2p = 1;
    33. if (path->type == PATH_NVL) {
    34. struct ncclTopoNode* gpu2 = system->nodes[GPU].nodes+g2;
    35. // Enable P2P Read for Ampere/NVLink only
    36. if ((gpu1->gpu.cudaCompCap == gpu2->gpu.cudaCompCap) && (gpu1->gpu.cudaCompCap == 80)) *read = 1;
    37. }
    38. return ncclSuccess;
    39. }

    然后判断当前GPU和其他GPU是否可以通过shm通信,因为在docker环境中如果shm挂载的不一样就无法通信,如果无法通过shm通信的话就将path的count设置为0,之后会删除掉对应节点(但是这里有个疑问,shm不通的话为什么没有继续判断p2p是否可用)。

    最后类似GPU,然后对所有的NIC执行ncclTopoSetPaths计算出路径,然后遍历每个NIC和每个GPU,判断是否支持gdr。

     
     
    1. ncclResult_t ncclTopoCheckGdr(struct ncclTopoSystem* system, int64_t busId, int netDev, int read, int* useGdr) {
    2. *useGdr = 0;
    3. // Get GPU and NET
    4. int n, g;
    5. NCCLCHECK(ncclTopoIdToIndex(system, NET, netDev, &n));
    6. struct ncclTopoNode* net = system->nodes[NET].nodes+n;
    7. NCCLCHECK(ncclTopoIdToIndex(system, GPU, busId, &g));
    8. struct ncclTopoNode* gpu = system->nodes[GPU].nodes+g;
    9. // Check that both the NIC and GPUs support it
    10. if (net->net.gdrSupport == 0) return ncclSuccess;
    11. if (gpu->gpu.gdrSupport == 0) return ncclSuccess;
    12. if (read) { // For reads (sends) only enable under certain conditions
    13. int gdrReadParam = ncclParamNetGdrRead();
    14. if (gdrReadParam == 0) return ncclSuccess;
    15. if (gdrReadParam < 0) {
    16. int nvlink = 0;
    17. // Since we don't know whether there are other communicators,
    18. // it's better to keep things local if we have a single GPU.
    19. if (system->nodes[GPU].count == 1) nvlink = 1;
    20. for (int i=0; i<system->nodes[GPU].count; i++) {
    21. if (i == g) continue;
    22. if (gpu->paths[GPU][i].type == PATH_NVL) {
    23. nvlink = 1;
    24. break;
    25. }
    26. }
    27. if (!nvlink) return ncclSuccess;
    28. }
    29. }
    30. // Check if we are close enough that it makes sense to enable GDR
    31. int netGdrLevel = PATH_PXB;
    32. NCCLCHECK(ncclGetLevel(&ncclTopoUserGdrLevel, NULL, "NCCL_NET_GDR_LEVEL"));
    33. if (ncclTopoUserGdrLevel != -2) netGdrLevel = ncclTopoUserGdrLevel;
    34. int distance = gpu->paths[NET][n].type;
    35. if (distance > netGdrLevel) {
    36. INFO(NCCL_NET,"GPU Direct RDMA Disabled for GPU %lx / HCA %d (distance %d > %d)", busId, netDev, distance, netGdrLevel);
    37. return ncclSuccess;
    38. }
    39. *useGdr = 1;
    40. INFO(NCCL_NET,"GPU Direct RDMA Enabled for GPU %lx / HCA %d (distance %d <= %d), read %d", busId, netDev, distance, netGdrLevel, read);
    41. return ncclSuccess;
    42. }

    这里除了看之前判断是否支持gdr之外,还要看GPU和NIC之间的距离是否小于netGdrLevel,netGdrLevel默认是PATH_PXB,用户也可以自定义,默认值为PXB的原因可见官方文档:

    Even though the only theoretical requirement for GPUDirect RDMA to work between a third-party device and an NVIDIA GPU is that they share the same root complex, there exist bugs (mostly in chipsets) causing it to perform badly, or not work at all in certain setups. 

    We can distinguish between three situations, depending on what is on the path between the GPU and the third-party device: 

    PCIe switches only

    single CPU/IOH

    CPU/IOH <-> QPI/HT <-> CPU/IOH

    The first situation, where there are only PCIe switches on the path, is optimal and yields the best performance. The second one, where a single CPU/IOH is involved, works, but yields worse performance ( especially peer-to-peer read bandwidth has been shown to be severely limited on some processor architectures ). Finally, the third situation, where the path traverses a QPI/HT link, may be extremely performance-limited or even not work reliably.

    可以看到在只有经过PCIe switch的时候性能最好,在经过CPU的时候性能较差,在跨numa的时候性能很差,甚至不可用。

    当p2p或者gdr不支持的时候,会通过CPU进行中转,通过getLocalCpu找到最近的CPU c。

     
     
    1. static ncclResult_t getLocalCpu(struct ncclTopoSystem* system, int gpu, int* retCpu) {
    2. // Find the closest CPU to a GPU
    3. int minHops = 0;
    4. int localCpu = -1;
    5. struct ncclTopoLinkList* paths = system->nodes[GPU].nodes[gpu].paths[CPU];
    6. for (int c=0; cnodes[CPU].count; c++) {
    7. int hops = paths[c].count;
    8. if (minHops == 0 || hops < minHops) {
    9. localCpu = c;
    10. minHops = hops;
    11. }
    12. }
    13. if (localCpu == -1) {
    14. WARN("Error : could not find CPU close to GPU %d", gpu);
    15. return ncclInternalError;
    16. }
    17. *retCpu = localCpu;
    18. return ncclSuccess;
    19. }

    然后addCpuStep将 i1 到 i2 的路径修改为 i1 到 c 的路径 + cpu到 i2 的路径。

     
     
    1. static ncclResult_t addCpuStep(struct ncclTopoSystem* system, int c, int t1, int i1, int t2, int i2) {
    2. struct ncclTopoNode* cpuNode = system->nodes[CPU].nodes+c;
    3. struct ncclTopoNode* srcNode = system->nodes[t1].nodes+i1;
    4. int l=0;
    5. // Node 1 -> CPU
    6. for (int i=0; i<srcNode->paths[CPU][c].count; i++) srcNode->paths[t2][i2].list[l++] = srcNode->paths[CPU][c].list[i];
    7. // CPU -> Node 2
    8. for (int i=0; i<cpuNode->paths[t2][i2].count; i++) srcNode->paths[t2][i2].list[l++] = cpuNode->paths[t2][i2].list[i];
    9. // Update path characteristics
    10. srcNode->paths[t2][i2].count = l;
    11. srcNode->paths[t2][i2].type = std::max(srcNode->paths[CPU][c].type, cpuNode->paths[t2][i2].type);
    12. srcNode->paths[t2][i2].width = std::min(srcNode->paths[CPU][c].width, cpuNode->paths[t2][i2].width);
    13. return ncclSuccess;
    14. }

    到这里ncclTopoComputePaths就完成了,接下来会通过ncclTopoTrimSystem删除图中不可达的GPU节点和用不到的NIC。

     
     
    1. ncclResult_t ncclTopoTrimSystem(struct ncclTopoSystem* system, struct ncclComm* comm) {
    2. int *domains;
    3. int64_t *ids;
    4. NCCLCHECK(ncclCalloc(&domains, system->nodes[GPU].count));
    5. NCCLCHECK(ncclCalloc(&ids, system->nodes[GPU].count));
    6. int myDomain = 0;
    7. for (int g=0; gnodes[GPU].count; g++) {
    8. struct ncclTopoNode* gpu = system->nodes[GPU].nodes+g;
    9. domains[g] = g;
    10. ids[g] = gpu->id;
    11. for (int p=0; p
    12. if (gpu->paths[GPU][p].count > 0) {
    13. domains[g] = std::min(domains[g], domains[p]);
    14. }
    15. }
    16. if (gpu->gpu.rank == comm->rank) myDomain = domains[g];
    17. }
    18. int ngpus = system->nodes[GPU].count;
    19. for (int i=0; i
    20. if (domains[i] == myDomain) continue;
    21. struct ncclTopoNode* gpu = NULL;
    22. int g;
    23. for (g=0; gnodes[GPU].count /* This one varies over the loops */; g++) {
    24. gpu = system->nodes[GPU].nodes+g;
    25. if (gpu->id == ids[i]) break; else gpu=NULL;
    26. }
    27. if (gpu == NULL) {
    28. WARN("Could not find id %lx", ids[i]);
    29. free(domains);
    30. free(ids);
    31. return ncclInternalError;
    32. }
    33. NCCLCHECK(ncclTopoRemoveNode(system, GPU, g));
    34. }
    35. comm->localRanks = system->nodes[GPU].count;
    36. if (system->nodes[GPU].count == comm->nRanks) {
    37. for (int n=system->nodes[NET].count-1; n>=0; n--)
    38. NCCLCHECK(ncclTopoRemoveNode(system, NET, n));
    39. }
    40. free(domains);
    41. free(ids);
    42. return ncclSuccess;
    43. }

    首先通过类似并查集的思路将多个GPU节点合并成多个集合,myDomain为当前rank的GPU所对应的集合号,然后将不属于myDomain集合的GPU节点在图中删除掉,最后判断下如果comm的rank数等于当前图中的gpu节点数,那么说明不需要网卡,所以也将网卡从图中删除。

    得到新的图结构后再重新执行一次ncclTopoComputePaths就得到最终各个节点之间的路径了。

    (本文经授权后由OneFlow发布。原文:https://blog.csdn.net/KIDGIN7439/article/details/127849771)

    其他人都在看

    试用OneFlow:github.com/Oneflow-Inc/oneflow/

  • 相关阅读:
    C++官网 Tutorials C++ Language Program structure:Statements and flow control
    【二分法】
    巨控GRM230模块在分散式污水行业专用解决方案
    VSG-001
    Android 单一音量控制
    【cmake】彻底弄懂cmake中的可见性/传递性 INTEFACE 问题
    Jackson 的 SNAKE_CASE 反序列化
    零拷贝、MMAP、堆外内存
    人工智能基础_机器学习040_Sigmoid函数详解_单位阶跃函数与对数几率函数_伯努利分布---人工智能工作笔记0080
    阿里云-源码构建容器镜像
  • 原文地址:https://blog.csdn.net/OneFlow_Official/article/details/131843165