---
title: GPUs
heading: SIMT execution, warps and occupancy, branch divergence, memory coalescing and shared-memory banks
eyebrow: S4 · Lesson 29 · GPUs
xp: 50
---

::: hook
A GPU is a machine for throughput: dozens of streaming multiprocessors, each running thousands of threads in groups of 32 called warps, with all the threads of a warp executing the same instruction at once. It hides memory latency with sheer parallelism instead of big caches, switching warps every cycle (lesson 14), and it spends its transistors on arithmetic instead of prediction and reordering. Programming one well means keeping enough warps resident to hide latency, keeping the threads of a warp on the same path, and making a warp's 32 memory accesses land in as few memory transactions as possible.
:::

::: play
- **Occupancy**, the resident warps as a fraction of the most an SM can hold, against registers per thread, for an SM with 65,536 registers and room for 64 warps of 32 threads.
- Each warp needs 32 × r registers, so a register-hungry kernel fits fewer warps and has fewer to switch among while some wait on memory.
- Up to 32 registers per thread, all 64 warps fit; at 64, half; at 128, a quarter. (Real GPUs allocate registers in blocks and also limit by shared memory and block size, ignored here.)
:::
:::

```sim plot
view: graph
x: r = 16..255
y: o = 0..1
slider: R = 32768..131072 = 65536 log
slider: W = 32..64 step 8 = 64
plot: (W + floor(R/(r*32) + 0.0001) - abs(W - floor(R/(r*32) + 0.0001)))/2/W | samples: 600 | label: occupancy
readout: warps that fit at 64 registers per thread = (W + floor(R/2048 + 0.0001) - abs(W - floor(R/2048 + 0.0001)))/2
```

::: predict
## Half the threads of a warp take an if-branch of 20 instructions; the other half take the else-branch of 10. What fraction of the warp's lanes do useful work across both branches?

- [ ] 100%
- [ ] 67%
- [x] 50%
- [ ] 33%

::: reveal
50%. A warp issues one instruction for all 32 lanes, so it runs the 20-instruction path with 16 lanes active, then the 10-instruction path with the other 16: 30 issue slots × 32 lanes = 960 lane-slots, of which 16 × 20 + 16 × 10 = 480 do work. Divergence costs the sum of both paths, which is why GPU code avoids data-dependent branches inside a warp.
:::
:::

::: nameit
## SIMT

Threads are grouped into **warps** of 32 that share an instruction stream; warps are grouped into **thread blocks** that run on one streaming multiprocessor (SM) and share its **shared memory**. Each cycle a warp scheduler picks a ready warp and issues its next instruction to 32 lanes.

## Occupancy and latency hiding

An SM holds as many warps as its registers, shared memory and slots allow:

$$\text{warps} = \min\left(W_\text{max}, \left\lfloor\frac{R_\text{SM}}{32r}\right\rfloor\right)$$

for r registers per thread. By Little's law, hiding a memory latency L when each warp does c cycles of work between memory accesses takes about L/c warps.

## Divergence

When lanes of a warp disagree at a branch, the warp runs each path with the other lanes masked off. With k of 32 lanes taking a path of A instructions and the rest a path of B, lane efficiency is $\frac{kA + (32 - k)B}{32(A + B)}$.

## Memory

- **Coalescing:** the 32 addresses of a warp's load are combined into 32-byte sectors. Consecutive 4-byte words need 4 sectors; a stride of s words needs about $\min(32, 4s)$, multiplying traffic.
- **Shared memory** has 32 banks of 4-byte words; addresses in the same bank serialize. A stride of s words (s a power of two up to 32) gives an s-way conflict.
- **HBM** stacks DRAM beside the GPU on wide interfaces for terabytes per second.

Peak rate = SMs × FP32 lanes per SM × 2 (FMA) × clock.
:::

::: example
## An SM has 65,536 registers and room for 64 warps. What occupancy does a kernel get at 40 and at 64 registers per thread?

::: solution
1. **At 40 registers per thread.**
   $$\lfloor 65536/(40 \times 32) \rfloor = 51\ \text{warps}$$
   which is 80% occupancy.
2. **At 64 registers per thread.**
   $$65536/(64 \times 32) = 32\ \text{warps}$$
   which is 50%.
3. **Hiding the latency.** If each warp computes for 20 cycles between 400-cycle memory accesses, hiding the latency takes about
   $$400/20 = 20\ \text{warps}$$
   so both are enough here.
4. **A leaner kernel.** One that computes only 5 cycles between loads would need 400/5 = 80 warps, more than the SM can hold.

**Answer:** 51 warps (80% occupancy) at 40 registers per thread and 32 warps (50%) at 64.
:::
:::

::: example
## A GPU has 108 SMs with 64 FP32 lanes each at 1.41 GHz. What's its peak FP32 rate? And how many 32-byte sectors does a warp's load touch at strides of 1, 2 and 32 words?

::: solution
1. **Peak rate.**
   $$\begin{aligned} R &= 108 \times 64 \times 2 \times 1.41\ \text{GHz} \\ &= 19.5\ \text{TFLOPS} \end{aligned}$$
2. **Stride 1.** The warp reads 128 contiguous bytes: 4 sectors.
3. **Stride 2.** It touches 8 sectors, half of each wasted.
4. **Stride 32.** Every address lands in its own sector: 32 sectors, 8 times the traffic for the same data.
5. **The fix.** Transposing arrays (structure of arrays instead of array of structures) restores stride 1.

**Answer:** the peak is 19.5 TFLOPS; a warp's load touches 4, 8 and 32 sectors at strides of 1, 2 and 32 words.
:::
:::

::: example
## In one warp, 8 of the 32 lanes take a 20-instruction path and the rest a 10-instruction path. What is the lane efficiency? And how many warps hide a 400-cycle memory latency if each does 20 cycles of work between accesses?

::: solution
1. **Instruction slots.** The warp runs both paths, 30 instruction slots.
2. **Useful lane-slots.**
   $$8 \times 20 + 24 \times 10 = 400$$
3. **Lane efficiency.**
   $$\begin{aligned} \eta &= \frac{400}{32 \times 30} \\ &= \frac{400}{960} \\ &= 41.7\% \end{aligned}$$
4. **Hiding the latency.** It needs about
   $$400/20 = 20\ \text{warps}$$
   ready to take over, so occupancy and divergence together decide how much of the peak a kernel sees.

**Answer:** the lane efficiency is 41.7%, and about 20 warps hide the latency.
:::
:::

::: check
given: R = 65536 | 131072; r = 24 | 48 | 72 | 96; W = 48 | 64
answer: (W + floor(R/(r*32) + 0.0001) - abs(W - floor(R/(r*32) + 0.0001)))/2
tolerance: 0

## An SM has {R} registers and room for {W} warps. A kernel uses {r} registers per thread. How many warps fit?

::: right
Right. The smaller of {W} and $\lfloor R/32r \rfloor$: {answer}.
:::

::: misses
- [floor(R/r + 0.0001)] That counts threads; a warp is 32 threads.
- [any] Not yet. Registers per warp are 32r.
:::
:::

::: check
given: k = 8 | 12 | 24; A = 10 | 30; B = 5 | 20
answer: 100*(k*A + (32 - k)*B)/(32*(A + B))
unit: %
tolerance: 0.5%

## In a warp of 32, {k} threads take a path of {A} instructions and the rest a path of {B}. What's the lane efficiency?

::: right
Right. $\frac{kA + (32 - k)B}{32(A + B)}$ = {answer}%.
:::

::: misses
- [100*k/32] Both paths run; weight each by its length.
- [any] Not yet. Useful lane-slots over issued lane-slots.
:::
:::

::: check
given: s = 1 | 2 | 4 | 16 | 32
answer: (32 + 4*s - abs(32 - 4*s))/2
tolerance: 0

## A warp loads 4-byte words at a stride of {s} words. How many 32-byte sectors does it touch?

::: right
Right. min(32, 4s) = {answer}.
:::

::: misses
- [any] Not yet. Contiguous words share sectors: 32 words × 4 bytes × stride, in 32-byte sectors, at most one per thread.
:::
:::

::: check
given: L = 300 | 500 | 800; c = 10 | 25 | 40
answer: L/c
tolerance: 0.5%

## Memory latency is {L} cycles, and each warp computes for {c} cycles between memory accesses. About how many warps does an SM need to stay busy?

::: right
Right. L/c = {answer} warps.
:::

::: misses
- [L] Each warp covers c cycles of the wait.
- [any] Not yet. Little's law.
:::
:::

::: check
given: N = 80 | 132; l = 64 | 128; f = 1.4 | 1.8
answer: N*l*2*f/1000
unit: TFLOPS
tolerance: 0.5%

## A GPU has {N} SMs of {l} FP32 lanes at {f} GHz. What's its peak FP32 rate?

::: right
Right. {answer} TFLOPS.
:::

::: misses
- [N*l*f/1000] Each FMA counts as two operations.
- [any] Not yet. SMs × lanes × 2 × clock.
:::
:::

::: check
given: s = 2 | 4 | 8 | 16
answer: s
tolerance: 0

## Threads of a warp access shared memory (32 banks of 4-byte words) at a stride of {s} words. How many ways does each bank conflict?

::: right
Right. {answer}-way: the access takes {answer} times as long.
:::

::: misses
- [1] A power-of-two stride folds the 32 threads onto fewer banks.
- [any] Not yet. 32 threads spread over 32/s banks.
:::
:::

::: check
kind: parts
given: r = 32 | 48 | 96 | 128; W = 48 | 64; L = 300 | 600; c = 10 | 30

## An SM has 65 536 registers and room for {W} warps; a kernel uses {r} registers per thread and does {c} cycles of work between {L}-cycle memory accesses.

::: part
answer: floor(65536/(32*r)) - (floor(65536/(32*r)) - W)*step(floor(65536/(32*r)) - W)
unit: warps
tolerance: 0

### How many warps fit?

::: misses
- [any] $\min(W_\text{max}, \lfloor R_\text{SM}/32r\rfloor)$.
:::
:::

::: part
answer: L/c
unit: warps
tolerance: 0.5%

### How many warps does hiding the latency take?

::: misses
- [any] Little's law: L/c.
:::
:::
:::

::: check
kind: parts
given: k = 4 | 16 | 28; A = 10 | 40; B = 10 | 20

## In a warp, {k} of 32 lanes take an {A}-instruction path and the rest a {B}-instruction path.

::: part
answer: (k*A + (32 - k)*B)/(32*(A + B))*100
unit: %
tolerance: 0.5%

### What is the lane efficiency?

::: misses
- [any] $\frac{kA + (32 - k)B}{32(A + B)}$.
:::
:::
:::

::: check
kind: derivation
vars: s = 1..8

## A warp's 32 threads load 4-byte words at a stride of s words.

::: step
answer: 4*s

### How many 32-byte sectors does the load touch (for s up to 8)?
:::

::: step
answer: 128*s

### How many bytes move?
:::

::: step
answer: 1/s

### What fraction of them are used?
:::

::: right
Right. Strided access wastes bandwidth in proportion to the stride, which is why GPU data is laid out as structures of arrays.
:::
:::

::: explain
## Why does a GPU need far more threads than it has lanes?

::: model
A GPU keeps its arithmetic units busy by switching to another warp whenever the current one is waiting, mostly for memory, which takes hundreds of cycles. It has no large caches or out-of-order logic to hide that wait within a thread, so the only way to cover it is to have enough other warps ready to issue during it. By Little's law, the number of warps needed is roughly the latency divided by how many cycles of work each warp does between memory accesses. That comes to tens of warps per SM, each of 32 threads, so tens of thousands of threads across the chip, far more than the number of lanes that execute in any one cycle.
:::
:::

::: practice

::: check
given: R = 65536; r = 32 | 56 | 80; W = 64
answer: (W + floor(R/(r*32) + 0.0001) - abs(W - floor(R/(r*32) + 0.0001)))/2
tolerance: 0

## Warps that fit: {R} registers, {r} per thread, at most {W}?

::: misses
- [any] min(W, ⌊R/32r⌋).
:::
:::

::: check
given: k = 4 | 20; A = 15 | 40; B = 8 | 25
answer: 100*(k*A + (32 - k)*B)/(32*(A + B))
unit: %
tolerance: 0.5%

## Lane efficiency: {k} lanes on {A} instructions, the rest on {B}?

::: misses
- [any] $\frac{kA + (32 - k)B}{32(A + B)}$.
:::
:::

::: check
given: L = 400 | 600; c = 8 | 30
answer: L/c
tolerance: 0.5%

## Warps to hide {L} cycles with {c} cycles of work each?

::: misses
- [any] L/c.
:::
:::
:::
