Occupancy, registers and latency hiding
Why a GPU needs many warps in flight to hide memory latency, how registers, shared memory and block size limit the number of warps per SM, how to compute occupancy by hand and with the CUDA API, and why the fastest kernels often run at low occupancy and hide latency with more work per thread instead.
By Sandip Maity, Applied AI Engineer
In the last lesson you made a matmul faster by giving each thread more registers. That raises a question: if each thread uses more registers, fewer threads fit on an SM. When is that a good trade? This lesson gives you the model to answer it.
#The problem: memory is far away
A load from GPU memory takes a long time to come back: hundreds of clock cycles. An SM doesn't wait idly while it does. Each SM has several warp schedulers, and every cycle each one picks a warp that's ready to run (its operands have arrived) and issues its next instruction. While one warp waits for memory, others run. This is latency hiding, and it's the central trick of GPU design.
How much work has to be in flight? Little's law: the amount of data in flight equals bandwidth times latency. As an illustration, take a latency of 400 nanoseconds:
| Bandwidth | Bytes in flight needed (400 ns) | Per SM | |
|---|---|---|---|
| T4 | 320 GB/s | about 3.2 KB (40 SMs) | |
| H100 | 3,350 GB/s | about 1.3 MB | about 10 KB (132 SMs) |
If each thread has one 4-byte load outstanding, a T4 SM needs about 800 threads (25 warps) doing nothing but loads to keep memory busy. An H100 SM would need about 2,500 threads, more than the 2,048 it can hold. So on an H100, one scalar load per thread can't saturate memory, whatever the occupancy. You need each thread to have several loads in flight, or to load 16 bytes at a time. That's the deeper reason for the vectorised loads in the coalescing lesson.
The latency figure is illustrative; real latency depends on the GPU, the clock and how loaded memory is. The shape of the argument doesn't change.
#Occupancy
Occupancy is the number of warps resident on an SM divided by the maximum it can hold: 32 on a T4, 64 on an H100. Three resources decide how many warps fit:
| Resource (per SM) | T4 | H100 | How a kernel uses it |
|---|---|---|---|
| Registers | 65,536 | 65,536 | registers per thread times threads |
| Shared memory | up to 64 KB | up to 228 KB | shared memory per block times blocks |
| Warp slots | 32 | 64 | warps per block times blocks |
| Block slots | 16 | 32 | one per block |
Whichever runs out first sets the limit. Registers are allocated per warp in chunks (256 registers at a time on recent GPUs), and blocks fit whole or not at all, so the real calculation has a little rounding. Try a few configurations:
GPU
Threads per block
blocks per SM
4
warps (of 32)
32
occupancy
100%
Limited by: Warp slots.
Simplified: registers are allocated per warp in chunks of 256, and other allocation details are ignored. Nsight Compute and cudaOccupancyMaxActiveBlocksPerMultiprocessor give the exact answer.
A worked example on a T4, with 256-thread blocks (8 warps each):
- 32 registers per thread: a warp needs registers, so registers allow 64 warps. The 32-warp slot limit is lower: 4 blocks, 32 warps, 100%.
- 64 registers per thread: 2,048 per warp, so registers allow 32 warps: still 4 blocks, 100%.
- 128 registers per thread: 4,096 per warp, so only 16 warps: 2 blocks, 50%.
- 64 registers and 24 KB of shared memory per block: shared memory allows blocks, so 16 warps, 50%, even though registers would allow 100%.
Pro content
Keep reading with Inference Academy Pro
Unlock the rest of this lesson, plus every advanced lesson, hands-on lab, the larger playground models and completion certificates.
Still to come in this lesson
- Checking occupancy
- Controlling register use
- Occupancy is a means, not a goal
- Where this shows up in inference
- What you can now do
- Common mistakes
- Exercise
- Going deeper
You will be able to answer
- What does occupancy measure?
- On a T4 (65,536 registers per SM, 32 warps max), a kernel uses 128 registers per thread. What is the most warps per SM?
- Why do GPUs need many memory requests in flight to reach full bandwidth?
- A matmul kernel runs at 25% occupancy but is faster than a 100% occupancy version. How is that possible?
- What does __launch_bounds__(256, 2) tell the compiler?