Pidoku

PCIe, DMA and the Host Interconnect

Module 9 Lesson 9.b 1h Difficulty 3/5

Prerequisites 3.c, 3.f, 4.g


Before you start: words you need#

WordPlain meaning
PCIeThe standard bus that connects GPUs, NICs and SSDs to the CPU. Speed depends on its generation and its number of lanes (x16 for a GPU).
GT/sGiga-transfers per second: the raw signalling rate of one lane. Usable bytes per second are a little lower after encoding overhead.
DMADirect memory access: a hardware copy engine moves data between host memory and the device without the CPU touching each byte.
Pinned memoryHost memory the operating system promises not to move or swap, so a DMA engine can read it directly (3.f).
PCIe switchA chip that fans one PCIe connection out to several devices, like a network switch for the bus.
NUMAOn multi-socket servers, each CPU has its own local memory and PCIe slots; reaching the other socket’s is slower (3.c).
NVLink-C2CNVIDIA’s chip-to-chip link that joins a Grace or Vera CPU to its GPUs, much faster than PCIe and cache-coherent.
CXLCompute Express Link: a protocol that runs on the PCIe physical layer and lets devices share memory coherently, used mostly for memory expansion.

1. What is it?#

The GPU is not in the CPU’s memory. It is a peripheral at the end of a bus. Everything that moves between them crosses PCIe, and it is moved by a DMA engine, not by the CPU.

CPU ──── PCIe 5 x16 ──── GPU
         ~50 GB/s real          vs   HBM3e at 8,000 GB/s on a B200
                                     → about 160x slower

There are faster paths: NVLink between GPUs (9.c), and NVLink-C2C between a Grace or Vera CPU and its GPUs. Knowing which path your data takes is the difference between a design that works and one that does not. Lesson 4.g introduced the host link; this lesson goes into it in depth.

Facts checked on 12 November 2026.


2. Why does it exist?#

Modularity. A GPU is a card you plug in, from any vendor, into any server. That flexibility costs a bus two orders of magnitude slower than the GPU’s own memory. Every inference decision about moving data between host and device follows from that ratio:

  • weights are loaded once and stay in HBM;
  • only small things (token IDs, sampled tokens) cross per step;
  • offloading anything large to host memory needs a fast link or careful overlap.

3. Simple analogy#

A workshop with a supply depot down the road. Inside the workshop everything is at arm’s reach (HBM). The depot is a 20-minute drive (PCIe). You would never drive to the depot for each screw; you bring a whole crate once and keep it on the shelf.

The rule: load weights once, keep them resident, and only ship small things across the bus.


4. Tiny example#

Python
import torch, time

def h2d(size_mb, pinned):
    n = size_mb * 1024 * 1024 // 4
    src = torch.empty(n, dtype=torch.float32, pin_memory=pinned)
    dst = torch.empty(n, dtype=torch.float32, device='cuda')
    for _ in range(3): dst.copy_(src, non_blocking=pinned)
    torch.cuda.synchronize()
    t0 = time.perf_counter()
    for _ in range(10): dst.copy_(src, non_blocking=pinned)
    torch.cuda.synchronize()
    dt = (time.perf_counter()-t0)/10
    print(f"{size_mb:6d} MB  pinned={pinned!s:5s}  {dt*1e3:8.2f} ms  {size_mb/1024/dt:7.2f} GB/s")

for mb in [1, 4, 16, 64, 256, 1024]:
    h2d(mb, False)
    h2d(mb, True)

Typical results on one PCIe 4 x16 machine (yours will differ; measure them):

     1 MB  pinned=False      0.35 ms     2.79 GB/s
     1 MB  pinned=True       0.09 ms    10.85 GB/s
  1024 MB  pinned=False     92.10 ms    10.86 GB/s
  1024 MB  pinned=True      41.20 ms    24.27 GB/s

Two lessons:

  1. Pinned memory roughly doubles bandwidth, because there is no hidden staging copy.
  2. Small transfers are dominated by fixed overhead (about 5–10 µs per copy). Batch them.

5. Technical explanation#

PCIe generations#

GenPer-lane ratex16, each direction (after encoding)Realistic copyStatus (Nov 2026)
3.08 GT/s~15.8 GB/s~12 GB/solder servers
4.016 GT/s~31.5 GB/s~25 GB/sA100 era
5.032 GT/s~63 GB/s~50 GB/sH100, B200 servers
6.064 GT/s~121 GB/s~100 GB/sB300 HGX, ConnectX-8 and ConnectX-9 NICs
7.0128 GT/s~242 GB/s—spec published June 2025; products not yet in servers
8.0256 GT/s~1 TB/s both directions combined (target)—draft 0.5 (May 2026); final spec targeted for 2028

Each generation doubles the rate. PCIe 6.0 switched to four-level signalling (PAM4) and fixed-size packets (FLITs) to do it. Real copies reach roughly 75–85% of the theoretical figure.

Check your actual link:

Shell
nvidia-smi -q | grep -A4 "GPU Link Info"
sudo lspci -vv -s <bus:dev.fn> | grep -E "LnkCap|LnkSta"

Watch for downgraded links. A GPU in an x16 slot may negotiate x8 or a lower generation because of a riser, bifurcation or a bad seat. That halves or quarters transfer bandwidth and is invisible unless you look. GPUs also drop the link speed when idle to save power, so check under load.

DMA#

The CPU does not copy data to the GPU. It programs a copy engine on the GPU with source, destination and length; the engine moves the data while the CPU and the GPU’s SMs do other work.

  • The source must be pinned, or CUDA stages through an internal pinned buffer, costing an extra copy.
  • Copies on a separate stream overlap with compute (5.e). Data-center GPUs have several copy engines, so host-to-device, device-to-host and compute can all run at once.
  • cudaMemcpyAsync from pageable memory is not really asynchronous.
Python
stream = torch.cuda.Stream()
with torch.cuda.stream(stream):
    gpu_buf.copy_(pinned_buf, non_blocking=True)   # overlaps with default-stream compute

Topology inside the server#

An 8-GPU server is a tree of PCIe switches under one or two CPUs. GPUs and NICs that share a switch talk directly; others go through the CPU, or across to the other socket.

Shell
nvidia-smi topo -m       # the connection type between every pair of GPUs and NICs
CodeMeaningQuality
NV#NVLink with # links (9.c)best
PIXthe same PCIe switchgood
PXBseveral PCIe switches, no CPUfair
PHBthrough the CPU’s PCIe host bridgeworse
NODEacross PCIe host bridges within one NUMA nodeworse
SYSacross the link between CPU socketsworst

This matters for two things: which GPUs to group for tensor parallelism when there is no NVLink, and which NIC each GPU should use for RDMA (9.c). AI servers pair each GPU with its own NIC under the same switch for exactly this reason.

Faster host paths#

  • NVLink-C2C joins a Grace CPU to its GPUs at 900 GB/s total (GH200, GB200, GB300) and a Vera CPU at 1.8 TB/s (Vera Rubin, 7.e). It is cache-coherent: the GPU can read host memory almost as if it were its own, only slower than HBM. This makes KV-cache and optimizer offload to host memory practical (14.k, 8.c).
  • NVLink Fusion licenses NVLink and C2C to other chips, including x86 CPUs, so the same kind of fast host link can appear outside NVIDIA’s own CPUs (9.d).
  • GPUDirect is a family of paths that skip host memory: P2P (GPU to GPU over PCIe or NVLink), RDMA (NIC straight into GPU memory, 9.c) and Storage (NVMe straight into GPU memory, 3.g).

CXL#

CXL reuses the PCIe physical layer to add cache-coherent memory protocols. Its main use today is memory expansion: a CXL card full of DRAM adds hundreds of gigabytes to a server, at roughly PCIe bandwidth and somewhat higher latency than local DRAM.

  • CXL 2.0 memory expanders are in shipping servers; CXL 3.x adds switching and memory sharing between hosts.
  • CXL 4.0 (November 2025) doubles the rate to 128 GT/s on the PCIe 7.0 physical layer and lets several links be bundled into one logical port.
  • For inference, CXL memory is a candidate tier for KV cache between DRAM and SSD (8.c). Most fleets in 2026 still use plain DRAM and NVMe; treat vendor KV-offload claims for CXL as claims until you measure them.

6. Under the hood#

The program costs the transfers that cross the host link in inference, at achieved rates, with a fixed 10 µs per copy.

Go
// pcie.go — what crosses the host link, and what it costs per step.
package main

import "fmt"

type link struct {
	name string
	gbs  float64 // achieved GB/s, one direction
}

type transfer struct {
	name  string
	bytes float64
	when  string
}

func main() {
	links := []link{{"PCIe 4 x16", 25}, {"PCIe 5 x16", 50}, {"PCIe 6 x16", 100}, {"NVLink-C2C (Grace)", 700}}
	xs := []transfer{
		{"token IDs, batch 64", 64 * 4, "per step"},
		{"logits, batch 16 × 128k vocab, FP32", 16 * 128e3 * 4, "per step"},
		{"KV for one 8k-token request (70B, FP8)", 8192 * 163840, "per offload"},
		{"8B model weights, BF16", 16e9, "per step if streamed"},
		{"70B model weights, BF16", 140e9, "per step if streamed"},
	}
	fmt.Printf("%-40s", "transfer")
	for _, l := range links {
		fmt.Printf(" %19s", l.name)
	}
	fmt.Println()
	for _, x := range xs {
		fmt.Printf("%-40s", x.name)
		for _, l := range links {
			ms := x.bytes/(l.gbs*1e9)*1000 + 0.01 // + ~10 µs fixed cost per copy
			fmt.Printf(" %16.2f ms", ms)
		}
		fmt.Printf("   %s\n", x.when)
	}
	fmt.Printf("\n%-40s", "decode ceiling if weights stream (tok/s)")
	for _, l := range links {
		fmt.Printf(" %19s", l.name)
	}
	fmt.Println()
	for _, w := range []float64{16e9, 140e9} {
		fmt.Printf("%-40s", fmt.Sprintf("  %.0f GB of weights", w/1e9))
		for _, l := range links {
			fmt.Printf(" %19.2f", l.gbs*1e9/w)
		}
		fmt.Println()
	}
}

Output:

transfer                                          PCIe 4 x16          PCIe 5 x16          PCIe 6 x16  NVLink-C2C (Grace)
token IDs, batch 64                                  0.01 ms             0.01 ms             0.01 ms             0.01 ms   per step
logits, batch 16 × 128k vocab, FP32                  0.34 ms             0.17 ms             0.09 ms             0.02 ms   per step
KV for one 8k-token request (70B, FP8)              53.70 ms            26.85 ms            13.43 ms             1.93 ms   per offload
8B model weights, BF16                             640.01 ms           320.01 ms           160.01 ms            22.87 ms   per step if streamed
70B model weights, BF16                           5600.01 ms          2800.01 ms          1400.01 ms           200.01 ms   per step if streamed

decode ceiling if weights stream (tok/s)          PCIe 4 x16          PCIe 5 x16          PCIe 6 x16  NVLink-C2C (Grace)
  16 GB of weights                                      1.56                3.12                6.25               43.75
  140 GB of weights                                     0.18                0.36                0.71                5.00

How to read it:

  • Token IDs are free on any link.
  • Logits copied to the CPU for sampling cost 0.1–0.3 ms per step, plus a synchronisation that stalls the GPU. That adds directly to inter-token latency, which is why engines sample on the GPU.
  • A KV offload of one 8k-token request takes 13–54 ms over PCIe and 2 ms over C2C. Fine as an occasional swap, too slow for every step.
  • Streaming weights every step gives 0.2–6 tokens per second over PCIe. “Just offload the model to CPU RAM” produces a hobby system, not a service.

Tools for live measurement:

Shell
# Live PCIe throughput per GPU (rxpci / txpci columns, MB/s)
nvidia-smi dmon -s t

# From NVIDIA's cuda-samples
./bandwidthTest --memory=pinned --mode=range --start=1024 --end=1073741824 --increment=1048576
./p2pBandwidthLatencyTest

In Nsight Systems (22.h), host-to-device copies appear on their own rows. Copies on the critical path between kernels every step mean a design problem: find what is being moved and stop moving it.


7. Performance implications#

PatternCostVerdict
Weight load at startup140 GB ÷ 50 GB/s ≈ 3 s over PCIe 5fine, once; storage is usually slower (3.g)
Token IDs per stepa few hundred bytesnegligible
Logits to the CPU every step~0.2 ms + a syncavoid: sample on the GPU
KV offload / reloadtens of ms per long request over PCIeviable with overlap; easy over C2C
Weights streamed every stepseconds per token for a 70B modelhobby use only
Tensor-parallel all-reduce over PCIeoften longer than the computeuse NVLink (9.g)

Any per-step device-to-host copy that needs a synchronisation adds directly to inter-token latency.


8. Production implications#

  • Verify link width and generation on every new node type. Downgraded links are common and silent.
  • Check nvidia-smi topo -m before choosing a TP degree on servers without NVLink. TP=8 across two PCIe islands is usually worse than TP=4 within one island plus data parallelism.
  • Use a pinned buffer pool for any recurring transfer.
  • Keep sampling, stop checks and logit processing on the GPU.
  • Pin processes to the right NUMA node. The CPU threads and pinned buffers for a GPU should sit on the socket that owns its PCIe slot (3.c).
  • In Kubernetes, ask for GPUs that share a switch or NVLink domain (21.c).

9. Common mistakes#

  1. Designing around CPU offload without checking PCIe bandwidth. Do the arithmetic first.
  2. Pageable transfers in the hot path. Half the bandwidth, and they block.
  3. Per-step device-to-host copies with a synchronisation. Milliseconds added to inter-token latency over a long generation.
  4. Assuming all 8 GPUs in a box are equally connected. They often are not.
  5. Ignoring shared uplinks. Several GPUs behind one PCIe switch share its uplink to the CPU.
  6. Forgetting that NCCL and RDMA use these same paths. Your “network” speed in 9.c–9.f starts here.

10. Hands-on exercise#

  1. Run pcie.go. Change the vocabulary to 200k and batch to 64. When does the logits copy pass 1 ms per step on PCIe 4?
  2. Measure your bus. Run the pinned/pageable benchmark in section 4. Fit a line to time against size: the slope is bandwidth, the intercept is fixed overhead.
  3. Verify link status. Is every GPU at full width and generation under load?
  4. Map the topology. Run nvidia-smi topo -m and draw the graph. Which GPUs would you group for TP=4? Which NIC belongs to each GPU?
  5. Overlap. Copy a large buffer on a side stream while a matmul runs on the default stream. Verify the overlap in Nsight Systems, or by timing: total < sum of parts.

11. Interview questions#

  1. What is the bandwidth of PCIe 5 x16, and how does it compare with HBM?
  2. Why is pinned memory faster for host-to-device copies? What does it cost?
  3. What is DMA, and why does it let transfers overlap with compute?
  4. Explain nvidia-smi topo -m and how it affects tensor-parallel grouping.
  5. Why is streaming weights from host memory impractical? Show the arithmetic.
  6. Why should sampling happen on the GPU?
  7. What does NVLink-C2C change about CPU offload?
  8. What is CXL, and where might it fit in an inference server?

12. Further reading#

Checked on 12 November 2026.

↑↓ navigate↵ openesc close

100% drag to pan · scroll or pinch to zoom · double-click to zoom in