3. Memory Herachy
3์ฅ Memory Hierarchy ๊ฐ์
Memory Hierarchy๋ "๊ฐ์ฅ ๋น ๋ฅธ ๋ฉ๋ชจ๋ฆฌ์ ์๋ + ๊ฐ์ฅ ํฐ ๋ฉ๋ชจ๋ฆฌ์ ์ฉ๋"์ด๋ผ๋ ๊ฐ์ ๋ฉ๋ชจ๋ฆฌ ํ์(illusion)์ ๋ง๋ค๊ธฐ ์ํ ํต์ฌ ์ต์ ํ. Program locality(Temporal & Spatial)๋ฅผ ํ์ฉํด ์์ ๊ณ ์ ๋ฉ๋ชจ๋ฆฌ์ ํฐ ์ ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์กฐํฉํ๋ค.
Memory Hierarchy: Solution to Memory Wall
๋ฉ๋ชจ๋ฆฌ ์์คํ ์ Big Picture
- Memory: Stores programs and data
- Problem: Memory is too slow and too small compared to CPU
(fast, small, expensive)
[ Register ]
[ L1 $ ]
[ L2 $ ]
[ Main memory ]
[ Disk ]
[ Tape (slow, huge, cheap) ]
- Speed: sub-ns โ ms
- Size: 1 KB โ TB
Memory Hierarchy์ ์ ์
An optimization resulting from a perfect match between memory technology and two types of program locality.
- Temporal locality: ์๊ฐ์ ์ง์ญ์ฑ. ํ ๋ฒ ์ฐธ์กฐ๋ ๋ฐ์ดํฐ๋ ๊ณง ๋ค์ ์ฐธ์กฐ๋ ๊ฐ๋ฅ์ฑ์ด ๋์
- Spatial locality: ๊ณต๊ฐ์ ์ง์ญ์ฑ. ํ ๋ฐ์ดํฐ์ ์ ๊ทผํ๋ฉด ๊ทธ ๊ทผ์ฒ์ ๋ฐ์ดํฐ๋ ๊ณง ์ฐธ์กฐ๋จ
Goal: Provide a "virtual" memory technology (illusion) that has an access time of the highest-level memory with the size and cost of the lowest-level memory.

Memory Hierarchy Terminology
- Hit: Accessed data is found in upper level (์์ ๋ ๋ฒจ์ ์์ โ ์ฑ๊ณต)
- Miss: ์์ ๋ ๋ฒจ์ ์์, ์๋ ๋ ๋ฒจ์์ ๊ฐ์ ธ์์ผ ํจ
- Hit time: ์์ ๋ ๋ฒจ access ์๊ฐ
- Miss penalty: Miss ๋ฐ์ ์ ์๋ ๋ ๋ฒจ์์ ๊ฐ์ ธ์ค๋ ์ถ๊ฐ ์๊ฐ
- Hit ratio (Hit rate): $\frac{\text{Hits}}{\text{Accesses}}$, Miss ratio = 1 - Hit ratio
- Average memory access time (AMAT):
$$\text{AMAT} = \text{Hit time} + \text{Miss rate} \times \text{Miss penalty}$$
L0/L1 Cache ๊ฐ์
- Upper level: SRAM (fast, expensive)
- Lower level: DRAM (large, slow, cheap)
- Goal: To provide a "virtual" memory technology
- Additional benefits:
- Reduction of memory bandwidth consumed by processor
- More memory bandwidth to I/O
- No need to change the ISA
- CPU ์ ์ฅ์์๋ ํ๋์ ํฐ ๋ฉ๋ชจ๋ฆฌ๋ก ๋ณด์ธ๋ค (Illusion)

Four Big Cache Questions
- Q1: Where can block be placed in cache? โ Block placement
- Q2: How can block be found in cache? โ Block identification
- Q3: Which block should be replaced on a miss? โ Block replacement
- Q4: What happens on a write? โ Write strategy
Block Placement โ 3๊ฐ์ง ๋ฐฉ์
- Direct-mapped: ๊ฐ block์ด ์บ์์ ํ ์๋ฆฌ์๋ง ์ฌ ์ ์์. $$\text{Cache index} = \text{Block address} \bmod \text{Cache block ์}$$
- Fully associative: ์ด๋ ์๋ฆฌ๋ ๊ฐ๋ฅ (๋ชจ๋ slot ๊ฒ์ ํ์)
- Set associative: ์ค๊ฐ. ์ฌ๋ฌ set๋ก ๋๋๊ณ set ๋ด์์๋ fully associative. N-way set associative.
Direct-Mapped Cache ์์ธ
- Each memory block is mapped to a single block in cache
- Mapped cache block is determined by memory block address $\bmod$ number of blocks in cache
- Cache index: Block # (bit address)
- ๊ฒฐ๊ณผ ๋น๊ต: tag์ block number๋ก hit/miss ํ๋จ + valid bit
Block Identification
- Every cache block has an address tag that identifies its location in memory
- Hit when tag and valid bits match + Miss otherwise
- What happens when a cache block is empty? โ Mark this condition with a valid bit (0 = empty)

Direct-Mapped Cache ์์
16B block, 8 cache blocks, 5-bit index, 3-bit offset. Memory์์ Cache block index๋ก ์ ๊ทผ โ tag ๋น๊ต โ Hit/Miss ํ์ .
Step by step:
- Address decoding:
[tag | index | offset] - Block address = Memory address / Block size
- Cache index = Block address mod Cache blocks
- Miss ratio๋ temporal/spatial locality์ ๋ฐ๋ผ ๋ฌ๋ผ์ง

Block Size Considerations
- Larger blocks: spatial locality ํ์ฉ ๊ฐ์, but latency ์ฆ๊ฐ
- But in a fixed-size cache:
- Larger blocks โ fewer of them โ more competition โ miss rate ์ฆ๊ฐ
- Larger blocks โ pollution (cache์ ์ ์ฐ์ด๋ ๋ฐ์ดํฐ๊ฐ ๋ค์ด์ด)
- Larger miss penalty โ can override benefit of reduced miss rate
- Early restart and critical-word-first can help
Miss rate vs Block size
- ๋๋ฌด ์์ผ๋ฉด spatial locality ํ์ฉ ๋ถ์กฑ
- ๋๋ฌด ํฌ๋ฉด cache ์ค์ผ + miss penalty ์ฆ๊ฐ
- ์ค๊ฐ ๊ฐ์ด optimal (๋ณดํต 32~128B)

Cache Organization Spectrum
Direct-mapped vs Fully associative vs Set-associative
- Set-associative cache: ๋ฏธ๋ฆฌ ์ ํด์ง set ์์์๋ fully associative
- Four-way set associative: 4๊ฐ set ร N blocks
- For fixed cache capacity, higher associativity tends to higher hit rate
Set-Associative Cache ๊ตฌ์ฑ
- Index โ Set ์ ํ โ Set ๋ด tag ๋น๊ต (fully associative)
- 4-way์ ๊ฒฝ์ฐ ๋์ผ index๋ก 4๊ฐ tag์ ๋ณ๋ ฌ ๋น๊ต

Memory Reference Sequence ์์
Cache initially empty, 1 hit for the third memory reference with the same capacity.
Step by step access pattern์ ์ถ์ ํ๋ฉฐ ๊ฐ cache์ hit/miss ์นด์ดํธ๋ฅผ ๋น๊ต. Set-associative๊ฐ direct-mapped๋ณด๋ค hit ๋ง์.

Cache Read ๋์
On cache hit
CPU proceeds normally โ ์ ์ ์๋.
On cache miss (handled completely by hardware)
- Stall the CPU pipeline
- Fetch the missed block from the next level of hierarchy
- Instruction cache miss โ Restart instruction fetch
- Data cache miss โ Complete data access
Instruction Fetch Flow (Instruction Cache Miss)
Clock Cycle 1์์ Compare, Cycle 2์์ Miss detected โ Stall โ Fetch completes โ Pipeline restart. DRAM์์ ์ฝ์ด์ฌ ๋๊น์ง Stall.
Load Instruction Flow (Data Cache Miss)
Data๋ฅผ ์ฝ๋ค๊ฐ miss๊ฐ ๋๋ ๊ฒ์ Load instruction์ผ ๋๋ง. Cycle 4์์ compare, Cycle 5์์ miss detected โ Stall โ Load completes โ Pipeline restart.

Cache Write ์ ๋ต
Write-Through
- Always write the data into both the cache and main memory
- Simple but slow โ increases memory traffic
- Needs a write buffer (์ฃผ๋ฉ๋ชจ๋ฆฌ ์ฐ๊ธฐ๋ฅผ ๋น๋๊ธฐ๋ก)
Write-Back
- Write the data into the cache only
- Update the main memory when a dirty block is replaced
- Requires a dirty bit (๋ณ๊ฒฝ ์ฌ๋ถ ํ์)
- Fast but complex to implement and causes a consistency problem
Write Miss Handling
- Write-allocate: Miss ์ block์ ์บ์์ ์ฌ๋ฆฌ๊ณ write
- No-write-allocate: Miss ์ ๋ฐ๋ก ๋ฉ๋ชจ๋ฆฌ์๋ง write (cache ๊ฑด๋๋ฆฌ์ง X)
์ผ๋ฐ์ ์ผ๋ก Write-back์ Write-allocate, Write-through๋ No-write-allocate์ ์กฐํฉ.

Cache Performance
AMAT
$$\text{AMAT} = \text{Hit time} + \text{Miss rate} \times \text{Miss penalty}$$
$$\text{Average memory access time} = \frac{1}{N} \sum (\text{hit time} + \text{miss rate} \times \text{miss penalty})$$
Improving Cache Performance
- Decrease hit time: Small & direct-mapped cache
- Decrease miss rate: Bigger cache, more associativity
- Decrease miss penalty: Multi-level cache, critical-word first, prefetching
Current Cache Organizations
| Level | Cache size | Access time | Associativity |
|---|---|---|---|
| L1 D-cache | 32 KB | 1 cycle | 4-way |
| L1 I-cache | 32 KB | 1 cycle | 4-way |
| L2 | 256 KB | 10 cycles | 8-way |
| L3 | 8 MB | 30 cycles | 16-way |
| Main memory | GB | 100+ cycles | - |
Cache Coherence Problem
Suppose two CPU cores share a physical address space.
Write-through caches ์์:
| Time step | Event | CPU A''s cache | CPU B''s cache | Memory |
|---|---|---|---|---|
| 0 | - | - | - | 0 |
| 1 | CPU A reads X | 0 | - | 0 |
| 2 | CPU A writes 1 to X | 1 | - | 1 |
| 3 | CPU B reads X | 1 | 0 | 1 |
โ CPU B์ ์บ์๊ฐ stale(0) โ coherence problem.
Snoopy Protocol
- Write-Broadcast: Write to shared data โ an invalidate is sent to all other caches
- Write-Invalidate: Write to shared data โ broadcast on bus, processors snoop and update copies

Write Invalidate Protocol ์์ธ
Cache gets exclusive access to a block when it is to be written:
- Broadcasts an invalidate message on the bus
- Subsequent read in another cache misses โ Owning cache supplies updated value
์์ ์๋๋ฆฌ์ค (Write-back ์ฌ์ฉ):
| CPU activity | Bus activity | CPU A''s cache | CPU B''s cache | Memory |
|---|---|---|---|---|
| - | - | - | - | 0 |
| CPU A reads X | Cache miss for X | 0 | - | 0 |
| CPU B reads X | Cache miss for X | 0 | 0 | 0 |
| CPU A writes 1 to X | Invalidate for X | 1 | X (invalid) | 0 |
| CPU B reads X | Cache miss for X | 1 | 1 | 1 |
โ Owning cache (A)๊ฐ ์ต์ ๊ฐ์ supply.
์ ๋ฆฌ
- Memory hierarchies are an optimization resulting from a perfect match between memory technology and two types of program locality
- Temporal locality / Spatial locality
- The goal is to provide a "virtual" memory technology (illusion) that has an access time of the highest-level memory with the size and cost of the lowest-level memory
- Cache memory is an instance of a memory hierarchy
- Exploits both temporal and spatial localities
- Direct-mapped caches are simple and fast but have higher miss rates
- Set-associative caches have lower miss rates but are complex and slow
- Multilevel caches are becoming increasingly popular
- Cache coherence protocols ensure consistency among multiple caches

Comments (0)
No comments yet. Be the first to comment!
Please to write a comment.