Sub topic 18
Cache-oblivious algorithms and layouts
This chapter assumes you know cache lines, spatial and temporal locality, and blocking (tiling) for nested loops. It also assumes you have seen B-trees and the idea of wide nodes. Here you will see how to get good cache behavior without hard-coding the cache size.
The problem with tuned block sizes
The blocked transpose from the blocking chapter uses a constant B = 32. That value fits one machine's cache. A different CPU may have a different L1 size, and a real machine has several levels (L1, L2, L3), each with its own size. A single B can fit at most one level well.
A cache-oblivious algorithm avoids this. It never mentions the cache size or line size. It uses divide and conquer, so at some recursion depth the subproblem fits in each level of the hierarchy, whatever that level's size is.
Recursive transpose
Split the matrix into halves along its longer side. Keep splitting until the piece is small. The pieces at every scale are compact blocks, so they fit in whichever cache level is the right size.
#include <stddef.h>
#define CUTOFF 16
/* Transposes the rows [r0, r1) and columns [c0, c1) of src into dst.
Both matrices are row-major with row length n. */
static void transpose_rec(double *dst, const double *src, size_t n,
size_t r0, size_t r1, size_t c0, size_t c1) {
size_t rows = r1 - r0, cols = c1 - c0;
if (rows <= CUTOFF && cols <= CUTOFF) {
for (size_t i = r0; i < r1; i++)
for (size_t j = c0; j < c1; j++)
dst[j * n + i] = src[i * n + j];
} else if (rows >= cols) {
size_t mid = r0 + rows / 2;
transpose_rec(dst, src, n, r0, mid, c0, c1);
transpose_rec(dst, src, n, mid, r1, c0, c1);
} else {
size_t mid = c0 + cols / 2;
transpose_rec(dst, src, n, r0, r1, c0, mid);
transpose_rec(dst, src, n, r0, r1, mid, c1);
}
}
void transpose_oblivious(double *dst, const double *src, size_t n) {
transpose_rec(dst, src, n, 0, n, 0, n);
}CUTOFF is not a cache parameter. It only stops the recursion from costing more in call overhead than it saves. Any small value works.
Cache-oblivious layouts
The same idea applies to storage. A search tree stored in the van Emde Boas (vEB) layout is cut at half its height. The top subtree is stored contiguously, followed by each bottom subtree, and each of those is laid out the same way. Any root-to-leaf path then crosses few cache lines at every block size, so one layout helps all cache levels, and the page-level locality from the TLB chapter as well.
The cost is complexity. Index arithmetic is harder than for a plain array, and inserts are awkward.
Trade-offs
| Approach | Needs cache size | Typical result |
|---|---|---|
| Tuned blocking | Yes | Best on one known machine |
| Cache-oblivious | No | Good everywhere, rarely the peak |
| Wide-node B-tree | Yes (node size) | Simple and fast in practice |
Recursion adds overhead, and in practice the base case is often a tuned kernel. Measure before choosing, as the measurement chapter describes.
Exercise
Why does the recursive transpose need no knowledge of the cache size, and what is the role of CUTOFF?
Show answer
Halving continues through every scale, so some recursion level produces blocks that fit each cache level, whatever its size. CUTOFF only stops recursion once pieces are small, to avoid call overhead, and has no connection to any cache size.