Before you start: words you need#
| Word | Plain meaning |
|---|---|
| Kernel | A function that runs on the GPU. One “launch” runs it on many threads at once. |
| Thread | One running copy of the kernel, working on its own piece of data, with its own registers. |
| Warp | 32 threads that the hardware executes together, one instruction for all 32 at a time. |
| Block (thread block, CTA) | A group of up to 1,024 threads that run on the same SM and can share fast memory. |
| Grid | All the blocks of one kernel launch. |
| Lockstep | Everyone does the same step at the same moment. |
| SIMD / SIMT | Single instruction, multiple data (3.d): one instruction applied to many numbers. NVIDIA calls its version SIMT — single instruction, multiple threads. |
1. What is it?#
The way CUDA organises parallel work into four levels:
GRID all the work for one kernel launch
├─ BLOCK 0 runs entirely on ONE SM; its threads can cooperate
│ ├─ WARP 0 32 threads executing in LOCKSTEP ← what the hardware actually runs
│ │ ├─ thread 0
│ │ ├─ thread 1
│ │ └─ ... thread 31
│ ├─ WARP 1
│ └─ ...
├─ BLOCK 1
└─ ...The warp is the unit that matters. Threads are how you write the program; warps are what the hardware schedules and executes.
Diagram — how a launch breaks down#
flowchart LR K["Kernel launch"] --> G["Grid<br/>all the blocks"] G --> B["Block<br/>runs on one SM, shares memory"] B --> W["Warp<br/>32 threads in lockstep"] W --> T["Thread<br/>one lane, own registers"] class K neutral class G io class B queue class W compute class T memory
2. Why does it exist?#
You need a way to describe “do this to a million elements” that maps naturally onto both the problem and the hardware. Each level corresponds to a physical reality from 4.a:
| Level | Hardware it maps to | What it gives you |
|---|---|---|
| Thread | one lane of a 32-wide arithmetic unit | its own registers and its own data element |
| Warp | one warp scheduler issuing one instruction | the real unit of execution |
| Block | the resources of one SM | shared memory and the ability to synchronise |
| Grid | all SMs | scale across the whole chip |
The grid is deliberately loose: blocks run in any order on any SM, and cannot wait for each other. That freedom is what lets the same program run on a 20-SM laptop GPU and a 148-SM B200.
3. Simple analogy#
An army on parade.
A warp is a squad of 32 soldiers who march in perfect lockstep. If the order is “go left” but some soldiers should go right, the squad first goes left while the right-goers stand still, then goes right while the left-goers stand still. That is warp divergence, and it costs exactly what it sounds like (6.c).
A block is a platoon assigned to one building (an SM). The platoon shares a supply room
(shared memory) and can coordinate (“everyone wait here” = __syncthreads()).
The grid is the whole operation. Platoons are sent to buildings as buildings become free; they cannot talk to each other.
4. Tiny example#
The “hello world” of CUDA adds two arrays:
__global__ void add(const float* a, const float* b, float* c, int n) {
int i = blockIdx.x * blockDim.x + threadIdx.x; // which element am I?
if (i < n) c[i] = a[i] + b[i]; // don't run past the end
}
int main() {
int n = 1'000'000;
int threads = 256; // threads per block
int blocks = (n + threads - 1) / threads; // round up
add<<<blocks, threads>>>(d_a, d_b, d_c, n); // launch: grid of blocks
}blockIdx.xis this block’s number in the grid,blockDim.xthe threads per block,threadIdx.xthis thread’s number in its block. Together they give each thread a unique element index.- The
if (i < n)guard is needed because the number of threads is rounded up.
n = 1,000,000, threads = 256
blocks = 3,907
total threads = 3,907 × 256 = 1,000,192 (192 threads do nothing, thanks to the guard)
warps = 1,000,192 / 32 = 31,256Lesson 5.b writes, compiles and runs this kernel end to end.
5. Technical explanation#
SIMT: threads are a convenient fiction, warps are real#
How you write it: 32 independent threads
What the hardware does: 1 instruction stream, 32 data lanes, a mask saying which lanes are activeConsequences:
- Threads in a warp cannot truly go separate ways; the hardware runs each path in turn with the other lanes masked off. Since Volta (2017) each thread has its own program counter, which makes some synchronisation patterns legal, but execution is still warp-wide.
- Warp-level instructions (
__shfl_sync,__ballot_sync,__reduce_add_sync) let the 32 threads exchange values directly from each other’s registers in a few cycles. Fast reductions — the sums inside softmax and normalisation — are built on them.
Warpgroups: the unit for modern matrix maths#
On Hopper, the big tensor-core instruction (wgmma) is issued by a warpgroup — 4 warps,
128 threads — working on one large matrix tile together. On Blackwell, a single thread can issue
tcgen05.mma, and the results go into tensor memory (4.a, 4.f). You do not need to program these
by hand; libraries (cuBLAS, CUTLASS, FlashAttention, 6.e) do. But when a profile or release note
says “warp-specialised kernel”, it means different warps in a block are given different jobs —
some load data, some do maths — instead of all doing the same thing.
Choosing a block size#
Hard rules:
- a multiple of 32 (otherwise the last warp has idle lanes)
- at most 1,024 threads per block
- shared memory per block: 48 KB by default, up to ~227 KB on Hopper and Blackwell with opt-in
Practical guidance:
128 or 256 threads suits most kernels.
Bigger blocks: more threads share one block's shared memory (more reuse).
Smaller blocks: finer load balancing across SMs.Occupancy, in one paragraph#
An SM can hold up to 64 warps (2,048 threads) at once, but only if each block’s registers and shared memory fit. Occupancy is the fraction of those 64 slots actually filled. More resident warps give the scheduler more choices when some are waiting on memory (section 6). Lesson 6.c does the full arithmetic — registers per thread, shared memory per block, and why 100% is not always the goal.
Grid size and waves#
The GPU runs as many blocks as fit on all SMs at once — one wave — then the next wave as blocks finish.
H100: 132 SMs, say 4 blocks fit per SM → 528 blocks per wave
Grid of 500 blocks: one wave, 95% of slots used. good
Grid of 530 blocks: two waves; the second runs only 2 blocks. ~half the chip idle for a whole wave
Grid of 5,000 blocks: ~9.5 waves; the partial last wave costs ~5%. fineRule: make the grid either much larger than one wave (so the tail does not matter) or close to an exact number of waves. The bad case is “slightly more than one wave”.
This matters directly for inference. A decode attention kernel for batch 8 with 32 heads has a natural grid of 8 × 32 = 256 blocks — under two waves on a 132-SM GPU, with most SMs idle in the second. That is why decode attention kernels split each head’s work along the sequence (“split-K” or “flash-decoding”, 11.f, 13.l): to create enough blocks to fill the chip.
Thread block clusters (Hopper and later)#
Since Hopper, blocks can be grouped into clusters that are guaranteed to run at the same time on neighbouring SMs and can read each other’s shared memory (distributed shared memory). Blackwell extends this so two SMs can share one matrix operation. Clusters let a kernel work on bigger tiles than one SM’s shared memory holds; FlashAttention-3/4 and modern GEMM libraries use them.
__global__ void __cluster_dims__(2, 1, 1) my_kernel(...) { ... } // pairs of blocks
6. Under the hood#
What a warp scheduler does every clock cycle:
1. Look at all its resident warps (up to 16 per scheduler, 4 schedulers per SM)
2. Find those whose next instruction has its inputs ready
(not waiting for a memory load, not waiting at a barrier)
3. Pick one
4. Issue its instruction to the right unit (FP32 lanes, tensor core, load/store, SFU)If no warp is ready, the scheduler issues nothing: a stall cycle. A load from HBM takes roughly 500–800 cycles; with 16 warps per scheduler, there is usually another warp with work to do while one waits. That is the GPU’s whole strategy for memory latency: not avoiding it like a CPU’s caches, but hiding it behind other warps.
Nsight Compute’s “Warp State Statistics” shows why warps were stalled (“long scoreboard” =
waiting for memory, “barrier” = waiting at __syncthreads()); 6.d shows how to read it.
7. Performance implications#
Problem Typical cost
Block size not a multiple of 32 up to 31 of 32 lanes idle in the last warp
Too few resident warps memory latency exposed; 20–50% slower (6.c)
Partial last wave up to ~50% on small grids
Warp divergence up to 32x on the divergent part (6.c)
Fewer blocks than SMs whole SMs idle
Unnecessary __syncthreads() warps wait for each other8. Production implications#
You will rarely write kernels in production, but you will read profiles that speak this language. “Achieved occupancy 23%”, “grid smaller than one wave” or “warp execution efficiency 40%” turn a profile from noise into a diagnosis (6.d, 22.h). And when you do write a kernel — a custom fused operation, a new attention variant — these are the constraints you design against.
9. Common mistakes#
Block sizes that are not a multiple of 32.
Forgetting the bounds guard. Out-of-bounds writes corrupt memory silently.
Assuming all threads in a block run at the same moment. They run as warps, scheduled
independently. Only __syncthreads() guarantees an order.
Calling __syncthreads() inside an if that not all threads reach. Undefined behaviour; the
kernel can hang.
Assuming blocks can wait for each other. They cannot (except within a cluster, or with special cooperative launches); design so they never need to.
10. Hands-on exercise#
A. Vector add. Write, compile and run the kernel from section 4 (5.b shows the build). Check the result. Sweep the block size over 32, 64, 128, 256, 512 and 1,024 and plot bandwidth. Explain the shape.
B. Waves. Write a kernel where each block does a fixed amount of work and launch it with grid sizes from 1 to 4 × the number of SMs. Plot time against grid size and find the wave boundaries.
C. Warp shuffle. Implement a sum over a block using __shfl_down_sync within each warp and
shared memory across warps. Compare it with a version that uses only shared memory.
D. Decode grid. For a decode attention kernel with batch size B and H heads on your GPU, how many blocks does one-block-per-head launch? How many waves? How many splits along the sequence would you need to fill the chip?
11. Interview questions#
- Explain the thread, warp, block and grid hierarchy and what each maps to in hardware.
- What is SIMT, and how does it differ from the thread abstraction it presents?
- How does a GPU hide memory latency, and what does that require from the program?
- What is a wave, and why is a grid slightly larger than one wave a problem?
- Why do decode attention kernels split work along the sequence dimension?
- What is a thread block cluster, and what does it enable?
12. Further reading#
Checked on 11 November 2026.
- [REFERENCE] CUDA C++ Programming Guide — programming model, thread block clusters, compute capabilities
- [REFERENCE] CUDA C++ Best Practices Guide — execution configuration
- [ESTABLISHED] Volkov, “Better Performance at Lower Occupancy” (GTC 2010) — the classic argument that occupancy is a means, not a goal
- [ESTABLISHED] Flash-Decoding for long-context inference — splitting along the sequence to fill the GPU