NCCL

Also known as: NVIDIA Collective Communications Library

On this page 7

NCCL (the NVIDIA Collective Communications Library, usually pronounced “nickel”) is an open-source library that implements multi-GPU and multi-node collective operations such as AllReduce, AllGather and Broadcast, optimized for NVIDIA GPUs and their interconnects. Among AI frameworks & tools, it is the communication layer underneath distributed training: when PyTorch, JAX, DeepSpeed or Megatron-LM synchronize gradients or shard parameters across many GPUs, the actual data movement is usually performed by NCCL.

NCCL does not train models or schedule jobs. Its single job is to move tensors between GPU memories as fast as the hardware allows, choosing communication algorithms and transports based on the detected topology of NVLink, PCIe and network links. In large clusters, its efficiency often decides whether adding GPUs actually shortens training time.

How NCCL moves data between GPUs

NCCL works by forming a communicator, a group of ranks in which each rank is usually one GPU, and then running collective kernels directly on the GPUs. A process creates a unique identifier with ncclGetUniqueId, shares it with all other processes through an external channel such as MPI or a TCP store, and each rank joins with ncclCommInitRank.

At initialization, NCCL detects the system topology: which GPUs share NVLink or NVSwitch, which sit behind the same PCIe switch, and which network adapters are closest to each GPU. From this graph it builds rings and trees of connections and assigns them to several parallel channels, each driven by a few GPU thread blocks, so that many links carry traffic at the same time.

Collectives are enqueued on CUDA streams, which means they run asynchronously and can overlap with computation. Frameworks exploit this by launching gradient AllReduce for finished layers while the backward pass continues on later layers, a technique known as communication and computation overlap.

The table below lists the main operations NCCL provides and where they appear in typical AI workloads.

OperationWhat it doesTypical use in AI
AllReduceCombines buffers from all ranks and returns the result to every rankGradient averaging in data parallelism
AllGatherConcatenates a piece from each rank onto every rankRebuilding sharded parameters in FSDP or ZeRO
ReduceScatterReduces buffers and gives each rank one slice of the resultSharded gradient reduction
BroadcastCopies one rank’s buffer to all ranksDistributing initial weights
Send and receivePoint-to-point transfer between two ranksPipeline parallelism and all-to-all for mixture of experts
Core NCCL operations and their typical use

Algorithms, protocols and transports in NCCL

NCCL selects an algorithm, a protocol and a transport for every collective call, based on message size, rank count and topology. The ring algorithm sends data around a logical circle and achieves near-optimal bandwidth: in an AllReduce over N ranks, each rank transmits about 2(N-1)/N times the buffer size, which approaches twice the data regardless of cluster size. Its weakness is latency that grows linearly with the number of ranks.

The double binary tree algorithm reduces latency to a logarithmic number of steps and therefore helps with small and medium messages on large clusters. Newer versions add NVLink SHARP (NVLS) and CollNet, which offload reductions into NVSwitch or network switches, and the PAT algorithm for AllGather and ReduceScatter at scale.

AlgorithmStrengthLimitationTypical selection
RingMaximum bandwidth on large buffersLatency scales with rank countLarge gradients, moderate cluster size
TreeLogarithmic latencyLower peak bandwidth within a nodeSmall and medium messages across many nodes
NVLSIn-switch reduction on NVSwitch systemsRequires recent NVSwitch hardwareSingle-node and rack-scale AllReduce
CollNetNetwork-side reductionRequires SHARP-capable switches and pluginsVery large InfiniBand clusters
Main algorithms and when NCCL prefers them

Three wire protocols trade latency for bandwidth: Simple uses large chunks and memory fences for peak throughput, LL (low latency) uses 8-byte flag-and-data stores for tiny messages, and LL128 uses 128-byte units that keep most of the bandwidth with much lower latency. Transports include NVLink and NVSwitch, PCIe peer-to-peer, shared host memory, InfiniBand and RoCE with GPUDirect RDMA, and plain TCP sockets as a fallback. Network plugins, such as the one for AWS Elastic Fabric Adapter, extend NCCL to other fabrics.

NCCL compared with other communication libraries

NCCL is the default GPU backend in PyTorch distributed, but it is one of several collective libraries. The comparison below shows how it relates to the alternatives that practitioners most often meet.

LibraryTarget hardwareRelation to NCCL
NCCLNVIDIA GPUsReference implementation for CUDA collectives
RCCLAMD GPUs through ROCmPort with a largely compatible API
GlooCPUs, basic GPU supportPyTorch fallback for CPU tensors and coordination
MPIGeneral clustersOften used only to bootstrap NCCL communicators
oneCCLIntel CPUs and GPUsComparable role in the Intel ecosystem
NCCL and related collective libraries

Higher-level tools sit above NCCL rather than competing with it. DeepSpeed, Megatron-LM and Hugging Face Accelerate decide which collectives to call and when, while XLA in JAX compiles collectives that are lowered to NCCL on NVIDIA hardware. Inference engines such as TensorRT-LLM and SGLang also rely on NCCL for tensor parallelism across GPUs.

Configuring and tuning NCCL in practice

Most users never call NCCL directly; they select it with a setting such as backend="nccl" in PyTorch. Tuning happens mostly through environment variables, and the defaults are usually correct, so changes should follow measurement with the nccl-tests benchmarks, which report algorithm bandwidth and bus bandwidth comparable with link speed.

VariablePurposeWhen to set it
NCCL_DEBUG=INFOPrints topology, rings and chosen transportsFirst step in any diagnosis
NCCL_SOCKET_IFNAMESelects the network interface for bootstrap and socketsHosts with several interfaces
NCCL_IB_HCARestricts which InfiniBand adapters are usedMulti-rail or mixed-adapter nodes
NCCL_ALGO and NCCL_PROTOForce or exclude algorithms and protocolsControlled experiments only
NCCL_P2P_DISABLETurns off direct GPU peer-to-peer transfersDebugging suspected PCIe problems
Frequently used environment variables

Good practice is to keep NCCL, the CUDA driver and the network stack versions consistent across all nodes, place each GPU process near its local network adapter, and verify that GPUDirect RDMA is active in the debug log when the fabric supports it.

Limitations and common failure modes

NCCL assumes that every rank calls the same collectives in the same order with matching sizes and data types. When this rule is broken, for example when one rank skips a step because of a data-dependent branch, the job typically hangs rather than failing with a clear error. PyTorch therefore wraps NCCL with a watchdog and timeout that aborts stalled communicators.

  • Wrong network interface: bootstrap traffic tries an unreachable address, and initialization stalls.
  • PCIe access control services enabled in a virtualized host can silently block peer-to-peer traffic and cut bandwidth.
  • Mismatched library versions across nodes cause protocol errors or crashes at startup.
  • Small messages on large clusters are latency bound, so bucketing gradients into larger buffers often helps.
  • Memory overhead: each communicator and channel reserves GPU buffers, which matters when many communicators exist.

NCCL is also tied to NVIDIA hardware. Mixed-vendor clusters need different libraries per vendor, and NCCL does not provide fault tolerance: a single failed GPU or link usually requires restarting the job from a checkpoint.

A plain-language summary

Large AI models are trained on many graphics chips at once. Each chip works on its own share of data. After every step, the chips must share what they learned and agree on one result. NCCL is the software that handles this exchange.

Think of a team of accountants who each total part of a ledger. Someone must collect the subtotals and give everyone the final sum. NCCL does that job for GPUs. It finds the fastest cables between the chips and uses them all at once. When it works well, the chips spend their time computing instead of waiting.

An example from practice

A machine learning team fine-tunes a 13-billion-parameter language model on four servers, each with eight GPUs connected by NVLink and two 200 Gb/s InfiniBand adapters. Training uses PyTorch with fully sharded data parallelism, so every step runs AllGather and ReduceScatter through NCCL. The first run reaches only about half the expected throughput per GPU.

With NCCL_DEBUG=INFO, the engineers see that inter-node traffic uses TCP sockets instead of InfiniBand, because the container lacks the RDMA libraries. After they add the libraries and set NCCL_IB_HCA to the two adapters, the log shows GPUDirect RDMA in use, and nccl-tests reports bus bandwidth close to the link limit. Step time drops by roughly 40 percent, and the planned training run finishes days earlier on the same hardware.