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.

✦ Interactive~15 min

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.

thread block=several warpswarp=32 threads in locksteplane=one thread’s slot in its warp\text{thread block} = \text{several warps} \qquad \text{warp} = 32 \text{ threads in lockstep} \qquad \text{lane} = \text{one thread's slot in its warp}

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.

Branch

One warp: 32 lanes, one instruction stream

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
■ takes if (16)■ takes else (16)

What the warp executes, in order

if-body
6 instr · 16/32 on
else-body
6 instr · 16/32 on
instructions issued
12
if no divergence
6
lane efficiency
50%

Half the lanes take each path. The warp runs both, one after the other.

fig. 2.4One warp hitting a branch. Choose a branch condition and the length of each body, and see which lanes are active during each pass and how much of the warp's work is wasted.

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 blockIdx or warp index so all lanes of a warp agree.
  • Compute both sides and select. For short bodies, y = cond ? a : b compiles to a select or predicated instructions with no divergence. The attention mask in 1.3 works this way: scores are replaced by −∞-\infty with a select, not skipped with an if.
  • 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

repo · BGEBEFORE-GPU-ENGINEERINGBEFORE-GPU-ENGINEERINGPhase 1: warps, SIMT execution and branch divergence.
book · FREGLYAI Systems Performance EngineeringChris FreglyGPU architecture chapters: warp schedulers and independent thread scheduling on Volta and later.

Check your understanding

0 / 4 answered
  1. 1.How many threads are in a warp on NVIDIA GPUs?

  2. 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. 3.Which branch causes no divergence?

  4. 4.How is a causal attention mask usually applied inside a kernel?