S429 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

S4 · Lesson 29 · GPUs

SIMT execution, warps and occupancy, branch divergence, memory coalescing and shared-memory banks

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.

  • 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.)

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?

Name it

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:

warps=min⁡(Wmax,⌊RSM32r⌋)

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 kA+(32−k)B32(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.

Worked 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?

Show the solution
  1. At 40 registers per thread. ⌊65536/(40×32)⌋=51warps which is 80% occupancy.
  2. At 64 registers per thread. 65536/(64×32)=32warps 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=20warps 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.

Worked 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?

Show the solution
  1. Peak rate. R=108×64×2×1.41GHz=19.5TFLOPS
  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.

Worked 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?

Show the solution
  1. Instruction slots. The warp runs both paths, 30 instruction slots.
  2. Useful lane-slots. 8×20+24×10=400
  3. Lane efficiency. η=40032×30=400960=41.7%
  4. Hiding the latency. It needs about 400/20=20warps 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

An SM has 65536 registers and room for 64 warps. A kernel uses 24 registers per thread. How many warps fit?

Check

In a warp of 32, 12 threads take a path of 10 instructions and the rest a path of 20. What's the lane efficiency?

Check

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

Check

Memory latency is 500 cycles, and each warp computes for 10 cycles between memory accesses. About how many warps does an SM need to stay busy?

Check

A GPU has 80 SMs of 64 FP32 lanes at 1.8 GHz. What's its peak FP32 rate?

Check

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

Check

An SM has 65 536 registers and room for 48 warps; a kernel uses 48 registers per thread and does 30 cycles of work between 600-cycle memory accesses.

(a)

How many warps fit?

(b)

How many warps does hiding the latency take?

Check

In a warp, 16 of 32 lanes take a 10-instruction path and the rest a 20-instruction path.

(a)

What is the lane efficiency?

Check

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

Step 1

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

Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.

Explain why

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

Practice

Keep practicing

11 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.