C8 · Lesson 49 · Computer organization lab
Simulating a cache, a branch predictor and a pipeline in Python
Architects don't build a processor to find out whether a bigger cache or a better predictor is worth it. They write simulators: programs that replay the addresses and branch outcomes of real software through a model of the hardware and count the misses and stalls. In this lab you'll write the three classic ones, an LRU set-associative cache, 1-bit and 2-bit branch predictors, and a cycle counter for the five-stage pipeline, and check each against the numbers worked out by hand in lessons 40, 25 and 27.
- A loop sweeps an array of W KiB again and again, P times, through a fully associative LRU cache with 64-byte lines, reading 4-byte words. The curve is the miss rate against the cache size.
- Once the cache holds the whole array, only the first pass misses. Shrink the cache below W and the rate jumps to one miss per line on every pass: LRU always throws out exactly the line the sweep needs next.
- Your cache simulator will reproduce this cliff, at 17 KiB against 15 KiB in a 16 KiB cache.
Predict
A 16 KiB fully associative LRU cache with 64-byte lines. A loop reads every word of a 17 KiB array, three times over. How many misses?
- The array is 272 lines and the cache holds 256. When the sweep comes back to the start, LRU has evicted exactly the oldest lines, the ones about to be read, and each miss evicts the next one needed. All 272 lines miss on each of the three passes. A 15 KiB array would miss only 240 times, all on the first pass.
Name it
A cache simulator
For each address: the block is address // line, the set is block % sets (sets = size / (line × ways)), and each set is a list of blocks in order of use. A hit moves the block to the most recently used end; a miss appends it, first evicting the least recently used block if the set is full.
A predictor simulator
Keep one state per branch (here, a single branch). A 1-bit predictor's state is the last outcome. A 2-bit counter runs from 0 to 3, predicts taken at 2 or 3, and moves one step toward each outcome. Start the 1-bit predictor at not taken and the 2-bit counter at 1, weakly not taken.
A pipeline cycle counter
For a five-stage pipeline with forwarding, predicting branches not taken: N instructions take 4 + N cycles, plus 1 for each load whose result the very next instruction uses, plus 2 for each taken branch. A trace entry here is (kind, dest, sources, taken), with kind one of 'alu', 'load', 'store' or 'branch'.
Worked example
Trace a 2-way LRU set through the blocks 1, 2, 1, 3, 2.
Show the solution
- Block 1. It misses: [1].
- Block 2. It misses: [1, 2].
- Block 1 again. It hits and becomes most recent: [2, 1].
- Block 3. It misses and evicts 2, the least recent: [1, 3].
- Block 2 again. It misses and evicts 1: [3, 2].
Answer four misses and one hit, leaving [3, 2] in the set.
Worked example
Count the cycles for: lw t0, 0(a0); add t1, t0, t2; a taken branch; addi t3, t3, 1 (the branch target).
Show the solution
- Fill the pipeline. Four instructions take 4 + 4 = 8 cycles.
- The load-use pair. It adds 1.
- The taken branch. It adds 2.
- The total. a CPI of 2.75 over this short stretch.
- Why so high. Most of that is the pipeline filling; over thousands of instructions the 4 disappears into the average.
Answer 11 cycles, a CPI of 2.75.
Check
A trace of 4 instructions has one load-use pair and one taken branch. How many cycles does the five-stage pipeline take?
Check
A branch's outcome alternates taken, not taken, for 5 pairs. How many times does a 2-bit counter that starts at 1 mispredict it?
Check
Write cache_misses(addresses, size, line, ways): the number of misses for a list of byte addresses in an LRU set-associative cache of size bytes, with line-byte lines and ways lines per set, starting empty.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write mispredictions(outcomes, bits): the number of wrong guesses a predictor makes on a list of outcomes (1 taken, 0 not taken). bits is 1 (start at not taken) or 2 (a counter from 0 to 3, starting at 1, predicting taken at 2 or 3).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write pipeline_cycles(trace): the cycles a five-stage pipeline with forwarding takes for a list of (kind, dest, sources, taken) entries. N instructions take 4 + N cycles, plus 1 when a 'load' is followed at once by an instruction whose sources include its dest, plus 2 for each taken 'branch'.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does an LRU cache do so badly when a loop sweeps an array just slightly larger than the cache?
LRU evicts the line that was used longest ago. In a repeated sweep, the line used longest ago is always the one the sweep will need soonest, because the sweep returns to lines in the same order it left them. So when the array doesn't quite fit, every new line evicts the next line to be read, and that line then misses and evicts the one after it, all the way round. The cache keeps the wrong lines at every moment and the hit rate collapses from nearly perfect to nothing. Random replacement, which sometimes keeps old lines, does much better in exactly this case.
Practice
Keep practicing
5 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.