Exercise 5.16 — Write Buffer FSM 設計
Cache:direct-mapped, write-back, write-allocate(參考 Figure 5.40)
在此基礎上加入一個容量 1 block 的 write buffer。
Write buffer 的作用:dirty miss 時,不用等 writeback 完才讀新 block,先把 dirty block 丟進 write buffer,立刻 fetch 新 block;dirty block 再慢慢寫回 memory。
5.16.1
Write buffer 正在把 dirty block writeback 到 memory 時,CPU 發出一個 cache hit 的請求,應該怎麼處理?
看答案
Cache hit 不需要存取 memory,直接從 cache 服務 CPU。
CPU 不需要等,write buffer 繼續在背景 writeback,兩者並行。
5.16.2
Write buffer 正在 writeback 時,CPU 發出一個 cache miss 的請求,應該怎麼處理?
看答案
Cache miss 需要存取 memory bus 來 fetch 新 block,但 bus 正被 write buffer 佔用。
CPU 需要等,直到 write buffer 完成 writeback 釋放 bus,才能開始 fetch。
5.16.3
設計一個支援 write buffer 的 FSM,說明各狀態及轉換條件。
看答案
State 0:Idle(等待 CPU 請求)
→ Hit:State 0(直接服務)
→ Miss,block not dirty:State 1(fetch)
→ Miss,block dirty:State 2(把 dirty block → write buffer,同時開始 fetch)
State 1:Fetching(從 memory 取新 block,write buffer 空閒)
→ Fetch 完成:State 0
State 2:Fetching + Write Buffer Busy(fetch 新 block 同時 write buffer 有 dirty block 待寫)
→ CPU hit 請求:繼續 State 2(cache hit 不需要 bus,直接服務)
→ CPU miss 請求:繼續 State 2(需要 bus 但忙,等待)
→ Fetch 完成:State 3(開始 writeback)
State 3:Writing Back(把 write buffer 的 dirty block 寫回 memory)
→ Writeback 完成,無 pending miss:State 0
→ Writeback 完成,有 pending miss:State 1(開始 fetch)
Exercise 5.17 — Cache Coherence & Memory Consistency
X[0] = X[1] = 0(初始,同一個 cache block X)
P1: X[0]++; X[1] = 3; | P2: X[0] = 5; X[1] += 2;
5.17.1
正確的 coherence protocol 下,X[0] 和 X[1] 有哪些可能的最終值組合?
若 protocol 不保證 coherence,再列出至少一個額外可能的值。
看答案
Coherent 下可能的值(X[0], X[1]):
• P1全先:X[0]=1→P2寫5,X[1]=3→P2+=2=5 → (5, 5)
• P2全先:X[0]=5→P1++=6,X[1]=2→P1=3 → (6, 3)
• 交錯:P1寫X[0]=1,P2覆寫X[0]=5,P1寫X[1]=3,P2讀X[1]=3+2=5 → (5, 5)
• 交錯:P2寫X[0]=5,P1++=6,P2寫X[1]+=2=2,P1=3 → (6, 3)
→ 結合所有合法交錯:(5,5), (6,3), (5,3), (6,5)
不 coherent 下額外可能:
P2 看不到 P1 的 X[0]=1 就讀了舊值 → 如 (1, 2) 或 (1, 5)
5.17.2
以 Snooping protocol 為例,列出一個合法的操作序列(以得到其中一種結果為例)。
看答案
以 P1 先執行為例,最終結果 (5, 5):
1. P1 讀 X[0](miss)→ 廣播讀取,取得 block,X[0]=0
2. P1 寫 X[0]=1(hit)→ 廣播 Invalidate,P2 的 copy 失效
3. P2 讀 X[0](miss,因被 invalidate)→ 取得 X[0]=1
4. P2 寫 X[0]=5 → 廣播 Invalidate
5. P1 寫 X[1]=3 → 廣播 Invalidate
6. P2 讀 X[1](miss)→ 取得 X[1]=3
7. P2 寫 X[1]=3+2=5
最終:X[0]=5, X[1]=5
5.17.3
執行以上讀寫操作,best-case 和 worst-case 各需要幾次 cache miss?
看答案
Best case(2 misses):block 本來就在兩個 cache 中,只有寫入時的 invalidate 造成對方 miss,P1 寫 X[0] invalidate P2 → P2 再讀時 miss (1),P1 寫 X[1] invalidate P2 → P2 再讀時 miss (2)
Worst case(4 misses):P1 讀 X[0] miss (1),P2 讀 X[0] miss (2)(被 invalidate),P1 讀 X[1] miss (3),P2 讀 X[1] miss (4)
Memory consistency 部分(A=B=0 初始,不同 cache blocks):
P1: A=1; B=2; A+=2; B++; (最終 A=3, B=3)
P2: C=B; D=A;
5.17.4
在保證 consistency 的情況下,C 和 D 有哪些可能的值組合?
看答案
Consistency 保證:P2 看到的 B 和 A 必須符合 P1 的寫入順序(看到比較新的 B,就不能看到比 B 更舊版本對應的 A)
合法的 (C, D) 組合:
(0,0), (0,1), (0,3), (2,1), (2,3), (3,3)
5.17.5
若不保證 consistency,額外可能出現哪些 (C,D) 值?
看答案
如 (3, 0)(看到最新 B=3 但 A=0,違反寫入順序)或 (2, 0)
5.17.6
哪種 write policy + write allocation 組合,最能讓 coherence protocol 實作變簡單?
看答案
Write-through + No write-allocate 最簡單:
每次寫入直接寫到 memory,memory 永遠是最新值;其他 cache 只需 invalidate 或直接從 memory 讀;不用追蹤 dirty bits,snoop 邏輯最簡單。
Write-back + write-allocate 效能好但 dirty block 管理讓 coherence 複雜很多。
Exercise 5.18 — CMP Private vs Shared L2 Cache
L1 miss:每 32 instructions 一次(= 0.03125 misses/instr)
Private L2 hit = 5 cycles,Shared L2 hit = 20 cycles,Memory = 180 cycles
| Private MPI(misses/instr) | Shared MPI |
| Benchmark A | 0.30% = 0.003 | 0.12% = 0.0012 |
| Benchmark B | 0.06% = 0.0006 | 0.03% = 0.0003 |
5.18.1
哪種 cache 設計對各 benchmark 更好?用數據支持你的結論。
看答案
CPI = 1 + (MPI × memory_cycles) + ((L1_miss_rate - MPI) × L2_hit_cycles)
L1 miss rate = 1/32 = 0.03125
Benchmark A:
Private: 1 + 0.003×180 + (0.03125-0.003)×5 = 1 + 0.54 + 0.141 = 1.681
Shared: 1 + 0.0012×180 + (0.03125-0.0012)×20 = 1 + 0.216 + 0.601 = 1.817
→ Private 較好
Benchmark B:
Private: 1 + 0.0006×180 + (0.03125-0.0006)×5 = 1 + 0.108 + 0.153 = 1.261
Shared: 1 + 0.0003×180 + (0.03125-0.0003)×20 = 1 + 0.054 + 0.619 = 1.673
→ Private 較好
5.18.2
① Shared cache latency 加倍(→40 cycles)時哪種更好?
② Off-chip memory latency 加倍(→360 cycles)時哪種更好?
看答案
① Shared latency 加倍 → Shared 的 L2 hit cost 更高,Private 優勢更大 → Private 更好
② Memory latency 加倍 → miss penalty 大增,Shared 的低 MPI 優勢更顯著(少一半的 memory access)→ Shared 可能更好
5.18.3
討論 single-threaded、multi-threaded、multiprogrammed workload 下 private vs shared L2 的優缺點。若加上 on-chip L3 cache 呢?
看答案
Private L2:優→低延遲;缺→跨 core 無法共享資料,重複資料浪費空間
Shared L2:優→多執行緒可共享資料減少 miss,容量利用率高;缺→延遲高,bandwidth 競爭
Single-threaded:Private 優(低延遲);Multi-threaded:Shared 優(thread 間共享資料多);Multiprogrammed:Private 優(各程式資料獨立)
加上 L3:主流設計變成 Private L2 + Shared L3,兩全其美——L2 快,L3 大且共享
5.18.4
Base CPI = 1(ideal L2),non-blocking cache 讓 concurrent L2 misses 從 1 提升到 2,
對 Shared L2 和 Private L2 各提升多少效能?
看答案
Concurrent misses 加倍 → effective miss penalty 減半 → memory stall 部分大約砍半
以 Benchmark A 為例:
Shared L2 原 CPI=1.817,stall=0.817 → 砍半 → stall≈0.41 → 新 CPI≈1.41(提升約 22%)
Private L2 原 CPI=1.681,stall=0.681 → 砍半 → stall≈0.34 → 新 CPI≈1.34(提升約 20%)
5.18.5
假設每 18 個月 core 數加倍,3 年後需要多少倍的 off-chip memory bandwidth 才能維持相同的 per-core 效能?
看答案
3 年 = 2 個 18 個月週期 → cores × 2² = 4×
維持相同 per-core bandwidth → 總 bandwidth 需要 4 倍
5.18.6
在整個 memory hierarchy 中,哪些優化可以增加 concurrent misses 的數量?
看答案
① Non-blocking (lockup-free) cache:允許 hit under miss / miss under miss
② Out-of-order execution:CPU 在 miss 期間繼續執行不相依指令
③ Hardware prefetching:預取資料,隱藏 miss latency
④ MSHR (Miss Status Holding Registers):同時追蹤多個 outstanding misses
⑤ Memory-level parallelism (MLP):多個 miss 同時在 memory 系統中進行