What is Loop blocking?

An optimization that processes large arrays in small blocks that fit in the cache so loaded data is reused many times before eviction. Very effective for matrix multiplication.

Field: Memory Hierarchy

Japanese name: ループブロッキング

Why naive matrix multiplication misses

Consider C = A × B for N × N matrices of doubles with the textbook triple loop. Each element of C needs a full row of A and a full column of B, and walking down a column of B in row-major storage touches a different cache line for every element. When N is large, a column of B and the rows of A do not stay in cache long enough to be reused, so the same data is reloaded from main memory again and again. These are capacity misses: making the cache more associative does not help, because the working set is simply too big.

How blocking works

Loop blocking, or tiling, splits each matrix into b × b tiles and reorders the loops so that one tile product, C-tile += A-tile × B-tile, is completed while all three tiles are resident in cache. Each loaded element is then reused about b times before eviction, and memory traffic falls from on the order of N³ elements to about N³/b. The arithmetic and the total memory footprint are unchanged; only the order of operations changes. Arithmetic intensity, operations per byte moved, rises roughly in proportion to the tile size.

Choosing a tile size

A first estimate is that three tiles must fit in the target cache: 3 × b² × 8 bytes for doubles. For a 32 KB L1 cache that gives b of about 36, so a tile of around 32 is a natural choice, leaving room for other data. In practice, conflict misses from power-of-two strides, TLB reach and hardware prefetching also matter, so tile sizes are tuned empirically, and tiles are sometimes copied into contiguous buffers. High-performance libraries block at several levels at once: registers, L1, L2 and L3.

Blocking vs. other loop transformations

Loop unrolling replicates the loop body to cut loop overhead and expose instruction-level parallelism; it does not improve cache reuse by itself. Loop interchange swaps nesting order so the innermost loop walks memory contiguously, improving spatial locality. Blocking targets temporal locality across large data sets and is often combined with both. The same idea appears on GPUs, where tiles are staged in on-chip shared memory, and in stencil codes, convolutions and many dense linear-algebra kernels.

Try it: questions about Loop blocking

What is the main purpose of loop blocking (tiling), used for example in matrix multiplication?

  1. ATo compute in small cache-sized blocks and increase data reuse
  2. BTo reduce the number of iterations and hence the branch instructions executed at the end of each iteration
  3. CTo halve total memory usage without changing the order of computation
Show answer and explanation

Answer: A. To compute in small cache-sized blocks and increase data reuse

Computing a large matrix product directly evicts elements before they are reused, forcing repeated reloads from main memory. Loop blocking splits the matrices into small blocks that fit in the cache and finishes the work within each block, reusing loaded data many times and sharply reducing capacity misses. Reducing branches is the goal of loop unrolling, and total memory usage does not change.

FAQ

What is loop tiling used for?

It restructures loops to work on cache-sized blocks of data so that each loaded element is reused many times before eviction, sharply reducing capacity misses and memory traffic.

How do you choose the block size for matrix multiplication?

Start with a size where the working tiles fit in the target cache, for example three b × b tiles of doubles in a 32 KB L1 gives b around 32, then tune empirically for conflicts, TLB and prefetching.

Is loop blocking the same as loop unrolling?

No. Unrolling reduces loop overhead and branches, while blocking reorders iterations to improve cache reuse.

Questions that mention Loop blocking (1)

Loop blocking appears in 2 practice questions across 1 topic, including Memory Hierarchy.

  1. Which misses are not reduced even by making a cache fully associative?

Books on Memory Hierarchy Ad

As an Amazon Associate I earn from qualifying purchases.

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary