Memory hierarchy
แนะนำใหม่ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| memory hierarchy/ˈmeməri ˈhaɪərɑːki/ | The layers of storage from small, fast registers and caches to large, slow main memory and disk. |
| locality/ləʊˈkælɪti/ | ความเป็นท้องถิ่น |
| direct-mapped cache/daɪˈrekt mæpt kæʃ/ | แคชแบบแมปโดยตรง |
| tag/tæɡ/ | แท็ก |
| index/ˈɪndeks/ | ดรรชนี |
| block offset/blɒk ˈɒfset/ | ตำแหน่งภายในบล็อก |
| Average memory access time/ˈævrɪdʒ ˈmeməri ˈækses taɪm/ | hit time plus miss rate times miss penalty |
| evicts/ɪˈvɪkts/ | 驱逐 |
| 2-way set-associative/tuː weɪ set əˈsəʊsɪətɪv/ | a cache in which each memory block may be placed in either of the two lines of one set |
| write-back/raɪt bæk/ | a cache policy that updates main memory only when a changed line is evicted |
| write-through/raɪt θruː/ | a cache policy that writes every update to main memory at once |
Split the address
- The memory hierarchy 存储层次 puts small, fast memory above big, slow memory and relies on locality 局部性. A direct-mapped cache 直接映射缓存 gives each block exactly one slot, so the address splits into tag 标记 | index 索引 | block offset 块内偏移.
- The course cache: 4 KiB with 16-byte lines, so 256 lines. That gives offset 4 bits, index 8 bits and tag 20 bits. Address $0x4A8$ has offset 8 and index 74.

A 4 KiB direct-mapped cache with 16-byte lines has — index bits
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
256 lines need log2 256 = 8 index bits; offset 4, tag 20.
Put the steps of a direct-mapped cache lookup in order.
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Direct-mapped means the index alone picks the line, so only one tag comparison is needed: fast, but two blocks with the same index evict each other.
AMAT prices the misses
- Average memory access time 平均访存时间 (AMAT) $= t_{\text{hit}} + p_{\text{miss}} \times t_{\text{penalty}}$. With a 1-cycle hit, a 2% miss ratio and a 100-cycle penalty, AMAT $= 1 + 0.02 \times 100 = 3$ cycles. Halve the misses to 1% and AMAT $= 2$.
- AMAT depends on the misses, not the hits, so the hierarchy only works if locality is good. Any other block that maps to line 74 evicts 驱逐 the block that is there: that is the price of direct mapping, which needs only one comparator.
Hit 1 cycle, miss ratio 2%, penalty 100: AMAT is —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
AMAT = 1 + 0.02 × 100 = 3 cycles; misses, not hits, set the sensitivity.
Locality by layout
- Scan a $64 \times 64$ int array row by row, with 16-byte lines ($4$ ints per line): the first access misses and the next three hit. The miss rate is 25%, all compulsory.
- Jump by 16 ints (64 bytes) at a time and each access lands on a new line that is never reused before it is evicted: every access misses, 100%. Same array, same cache, about four times the AMAT (101 cycles against 26). Data layout is a performance feature.
Row-major scanning of ints with 16-byte lines gives a miss rate of —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Four ints per line: one miss, three hits — 25% plus compulsory only.
Associativity and a second level
- A 2-way set-associative 两路组相联 cache of the same 4 KiB with 16-byte lines has $128$ sets: offset 4 bits, index 7, tag 21. Two blocks that fought over one direct-mapped line can now sit side by side. The price is two tag comparators.
- Add an L2 cache: L1 hits in 1 cycle and misses $5\%$ of the time; L2 takes 10 cycles and misses $20\%$ of what reaches it; memory costs 100. Each miss pays for the next level:
- Without the L2 it would be $1 + 0.05 \times 100 = 6$ cycles: the second level cuts the average by more than half.
- A write-back 写回 cache writes a changed (dirty) line only when it is evicted; write-through 写直达 writes every store.
A 4-way set-associative cache holds 4 KiB in 16-byte lines. How many index bits does an address need?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
256 lines / 4 ways = 64 sets, and 2^6 = 64.
L1: 1-cycle hit, 4% misses. L2: 12 cycles, 25% local misses. Memory: 200 cycles. What is the AMAT in cycles?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
1 + 0.04 x (12 + 0.25 x 200) = 1 + 0.04 x 62 = 3.48.
Three kinds of miss
- Every miss has one of three causes, and each cause has its own cure:
| Miss | Cause | Cure |
|---|---|---|
| Compulsory | the first touch of a block | longer lines, prefetching |
| Capacity | working set bigger than the cache | a bigger cache, a smaller working set |
| Conflict | blocks fight over one set | more ways, or move the data |
- Two arrays exactly 4 KiB apart map to the same lines of the 4 KiB direct-mapped cache, so
a[i] + b[i]misses on every access. A 2-way cache, or moving one array by one line, removes those conflict misses without changing the program's logic.
Two arrays 4 KiB apart in a 4 KiB direct-mapped cache miss on every access. These are:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
They fight over the same lines; more associativity fixes them.
Only a bigger cache can remove conflict misses.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Associativity, or moving one array by a line, removes them in the same capacity.
Worked example: misses in the CPI
- Each instruction makes 1.3 memory accesses (1 fetch plus 0.3 data). With a 2% miss rate and a 100-cycle penalty, stalls add $1.3 \times 0.02 \times 100 = 2.6$ cycles: CPI $1.129 + 2.6 = \mathbf{3.73}$.
- Halving the miss rate gives 2.43, a bigger gain than any pipeline trick. In a real program, memory stalls usually make up most of the CPI.
Base CPI 1.129, 1.3 accesses per instruction, 2% misses, 100-cycle penalty: CPI (two decimals)?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
$1.129 + 1.3 \times 0.02 \times 100 = 3.729$.
Marks that slip away
- Splitting the address the wrong way round. Offset is the low bits, then index, then tag: $20 \mid 8 \mid 4$ for the course cache.
- Leaving out the hit time. AMAT $= t_{\text{hit}} + p_{\text{miss}} \times t_{\text{penalty}}$: every access pays the hit time.
- Ignoring the access order. The same array costs four times the AMAT with stride 16 as row by row.
Recap
- Address = tag | index | offset; 4 KiB direct-mapped with 16-byte lines: 20 | 8 | 4.
- AMAT $= t_{\text{hit}} + p_{\text{miss}} \times t_{\text{penalty}}$, nested for each level.
- Locality decides the miss rate: row by row $25\%$, stride 16 $100\%$.
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้