L1 Beginner · Module 2: Why GPUs: hardware first principles

2.5Occupancy and latency hiding

A GPU hides the hundreds of cycles a memory load takes by keeping many warps resident and switching to whichever one is ready. How many warps fit is called occupancy, and it is limited by registers, shared memory and thread count. More is not always better.

Runs on a CPU or in the browser✦ Interactive~20 min

By the end you will be able to

  • ✓Explain how warp schedulers hide memory latency by switching warps
  • ✓Compute occupancy from threads per block, registers per thread and shared memory per block
  • ✓Use Little's law to reason about how many bytes must be in flight
  • ✓Explain why maximum occupancy is not always the goal

A load from HBM takes several hundred cycles (2.3). A CPU core would cover that with caches and speculation. A GPU does something simpler: it switches to another warp.

Switching costs nothing

Each SM has four warp schedulers. Every cycle, each scheduler looks at the warps assigned to it, picks one whose next instruction is ready, and issues it. A warp waiting on a load is simply skipped. Because each warp’s registers stay in the SM’s huge register file the whole time, switching takes zero cycles: there is no state to save or restore.

So latency is hidden if, at every cycle, some warp has work ready. That depends on two things:

  • how many warps the scheduler has to choose from, and
  • how much independent work each warp does between memory waits.

Occupancy

Occupancy is the number of warps resident on an SM, as a fraction of the maximum (64 on an H100). It is capped by whichever resource runs out first:

Resource per SM (H100)LimitUsed by each block
Warp slots64 warps (2,048 threads)threads per block ÷ 32
Block slots32 blocks1
Registers65,536 × 32-bitregisters per thread × threads per block
Shared memoryup to 228 KBwhatever the kernel declares

A kernel with 256 threads per block that uses 64 registers per thread needs 16,384 registers per block, so only 4 blocks fit: 32 warps, or 50% occupancy, limited by registers.

1 · What one block of your kernel needs

2 · How many blocks fit on one SM

warp slots8 blocks
block slots32 blocks
registers4 blocks
shared memory32 blocks

4 blocks × 8 warps = 32 / 64 warps

Occupancy 50%, limited by registers.

3 · One of the 4 schedulers, with its 8 warps

01040 cycles

Blue = the warp is issuing instructions; pale red = waiting on memory. The scheduler is busy 31% of cycles. Full speed needs about 26 warps per scheduler.

fig. 2.5Top: set what one block needs and see how many fit on an SM, and which limit binds. Bottom: one scheduler running its share of those warps. Each warp does some math, then waits on a load.

Little’s law: count the bytes in flight

How much has to be in flight to keep HBM busy? Little’s law says

bytes in flight=bandwidth×latency.\text{bytes in flight} = \text{bandwidth} \times \text{latency}.

For an H100, 3.35 TB/s×∼300 ns≈1 MB3.35\ \text{TB/s} \times {\sim}300\ \text{ns} \approx 1\ \text{MB} across the GPU, or about 7.6 KB per SM at every moment. If each warp has one 128-byte load outstanding, an SM would need about 60 warps to cover that, nearly full occupancy. If each warp has eight 16-byte-per-lane loads outstanding (4 KB), two warps are enough.

Lab: an occupancy calculator 🟢

labs/m02/2.5/occupancy.py
limits = {
"warps": SM["max_warps"] // warps_per_block,
"blocks": SM["max_blocks"],
"registers": SM["regs"] // (regs_per_thread * threads_per_block),
"shared mem": SM["smem"] // smem_per_block,
}
blocks = min(limits.values())
Terminal window
python labs/m02/2.5/occupancy.py
output
threads/blk regs/thr smem/blk warps occ. limited by
256 32 0KB 64 100% warps
256 64 0KB 32 50% registers
256 128 0KB 16 25% registers
256 255 0KB 8 12% registers
128 64 48KB 16 25% shared mem
256 64 100KB 16 25% shared mem
Latency hiding: 20 cycles of math per load, ~500-cycle memory latency
1 warps/scheduler -> scheduler busy 3.8%
2 warps/scheduler -> scheduler busy 7.7%
4 warps/scheduler -> scheduler busy 15.4%
8 warps/scheduler -> scheduler busy 30.8%
16 warps/scheduler -> scheduler busy 61.5%
needed for 100%: ~26 warps/scheduler, but an SM holds at most 16 per scheduler.

The last line is the important one: with only one load in flight per warp, even full occupancy can’t hide a 500-cycle latency. Real kernels close the gap with more loads in flight per warp.

Sources

repo · BGEBEFORE-GPU-ENGINEERINGBEFORE-GPU-ENGINEERINGPhase 1: occupancy, warp scheduling and latency hiding.
book · FREGLYAI Systems Performance EngineeringChris FreglyOccupancy and ILP chapters: why high occupancy is not always the target.

Check your understanding

0 / 4 answered
  1. 1.Why can a GPU switch between warps in zero cycles?

  2. 2.A block has 512 threads using 64 registers each. How many blocks fit on an H100 SM by registers alone?

  3. 3.Little's law says bytes in flight equals…

  4. 4.Why do some of the fastest matmul kernels run at low occupancy?