S4 · Lesson 23 · Cache lab
Classifying misses into the three Cs, blocking a matrix multiply, and fitting the √2 rule, in Python
The three Cs, blocking and the √2 rule from lessons 18 to 20 are all statements about miss counts, and miss counts are what a cache simulator produces. This lab writes a set-associative LRU simulator and uses it three ways: to split a trace's misses into compulsory, capacity and conflict by simulating an infinite cache, a fully associative one and the real one; to measure how much blocking cuts a matrix multiply's misses; and to fit the exponent of the √2 rule to measured miss rates.
- The lines are miss rate against cache size on log–log axes for the rule ; the slider sets α.
- α = 0.5 is the √2 rule: each doubling of the cache divides the miss rate by √2. Programs with little reuse beyond a working set show smaller α until the working set fits, then a cliff.
- The lab fits α to measured points by least squares on the logarithms.
Predict
A 256 × 256 matrix of 4-byte integers, stored by rows, is read down its columns through a 32 KB, 4-way cache with 64-byte lines. Its 65 536 accesses all miss. How do those misses split into the three Cs?
The matrix occupies lines: those first touches are compulsory. A fully associative 32 KB cache holds 512 lines, enough for the 256 lines one column walk touches, so the next column finds them all: no capacity misses. The real cache maps all 256 of them into 8 sets, and evicts each before its reuse: the other 61 440 are conflicts.
Name it
The simulator
For a cache of S bytes, B-byte lines and W ways: block = address ÷ B, set = block mod (S/(BW)), tag = block ÷ (S/(BW)); each set is an LRU list of up to W tags.
The three Cs, measured
- Compulsory = distinct lines touched (an infinite cache's misses).
- Capacity = fully associative LRU cache of the same size's misses − compulsory.
- Conflict = the real cache's misses − the fully associative cache's.
Blocking
A matrix multiply's inner loop touches a row of A and a column of B; once N grows past what the cache holds, B's column lines are evicted before reuse. Working on tiles small enough that three fit in the cache reuses each line many times.
Fitting α
On log axes the rule is a line: , so α is minus the least-squares slope of against .
Check
Write misses(addrs, size, line, ways): an LRU set-associative cache simulator returning the miss count, and three_c(addrs, size, line, ways): return (compulsory, capacity, conflict).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write mm_trace(N, block): the address trace of for N × N matrices of 8-byte doubles at base addresses 0, and , reading A[i][k] and B[k][j] in the inner loop and writing C[i][j] after it; with block = None use plain i, j, k loops, otherwise tile the j and k loops in blocks of that size (jj, kk outermost, then i, j, k).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write fit_alpha(sizes, rates): the least-squares exponent α in .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
How does simulating a fully associative cache of the same size separate capacity misses from conflict misses?
A fully associative cache can place any line anywhere, so the only reason it misses, after the first touch of each line, is that the program's working set doesn't fit and LRU had to evict something useful: those are capacity misses. The real set-associative cache has the same total size but restricts each line to one set, so it can also miss when too many active lines crowd into the same set while other sets sit idle. Its extra misses beyond the fully associative cache's are therefore due to the mapping alone, conflict misses, and they're the ones that more associativity or a different address mapping would remove.