S4 · Lesson 19 · Advanced cache optimizations
Faster hits, memory-level parallelism, prefetching, victim caches and blocking in software
The average memory access time has three terms, hit time, miss rate and miss penalty, and architects have a bag of tricks for each. Way prediction and pipelining shorten hits. Victim caches and prefetchers cut misses. Non-blocking caches let several misses overlap, so the penalty is paid once for many of them. And some of the largest gains come from software: restructuring a loop so it reuses data while it's still in the cache can cut a matrix multiply's memory traffic by a factor of thirty without changing the hardware at all.
- Words moved between cache and memory for an N × N matrix multiply, divided by , against the block size B of a blocked (tiled) version: about .
- The unblocked loop (B = 1) moves about 2 words per multiply-add. Blocking reuses each loaded block B times, cutting traffic by about B.
- B is limited by the cache: three B × B blocks must fit in C words, so (the marker).
Predict
A program misses 10 times in a stretch, each miss taking 200 cycles. If the cache can keep 4 misses outstanding at once and the core has enough independent work, about how many stall cycles do the 10 misses cost?
About 500: overlapped four at a time, the ten misses take about 10 × 200 / 4 cycles. That's memory-level parallelism (MLP), and it needs a non-blocking cache with miss status holding registers (MSHRs) to track the outstanding misses, plus an out-of-order core or prefetcher to issue them early. A blocking cache would stall all 2000 cycles.
Name it
Faster hits
- Small, simple L1s with low associativity; way prediction guesses which way will hit and reads only it. With prediction accuracy p, hit time = .
- Pipelined cache access raises bandwidth (and adds cycles of latency); banks let several accesses proceed at once.
Fewer or cheaper misses
- Victim cache: a small fully associative buffer of recently evicted lines catches conflict misses of a direct-mapped cache.
- Hardware prefetching: stride and stream prefetchers detect regular patterns and fetch ahead. To hide a latency L with iterations taking t cycles, prefetch iterations ahead.
- Critical word first and early restart shorten the effective penalty.
- Non-blocking caches: with an average of MLP misses overlapped, the stall per miss is about penalty/MLP.
- Merging write buffers combine writes to the same line.
Compiler and programmer optimizations
- Loop interchange walks arrays in the order they're stored.
- Blocking (tiling) works on sub-blocks that fit in the cache. For an N × N matrix multiply, the unblocked version moves about words in the worst case; with B × B blocks, about . Three blocks must fit: .
Worked example
A 1024 × 1024 matrix multiply in doubles runs with a 32 KB cache (4096 doubles). What's the largest block, and how much traffic does blocking with B = 32 save?
Show the solution
- Largest block. so a power-of-two block of 32.
- Unblocked traffic.
- Blocked traffic.
- Saving. That's 31.5 times less traffic for identical arithmetic. Libraries block at several levels (registers, L1, L2) for the same reason.
Answer B can be at most 36.95, so the largest power-of-two block is 32, and blocking with B = 32 cuts the traffic 31.5 times.
Worked example
A loop takes 25 cycles per iteration and memory latency is 200 cycles. How far ahead should it prefetch? And with way prediction 90% accurate, a 1-cycle fast hit and a 3-cycle mispredicted hit, what's the hit time?
Show the solution
- Prefetch distance. ahead, so the data arrives just as it's needed (a little more allows for variation).
- Hit time with way prediction. against perhaps 2 cycles for reading every way of a set-associative cache in parallel.
Answer prefetch 8 iterations ahead; way prediction gives a 1.2-cycle hit time.
Worked example
A program makes 10 last-level misses per thousand instructions, each costing 200 cycles. What stall CPI results if misses are serviced one at a time, and if four overlap on average?
Show the solution
- One at a time. cycles per instruction.
- Four overlapping. With memory-level parallelism of 4 the effective cost per miss is about 50 cycles: cycles per instruction.
- The lesson. Non-blocking caches and enough MSHRs turn a latency problem into a bandwidth problem.
Answer 2.0 stall cycles per instruction one at a time, and 0.5 when four overlap.
Check
A 512 × 512 matrix multiply is blocked with 16 × 16 tiles. By what factor does the worst-case traffic, against , fall?
Check
A 32 KB cache holds 4-byte elements. What's the largest whole B such that three B × B blocks fit?
Check
8 independent misses of 150 cycles each overlap 8 at a time. About how many stall cycles do they cost?
Check
Memory latency is 250 cycles and each loop iteration takes 15. How many iterations ahead should the loop prefetch?
Check
Way prediction is 0.85 accurate; a correct guess hits in 1 cycle(s) and a wrong one in 4. What's the average hit time?
Check
A direct-mapped cache misses 0.04 of the time; 0.3 of its misses are conflicts, and a victim cache catches 0.7 of those. What's the new miss rate?
Check
An N × N matrix multiply (N = 1024) is blocked into 16 × 16 tiles.
(a)
About how many words does the unblocked version move in the worst case?
(b)
And the blocked version?
(c)
How much cache do three tiles of doubles need?
Check
A loop takes 10 cycles per iteration against 150 cycles of memory latency. Way prediction is 0.95 accurate, with 2-cycle predicted hits and 3-cycle mispredicted ones.
(a)
How far ahead should the loop prefetch?
(b)
What is the average hit time?
Check
A program misses k times per instruction, each costing P cycles, with M misses overlapped on average.
Step 1
What stall CPI does it see with blocking misses?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
And with the overlap?
Explain why
Why does blocking a matrix multiply cut its memory traffic without changing how many arithmetic operations it does?
A plain triple loop walks a whole row of one matrix and a whole column of another for every element it computes, and when the matrices are bigger than the cache, those rows and columns have been evicted by the time they're needed again, so the same data is fetched from memory over and over. Blocking reorders the loops so the computation works on small square tiles, three of which fit in the cache together. Each element of a tile is loaded once and then used B times while it's still cached before the code moves on. The multiply-adds are the same ones in a different order, but each word fetched from memory now does B times as much work, so the traffic drops by about that factor.
Practice
Keep practicing
11 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.