Hardware and warps
The launch syntax hides a machine. <<<blocks, threads>>> becomes blocks placed on streaming multiprocessors, blocks split into warps, and warps issued instruction by instruction. This chapter is the tour.
The hierarchy#
The terms to keep straight:
- SM (streaming multiprocessor) — the unit that executes blocks. It has a register file, a unified L1/shared-memory store, and several schedulers.
- GPC — a cluster of SMs on the die. Blocks in a thread block cluster (Chapter 15) are guaranteed to land in one GPC.
- Warp — 32 threads that are issued together. Every thread block is split into warps in linear order.
- Lane — a thread's position inside its warp, 0–31.
A block runs entirely on one SM. A grid is spread across as many SMs as the hardware can staff.
SIMT: one instruction, many threads#
A warp executes in the Single Instruction, Multiple Threads model. All 32 lanes share a program counter and fetch the same instruction. Each lane has its own registers and its own predicate, so each can take a different branch — but not simultaneously.
When lanes disagree about a branch, the warp serialises the paths:
__global__ void threshold(float* out, const float* in, int n) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i >= n) return;
if (in[i] > 0.0f) {
out[i] = sqrtf(in[i]); // path A
} else {
out[i] = 0.0f; // path B
}
}If half the lanes in a warp have positive input, the warp executes path A with the other half masked off, then path B with the first half masked off. Twice the instruction issue for the same 32 outputs.
Push the widget between lane % 2 and all lanes. Divergence is not an error — the answer is still correct — it is a utilisation tax. A warp with 32 different paths could take 32 passes.
Modern compilers are smart about small branches: a two-instruction if/else is often converted to predicated instructions (both sides computed, one selected), which costs no extra passes. Divergence hurts when the branches contain loops, function calls, or substantial work.
Hiding latency with warps#
Global memory takes hundreds of cycles to answer. A CPU hides that with caches and speculation; a GPU hides it with other warps. Each SM keeps many warps resident. When one warp stalls on a load, the scheduler issues an instruction from another warp. This is why occupancy — how many warps are resident — matters more than per-thread speed.
The arithmetic is simple: if a load has 500 cycles of latency and the SM can switch between 16 warps that each have one instruction ready, the latency is covered. If only two warps are resident, the SM sits idle.
That is the whole reason to care about register usage and block size: they determine how many blocks (and therefore warps) fit on an SM. Chapter 13 has the calculator.
Warp-level thinking, without relying on lockstep#
For most kernels you can ignore warps entirely. They become relevant when you:
- Read memory. Coalescing is decided per warp: 32 lanes touching one 128-byte region is four transactions; 32 lanes touching 32 separate regions is 32 transactions.
- Use shared memory. Bank conflicts are decided per warp.
- Use warp primitives.
__shfl_down_sync,__ballot_sync, and__reduce_add_syncexchange values between lanes without shared memory. - Tune divergence. Reordering work so neighbouring lanes share a branch can be a large win.
One warning: since Volta, threads in a warp have independent thread scheduling. Lanes may progress at different rates, and a warp is not guaranteed to be in lockstep at every instruction. Code that assumed implicit lockstep is wrong. If lanes need to rendezvous, say so:
__syncwarp(); // mask defaults to the full warp__syncwarp() is cheap and explicit. __syncthreads() synchronises the whole block, which is a much stronger (and slower) operation.
The block/warp relationship#
A block of 256 threads is eight warps. A block of 40 threads is two warps: the first with 32 active lanes, the second with 8 active lanes and 24 permanently idle. Those idle lanes still consume warp slots and issue cycles.
That is why block sizes are multiples of 32. The hardware will not stop you from launching 33 threads, but you have paid for 64.
blockDim.x = 40
warp 0: lanes 0–31 ← full
warp 1: lanes 0–7 ← 24 lanes idle for the whole kernelWhat this means for your code#
- Launch blocks that are multiples of 32 threads, usually 128–256.
- Keep neighbouring lanes on neighbouring data.
- Prefer branch-free arithmetic for tiny conditions; restructure big divergent branches.
- Give the SM enough warps to hide memory latency.
- Never depend on blocks running in a particular order or at the same time.
The next chapter is the other half of performance: where those warps read and write, and why "global memory" is not one thing.
Exercises#
- Launch a block of 40 threads. Print
threadIdx.xandthreadIdx.x / 32. How many warps run? How many lanes are idle? - In the divergence widget, switch to all lanes and compute the speedup in issued lane-slots: 64 vs 32. When would the divergent version still be acceptable?
- Rewrite
thresholdso each warp handles a contiguous range and the predicate tends to be uniform within a warp. (Hint: sort or bucket the input first.) - Why can a thread block never span two SMs? What would break if it could?
- Look up
__ballot_syncand use it to count how many lanes in a warp satisfyin[i] > 0without shared memory or atomics.