L1 Beginner · Module 2: Why GPUs: hardware first principles
2.4Warps, SIMT and branching
GPU threads come in groups of 32 called warps, and a warp shares one instruction stream. When threads in a warp disagree at an if, the warp runs both sides. This chapter shows what that costs and how kernel authors avoid it.
By the end you will be able to
- ✓Explain SIMT and the relationship between threads, warps and lanes
- ✓Predict the cost of a divergent branch from the branch pattern
- ✓Distinguish warp-uniform branches, which are free, from divergent ones
- ✓Recognize branch-free patterns used in inference kernels
In 2.1 we said a GPU shares one instruction decoder across 32 lanes. This chapter shows what that means for the code you write.
SIMT: one instruction, many threads
You write a CUDA kernel as code for one thread. The hardware groups threads into warps of 32 and runs each warp as a unit: one instruction is fetched and decoded, then executed by all 32 lanes at once, each on its own data. NVIDIA calls this SIMT, single instruction, multiple threads.
When all 32 threads want the same instruction, SIMT costs nothing: you get 32 results for one instruction’s worth of control logic. The question is what happens when they don’t.
Divergence
Suppose half the threads in a warp take an if and half take the else. The warp can only issue one instruction at a time, so it runs
the if body with the else lanes masked off, then the else body with the if lanes masked off. The two paths run one after the
other, and during each one some lanes do nothing.
One warp: 32 lanes, one instruction stream
What the warp executes, in order
- instructions issued
- 12
- if no divergence
- 6
- lane efficiency
- 50%
Half the lanes take each path. The warp runs both, one after the other.
The cost of a divergent branch is the sum of its paths instead of their maximum. Nested divergent branches can serialize further. Since the Volta architecture each thread has its own program counter, which makes some synchronization patterns legal, but divergent paths within a warp still execute one after another.
How kernels avoid it
- Branch on warp- or block-level values. Choose work by
blockIdxor warp index so all lanes of a warp agree. - Compute both sides and select. For short bodies,
y = cond ? a : bcompiles to a select or predicated instructions with no divergence. The attention mask in 1.3 works this way: scores are replaced by with a select, not skipped with anif. - Sort or group the data. If items need different work, group similar items together so each warp gets a uniform batch.
- Pad. Variable-length sequences are often padded to a common length per tile so every lane runs the same loop count, and the padding is masked out at the end (9.4).
Sources
Check your understanding
0 / 4 answered1.How many threads are in a warp on NVIDIA GPUs?
2.Half a warp takes an if-body of 6 instructions and half takes an else-body of 6. How many instructions does the warp issue?
3.Which branch causes no divergence?
4.How is a causal attention mask usually applied inside a kernel?