Module 3 · Lesson 1 of 5

GPU execution

Trace a projection from mathematical work to logical threads and physical resources.

Follow one output onto the GPU

Imagine a job that must calculate 1,000 output numbers. We will follow just one, then see how the GPU organizes that work. By the end, distinguish a work item, its group, and the hardware that runs it.

Two terms first

Kernel
The function the GPU runs. To launch a kernel is to start that same function once per work item (here, once per output number). Each copy knows its own number and uses it to pick its data.
SM
Streaming multiprocessor: one of the many independent compute units on a GPU chip. Each SM has its own registers and shared memory and runs the work assigned to it. A data-center GPU has on the order of a hundred (the H100 has 132; sources in 3.2).

Follow one output number

Outputs are numbered 0–999. For each output, read an input row and a weight column, multiply matching entries, add the products, and store the result.

Work groups (labels, not hardware)

Grid: the whole launch, 1,000 outputs
Block: 128 threads; 8 blocks here
Warp: 32 threads that run one instruction together; 4 per block
Thread: computes 1 output

Hardware (physical chip)

SM: runs the threads of the blocks assigned to itOne SM can hold several blocks at once.
Read inside out: threads form a warp, warps form a block, blocks form the grid. Dashed boxes are labels; the solid box is a chip component. The last block is partly empty because 1,000 is not a multiple of 128.

Follow output 999: its global thread number is 999. With 128 threads per block it belongs to block 7, thread 103 within that block, warp 3, lane 7. A lane is a thread’s position 0–31 within its warp. The block runs on one SM; which SM is unspecified. A thread is a logical run of the GPU function, not a dedicated physical core.

Regrouping into 64 threads per block preserves output 999, but changes its address to block 15, thread 39, warp 1, lane 7. The same 1,000 outputs still need computing; grouping does not create more SMs.

Check the distinction: A block contains 128 threads. Does that mean it needs 128 SMs?

Why a warp: 32 lanes, one instruction

An SM does not run a separate instruction stream for every thread. Its scheduler picks one warp (32 consecutive threads of one block) and issues (starts) a single instruction, say “multiply”, for all 32 lanes at once. Each lane applies it to its own data: lane 0 multiplies its row-and-column pair while lane 7 multiplies its own. This one-instruction, many-lanes scheme is called SIMT (single instruction, multiple threads). It is why neighbouring threads are cheap to run together, and it has two consequences for later lessons: lanes that want different paths hold each other up (below), and lanes that load neighbouring addresses can be served together (3.2).

A branch chooses a path, such as the true or false side of an if. If lanes in one warp choose different paths, this is divergence: each path runs with only its participating lanes active. An active lane mask records which lanes participate; branching does not create another warp.

Predict: Two branches of one warp take different paths. Do we get twice the execution resources?

Open the visual GPU walkthrough — the same example (1,000 outputs, 128 threads per block, output 999) and the same order (thread, block, warp, SM) on an illustrated chip, then return to this lesson.

Keep this picture for the next lessons: neighboring lanes can request neighboring data; threads in a block can reuse shared memory; an SM has limited room for those blocks and their working storage.

Connect the picture to matrix multiplication

The 1,000 outputs are the entries of one matrix product, a GEMM (general matrix multiply): U [rrows × kinner] multiplies weights W [kinner × ccols] to produce Y [rrows × ccols]. These GPU lessons use the letters U, W and Y for any such product. In an LLM they stand for the Module 2 objects you already know:

Same projection, two vocabularies
GPU lessonsIn an LLMMeaning
UXToken activations: one row per token processed in this step
rrowstokens in this stepMany during prefill; one per active sequence in an ordinary decode step (3.5)
kinnerdmodelWidth of each token vector; the length of every dot product
WWQLearned weights, here the query projection [dmodel × dk]; WK and WV work the same way
ccolsdkColumns of that weight matrix: the width of each output row
YQThe projection result: one query row per token

So “output 999” means one number of one token’s query vector: its row says which token and its column says which query dimension (for instance, with ccols = 10 columns, output 999 is row 99, column 9, as in the visual walkthrough). Each output is a dot product. Different outputs can be computed independently once the inputs exist.

Y[irow, jcol] = Σ over psum from 0 to kinner − 1 of U[irow, psum] × W[psum, jcol]. Here irow selects a row, jcol a column, and psum walks through the shared width.

Before trying a larger launch

Grid
All the blocks in that launch.
ceil
Round up: leftover outputs still need another whole block.
Guard
A check that skips an out-of-range thread before it reads inputs or stores an output.

Try a separate launch

This experiment changes the total output count and inspects the final warp. Its settings are separate from the selected-output walkthrough above.

Example: 1,000 output elements with 128 threads/block need 8 blocks. The last warp has 8 valid output lanes and 24 guarded lanes. A block stays on one SM during ordinary execution; a warp contains 32 CUDA threads.

Indexing pseudocode and scheduling details

A thread is not a dedicated physical core. Ready warps can issue (start) instructions while others wait on dependencies or loads. Blocks need not start together or execute in numerical order. This ordinary mapping keeps a block on one SM; it does not specify which SM or when it runs.

// Schematic mapping, not an optimized CUDA GEMM.
flat = blockIdx.x * blockDim.x + threadIdx.x;
if (flat < rows * columns) {
    row = flat / columns; col = flat % columns;
    acc = 0;
    for (p = 0; p < reduction; ++p)
        acc += U[row][p] * W[p][col];
    Y[row][col] = acc;
}

The multiply-add loop has an accumulator dependency. Parallel output elements do not eliminate that dependency. Optimized kernels distribute work differently and can use tensor cores (3.3); this one-thread-per-output sketch explains indexing only.

Enough work is not enough resources

A block needs registers and shared memory to reside on an SM. A larger block or more storage per block can reduce how many warps are resident. Before counting how many fit, see why having several matters.

Latency hiding: why many resident warps help

Latency is the wait between asking for something and getting it. A load from device memory takes many clock cycles to return, and during that time the warp that asked cannot do its next multiply. A warp scheduler is the SM hardware that picks, each cycle, a warp that is ready and issues (starts) its next instruction. If another resident warp is ready while warp A waits, the scheduler runs that one instead. This is latency hiding: the wait is not shortened, it is overlapped with other warps’ work. Switching costs almost nothing because every resident warp’s registers stay allocated on the SM, which is why residency is limited by registers and shared memory.

Try the timeline. Each warp computes for 2 ticks, then waits 8 ticks for memory. With one warp the math units sit idle most of the time. About (2 + 8) / 2 = 5 warps cover one wait.

Hide a memory wait with other warps

Teaching numbers in abstract ticks, not clock cycles. Warp A computes for 2 ticks, then waits 8 ticks for a load. Alone, it keeps the math units busy 4 of 20 ticks (20%). With 5 resident warps, B to E run while A waits, and every tick does math (100%).

So residency is not the goal by itself: it is the supply of other warps to switch to. That is what the occupancy number below counts. Enough warps to cover the wait is useful; more than that adds nothing in this timeline.

Occupancy: how many warps fit

More resident warps may help hide latency, as above, but occupancy alone does not measure useful throughput.

Resident means currently allocated space on an SM, whether ready to run or waiting. Registers hold a thread’s working values, such as its running sum. Shared memory holds values cooperating threads in one block can reuse. Both have limited space.

Occupancy is the resident warp count divided by the SM’s maximum resident warp count. The toy SM below has 64 warp slots: 8 resident warps means 8 / 64 = 12.5% occupancy. This counts allocated warps, not how busy they are. KiB means 1,024 bytes; each 32-bit register holds 4 bytes.

Change one resource limit

Toy SM: 2,048 resident threads, 65,536 32-bit registers, 64 KiB shared memory, at most 8 blocks. With 128 threads/block, 64 registers/thread and 32 KiB/block, shared memory limits residency to 2 blocks (8 warps, 12.5% occupancy).

Predict: Double registers/thread. Must throughput improve?

Model boundaries

Mapping. One thread per output is an indexing sketch; optimized matrix kernels divide the work differently. The thread and launch labs simulate no scheduling: which SM runs a block, and when, is unspecified.

Latency timeline. Abstract ticks, not clock cycles; one issue slot and no other bottleneck.

Residency. The limits are invented and independently rounded. Real occupancy includes allocation granularity, architecture-specific limits and compiler decisions; the diagram ignores scheduling policy, issue width, instruction latency and register spilling. Too few blocks in the grid can leave SMs idle even when resource occupancy is high.

Registers hold per-thread state such as the accumulator. More registers can avoid spills or enable reuse; reducing them blindly can add device-memory traffic. Search for a causal trade-off, not maximum occupancy.

Module 3 overview · Revisit KV memory