L2 Intermediate · Module 4: CUDA programming fundamentals

4.3Host/device memory and transfers

A discrete GPU has its own memory, connected to the CPU by a bus that is ten to a hundred times slower than the GPU's own memory. Allocating, copying and pinning memory correctly, and avoiding copies entirely where possible, is basic inference hygiene.

GPU required (Colab or a rented GPU)~20 min

By the end you will be able to

  • ✓Allocate, copy and free device memory with the CUDA runtime API
  • ✓Explain the difference between pageable and pinned host memory
  • ✓Measure transfer bandwidth and latency, and compare them with on-GPU bandwidth
  • ✓Identify where host–device transfers appear in an inference server and how to keep them off the critical path

The CPU and a discrete GPU each have their own memory. The kernel from 4.1 can only read device memory, so data has to be moved across the bus between them, usually PCIe.

The basic API

float* d; // a device pointer
cudaMalloc(&d, bytes); // allocate on the GPU
cudaMemcpy(d, h, bytes, cudaMemcpyHostToDevice); // CPU -> GPU
kernel<<<blocks, threads>>>(d, n);
cudaMemcpy(h, d, bytes, cudaMemcpyDeviceToHost); // GPU -> CPU (waits for the kernel)
cudaFree(d);

A device pointer is just an address; nothing in C++ stops you from dereferencing it on the CPU, which crashes. A common convention is to name device pointers d_x and host pointers h_x.

The bus is the slow part

The lab measures every path on a laptop RTX 3050:

output (RTX 3050 6GB Laptop)
Copying 256 MB:
host->device, pageable : 9.0 GB/s
host->device, pinned : 12.9 GB/s
device->host, pageable : 8.5 GB/s
device->host, pinned : 13.2 GB/s
device->device : 77.7 GB/s (read + write = 155.4 GB/s of DRAM traffic)

On-GPU copies move data more than 10 times faster than the bus. On a data-centre GPU the gap is larger: PCIe Gen5 x16 gives about 64 GB/s per direction, while an H100’s HBM gives 3,350 GB/s, a ratio of about 50.

Pageable vs pinned memory

Ordinary malloc memory is pageable: the operating system can move it or swap it out. The GPU’s copy engine can’t read such memory safely, so the driver first copies it into a hidden pinned (page-locked) buffer, then sends it. Allocating pinned memory yourself with cudaMallocHost skips that staging copy:

  • Faster transfers: 12.9 GB/s vs 9.0 GB/s above.
  • Required for asynchronous copies that overlap with kernels (4.6).
  • But pinned memory can’t be swapped out, so pinning too much starves the rest of the system.

Latency: small copies are expensive

output
Small transfers are dominated by fixed latency:
4 bytes: 5.6 us -> 0.00 GB/s
4096 bytes: 6.5 us -> 0.63 GB/s
1048576 bytes: 87.5 us -> 11.99 GB/s
16777216 bytes: 1306.5 us -> 12.84 GB/s

Every copy pays about 5–6 µs of fixed cost before any data moves. Copying 4 bytes takes as long as copying 4 KB. Batch small transfers together, or avoid them.

Unified memory

cudaMallocManaged returns one pointer usable from both CPU and GPU, and the driver migrates pages on demand when either side touches them. It is convenient for prototypes. For performance-critical code, the page faults and migrations are unpredictable, so inference engines allocate device memory explicitly. (On chips where CPU and GPU share physical memory, such as Grace Hopper or Apple silicon, the trade-offs change.)

Where transfers appear in serving

TransferWhenSizeHot path?
Load weightsstartupthe whole model (16 GB for 8B BF16)no, but sets startup time
Token ids in, sampled ids outevery stepbytesyes: keep tiny, and async
KV cache offload to CPUwhen GPU memory is fullMBs per requestyes: hide it or avoid it (10.2)
KV transfer between GPUsdisaggregated servingMBs per requestyes: over NVLink/RDMA, not PCIe if possible (15.2)

Lab: measure your bus 🔴

Terminal window
cd labs/m04/4.3 && nvcc -O2 -arch=native transfers.cu -o transfers && ./transfers

Sources

Check your understanding

0 / 4 answered
  1. 1.Why are copies from pinned host memory faster than from pageable memory?

  2. 2.Copying 4 bytes from host to device takes about…

  3. 3.Why should model weights stay on the GPU between decode steps?

  4. 4.What does calling tensor.item() on a GPU tensor do to the pipeline?