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

C8 · Lesson 44 · Cache design

Associativity, replacement, writes and levels of cache

Main memory takes around a hundred processor cycles to answer, so a processor that waited for it on every load would spend almost all its time waiting. Caches hide that delay by keeping recently used memory close at hand, and lesson 39 showed the basic direct-mapped kind. Real caches make more choices: how many places each address may go, which line to throw out when a new one arrives, what to do on a write, and how many levels to stack. Each choice trades a little hardware or hit time against fewer misses, and the miss rate is what decides how fast programs really run.

  • A program reads an array from start to end with a stride of s bytes between accesses. Each miss brings in a whole line of L bytes, so with small strides most accesses find their data already loaded.
  • The curve is the miss rate against the stride. Move L: bigger lines mean fewer misses for sequential access, until the stride is larger than a line and every access misses.
  • The readouts give the average memory access time for 4-byte strides (reading every int) and for 64-byte strides (one int out of every 16).

Predict

A direct-mapped 4 KiB cache. A loop reads a[i] and then b[i], and the arrays start exactly 4 KiB apart. What happens?

Name it

Associativity

An N-way set-associative cache groups its lines into sets of N. An address picks a set by its index bits and may go in any line of that set; all N tags are compared at once.

sets=capacityline size×N,index bits=log2⁡(sets),offset bits=log2⁡(line size)

and the tag is the rest of the address. Direct-mapped is 1-way; fully associative is one set holding every line. The set an address uses is ⌊address/L⌋modsets.

Replacement and writes

  • Replacement: on a miss in a full set, evict the least recently used line (LRU), or a random one, which is cheaper and nearly as good for large sets.
  • Write-through: every store also goes to memory (usually through a write buffer). Simple, but heavy memory traffic.
  • Write-back: stores change only the cache and mark the line dirty; it's written to memory when evicted. One write for many stores to the same line.
  • Write-allocate: on a store miss, bring the line in first (usual with write-back).

The three Cs

Misses are compulsory (first touch of a line), capacity (the working set doesn't fit) or conflict (too many lines map to one set). Bigger caches cut capacity misses, more ways cut conflict misses, longer lines cut compulsory misses for sequential data.

Levels

A small, fast L1 backed by a larger L2 (and often L3):

AMAT=tL1+mL1(tL2+mL2⋅tmem)

where mL2 is the L2's local miss rate, the fraction of L1 misses that also miss in L2. The effect on CPI is the memory accesses per instruction × miss rate × penalty, added to the base CPI.

Worked example

Split a 32-bit address for a 32 KiB, 4-way cache with 64-byte lines.

Show the solution
  1. Lines and sets. 32 KiB of 64-byte lines is 512 lines, in 128 sets of 4.
  2. Offset. log2⁡64=6 bits.
  3. Index. log2⁡128=7 bits.
  4. Tag. The rest: 32−7−6=19bits

Answer a 19-bit tag, a 7-bit index and a 6-bit offset.

Worked example

L1 hits in 1 cycle and misses 5% of the time; L2 hits in 10 cycles and misses 20% of the accesses that reach it; memory takes 100 cycles. What is the AMAT, with and without the L2?

Show the solution
  1. With the L2. t=1+0.05(10+0.2×100)=1+0.05×30=2.5cycles
  2. Without it. t=1+0.05×100=6cycles
  3. Compare. The L2 more than halves the average.

Answer an AMAT of 2.5 cycles with the L2 and 6 cycles without it.

Worked example

Two arrays are exactly 8 KiB apart, and a loop alternates between them. What happens in an 8 KiB direct-mapped cache with 32-byte lines, and in a 2-way cache of the same size?

Show the solution
  1. Split the address. The direct-mapped cache has 256 lines: 5 offset bits and 8 index bits.
  2. Compare the two arrays. Addresses 8 KiB apart have the same index and different tags.
  3. Direct-mapped. Each access evicts the line the other just loaded: every access misses, although the cache has room for both. That's a conflict miss.
  4. 2-way. Both lines live in the same set, and after the first two misses every access hits.

Answer in the direct-mapped cache every access misses, a conflict miss each time; in the 2-way cache every access hits after the first two.

Check

A 32 KiB, 8-way set-associative cache has 32-byte lines; addresses are 32 bits.

(a)

How many sets?

(b)

How many index bits?

(c)

How many tag bits?

Check

L1 hits in 1 cycles and misses 0.03 of the time; L2 hits in 15 cycles and misses 0.4 of the accesses that reach it; memory takes 80 cycles.

(a)

What is the AMAT?

(b)

How many times better is that than L1 alone with memory behind it?

Check

A loop reads every element of an array of 1000 elements of 1 bytes, aligned to a line, once, through a cache with 64-byte lines that starts empty.

(a)

How many misses?

(b)

What is the miss rate?

Check

A cache has 256 sets of 16-byte lines. Which set does address 74565 map to, and at what offset?

(a)

Which set?

(b)

What offset within the line?

Check

A loop stores to the same word 100 times, and the line is evicted afterward.

(a)

How many memory writes does a write-through cache make?

(b)

How many line write-backs does a write-back cache make?

Check

One set of a 2-way LRU cache starts empty. Lines A, B, A, C, B, A are accessed in that order, all mapping to this set. How many misses?

Check

A program's misses fall sharply when the cache goes from direct-mapped to 2-way at the same size. What kind of misses were they?

Check

L1 hits in 1 cycle and misses 5%; L2 takes 8 cycles and misses 40% of what reaches it; memory takes 150.

(a)

What's the AMAT in cycles?

(b)

What fraction of all accesses go to memory?

Check

A 32 KiB, 2-way cache has 64-byte lines and 32-bit addresses.

(a)

How many index bits?

(b)

How many tag bits?

Explain why

Why does a write-back cache need a dirty bit on each line?

Practice

Keep practicing

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