The covariance matrix of 12 stocks over 48 days needs only 1,296 numbers from memory, yet the plain triple loop fetches 7,632 of them on our toy cache. The arithmetic is identical. What can a program change to close that gap?
The idea in plain words
Where we are. The last topic showed that a matrix times matrix does about n times more arithmetic than it has numbers to fetch, so in principle it can keep the CPU busy. This topic shows what a real program must do to get that benefit: reuse each number while it is still in the fast memory next to the CPU. The next topic looks at how the same matrix is laid out in memory.
Memory comes in layers. Right next to the CPU sits a small, very fast store called the cache. When the program asks for a number that is in the cache, that is a hit and it is nearly free. When the number is not there, that is a miss: the number is fetched from main memory, which costs one word of traffic, and it pushes an older number out. Our toy cache throws out the one used longest ago, the usual rule.
Splitting a big calculation into small tiles so that the numbers one tile needs all fit in the cache and are reused many times before the next tile is started.
The plain loop forgets its numbers
Here X is 48 days of returns for 12 stocks (48 rows, 12 columns) and Xt is its transpose. The covariance matrix is Xt times X, a 12 by 48 matrix times a 48 by 12 matrix. The cache holds only 108 numbers: it is a scale model, because a real cache holds thousands but a real matrix is thousands wide too.
# The plain triple loop on the 12-stock covariance job, with a cache of 108 numberscache = Cache(108)cov = gemm_naive(cache, Xt, X)print("steps:", cache.hits + cache.misses, "touches | hits", cache.hits, "| misses", cache.misses)print("each number once would need", 12 * 48 + 48 * 12 + 12 * 12, "words; the loop moved", cache.misses)1
steps: 20736 touches | hits 13104 | misses 7632 each number once would need 1296 words; the loop moved 7632
- 15.9 times more traffic than the minimum, for exactly the same arithmetic.
The plain loop walks along a row of Xt and down a column of X for each result, so by the time it needs the same row again, 12 columns later, the cache has long since thrown it out. Each number is fetched about six times instead of once.
Tiles that fit
Blocking changes the order of the same steps. Instead of finishing one result at a time, the program picks a tile of block by block results and the matching strips of the inputs, and does all the work for that tile before moving on. When the three tiles (one each of A, B and C) fit in the cache, every number is brought in once per tile and then reused block times.
# The same job in tiles of different sizes, the same cache of 108 numbersprint("cache of 108 numbers; best_block says", best_block(108))for block in (1, 3, 6, 8, 12): cache = Cache(108) gemm_blocked(cache, Xt, X, block) print("block", block, "->", cache.misses, "words")1
cache of 108 numbers; best_block says 6 block 1 -> 7632 words block 3 -> 4752 words block 6 -> 3428 words block 8 -> 3128 words block 12 -> 8064 words
- 1Tiles of 6 to 8 fit and cut the traffic by more than half. A tile of 12 does not fit, and the traffic is worse than the plain loop.
When the three tiles together are larger than the cache, each pass evicts numbers the next pass needs, and the cache thrashes. That is why best_block returns the largest size with 3 * b * b <= capacity and not simply the largest you can try. Real libraries tune the tile size for each CPU for exactly this reason.
# The model against the measurement, for square 24 by 24 matrices and the same cachesquare = [[1.0] * 24 for _ in range(24)]for block in (3, 4, 6, 8): cache = Cache(108) gemm_blocked(cache, square, square, block) print("block", block, "| measured", cache.misses, "| model", round(traffic_model(24, block)))1
block 3 | measured 9792 | model 9792 block 4 | measured 7488 | model 7488 block 6 | measured 6864 | model 5184 block 8 | measured 5184 | model 4032
- 1The model is exact at small tiles and about 25% low at tiles that nearly fill the cache, where neighbours start to evict each other.
What the tasks ask
- 1`Cache.touch`Answer hit or miss, update
hits,missesand the use order, and evict the least recently used number when the cache is full. - 2`gemm_blocked`The same product in tiles, touching the three numbers of every step in the order given, so the miss count is exact.
- 3`best_block` and `traffic_model`The largest tile whose three copies fit, and the traffic the model predicts; the tests compare it with what your
gemm_blockedreally measures.
best_block(300) return, and why is a tile of 12 a bad choice?Lab · what's graded
4 tasks · 100 pts- Least-recently-used cache with
touch25 pts - Multiply in tiles with
gemm_blocked25 pts - Pick a tile size with
best_block25 pts - Predict the traffic with
traffic_model25 pts