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

C8 · Lesson 51 · Cache and memory lab

Measuring write policies, access order and DRAM rows in Python

The formulas of the last few lessons predict averages. Real programs have particular access patterns, and the surprises live in the particulars: a matrix whose size is a power of two, a counter updated a thousand times, two arrays that happen to land in the same DRAM bank. In this lab you'll write small simulators that replay address traces and count what the hardware would do, then use them to find those surprises.

  • A loop walks through a large array with a fixed stride (the distance between consecutive accesses). Each cache line holds L bytes.
  • With a stride of 4 bytes, one access in L/4 misses. Once the stride reaches the line size, every access misses: each one lands in a new line.
  • Summing a matrix by columns is a stride of one whole row. For any matrix wider than a few lines, that's a miss on every access unless the cache can hold every row's line until the next column comes back for it.

Predict

A 64 × 64 matrix of ints (16 KiB) is summed column by column through a 4 KiB direct-mapped cache with 64-byte lines. How many misses?

Name it

Simulating a cache

A trace-driven simulator keeps, for each set, the tags it holds (and a dirty bit for write-back), and for each address computes

block=⌊a/L⌋,set=blockmodS,offset=amodL

A hit is a matching tag in that set. Everything else (traffic, AMAT, miss counts) is counting.

What to count

  • Traffic: bytes moved between cache and memory. Write-back moves whole lines, on misses and on dirty evictions, plus the dirty lines still in the cache at the end. Write-through without allocation moves each store's 4 bytes, and lines only for load misses.
  • AMAT: add up the time of every access (each level's time when it's consulted) and divide by the number of accesses.
  • DRAM row hits: with an open-row policy, each bank keeps its last row open; an access to that row is a hit.

Power-of-two strides

Set indices come from the low bits of the block number. Addresses that differ by a multiple of the cache size (or of a large power of two) share low bits and so share a set. A matrix whose rows are a power of two bytes long sends a whole column into a handful of sets. Padding each row by a line or so spreads them out.

Worked example

A counter at one address is loaded and stored 1000 times. How many bytes cross the bus with a write-back, write-allocate cache, and with a write-through, no-allocate cache (64-byte lines)?

Show the solution
  1. Write-back, write-allocate. The first load misses and reads the line, 64 bytes; every later access hits; at the end the dirty line is written back, 64 more: 128 bytes.
  2. Write-through, no-allocate. The first load reads the line, 64 bytes, and every one of the 1000 stores sends 4 bytes: 64+1000×4=4064bytes
  3. Compare. That's 32 times as much.

Answer 128 bytes with write-back and 4064 bytes with write-through, 32 times as much.

Worked example

The 64 × 64 matrix above misses 4096 times when summed by columns. What happens if each row is padded from 64 to 80 ints?

Show the solution
  1. The new row spacing. Rows are now 320 bytes apart, 5 lines.
  2. Where each row's line lands. Because 5 and 64 have no common factor, row i's line for a given column lands in set (5i+c)mod64, which is different for each of the 64 rows.
  3. No more evictions. The column's lines no longer evict each other, and the next 15 columns find them all still cached.
  4. The misses. The sum misses 256 times, only the unavoidable first touch of each line.
  5. The cost. The padding wastes 4 KiB of memory to save 3840 misses.

Answer the sum misses 256 times instead of 4096.

Check

Address 74565 in a cache with 16-byte lines and 256 sets. Which set?

Check

Write split(addr, line, sets): return (tag, index, offset) for a cache with line-byte lines and sets sets (both powers of two).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write traffic(trace, size, line, policy): the bytes moved between a direct-mapped cache and memory for a list of (address, is_write) pairs. Policy 'wb' is write-back with write-allocate, counting dirty lines left at the end; 'wt' is write-through with no write-allocate (each store moves 4 bytes; loads that miss fetch a line).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write matrix_misses(n, order, size, line): the misses of a direct-mapped cache (starting empty) when an n × n matrix of 4-byte ints, stored row by row from address 0, is read by rows ('row') or by columns ('col').

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write amat(trace, l1, l2, line, t1, t2, tmem): the average access time of a list of addresses through a direct-mapped L1 of l1 bytes and a direct-mapped L2 of l2 bytes (both starting empty, line-byte lines). Every access costs t1; an L1 miss adds t2; an L2 miss adds tmem as well. A miss fills both levels.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write row_hit_rate(addresses, row_bytes, banks): the fraction of DRAM accesses that find their row open. Consecutive rows of row_bytes go to consecutive banks (row r of the address space is in bank r mod banks, as that bank's row r // banks), and each bank keeps its last row open.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why do trace-driven simulators find problems that average-based formulas miss?

Practice

Keep practicing

One kind of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.