Module 3 · Lesson 3 of 5
Matrix multiplication and tiling
Keep the answer fixed. Change how many times an operand has to travel.
One output, four products
Use a tiny invented projection: U [4 × 4] times W [4 × 4] gives Y [4 × 4]. Select an output cell. Reveal each product and accumulate it. Inputs here are illustrative numeric rows, not the learned embeddings from Module 2.
A dot product multiplies matching entries from a row and a column, then sums the products. An accumulator is that running sum. Each multiply-add adds one product to it. One FLOP is one floating-point arithmetic operation; we count a multiply-add as two FLOPs, even when one fused instruction performs it.
Tensor cores. A GPU’s ordinary arithmetic units do one multiply-add per thread per instruction. A tensor core (TC) is a separate hardware unit inside each SM built for matrix multiply-accumulate: in one operation it multiplies a small tile of one matrix by a small tile of another and adds the result into an accumulator tile, so one instruction performs many of the multiply-adds above. NVIDIA describes them as “specialized high-performance compute cores for matrix multiply and accumulate (MMA) math operations”; for example, it states that an A100 has four tensor cores per SM delivering 1,024 dense FP16 fused multiply-adds per clock per SM (Ampere blog). They are why a GPU’s peak dense TFLOP/s is so high, and the peak-compute ceiling P in lesson 3.4 assumes them. The catch: they only run when operands are laid out as the small tiles they expect, which is what tiling prepares. Code that does not use them runs at a much lower rate. This lesson’s scalar loops count FLOPs and do not use tensor cores.
Inspect products and sums
Y[0,0] = 1×1 + 2×0 + 0×2 + 1×1 = 2. Changing output cell changes the selected row and column. A fused multiply-add counts as 2 FLOPs even when implemented as one instruction.
Notation → dimensions → loops
Y[irow,jcol] = Σ over psum of U[irow,psum] × W[psum,jcol]. The reduction has kinner terms. There are rrows × ccols outputs. Conventional GEMM accounting uses F = 2rrowsccolskinner FLOPs (one multiply-add per term), even though a mathematical dot product needs only k − 1 additions if initialized from its first product.
for row in range(rows):
for col in range(columns):
acc = 0
for p in range(reduction):
acc += U[row][p] * W[p][col]
Y[row][col] = accFor the tiny example: 16 outputs × 4 terms × 2 FLOPs = 128 FLOPs. This scalar loop is a reference calculation, not a tensor-core implementation.
Load a tile, reuse its operands
Instead of independently reloading each dot product’s operands, compute a btile × btile patch of outputs together. One input tile and one weight tile supply many products. Tile size changes data reuse without changing the mathematical result.
A tile is a small rectangular portion of a matrix. The control chooses a square tile’s side length in elements. A reduction chunk covers part of the shared inner dimension: load two operand tiles, add their products to the output accumulators, then continue with the next chunk. The accumulators retain earlier partial sums until every chunk is complete.
The rule. A btile × btile tile of outputs needs each loaded input or weight element for btile of its outputs, so each loaded element is reused btile times and global loads fall by about btile×. In the lab below the operand loads go 128 → 64 → 32 for tile sides 1, 2, 4. The 16 output stores do not shrink, so total bytes fall by a little less than btile× (576 → 320 → 192 bytes).
Walk through load → barrier → reuse — follow exact partial sums and see when shared tiles are safe to overwrite.
Count work versus movement
For 4×4 matrices with 4-byte elements: tile side 1 issues 128 operand element loads plus 16 output stores = 576 bytes. Tile side 2 issues 64 loads plus 16 stores = 320 bytes. Tile side 4 issues 32 loads plus 16 stores = 192 bytes. Teaching numbers, not measurements.
Derive the traffic and storage
For square tiles whose side btile divides all dimensions, there are (rrows/btile)(ccols/btile) output tiles. Each streams kinner/btile pairs of input tiles. Each pair holds 2btile² elements. Total operand loads = 2rrowsccolskinner/btile. Add rrowsccols output stores, then multiply by element bytes.
Two single-buffered input tiles consume 2btile² × element bytes in shared memory. Accumulators also need registers. Doubling btile increases this shared storage fourfold; double buffering would consume more.
for each output_tile:
accumulators = 0
for each reduction_tile:
cooperatively load U_tile and W_tile
block_barrier()
accumulate products into registers
block_barrier() // finish before overwriting tiles
store output_tilePredict: A larger tile always runs faster?
Fewer loads means fewer bytes crossing the slow memory boundary even when the useful arithmetic is unchanged. Next, 3.4 turns that observation into a bound.
Model boundaries
Matrix product. The 4 × 4 inputs are invented rows, not learned embeddings from Module 2. The scalar loops count FLOPs and do not use tensor cores.
Tile traffic. The counts are logical global-memory demands of the teaching algorithm, not measured HBM traffic. The model omits caches, padding, register spills, synchronization cost and traffic between shared memory and registers; it assumes the old output is not read (the GEMM form Y = αUW + βY with β = 0). Edge tiles, tensor-core layouts and cache behavior change real results.