Sub topic 13
Blocking (tiling) for matrix-style loops
This chapter assumes you know that a cache line is the unit of transfer, and that you have seen row-major traversal and strides. You should also be comfortable with nested loops over 2D data stored in a flat array.
Why a good loop order is not enough
A matrix transpose reads src[i * n + j] and writes dst[j * n + i]. Whichever loop order you pick, one of the two arrays is accessed with a stride of n doubles. For a large n, every strided access touches a different cache line. By the time the loop returns to that column for the next element, the line has been evicted. Each line is loaded, one double is used, and the line is lost. That is a capacity problem, so reordering the loops cannot fix it.
The idea: work on tiles that fit in cache
Blocking splits the iteration space into small B-by-B tiles. You finish one tile before moving to the next. Within a tile, every cache line you load is used fully before it is evicted.
A runnable example
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#define B 32
static void transpose_naive(double *dst, const double *src, size_t n) {
for (size_t i = 0; i < n; i++)
for (size_t j = 0; j < n; j++)
dst[j * n + i] = src[i * n + j];
}
static void transpose_blocked(double *dst, const double *src, size_t n) {
for (size_t ii = 0; ii < n; ii += B)
for (size_t jj = 0; jj < n; jj += B)
for (size_t i = ii; i < ii + B && i < n; i++)
for (size_t j = jj; j < jj + B && j < n; j++)
dst[j * n + i] = src[i * n + j];
}
int main(void) {
size_t n = 1000; /* not a multiple of B, so the edge tiles are tested */
double *src = malloc(n * n * sizeof *src);
double *a = malloc(n * n * sizeof *a);
double *b = malloc(n * n * sizeof *b);
if (!src || !a || !b) return 1;
for (size_t k = 0; k < n * n; k++) src[k] = (double)k;
transpose_naive(a, src, n);
transpose_blocked(b, src, n);
int same = 1;
for (size_t k = 0; k < n * n; k++)
if (a[k] != b[k]) { same = 0; break; }
printf("%s\n", same ? "match" : "mismatch");
free(src); free(a); free(b);
return 0;
}The i < n and j < n checks handle edge tiles. Both versions do the same work and give the same result. Only the order of memory accesses differs.
Choosing the tile size
Pick B so that the tiles you touch at once fit in L1 with room to spare. Here a 32-by-32 tile of doubles is 8 KiB. Source and destination tiles together take 16 KiB, which fits in a typical 32 KiB L1 data cache. A tile row is 256 bytes, which is four full 64-byte lines. Treat 32 as a starting point and benchmark B values such as 16, 32, 64 and 128 on your own machine.
Common errors
- Power-of-two sizes. When
nis a power of two, tile rows map to the same cache sets and evict each other, even though the tile is small enough to fit. Padding the row length (for example ton + 8) often fixes it. - Tiles that are too large. A tile that spills out of L1 loses most of the benefit.
- Blocking code with no reuse. Blocking only helps when elements or lines are touched more than once, or partially used. A single streaming pass gains nothing.
- Forgetting the edges. Without bounds checks on the inner loops,
nvalues that are not multiples of B read and write out of bounds.
Exercise
A blocked transpose uses B = 64 on doubles. How much memory do the source and destination tiles occupy together, and why might that be a poor choice for a 32 KiB L1 cache?
Show answer
Each tile is 64 64 8 bytes = 32 KiB, so the two tiles total 64 KiB. That is larger than the 32 KiB L1 cache, so lines from the tiles evict each other before they are fully reused, and much of the benefit of blocking is lost. A smaller B such as 32 keeps both tiles within 16 KiB.