# NCCL

**NCCL**（NVIDIA Collective Communications Library，英伟达集合通信库）是英伟达（NVIDIA）推出的一个库，实现了多 GPU、多节点的通信原语，例如全归约（all-reduce）和广播（broadcast），并针对英伟达 GPU 和英伟达网络进行了优化。<sup>[1](https://developer.nvidia.com/nccl)</sup> 它是英伟达硬件上分布式深度学习框架之下的标准通信层：PyTorch 的文档推荐使用 NCCL 后端，因为它“目前能提供最佳的分布式 GPU 训练性能，尤其是在多进程单节点或多节点分布式训练中”。<sup>[2](https://github.com/pytorch/pytorch/blob/main/docs/source/distributed.md)</sup>

| 关键事实 | 详情 |
|---|---|
| 用途 | 针对英伟达 GPU 和网络优化的多 GPU、多节点集合通信原语<sup>[1](https://developer.nvidia.com/nccl)</sup> |
| 操作 | 全收集（all-gather）、全归约、广播、归约（reduce）、归约散射（reduce-scatter），以及点对点发送与接收<sup>[1](https://developer.nvidia.com/nccl)</sup> |
| 互连 | PCIe、NVLink、NVSwitch、InfiniBand、RoCE 及其他网络，支持自动拓扑检测<sup>[1](https://developer.nvidia.com/nccl)</sup> |
| API | 精简的 C API，紧密遵循消息传递接口（Message Passing Interface，MPI）所定义的集合通信 API<sup>[1](https://developer.nvidia.com/nccl)</sup> |
| 算法 | Ring 与 Tree（自 2.5 起）、Collnet（2.5 至 2.13）、CollnetChain 与 CollnetDirect（2.14 起）、NVLS（2.17 起）、NVLSTree（2.18 起）、PAT（2.23 起）<sup>[3](https://github.com/NVIDIA/nccl/blob/master/docs/userguide/source/env.rst)</sup> |
| 设备 API | NCCL 2.28 新增设备 API，包含 Load/Store Accessible、Multimem 和 GPU 发起网络（GPU-Initiated Networking，GIN）三种模式<sup>[4](https://arxiv.org/abs/2511.15076)</sup> |
| 分发方式 | 可从 GitHub 构建，随英伟达 HPC SDK 捆绑发布，也提供二进制文件；面向 CUDA 13.4 的 2.31.2 版日期为 2026 年 9 月 11 日<sup>[1](https://developer.nvidia.com/nccl)</sup><sup> • </sup><sup>[5](https://developer.nvidia.com/nccl/nccl-download/)</sup> |

## 该库提供的功能

NCCL 对外暴露的集合例程用于跨设备合并数据。在全归约中，每个秩（rank）提供一个含 N 个值的数组，并收到所有秩输入的归约结果，例如求和；在跨 k 个秩的求和全归约中，秩 i 收到 out[i] = in0[i] + in1[i] + … + in(k−1)[i]。广播把含 N 个元素的缓冲区从指定的根秩复制到每个秩，而根参数标识的是一个秩而非设备编号，因此它会受秩到设备映射的影响。<sup>[6](https://github.com/NVIDIA/nccl/blob/master/docs/userguide/source/usage/collectives.rst)</sup> 集合操作必须由每个秩以相同的计数和相同的数据类型调用，才能构成一次完整的操作；调用不匹配会产生未定义行为，包括挂起、崩溃或数据损坏。<sup>[6](https://github.com/NVIDIA/nccl/blob/master/docs/userguide/source/usage/collectives.rst)</sup>

该 API 是一个仿照 MPI 集合通信设计的小型 C 接口，可从其他语言调用，并兼容任何多 GPU 并行化模型：单线程、每 GPU 一线程的多线程，或将 MPI 与 GPU 线程结合的多进程程序。<sup>[1](https://developer.nvidia.com/nccl)</sup> 设备 API 还允许 CUDA 内核直接发起通信，从而降低延迟、改善计算与通信的重叠。<sup>[1](https://developer.nvidia.com/nccl)</sup>

## 拓扑感知与算法

NCCL 自动检测机器的互连拓扑，并使用图搜索算法构建 GPU 的环和树，以最大化带宽、最小化延迟，因此开发者无需针对特定硬件配置调优应用。<sup>[1](https://developer.nvidia.com/nccl)</sup> 在单个节点内，只要 CUDA 表明各 GPU 可直接访问彼此的内存——通常通过 NVLink，或在拓扑与驱动允许时通过 PCIe——NCCL 就优先采用点对点传输，其 GPU 间流量大部分由 [GPUDirect](https://www.edgepedia.org/zh/gpudirect) 承载。<sup>[7](https://github.com/NVIDIA/nccl/blob/master/docs/userguide/source/troubleshooting/gpu_troubleshooting.rst)</sup> 默认情况下，当拓扑允许时，NCCL 会启用 GPU Direct RDMA。<sup>[8](https://docs.nvidia.com/deeplearning/nccl/archives/nccl_205/pdf/NCCL-Developer-Guide.pdf)</sup>

算法集合随硬件发展而扩充。`NCCL_ALGO` 环境变量记录了这条时间线：Ring 与 Tree 自 2.5 版起可选，Collnet 从 2.5 到 2.13，CollnetChain 与 CollnetDirect 自 2.14 起，NVLS 自 2.17 起，NVLSTree 自 2.18 起，PAT 自 2.23 起。NVLS 与 NVLSTree 启用 NVLink SHARP 卸载，由交换机硬件执行集合归约。默认情况下该变量未设置，NCCL 会根据节点拓扑和架构自动选择算法。<sup>[3](https://github.com/NVIDIA/nccl/blob/master/docs/userguide/source/env.rst)</sup> 与之配套的 `NCCL_PROTO` 变量用于限制线路协议；英伟达不建议设置它，除非怀疑存在缺陷时需要禁用某种协议，因为在不支持 LL128 的平台上启用它可能导致数据损坏。<sup>[3](https://github.com/NVIDIA/nccl/blob/master/docs/userguide/source/env.rst)</sup>

## 可扩展性与运维工具

插件框架将 NCCL 二进制文件（针对特定 GPU 栈如 CUDA 构建）与针对特定网络栈构建的网络代码解耦；插件实现 NCCL 网络 API，以 `libnccl-net.so` 的形式加载，未找到插件时使用内置插件。<sup>[9](https://rocmdocs.amd.com/projects/rccl/en/develop/how-to/using-nccl.html)</sup><sup> • </sup><sup>[1](https://developer.nvidia.com/nccl)</sup> 在 InfiniBand 系统上，RDMA 与 SHARP 集合通信通过 NCCL-SHARP 插件启用，该插件要求在每个计算系统上加载 GPUDirect RDMA 内核模块。<sup>[10](https://docs.nvidia.com/networking/display/nvidia-scalable-hierarchical-aggregation-and-reduction-protocol-sharp-rev-3-9-0.0.pdf)</sup> 该库还附带性能分析、可靠性与可观测性工具，包括 NCCL RAS 和 NCCL Inspector，用于调试与性能调优。<sup>[1](https://developer.nvidia.com/nccl)</sup>

NCCL 2.28 引入的设备 API 提供三种操作模式：面向 NVLink 与 PCIe 的 Load/Store Accessible（LSA）、面向 NVLink SHARP 的 Multimem，以及面向网络 RDMA 的 GPU 发起网络（GIN）。GIN 分三层构建：用于设备通信器设置和集合内存窗口注册的主机侧核心 API；可从 CUDA 内核调用的、用于远程内存操作的设备侧 API；以及具有双重语义（GPUDirect 异步内核发起与代理）的网络插件架构，以提供广泛的硬件支持。<sup>[4](https://arxiv.org/abs/2511.15076)</sup>

## 在分布式训练中的角色

NCCL 是 PyTorch 分布式包的默认 GPU 后端，也是数据并行训练中梯度同步的底层支撑，其中梯度的全归约是主导性的集合操作。<sup>[2](https://github.com/pytorch/pytorch/blob/main/docs/source/distributed.md)</sup> 英伟达与 PyTorch 相关的 NCCLX 变体被描述为经过生产环境验证，可用于 Llama3、Llama4 等大语言模型的大规模训练与推理。<sup>[11](https://pytorch.org/blog/torchcomms/)</sup>

## 相关库

这一设计已在加速器生态中被移植和仿效。AMD 的 RCCL 实现了相同的编程模型，甚至记录了如何针对 NCCL Net 插件 API 编写代码，从而将库的构建与网络栈解耦。<sup>[9](https://rocmdocs.amd.com/projects/rccl/en/develop/how-to/using-nccl.html)</sup> 英特尔的 oneCCL 采取了不同路线：它不像 NCCL 和 RCCL 那样在 GPU 内核内执行集合操作，而是构建在 MPI 与 libfabric 之上，将传输选择和拓扑感知委托给底层通信栈，这在层次化多 GPU 系统上会产生不同的扩展模式。<sup>[12](https://doi.org/10.1145/3813799)</sup> NCCL、RCCL 和 oneCCL 都提供标准的集合通信函数，而 MPI 的 API 还包含它们之外的额外函数。<sup>[13](https://elliottslaughter.com/pdfs/ipdps2025.pdf)</sup>

## 历史

英伟达在 SC15 上推出 NCCL，当时它是一个研究性质的库，提供加速的、拓扑感知的集合通信，旨在提升多 GPU 应用的可扩展性，并最优地处理节点内通信。<sup>[14](https://images.nvidia.com/events/sc15/pdfs/NCCL-Woolley.pdf)</sup> 此后它从版本 1 API 演进到当前的版本 2 系列，并有文档化的迁移路径，涵盖初始化、计数、数据类型、全收集与归约散射的原地使用以及错误码等方面的变化。<sup>[15](https://docs.nvidia.com/deeplearning/nccl/user-guide/docs/)</sup> 近期版本继续增加与新硬件绑定的能力：NVLink SHARP 卸载随 2.17 中的 NVLS 到来，设备 API 与 GPU 发起网络则在 2.28 中到来。<sup>[3](https://github.com/NVIDIA/nccl/blob/master/docs/userguide/source/env.rst)</sup><sup> • </sup><sup>[4](https://arxiv.org/abs/2511.15076)</sup>

## References

1. [NVIDIA Collective Communications Library (NCCL), NVIDIA Developer](https://developer.nvidia.com/nccl)
2. [PyTorch distributed documentation, PyTorch GitHub](https://github.com/pytorch/pytorch/blob/main/docs/source/distributed.md)
3. [NCCL environment variables reference (NCCL_ALGO, NCCL_PROTO), NVIDIA/nccl GitHub](https://github.com/NVIDIA/nccl/blob/master/docs/userguide/source/env.rst)
4. [GPU-Initiated Networking for NCCL, arXiv](https://arxiv.org/abs/2511.15076)
5. [NCCL Download Page, NVIDIA Developer](https://developer.nvidia.com/nccl/nccl-download/)
6. [NCCL Collective Operations user guide, NVIDIA/nccl GitHub](https://github.com/NVIDIA/nccl/blob/master/docs/userguide/source/usage/collectives.rst)
7. [NCCL GPU troubleshooting guide, NVIDIA/nccl GitHub](https://github.com/NVIDIA/nccl/blob/master/docs/userguide/source/troubleshooting/gpu_troubleshooting.rst)
8. [NCCL Developer Guide (2.5 archive), NVIDIA documentation](https://docs.nvidia.com/deeplearning/nccl/archives/nccl_205/pdf/NCCL-Developer-Guide.pdf)
9. [Using the NCCL Net plugin API, RCCL documentation, AMD](https://rocmdocs.amd.com/projects/rccl/en/develop/how-to/using-nccl.html)
10. [Using NVIDIA SHARP with NVIDIA NCCL, NVIDIA Networking documentation](https://docs.nvidia.com/networking/display/nvidia-scalable-hierarchical-aggregation-and-reduction-protocol-sharp-rev-3-9-0.0.pdf)
11. [torchcomms: a modern PyTorch communications API, PyTorch blog](https://pytorch.org/blog/torchcomms/)
12. [The Landscape of GPU-Centric Communication, ACM Computing Surveys](https://doi.org/10.1145/3813799)
13. [Machine descriptions for optimized collectives (IPDPS 2025), Elliott Slaughter](https://elliottslaughter.com/pdfs/ipdps2025.pdf)
14. [Introducing NCCL: Accelerating multi-GPU collective communications, SC15 presentation, NVIDIA](https://images.nvidia.com/events/sc15/pdfs/NCCL-Woolley.pdf)
15. [NCCL 2.32.3 documentation, NVIDIA](https://docs.nvidia.com/deeplearning/nccl/user-guide/docs/)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Machine learning and neural computation › Neural networks and deep learning › Deep learning software and hardware › Distributed and large-scale training

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/nccl

English original: https://www.edgepedia.org/nccl
