Download Topic 16: Memory Caching and more Summaries Law in PDF only on Docsity!
Topic 16: Memory Caching 1
COS / ELE 375 Computer Architecture and Organization Princeton University^ Fall 2015 Prof. David August
These the same? Problem size is defined by SIZE_X and SIZE_Y Code 1: for(x^ =^ 0;^ x^ < 2
SIZE_X;^ x++) for(y = 0; y^ <^ SIZE_Y;^ y++) sum += Array[x][y]; Code 2: for(y = 0; y <^ SIZE_Y;^ y++) for(x = 0; x^ <^ SIZE_X;^ x++) sum += Array[x][y];
2 Bytes of Memory (circa 1947) 3
-^ Maurice Wilkes, in 1947,^ with first mercury tank^ memories built for EDSAC.
Memory (circa 2004) 4
We have a problem! 5
μProc 60%/yr. (2X/1.5yr) DRAM 9%/yr. (2X/10 yrs)
19891990199119921993199419951996199719981999 CPU DRAM^2000 1982
Processor-Memory Performance Gap: (grows 50% / year)
Performance
“Moore’s Law” Time Processor-DRAM Performance Gap (latency)
Memory Speed and Cost 6
The Principle of Locality •^ Program access a relatively small portion of the address^ space at any instant of time. •^ The^ “90-10”^ rule… Temporal Locality •^ If an item is referenced, it will tend to be referenced^ again soon Spatial Locality •^ If an item is referenced, nearby items will tend to be^ referenced soon 7
Temporal and Spatial Locality for^ (i=0;^ i<1000;^ i++)^8
Data reference stream locality? { for (j=0; j<1000; j++) { A[i,j] = B[i,j] + C[i,j]; Instruction stream locality? } } if (errorcond) { … Working Sets: } for (i=0; i<100; i++) { •^ Working set refers to portion for (j=0; j<100; j++) {^ of the address space accessed E[i,j] = D[i,j] * A[i,j]; } •^ Different phases of execution }^ may localize on different^ pieces of data/code (phased^ behavior)
Direct Mapped Cache^ Mapping: address is modulo the number of blocks^ When can this behave badly? 13
Pros?
Block Placement Direct Mapped Cache •^ How many hits, misses in direct mapped cache (mod 256)?^ •^ Read location 0: Miss^ •^ Read location 16: Miss^ •^ Read location 32: Miss^ •^ Read location 0: Hit^ •^ Read location 16: Hit^ •^ Read location 32: Hit^ •^ Read location 256: Miss^ •^ Read location 256: Hit^ •^ Read location 0: Miss •^ Miss rate = 5/9 = 55% •^ Note^ “types”^ of misses:^ •^ Cold misses^ •^ Conflict misses^ •^ Also a third type (not here): capacity misses^ •^ The three^ “C”s of cache misses 14
Direct Mapped Cache: Hardware 15
-^ Implementation of Mod •^ Tags •^ Valid •^ Data •^ How much state?
Direct Mapped Cache: Hardware Capture Spatial Locality
Know how to size these!
4 Questions for Caching Answers for Direct Mapped Caching? •^ Q1: Where can a block be placed in cache?^ (Block placement) •^ Q2: How is a block found if it is in cache?^ (Block identification) •^ Q3: Which block should be replaced on a miss?^ (Block replacement) •^ Q4: What happens on a write?^ (Write strategy) 17
18
Reduce Conflict Misses Memory time = Hit time + Prob(miss) * Miss penalty •^ Previous example demonstrated conflict misses in^ direct-mapped cache •^ Associativity: Allow blocks to go to several frames in^ cache •^ Helps avoid pathological conflicts: 0,256,0,256,0,256… •^ 2-way set associative: each block maps to either of 2^ cache frames •^ Fully associative: each block maps to any cache frame 19
Four-Way Set Associative Cache 20
Know how to size these!
25
We still have a problem! 26
μProc 60%/yr. (2X/1.5yr) DRAM 9%/yr. (2X/10 yrs)
19891990199119921993199419951996199719981999 CPU DRAM^2000 1982
Processor-Memory Performance Gap: (grows 50% / year)
Performance
“Moore’s Law” Time Processor-DRAM Performance Gap (latency)
Caching and The Principle of Locality •^ Program access a relatively small portion of the address^ space at any instant of time. (90-10 rule) Temporal Locality •^ If an item is referenced, it will tend to be referenced^ again soon Spatial Locality •^ If an item is referenced, nearby items will tend to be^ referenced soon 27
USE CACHES!
Spatial Locality in Instruction & Data Instruction and Data References have distinct behavior:^ Block size in Program^ words Split Instruction and Data Caches •^ Optimize for behavior •^ Smaller caches are faster •^ Problem - when data is code or code is data 28
Instruction^ Data miss^ miss rate^ rate
Effective combined^ miss rate gcc^1
6.1%^ 2.1%^
5.4% 4 2.0%^
1.7%^ 1.9% spice^1
1.2%^ 1.3%^
1.2% 4 0.3%^
0.6%^ 0.4%
Direct Mapped Cache, Increased Block Size Capture Spatial Locality
Block Size Increase: Miss Rate
Block Size Increase: Overall Performance^ Miss^ Penalty 31
Block Size Increased Miss Penalty Average^ and Miss Rate Access Time Block Size
Block Size Increase: Fill Time^ Larger Block Size 32
à^ Must Wait for Block to Fill Early Restart •^ Deliver word to process/continue execution when word^ requested is delivered. Critical Word First •^ Early Restart and Fetch the requested word first.
37 0.14 0.12^ 0.1 0.08 0.06 0.04 Miss Rate per Type 0.02^012481632 Cache Size (KB)
64128 1-way^ 2-way^ 4-way^ 8-way^ Capacity
Compulsory
Capacity Misses
Conflict Fully Associative Cache yields no conflict misses.
The Three Types of Cache Misses 3. Compulsory Misses •^ Occur when a program is first started •^ Cache does not contain any of program 38
’s data yet How do we reduce these?
Prefetching! Reduces all types of misses, including 39
“compulsory”!
Original Code:^ for(y^ =^ 0;^ y^ <^ SIZE_Y;
y++) for(x^ =^ 0;^ x^ <^ SIZE_X;
x++) sum^ +=^ Array[x][y]; Code with Prefetching (ignoring boundary condition): for(y =^ 0;^ y^ <^ SIZE_Y;
y++) for(x^ =^ 0;^ x^ <^ SIZE_X;
x++)^ { junk^ =^ Array[x+16][y]; sum^ +=^ Array[x][y]; }
40 0.14 0.12^ 0.1 0.08 0.06 0.04 Miss Rate per Type 0.02^012481632 Cache Size (KB)
64128 1-way^ 2-way^ 4-way^ 8-way^ Capacity
Compulsory Compulsory Misses
Conflict Fully Associative Cache yields no conflict misses.
3C Summary Compulsory misses (cold start) •^ Cold fact of life •^ First time data is referenced •^ Run billions of instructions, become insignificant Capacity misses •^ Working set is larger than cache size •^ Solution: increase cache size Conflict misses •^ Multiple memory locations mapped to the same location •^ One set fills up, but space in other cache sets •^ Solution 1: increase 41
cache size
-^ Solution 2: increase associative indexes
42
Multi-Level Caches Inclusive vs. Exclusive Sample Sizes: •^ L1: 32KB, 32 Byte Lines, 4-Way Set Associative •^ L2: 256KB, 128 Byte Lines, 8-Way Set Associative •^ L3: 4MB, 256 Byte Lines, Direct Mapped 43
Memory^ disk Options:^ separate^ data and instruction caches, or a^ Processor^ L1 Dcacheregs^ L2^ Cache^ L1 Icache
unified^ cache
Split Instruction and Data Caches Self-Modifying Code!?!? •^ Ignore problem, software must flush cache •^ Permit duplicate lines: invalidate I-cache line on write •^ Do not permit duplicate lines: data is exclusive to D- or^ I-Cache •^ Page Faults - More next week 44
- Cache Summary • Two types of locality: spatial and temporal • Spatial locality: larger block sizes • Cache contents include data, tags, and valid bits • Miss penalty is increasing (processor vs. memory) • Modern processors use set-associative caches worth the cost • Multi-level caches used to reduce miss penalty • Variations: Victim Caches, Trace Caches