Course contents
On this page
RDMA Scale-Out
Introduction
RDMA is the de facto communication mechanism for the scale-out domain, whether you use NVIDIA or AMD GPUs or even specialized accelerators. It originally came from the CPU HPC world to tightly connect many CPU servers in an MPI setting, and now it has evolved into “connecting” hundreds of thousands of GPU chips in an HBM-to-HBM way with the help of techniques like GPUDirect RDMA. This chapter will talk about how to program RDMA for GPU communication, dig into how CPUs, GPUs, and RDMA NICs interact with each other, look at some subtle topics around ordering and completion events, and finally get you familiar with typical RDMA performance numbers.
RDMA programming
RDMA programming is event-driven by default, which means the RDMA NIC hardware functions like a standalone state machine accepting input and giving output to interact with a host process.
There are two main libraries that let the host process interact with the RDMA state machine: libibverbs and libfabric.
libibverbs came out early and was mostly driven by Mellanox (later acquired by NVIDIA), and today most InfiniBand and RoCE NICs support libibverbs as a programming interface.
libfabric came later and aims to hide hardware heterogeneity from the programmer (e.g., different RDMA NICs support different hardware features).
In my experience, as I pursue extreme performance, the additional level of abstraction in libfabric usually gets in the way of that goal. Therefore, my preference is libibverbs, and this course will also focus on how to use it.
There are three sets of concepts in RDMA programming:
- QP (Queue Pair) and CQ (Completion Queue): the process abstractions for posting work on a connection and checking its completion.
- WQE (Work Queue Element) and CQE (Completion Queue Entry): the descriptors used to submit verbs and check the execution status of the submitted verbs.
- Verbs: the communication primitives/operations issued on the QP. There are roughly five operations:
send,recv,read,write, and atomics.
Of course, there are tons of connection setup concepts like PD (protection domain), RTR (ready to receive), RTS (ready to send), AH (address handle), LID (local identifier), GRH (global routing header), QP number, etc., but with long-lived connections and buffers, most of these are only needed once during setup. Therefore, we will skim them whenever needed; you can easily check how they work using other online materials.
QP, CQ, and WQE: A QP has two FIFO queues that let the host process send commands to the NIC: one SQ (Send Queue) and one RQ (Receive Queue). Collectively, we also just call the SQ and RQ work queues (WQs). A WQE is a descriptor that the host process prepares and puts into the WQ to tell the NIC how to transmit data.
- A
send/read/write/atomic WQE on the SQ describes the local memory region (address + length) the NIC should send to the peer, the remote memory region the NIC should read from or write to, or the remote counter the NIC should do atomics on. - A
recvWQE on the RQ describes the local memory region the NIC should receive the incomingsenddata into.sendandrecvare paired operations between the sending and receiving RDMA NICs.
When you post verbs on QPs, the memory regions you want the RDMA NIC to operate on must have been registered with the NIC. This is usually done by calling the ibv_reg_mr function with the memory address and size, which will return you an ibv_mr struct containing lkey and rkey.
lkeyis what the host process needs to supply to alibibverbsfunction when it needs to access a local registered memory region.rkeyis for accessing a remote memory region that has been registered with the remote NIC. It is usually passed from the remote process to the local process through out-of-band communication like TCP sockets.
CQE: Once a verb completes, the local RDMA NIC generates a CQE in the CQ bound to the corresponding SQ/RQ. This CQE describes the completion status (success or failure) and the reason if it fails.
Since RDMA programming is event-driven, the completion also provides a 64-bit opaque integer, and this integer is exactly the same one the host process supplied as wr_id when it issued the verb.
This way, with the same 64-bit integer in the request and completion, the host process can associate this completion event with the verb-issuing context and resume from it.
For example, a common practice is to malloc a context/state object describing the current progress and use that object’s pointer as the 64-bit integer when issuing the verb.
Note that in the above example, the two processes picked different IDs. The NICs exchange the payload, but each process gets back its own ID. If A issues a plain write, read, or fetch-add instead, it supplies B’s address and rkey, and B can leave its RQ empty for all three operations. For a read, the data comes back to A. For a fetch-add, only the old counter value comes back.
QP types: There are three common types of QPs: RC (Reliable Connected), UC (Unreliable Connected), and UD (Unreliable Datagram). They differ in transmission reliability (whether the transport recovers from packet loss) and connection semantics (connected or connectionless). Most of the time, we will use RC.
- RC provides one-to-one message semantics (up to 1 GiB per operation on our NICs), with the NIC hardware handling packet loss and retransmission.
- UC also provides one-to-one message semantics but without RC’s hardware retransmission guarantee.
- UD provides one-to-many datagram semantics (up to one path MTU per operation, max 4KB), without a delivery guarantee.
GPUDirect RDMA allows an RDMA NIC to directly read and write GPU memory, bypassing host memory in the data path. The programming interface is very simple: just cudaMalloc a piece of GPU memory and register it with the RDMA NIC, much like you register CPU memory.
There are two ways 1 to get that registration done. With nvidia-peermem, you can just pass the GPU pointer to ibv_reg_mr, and the peer-memory module helps the NIC driver pin and map the GPU pages. With DMA-BUF, you ask CUDA to export the allocation as a file descriptor and pass that descriptor to ibv_reg_dmabuf_mr. I would suggest choosing DMA-BUF for newly built RDMA applications. It uses a standard Linux interface across vendors, though the GPU driver and the NIC provider both need to support it. Either way, what you use to program the QP afterward is still an lkey and an rkey.
PCIe relaxed ordering. This can be useful when using GPU VMs. Think of a NIC writing several independent pieces of a message, i.e., packets. If one PCIe write stalls, relaxed ordering lets later writes make progress, which can help on a virtualized I/O path.
In practice, when you find the NCCL performance on GPU VMs to be very low, you should try to set NCCL_IB_PCI_RELAXED_ORDERING=2 2 (the default, enable when supported) for NCCL’s IB transport.
One note is that a synchronization protocol that polls a flag written by an ordinary RDMA write needs particular care here because two writes to a relaxed-ordering region can land out of order. For example, LL8 and LL128 in the NVLink chapter might not always work over RDMA.
See more details in our NCCL debugging post.
A working example
Now, let’s look at a working example that writes the number 123 into GPU 0 on the other machine and increments a counter right next to it. Both processes open the mlx5_0 RDMA device, allocate a PD and a CQ, and create one RC QP. Each process registers its GPU buffer in its PD and tells its peer four numbers over TCP: its LID, QP number, rkey, and GPU address. These numbers are used to drive both sides’ QPs through INIT, RTR, and RTS on our InfiniBand subnet.
ibv_send_wr write{}, add{};
write.opcode = IBV_WR_RDMA_WRITE;
write.sg_list = &payload;
write.num_sge = 1;
write.wr.rdma.remote_addr = dst.address;
write.wr.rdma.rkey = dst.rkey;
write.next = &add;
add.opcode = IBV_WR_ATOMIC_FETCH_AND_ADD;
add.sg_list = &old_counter;
add.num_sge = 1;
add.wr.atomic.remote_addr = dst.address + offsetof(Buffer, counter);
add.wr.atomic.rkey = dst.rkey;
add.wr.atomic.compare_add = 1;
add.send_flags = IBV_SEND_SIGNALED;
add.wr_id = 42;
ibv_send_wr* bad = nullptr;
if (ibv_post_send(qp, &write, &bad) != 0) {
std::cerr << "Posting write/add failed\n";
std::exit(EXIT_FAILURE);
}
Here is the interesting part on the sender. The first request writes eight bytes into the peer’s payload. The second increments the peer’s eight-byte-aligned counter and asks the NIC to return the old value into a registered local buffer. Here we use the chained WQE to reduce the number of ibv_post_send calls from two to one to save verb posting overhead. But note that both WQEs will generate a CQE locally once finished, and they will not generate a CQE on the remote side. IBV_SEND_SIGNALED means we explicitly ask the atomic fetch-add to generate a CQE locally.
On our two GH200s, the sender got a successful fetch-add completion with wr_id=42 and an old counter value of 0. It then told the receiver over TCP that the operation was done. The receiver copied both words out of GPU memory with cudaMemcpy and printed GPU payload=123 counter=1. Keep that last step in mind when we talk about a GPU kernel that is already running while the NIC writes.
Ordering and completion
Ordering on host memory: Real-world RDMA systems need ordering to achieve correct synchronization, like when one needs to inform a remote process that the data is ready or has been consumed.
Ordering can become tricky for read, write, and atomic verbs, which are one-sided operations, meaning that they will NOT notify the remote CPU process while they are silently reading/writing remote memory.
Note that send and recv are two-sided operations, which means both the local and remote processes need to participate.
To address this issue, people usually adopt the following two approaches:
write-with-immis a verb that lets the remote RDMA NIC generate a CQE in the remote completion queue, so the remote process knows the data has arrived. That CQE also contains a 32-bit immediate value supplied by the write-initiating process. The receiver must post arecvWQE for this notification, even though the payload goes to the address in thewrite.write-then-atomic: after you post awriteverb, you immediately post afetch-addatomic verb to a remote counter on the same RC QP. On an ordered RC path like the one above, the NIC orders the atomic after thewrite, so the remote process can spin on that counter and learn when thewritehas arrived from the change in its value.
There are exceptions to the ordering between a write and a subsequent signal: AWS EFA RDMA NICs do not guarantee such ordering or expose native fetch-add (see our UCCL blog for details), and OpenAI uses a special RDMA transport called MRC 3 that allows out-of-order write placement as well. The reason is that these transports use multiple network paths between RDMA NIC peers to avoid network congestion, which makes it hard for the NIC hardware to track ordering among verbs. MRC has write and write-with-imm on the wire. Its receiver can place packets as they arrive but must hold a write-with-imm completion until earlier writes on that QP have been processed. We will talk more about this design in the congestion chapter.
Ordering on GPU memory is more subtle when interacting with GPU memory.
Take the simple write-with-imm into GPU memory as an example. On a PCIe system the receiving NIC writes the payload toward the GPU and the CQE toward host memory. They have different destinations, even if they share a PCIe switch. When the CPU sees the CQE, it knows the NIC has completed the receive. A kernel already running on the GPU 4 can still see old or partial data.
How does NCCL handle this? It turns out that NCCL uses various means to trigger an explicit PCIe flush. NCCL’s IB transport 5 can post a small RDMA read of the receive buffer on a local loopback QP and wait for the read to complete. The traffic stays on the receiving machine. Another path, available on supported x86 systems, has the CPU read a GDRCopy-mapped word on the receiving GPU to force a PCIe read. Only after the flush does the proxy tell the GPU to consume the data.
What if the region targeted by the write and the counter targeted by the atomic in write-then-atomic are both in GPU memory? Because both the data and the signal go through the same PCIe path, the counter change can indicate that the write has arrived if the transport orders the two operations. This is the kind of data-and-signal protocol that GPU-initiated communication libraries use, including MSCCL++, NCCL GIN, and DeepEP. GPU kernels can use these libraries’ matching wait or acquire operations before reading the payload.
What does a CQE really mean? Think of a simple RDMA write over RC that moves 1 MB of local data to a remote 1 MB host memory buffer. Once the local process polls the successful CQE for that write, does it mean that the local 1 MB memory region is available for reuse, and does it mean that the remote 1 MB memory region already holds valid data?
The answer is surprisingly simple: yes to both, and this is basically what a successful RC write completion guarantees for ordinary coherent host memory.
Internally, the RDMA NIC hardware has a packet acknowledgment mechanism that lets the remote NIC acknowledge the transfer to the local NIC, so polling a successful CQE means the sender can reuse its buffer. You still need a notification if you want the remote process to do something with the data.
Practical performance numbers
Finally, let’s look at some real RDMA performance numbers that are very useful in performance engineering. Here, we used one GH200 GPU on each of two machines, one RC QP on mlx5_0 port 1, and a 400 Gb/s NVIDIA CX7 InfiniBand NIC with an active MTU of 4096 bytes. We ran perftest with CUDA device buffers on both ends (--use_cuda=0) and pinned each process to a CPU core. Benchmark runner
Achievable bandwidth vs. inflight bytes (one outstanding write):
- In a congestion-free network, the achievable bandwidth usually depends on the message size until the message is large enough to saturate the link. With
ib_write_bw -q 1 -t 1 -l 1 -Q 1 -I 0, the sender waits for each write to complete before posting the next one. The inflight bytes on the x-axis therefore equal the size of one write. We got 9.0 Gb/s at 4 KiB, 158.9 Gb/s at 256 KiB, and 368.3 Gb/s at 4 MiB. With a single 1 GiB (1,073,741,824 bytes) write, we got 387.59 Gb/s. The underlying measurements include the iteration count for each point. - BDP (bandwidth-delay product) is a back-of-the-envelope approximation telling you how many bytes you need in flight to keep the link busy, thus achieving line rate. At 400 Gb/s, every microsecond of round-trip time corresponds to 50 KB in flight. You can cover that with one large write or many small ones, provided the CPU can post them fast enough.
Latency: Small-message latency will increase with the number of switch hops the RDMA packet traverses. In our testbed, the RDMA link used a 4 KiB MTU. According to the eRPC NSDI paper 6, a 32-byte request and reply took 2.3 µs at the median on ConnectX-5 Ethernet, and the switch added about 300 ns each time a layer-3 packet crossed it. Note that for long distances, the physical distance between the two RDMA NICs and the speed of light in fiber determine the propagation latency, which is about 5 µs/km one way.
On the two GH200s, we ran ib_write_lat --write_with_imm --use_cuda=0 -I 0 -n 10000 7 at each size. We used write-with-immediate because perftest requires it for this latency test with CUDA buffers. One NIC writes to the peer’s GPU memory. When the peer sees the receive CQE, it posts a write of the same size back. The tool divides the ping-pong round-trip time by two to estimate the latency of one write. That estimate includes the time the hosts spend polling and posting. Results
| Write size | Median RTT/2 (µs) | p99 RTT/2 (µs) |
|---|---|---|
| 8 B | 2.48 | 3.06 |
| 64 B | 2.51 | 3.12 |
| 4 KiB | 2.80 | 3.23 |
Verb issuing rate: How many 8-byte writes can a single CPU thread keep issuing? We used the same two GPUs and the classic ibv_post_send interface. First we asked for a completion on every write. Then we signaled only every 32nd write (i.e., set IBV_SEND_SIGNALED for one CQE every 32). Finally we chained 32 work requests in one call, so the provider could ring one doorbell for the whole list.
Writes per post call (-l) | Writes per CQE (-Q) | Million writes/s |
|---|---|---|
| 1 | 1 | 4.13 |
| 1 | 32 | 4.42 |
| 32 | 32 | 18.36 |
Each row is the median of three runs of one million writes with ib_write_bw --use_old_post_send -s 8 -t 128 -I 0 --use_cuda=0. With the same CQ moderation, chaining the requests gave us 4.1× the rate. These are completed writes per second with one thread both posting and polling, so the number includes the work of the NIC and the fabric too.
Advanced techniques:
- SRQ (shared receive queue). If 100 peers can send to you but only a few are active at once, you can then use a single SRQ to allow multiple QPs to share a pool of
recvWQEs. Of course, you need to replenish that pool before a busy peer drains it. - Shared CQ (SCQ). You can attach several QPs to one SCQ and poll them all in one place. The QP number and
wr_idin each polled CQE tell you which request completed. You can size the CQ capacity for the completions from all those QPs together. - Scatter/gather list (
sg_list). If your header and payload already live in separate registered buffers, you can describe both in onesendWQE. The NIC gathers the pieces, which saves you a copy, and still sends one message.
Remark
To some extent, RDMA programming is like GPU kernel programming: they are both hard to get into the first time, but once you get in, you will find there are ultimately just dozens of performance optimization techniques that matter in practice, and many of them are hardware-generation specific. Once in a while, I joke that GPU kernel programming and RDMA programming are the most important low-level LLM infra jobs, but now it seems that they can be done quite well by agents. Nevertheless, there are tons of good materials that you might not even have the patience to look at for now, and I put them here in case you are trying to debug a hard hardware problem during RDMA programming:
- The RDMA textbook from NVIDIA.
- The RDMAMojo blog, a practitioner’s blog about RDMA (these posts were written by a Mellanox engineer, and they are remarkably useful for pinpointing practical issues).
References
Footnotes
-
NVIDIA, GPUDirect RDMA and GPUDirect Storage. See also the
libibverbsmemory registration API. ↩ -
NVIDIA, NCCL environment variables, PCIe relaxed ordering. The
libibverbsmemory registration manual describes the ordering and completion guarantees. ↩ -
OpenAI, Supercomputer networking to accelerate large-scale AI training. Open Compute Project, Multipath Reliable Connection Specification, v1.0, §6.3.2. ↩
-
NVIDIA, GPUDirect RDMA, Synchronization and Memory Ordering. ↩
-
NVIDIA, NCCL v2.27.3, IB transport loopback read and receive proxy flush paths. ↩
-
Anuj Kalia, Michael Kaminsky, and David G. Andersen, Datacenter RPCs can be General and Fast, NSDI 2019, §6.1. ↩
-
Linux RDMA, perftest testing methodology and write latency benchmark. ↩