Module 13 · Lesson 24 of 98

Roofline and register blocking for GEMM

Use a roofline model to explain tiled GEMM and calculate how a register block reduces shared-memory reads.

  • Beginner to advanced
  • 7 min read
  • 15 min lab
What you will need

Ubuntu 24.04 in WSL2 or a VM, plus a calculator or scratch paper. The practice requires no GPU.

Your place in the module
By the end you can 4 objectives
  • Calculate GEMM arithmetic intensity from operation counts and byte traffic.
  • Classify a GEMM model using the roofline ridge point.
  • Trace operand reuse in a 2 by 2 per-thread register block.
  • Explain how register pressure can offset the benefit of register blocking.
Video coming soonRoofline and register blocking for GEMM
The video for this lesson is being recorded. Read the lesson below in the meantime.
Recall first from Occupancy and what limits it
  • Theoretical occupancy is the maximum resident warp count for a launch divided by the hardware maximum per SM.
  • Registers, shared memory, resident threads, and resident blocks can each constrain how many blocks fit on an SM.
  • The CUDA occupancy API takes the total thread count per block and the dynamic shared-memory bytes per block.
01

What tiling changes on the roofline

4 min read

You know how many blocks fit. This lesson answers how tiling moves GEMM from memory-bound toward compute-bound and how register blocking reuses values further.

Suppose a program repeatedly multiplies matrices of the same size. The untiled and tiled kernels produce the same number of output elements and do the same count of useful arithmetic. Yet their execution times can differ because the tiled kernel can fetch fewer input bytes from DRAM. To explain that difference, count the work and traffic over the same kernel invocation. If you measure the whole program from the Linux host, that elapsed time can also include allocation, submission and waiting. A host timer using CLOCK_MONOTONIC can measure that broader interval, and perf stat can collect statistics for the command. The roofline calculation here uses GPU work and GPU memory traffic. clock_gettime(2), perf-stat(1)

Use the GEMM case C = A × B, with A sized M × K, B sized K × N, and C sized M × N. Each output is a dot product of length K. Starting the accumulator at zero and doing one fused multiply-add per inner-loop iteration gives 2MNK floating-point operations, or FLOPs. The convention counts a multiply and an add even when one instruction performs both. Use FP32 throughout this example. Each matrix value occupies four bytes, and this form of GEMM writes each C element without reading an old value.

Here the boundary is DRAM. If I is the intensity in FLOP/byte, B is the DRAM bandwidth in bytes per second, and P is the compute ceiling in FLOP/s for the chosen arithmetic path, the roofline gives the upper bound min(B × I, P). Below the intersection, called the ridge point, increasing intensity increases the bandwidth-limited bound. The ridge is at I = P / B. Above it, additional intensity leaves the bound at P.

Roofline regimesBelow ridgeAt ridgeAbove ridgeIntensityI < P/BI = P/BI > P/BThroughput boundB × IP = B × IPHigher intensityRaises boundtoward PBound staysat PBound staysat P
Compare the kernel's DRAM intensity with the ridge point to find the smaller throughput bound.

In the untiled model, one thread computes one output and requests two FP32 operands at each inner-loop step. Assume each request reaches DRAM and count one final store per output. That gives 8MNK + 4MN bytes for 2MNK FLOPs, or 2K / (8K + 4) FLOP/byte. For large K, the store contributes little to the ratio, and intensity approaches 0.25 FLOP/byte. This is a traffic model with a stated cache assumption. If input requests hit in cache, fewer bytes reach DRAM and the actual DRAM intensity is higher than this estimate.

Now count a full T × T output tile. Assume M, N and K are multiples of T, and count each block's staged input bytes as DRAM traffic. For one step covering T values of K, the block brings in T² values from A and T² from B, for 8T² bytes. Each staged A value contributes to T output columns, and each staged B value contributes to T output rows. The block performs T³ fused multiply-adds, or 2T³ FLOPs. Across all K/T steps, the input traffic is 8TK bytes. The final output adds 4T² bytes, giving intensity 2K / (8K/T + 4) FLOP/byte. With T fixed and K large, this approaches T/4 FLOP/byte.

For an arithmetic example, assume a compute ceiling of 1,000 GFLOP/s and a DRAM bandwidth of 200 GB/s, using decimal units. These are model inputs. They give a ridge of 5 FLOP/byte. The large-K untiled model has a bound of 50 GFLOP/s. With T = 16, the tiled model approaches 4 FLOP/byte and its bound approaches 800 GFLOP/s. It remains on the bandwidth-limited side, much closer to the ridge. A version that achieved 6 FLOP/byte at the same bandwidth and compute ceiling would be compute-limited in this model. Measured throughput can fall below either bound because the model assumes the kernel can sustain the limiting rate. Synchronization and instruction overhead can still matter.

02

Reuse operands within a thread

5 min read

Shared-memory tiling lets several threads use a value that the block staged once. A thread can also reuse values after it loads them from shared memory. Assign it a small rectangular group of outputs and give it one accumulator for each output. This is register blocking, a form of thread coarsening. The block still owns an output tile. Within that tile, each thread now owns several elements and keeps its running sums live until the final stores.

Consider a thread that owns rows r0, r1 and columns c0, c1 of the output tile. At a single inner-loop index k, it reads A[r0,k] and A[r1,k] from the staged tile into two operand variables. It also reads B[k,c0] and B[k,c1] into two more. Call these values a0, a1, b0 and b1. The thread accumulates the four products a0*b0, a0*b1, a1*b0 and a1*b1 into c00, c01, c10 and c11, respectively. Each loaded value appears in two products. At the next k, the operands change while the four accumulators carry their sums forward.

One thread, four updatesB operandsb0b1a0c00 +=a0*b0c01 +=a0*b1a1c10 +=a1*b0c11 +=a1*b1Each operand is used twice per k.
At one k index, each loaded A operand is paired with both loaded B operands.

With four independent one-output threads, the same four updates require eight logical shared-memory operand reads, two per thread. The register-blocked thread needs four logical reads per k for the same updates. These counts describe operands requested by threads. Hardware can serve some identical shared-memory addresses through broadcast, so the count alone gives neither the number of physical transactions nor the speedup. For an R × S group of outputs at a given k, the count generalizes to R + S operand reads and R × S fused multiply-adds. Each A operand feeds S updates and each B operand feeds R updates.

Compare kernels with the same block output footprint and the same staged input traffic. In that comparison, the DRAM arithmetic intensity is the same because both useful FLOPs and DRAM bytes are the same. Register blocking improves reuse between shared memory and registers. This can matter even when the DRAM roofline predicts a compute-limited kernel. The prediction describes the smaller of two bounds, while shared-memory delivery or instruction issue can keep measured performance below both.

The extra accumulators have a cost. A 2 × 2 group keeps four running sums per thread instead of one, and operand values and indices also need storage. The compiler determines the resulting register count. A larger group can help when operand reads limit the kernel, but it can also reduce the number of resident warps or lead to spills. Spill loads and stores add traffic and instructions. Compare variants for the same matrix dimensions and verify the output before comparing timings. When a larger group slows the kernel despite cutting logical shared-memory reads, register use and spills are concrete places to investigate.

Going deeperExplore the mechanism

A different compute ceiling on Tensor Cores

A CUDA-core tiled kernel issues scalar fused multiply-adds per lane. On supported NVIDIA GPUs, Tensor Cores execute matrix multiply-accumulate operations cooperatively across a warp. CUDA C++ exposes this path through WMMA. Lanes collectively load matrix fragments, call wmma::mma_sync, and store the result fragment. PTX also exposes mma.sync forms with operands arranged in lane registers according to the chosen instruction shape. The tiling principle still applies because the warp needs a supply of input fragments.

For a concrete operation count, a WMMA multiply-accumulate with shape 16 × 16 × 16 updates 256 accumulator elements using 16 products per element. This is 4,096 fused multiply-adds, or 8,192 FLOPs, for the collective operation. The shape describes the work of the WMMA operation. The compiler's instruction sequence and the execution rate depend on the GPU and data types. Dedicated matrix hardware and the amount of arithmetic per collective operation can raise the compute ceiling considerably. Narrow input formats, such as FP16 with FP32 accumulation on supported configurations, can also reduce input bytes compared with FP32 inputs. FP32 accumulation still uses the values represented by the narrower inputs.

A large speedup, often several times, is possible against a CUDA-core tiled implementation when the Tensor Core implementation sustains high throughput on a large, suitable GEMM. The operation count of a WMMA call alone cannot establish that speedup. Compare the same matrix dimensions, document the input and accumulation precision, and check that the results meet the same error tolerance. In roofline terms, raising P also raises P/B. If the new implementation leaves intensity fixed, bandwidth can become the limiting bound before the kernel gets close to the higher compute ceiling.

If a GEMM still falls short of its predicted throughput after you account for both kinds of reuse, look at how the block organizes its input loads and computation. The next lesson, Reduction from a block to the whole grid, moves from matrix products to a different pattern: combining many values into one result across all the blocks of a grid.

Key takeaways

  • Shared-memory tiling increases DRAM arithmetic intensity by reusing input values across outputs.
  • The roofline bounds throughput by the smaller of the compute ceiling and bandwidth times arithmetic intensity.
  • A 2 by 2 register block can perform four fused multiply-adds per four logical shared-memory operand reads at each inner-loop step.
  • Register blocking can reduce shared-memory reads even when the DRAM byte count stays fixed.
Reflect
  1. Explain to a colleague, in two sentences: calculate GEMM arithmetic intensity from operation counts and byte traffic.
  2. Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
  3. What surprised you when you ran the commands yourself?
On a real board

Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.

References and further reading

Find your next step

↑ ↓ to move · Enter to open · Esc to close