S345 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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: h1+m1(h2+m2tmem) 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?

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): cycles=4+N+load-use stalls+2×taken branches, where a load-use stall happens when an instruction reads the register the load just before it wrote (x0 excepted).
  • Two-level AMAT: h1+m1(h2+m2tmem), with m2 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?