L2 Intermediate · Module 6: GEMM and memory optimization

6.2Register tiling: the tile inside the tile

The shared-memory tiled matmul spends more time loading from shared memory than multiplying. Register tiling fixes that: each thread computes a small block of outputs, held in registers, and reuses every value it loads many times. Three changes take the kernel from 15% to 94% of cuBLAS.

GPU required (Colab or a rented GPU)~30 min

By the end you will be able to

  • ✓Count shared-memory loads per FMA for a kernel, and explain why that ratio limits it
  • ✓Write 1D and 2D register-tiled matmuls, where each thread computes an outer product per k-step
  • ✓Use float4 loads and a transposed shared-memory layout to cut instructions further
  • ✓Read registers per thread and blocks per SM, and connect them to occupancy

6.1 ended with a kernel limited by shared memory. Look at its inner loop:

for (int k = 0; k < 32; k++) acc += As[ty][k] * Bs[k][tx];

Each iteration is two shared-memory loads and one FMA. An Ampere SM can issue 128 FP32 FMAs per clock, but only 32 shared-memory load lanes per clock. The FMA units spend most of their time waiting.

The fix is the same idea as shared-memory tiling, one level down: reuse values from registers.

The outer product

Suppose a thread computes a TM×TNT_M \times T_N block of outputs instead of one. At each kk, it needs a column of TMT_M values of AA and a row of TNT_N values of BB. Every pair multiplies into one output:

acc[i][j]+=ai⋅bjfor all i<TM, j<TN\text{acc}[i][j] \mathrel{+}= a_i \cdot b_j \quad \text{for all } i < T_M,\ j < T_N

That is an outer product: TM+TNT_M + T_N loads feed TM⋅TNT_M \cdot T_N FMAs.

Per threadloads per kFMAs per kFMAs per load
1 output (6.1)210.5
8 outputs in a column (1D)980.9
8 × 8 outputs (2D)16644

1D, then 2D

1D tiling gives each thread 8 outputs down one column. A block computes a 64 × 64 tile with 512 threads. Each thread loads one value of BB and reuses it 8 times from a register.

2D tiling gives each thread an 8 × 8 square. A block computes a 128 × 128 tile with only 256 threads, and each thread keeps 64 accumulators, 8 values of AA and 8 of BB in registers:

// labs/m06/6.2/register_tiling.cu, mm_2d (inner loop, per k-step of 8)
for (int k = 0; k < BK; k++) {
for (int i = 0; i < TM; i++) regM[i] = As[(threadRow * TM + i) * BK + k]; // 8 loads of A
for (int j = 0; j < TN; j++) regN[j] = Bs[k * BN + threadCol * TN + j]; // 8 loads of B
for (int i = 0; i < TM; i++)
for (int j = 0; j < TN; j++) acc[i * TN + j] += regM[i] * regN[j]; // 64 FMAs
}

Everything here is fully unrolled (#pragma unroll), so acc, regM and regN become plain registers. That only works because every index is known at compile time; an array indexed by a runtime value would be placed in slow local memory instead (6.3).

Then: wider loads

Two more changes finish the job:

  • float4 global loads. Each thread brings in 16 bytes of AA and 16 of BB per k-step with one instruction each.
  • Store AA transposed in shared memory. The inner loop reads 8 consecutive values of AA for one kk. If AA‘s tile is stored kk-major (As[k][m]), those 8 values are adjacent, and the compiler can load them with two 16-byte LDS.128 instructions instead of eight 4-byte ones.

Measured

output (RTX 3050 6GB Laptop)
C = A * B, 4096 x 4096 FP32:
kernel time GFLOP/s of cuBLAS regs blocks/SM max error
tiled (6.1) 202.79 ms 678 14.9% 38 1 3.5e-04 PASS
1D tiling 68.16 ms 2016 44.3% 44 2 3.5e-04 PASS
2D tiling 46.22 ms 2973 65.3% 145 1 3.5e-04 PASS
2D + float4 32.04 ms 4290 94.2% 109 2 3.5e-04 PASS
cuBLAS 30.18 ms 4554 100.0%

1D tiling: 3×. 2D: another 1.5×. Vectorizing: another 1.4×. The last kernel is about 50 lines of CUDA, running at 94% of NVIDIA’s library.

Look at the regs column. The 2D kernel needs 145 registers per thread, so only one 256-thread block fits in an SM’s 64 K registers. The vectorized kernel, with fewer address computations, needs 109, and two blocks fit. Registers are a budget shared by every thread on the SM, and 6.3 is about spending it.

The last 6%

What is cuBLAS doing that this kernel doesn’t?

  • Double-buffering. While computing on one shared-memory tile, it loads the next. Our kernel waits at a barrier for every load.
  • Warp tiling. It arranges threads so that each warp covers a compact rectangle, which keeps shared-memory reads conflict-free and lets more values be broadcast.
  • Autotuning. It picks tile sizes per GPU and per matrix shape. Our 128 × 128 tile is tuned to nothing in particular.

For FP32 on CUDA cores that last 6% is rarely worth the effort. The bigger lever is moving to tensor cores (6.7).

Lab: register tiling 🔴

Terminal window
cd labs/m06/6.2 && nvcc -O2 -arch=native -DUSE_CUBLAS register_tiling.cu -o register_tiling -lcublas && ./register_tiling

Sources

Check your understanding

0 / 4 answered
  1. 1.A thread computes an 8 × 8 block of outputs. Per k-step, how many values does it load and how many FMAs does it do?

  2. 2.Why must the per-thread accumulator array be indexed only with compile-time constants?

  3. 3.Why store A's tile transposed (k-major) in shared memory?

  4. 4.The 2D kernel uses 145 registers per thread. How many 256-thread blocks can an SM with 65,536 registers hold?