---
title: Problem set 9
heading: "Problem set 9: caches, writes and coherence"
eyebrow: C8 · Problem set 9
type: problems
---

::: hook
These problems use lessons 39 to 45 with the weight on caches: organization and address splitting, average access time across levels, write policies, write buffers, DMA and coherence between cores. Addresses are 32 bits and words 4 bytes.
:::

::: pool
pick: 12

::: check
kind: parts
given: C = 16 | 32 | 64; w = 2 | 4 | 8; L = 32 | 64

## A {C} KiB, {w}-way set-associative cache has {L}-byte lines.

::: part
answer: round(log(C*1024/(L*w))/log(2))
tolerance: 0

### How many index bits?

::: misses
- [round(log(C*1024/L)/log(2))] Index the sets, not the lines.
- [any] $\log_2$ of the number of sets.
:::
:::

::: part
answer: 32 - round(log(C*1024/(L*w))/log(2)) - round(log(L)/log(2))
tolerance: 0

### How many tag bits?

::: misses
- [any] What's left after index and offset.
:::
:::
:::

::: check
kind: parts
given: m1 = 2 | 4 | 6; t2 = 10 | 15; m2 = 20 | 40; M = 100 | 200

## L1 hits in 1 cycle and misses {m1}%; L2 takes {t2} cycles and misses {m2}% of what reaches it; memory takes {M} cycles.

::: part
answer: 1 + m1/100*(t2 + m2/100*M)
tolerance: 0.5%

### What is the AMAT?

::: misses
- [1 + m1/100*M] With an L2, most L1 misses stop there.
- [any] $1 + m_1(t_2 + m_2 M)$.
:::
:::

::: part
answer: 1 + m1/100*M
tolerance: 0.5%

### What would it be without the L2?

::: misses
- [any] Every L1 miss goes to memory.
:::
:::
:::

::: check
kind: parts
given: k = 2 | 6 | 30; L = 32 | 64

## A loop stores to every word of a freshly allocated {L}-byte line {k} times before it's evicted.

::: part
answer: 4*k*L/4
unit: bytes
tolerance: 0

### How many bytes does write-through send to memory?

::: misses
- [any] Every store's 4 bytes: {k} passes over {L}/4 words.
:::
:::

::: part
answer: 2*L
unit: bytes
tolerance: 0

### How many does write-back with allocation move?

::: misses
- [L] It also reads the line in first.
- [any] One line in, one line out.
:::
:::
:::

::: check
kind: parts
given: f = 15 | 20 | 30; m = 6 | 8 | 10

## A program is {f}% stores through a write-through cache; the write buffer drains one store every {m} cycles.

::: part
answer: 1/m
tolerance: 0.5%

### How many stores per cycle can memory take?

::: misses
- [any] One per {m} cycles.
:::
:::

::: part
answer: f*m/100
tolerance: 0.5%

### What is the lowest CPI the stores allow, once the buffer is full?

::: misses
- [any] $fm$: instructions per cycle can't exceed $1/(fm)$.
:::
:::
:::

::: check
kind: parts
given: P = 40 | 80 | 120; d = 25 | 50; m = 3 | 5

## A write-back cache misses {m}% of the time; a miss takes {P} cycles, plus {P} more if the evicted line is dirty, which {d}% are. Hits take 1 cycle.

::: part
answer: P*(1 + d/100)
tolerance: 0.5%

### What is the average miss penalty?

::: misses
- [any] $P(1 + d)$.
:::
:::

::: part
answer: 1 + m/100*P*(1 + d/100)
tolerance: 0.5%

### What is the AMAT?

::: misses
- [any] 1 + miss rate × penalty.
:::
:::
:::

::: check
kind: parts
given: B = 512 | 1500 | 9000; L = 32 | 64; o = 12 | 24

## A DMA receive buffer of {B} bytes, with {L}-byte cache lines.

::: part
answer: ceil(B/L)
tolerance: 0

### How many lines must be invalidated if it starts on a line boundary?

::: misses
- [any] $\lceil B/L \rceil$.
:::
:::

::: part
answer: ceil((o + B)/L)
tolerance: 0

### And if it starts {o} bytes into a line?

::: misses
- [any] From the line's start to {o} + {B}.
:::
:::
:::

::: check
kind: parts
given: C = 8 | 32 | 64; L = 32 | 64

## A direct-mapped {C} KiB write-back cache with {L}-byte lines; each line keeps a tag, a valid bit and a dirty bit, for 32-bit addresses.

::: part
answer: 32 - round(log(C*1024)/log(2))
tolerance: 0

### How many tag bits per line?

::: misses
- [any] 32 less the index and offset bits together.
:::
:::

::: part
answer: C*1024/L*(32 - round(log(C*1024)/log(2)) + 2)
tolerance: 0

### How many bits of tags, valid and dirty bits are there in all?

::: misses
- [any] Lines × (tag + 2).
:::
:::
:::

::: check
kind: parts
given: h = 1 | 2; P = 50 | 100 | 200; T = 2 | 4

## A cache hits in {h} cycle(s) with a {P}-cycle miss penalty.

::: part
answer: (T - h)/P*100
unit: %
tolerance: 0.5%

### What miss rate gives an AMAT of {T} cycles?

::: misses
- [any] $h + mP = T$.
:::
:::

::: part
answer: 100 - (T - h)/P*100
unit: %
tolerance: 0.5%

### What hit rate is that?

::: misses
- [any] 100% less the miss rate.
:::
:::
:::

::: check
kind: parts
given: r = 1.2 | 1.4; m = 1 | 2 | 4; P = 50 | 100

## A processor with a base CPI of 1 makes {r} memory accesses per instruction; {m}% miss, costing {P} cycles each.

::: part
answer: 1 + r*m/100*P
tolerance: 0.5%

### What is its CPI?

::: misses
- [1 + m/100*P] Count every access, {r} per instruction.
- [any] $1 + r \cdot m \cdot P$.
:::
:::

::: part
answer: r*m/100*P/(1 + r*m/100*P)*100
unit: %
tolerance: 0.5%

### What share of its time goes to memory stalls?

::: misses
- [any] Stall cycles ÷ total cycles.
:::
:::
:::

::: check
kind: parts
given: N = 100 | 1000 | 50000

## Two cores each increment their own counter {N} times, taking turns. The counters share one 64-byte line.

::: part
answer: 2*N
tolerance: 0

### How many writes miss, with a write-invalidate protocol?

::: misses
- [2] That's with the counters in separate lines.
- [any] Each write finds the line taken by the other core.
:::
:::

::: part
answer: 2
tolerance: 0

### How many miss if each counter is padded into its own line?

::: misses
- [any] Only the first write by each core.
:::
:::
:::

::: check
kind: parts
given: T = 40 | 60; B = 8 | 16 | 32; L = 32 | 64

## A memory has {T} ns of latency and then delivers {B} bytes per ns.

::: part
answer: T + L/B
unit: ns
tolerance: 0.5%

### What is the miss penalty for {L}-byte lines?

::: misses
- [any] Latency plus transfer time.
:::
:::

::: part
answer: T + 2*L/B
unit: ns
tolerance: 0.5%

### And for lines twice as long?

::: misses
- [any] Only the transfer part doubles.
:::
:::
:::

::: check
kind: parts
given: N = 1000 | 8192; L = 32 | 64

## A loop stores {N} words, once each, into a fresh array, with {L}-byte lines.

::: part
answer: 4*N
unit: bytes
tolerance: 0

### How many bytes cross the bus with write-through, no-allocate?

::: misses
- [any] The bytes stored.
:::
:::

::: part
answer: 8*N
unit: bytes
tolerance: 0.5%

### And with write-back, write-allocate?

::: misses
- [4*N] Each line is read in before it's written.
- [any] Every line in, then every line out.
:::
:::
:::

::: check
kind: parts
given: m1 = 3 | 5 | 10; m2 = 10 | 30 | 50

## L1 misses {m1}% of accesses, and L2 misses {m2}% of the accesses that reach it.

::: part
answer: m1*m2/100
unit: %
tolerance: 0.5%

### What share of all accesses go to memory?

::: misses
- [m2] That's L2's local miss rate.
- [any] $m_1 m_2$.
:::
:::

::: part
answer: m1
unit: %
tolerance: 0.5%

### What share reach L2?

::: misses
- [any] The L1 misses.
:::
:::
:::

::: check
kind: parts
given: k = 3 | 4 | 6; w = 2 | 4 | 8

## A loop touches {k} arrays in turn, all mapping to the same set of a {w}-way LRU cache.

::: part
answer: k*step(k - w - 0.5)
tolerance: 0

### After the first round, how many misses does each round of {k} accesses take?

::: misses
- [any] If {k} lines fit in {w} ways, none; otherwise LRU evicts each just before it's needed.
:::
:::

::: part
answer: k
tolerance: 0

### And in a direct-mapped cache?

::: misses
- [any] One way: each access evicts the one before.
:::
:::
:::

::: check
kind: parts
given: n = 32 | 100 | 256; L = 32 | 64

## An n × n matrix of ints, n = {n}, is summed row by row through a cache with {L}-byte lines.

::: part
answer: ceil(n*n*4/L)
tolerance: 0

### How many lines does it touch?

::: misses
- [any] Its bytes ÷ the line size.
:::
:::

::: part
answer: 4/L
tolerance: 0.5%

### What fraction of accesses miss, if nothing was cached before?

::: misses
- [any] One per line of {L}/4 ints.
:::
:::
:::
:::
