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 ํ”ผ๋ผ๋ฏธ๋“œ ๋ฐ locality ๊ฐœ๋…

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)

Locality์™€ Cache ๊ฐœ๋…

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 ๊ตฌ์กฐ

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์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง

Direct-mapped Cache ์˜ˆ์ œ

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)

Block size ๋ฐ Miss rate ๊ด€๊ณ„

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์™€ ๋ณ‘๋ ฌ ๋น„๊ต

Set associative cache ๊ตฌ์กฐ

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 reference ์˜ˆ์ œ

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 read ๋ฐ miss handling

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 Write ์ „๋žต

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

Multi-level cache ๋ฐ Cache coherence

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

Write invalidate protocol ๋ฐ Summary

๋น„์Šทํ•œ ๊ธ€ ์ถ”์ฒœ

Comments (0)

No comments yet. Be the first to comment!