S3 · Lesson 45 · Core and cache lab
A set-associative cache simulator, a pipeline hazard counter and two-level AMAT, in Python
Lessons 41 to 43 built a processor's pipeline and caches in RTL. Before committing to sizes and policies, architects run traces through quick models: a cache simulator that counts hits for a real access pattern, a pipeline model that counts stalls and flushes, and the average memory access time of a two-level hierarchy. This lab writes all three and uses them to see a classic effect: walking a matrix down its columns can miss on every access in a cache that holds the whole column.
- The line is the average memory access time of a two-level hierarchy against the L1 miss rate: with a 1-cycle L1, a 10-cycle L2 and a 100-cycle memory.
- The slider sets the L2's local miss rate. A good L2 flattens the curve: L1 misses then cost about 10 cycles, not 100.
- The lab computes the same quantity and simulates the hit rates that feed it.
Predict
A program reads a 256 × 256 matrix of 4-byte integers, stored row by row, once in row order and once column by column. The cache is 32 KB, 4-way, with 64-byte lines. What hit rates do the two walks get?
Row order uses all 16 integers of each line before moving on: 15 hits per 16 accesses, 93.75%. Down a column, consecutive accesses are 1 KB apart, and with 128 sets each 1 KB step moves 16 sets on, so the 256 rows of a column fall into only 8 sets: 32 lines competing for 4 ways each. Every access misses, though the column's 256 lines (16 KB) would fit. The lab's simulator reproduces both numbers.
Name it
The models
- Cache: an address's block number is address ÷ line size; the set is block mod (number of sets) and the tag is block ÷ (number of sets). Each set keeps up to W tags in least-recently-used order: a hit moves the tag to the most recent end; a miss evicts the least recent if the set is full.
- Pipeline (five stages, forwarding, branches resolved in EX, predicted not taken): , where a load-use stall happens when an instruction reads the register the load just before it wrote (x0 excepted).
- Two-level AMAT: , with the L2's local miss rate (misses per L2 access).
Check
Write cache_sim(addrs, size, line, ways): simulate an LRU set-associative cache of size bytes with line-byte lines and ways ways over a list of byte addresses, and return the hit rate.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write pipe_cycles(prog): prog is a list of (op, rd, srcs, taken) tuples; count cycles on the five-stage pipeline with forwarding, one stall when an instruction uses the register a load ('lw') wrote in the instruction just before it (rd ≠ 0), and two flush cycles for each taken 'beq'.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write amat2(h1, m1, h2, m2, mem): the two-level average memory access time, and l1_miss_for(target, h1, h2, m2, mem): the largest L1 miss rate that meets a target AMAT.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
How can a cache that is big enough to hold a matrix column still miss on every access when walking down that column?
A set-associative cache doesn't let a line go anywhere: its address picks one set, and each set holds only a few lines. When the matrix's rows are a power of two bytes long, stepping down a column advances the address by that stride, and the set index, which is taken from low address bits, repeats after only a few rows. Hundreds of the column's lines then crowd into a handful of sets, each with room for only four, so each line is evicted before it's reused. These are conflict misses: the total capacity is ample, but the mapping concentrates the data. Padding each row or changing the loop order spreads the accesses across all the sets.