Memory Chapter 7 Cache Memories Memory Challenges q

  • Slides: 30
Download presentation
Memory Chapter 7 Cache Memories

Memory Chapter 7 Cache Memories

Memory Challenges q q q Ideally one desires a huge amount of very fast

Memory Challenges q q q Ideally one desires a huge amount of very fast memory for little cost, but: l Fast memory is expensive l Cheap memory is slow The solution on a fixed budget for memory is a hierarchy l A small amount of very fast memory (Think SRAM) l A medium amount of slower memory (Think DRAM) l A large amount of slower yet memory (Think Disk) Comparing: Technology Access Time Cost/GB SRAM 0. 5 – 5 ns $4, 000 - $10, 000 DRAM 50 – 70 ns $100 - $200 Disk 5 – 20 ms $0. 50 - $2 Recall: We used 200 ps or 0. 2 ns in our pipeline study. Why the difference?

The “Memory Wall” Clocks per DRAM access Logic vs DRAM speed gap continues to

The “Memory Wall” Clocks per DRAM access Logic vs DRAM speed gap continues to grow Clocks per instruction q

Philosophically q How does one UTILIZE the very fast memory effectively? q Think “The

Philosophically q How does one UTILIZE the very fast memory effectively? q Think “The Principal of Locality” l Temporal Locality (Close in Time) - Memory that has been accessed recently is most likely to be accessed sooner l Spatial Locality (Close in location) - Memory that is close to memory that has been accessed recently is most likely to be accessed sooner q Organize memory in blocks l Keep blocks likely to be used soon in the very fast memory l Keep the next most likely blocks in medium fast memory l Keep those not likely to be used soon in slower memory

Hierarchical Memory Organization

Hierarchical Memory Organization

Cache memory q What is cache? l q How is it organized? l q

Cache memory q What is cache? l q How is it organized? l q Likely in place of the last used block How do we rate the performance of the cache? l q An access fails to find the word in the cache Where does it get placed in the cache? l q When a block in main memory is more likely to be needed, that block replaces a block in the cache. How do we know it is needed? l q Organized in a number of uniform sized blocks of memory that have a high likelihood of being used. How is kept “current”? l q A small amount of very high speed memory between the “main memory” and the CPU Based upon Hit rates and Miss rates Should there be Instruction Caches and Data Caches?

Hierarchical Memory Organization Registers are the fastest Cache is the fastest “Memory” - SRAM

Hierarchical Memory Organization Registers are the fastest Cache is the fastest “Memory” - SRAM DRAM makes good main memory Disk is best for the rest (majority)

The Memory Hierarchy q Take advantage of the principle of locality to present the

The Memory Hierarchy q Take advantage of the principle of locality to present the user with as much memory as is available in the cheapest technology at the speed offered by the fastest technology Inclusive– Processor 4 -8 bytes (words) Increasing distance from the processor in access time L 1$ 8 -32 bytes (block) L 2$ 1 to 4 blocks Main Memory what is in L 1$ is a subset of what is in L 2$ is a subset of what is in MM that is a subset of is in SM 1, 024+ bytes (disk sector = page) Secondary Memory (Relative) size of the memory at each level

The Memory Hierarchy: Pictorially q Temporal Locality (Locality in Time): q Keep most recently

The Memory Hierarchy: Pictorially q Temporal Locality (Locality in Time): q Keep most recently accessed data items closer to the processor Spatial Locality (Locality in Space): Move blocks consisting of contiguous words to the upper levels To Processor Upper Level Memory Lower Level Memory Blk X From Processor Blk Y

The Memory Hierarchy: Terminology q Hit: data is in some block in the upper

The Memory Hierarchy: Terminology q Hit: data is in some block in the upper level (Blk X) l Hit Rate: the fraction of memory accesses found in the upper level l Hit Time: Time to access the upper level which consists of RAM access time + Time to determine hit/miss To Processor Upper Level Memory Lower Level Memory Blk X From Processor q Blk Y Miss: data is not in the upper level so needs to be retrieve from a block in the lower level (Blk Y) l l l Miss Rate = 1 - (Hit Rate) Miss Penalty: Time to replace a block in the upper level + Time to deliver the block the processor Hit Time << Miss Penalty

How is the Hierarchy Managed? q registers memory l q cache main memory l

How is the Hierarchy Managed? q registers memory l q cache main memory l q by compiler (or programmer? ) by the cache controller hardware main memory disks l by the operating system (virtual memory) l virtual to physical address mapping assisted by the hardware (TLB) l by the programmer (files)

Cache q q Two questions to answer (in hardware): l Q 1: How do

Cache q q Two questions to answer (in hardware): l Q 1: How do we know if a data item is in the cache? l Q 2: If it is, how do we find it? Direct Mapped Caching l l For each item of data at the lower level, there is exactly one location in the cache where it might be - so lots of items at the lower level must share locations in the upper level Address mapping: (block address) modulo (# of blocks in the cache) l First consider block sizes of one word

Caching: A Simple First Example Cache Index Valid Tag Data 00 01 10 11

Caching: A Simple First Example Cache Index Valid Tag Data 00 01 10 11 Q 1: Is it there? Compare the cache tag to the high order 2 memory address bits to tell if the memory block is in the cache Main Memory 0000 xx 0001 xx Two low order bits define the byte in the 0010 xx word (32 b words) 0011 xx 0100 xx 0101 xx 0110 xx 0111 xx Q 2: How do we find it? 1000 xx 1001 xx Use next 2 low order 1010 xx memory address bits – 1011 xx 1100 xx the index – to 1101 xx determine which 1110 xx cache block (i. e. , 1111 xx modulo the number of blocks in the cache) (block address) modulo (# of blocks in the cache)

Direct Mapped Cache q Consider the main memory word reference string Start with an

Direct Mapped Cache q Consider the main memory word reference string Start with an empty cache - all blocks initially marked as not valid 0 miss 00 01 Mem(0) 1 miss 00 Mem(0) 00 Mem(1) 4 miss 00 00 l 4 Mem(0) Mem(1) Mem(2) Mem(3) 0 1 2 3 4 15 2 miss 00 00 00 3 hit 01 00 00 00 8 requests, 6 misses Mem(4) Mem(1) Mem(2) Mem(3) Mem(0) Mem(1) Mem(2) 4 01 00 00 00 3 miss 00 00 hit Mem(4) Mem(1) Mem(2) Mem(3) Mem(0) Mem(1) Mem(2) Mem(3) 15 miss 01 00 00 11 00 Mem(4) Mem(1) Mem(2) Mem(3) 15

MIPS Direct Mapped Cache Example q One word/block, cache size = 1 K words

MIPS Direct Mapped Cache Example q One word/block, cache size = 1 K words 31 30 Hit Tag . . . 13 12 11 20 . . . 2 1 0 Byte offset Data 10 Index Valid Tag Data 0 1 2. . . 1021 1022 1023 20 32 What kind of locality are we taking advantage of?

Handling Cache Hits q Read hits (I$ and D$) l q this is what

Handling Cache Hits q Read hits (I$ and D$) l q this is what we want – no challenges Write hits (D$ only) l l What is the problem here? Strategies allow cache and memory to be inconsistent - write the data only into the cache block (write-back the cache contents to the next level in the memory hierarchy when that cache block is “evicted”) - need a dirty bit for each data cache block to tell if it needs to be written back to memory when it is evicted require the cache and memory to be consistent - always write the data into both the cache block and the next level in the memory hierarchy (write-through) so don’t need a dirty bit - writes run at the speed of the next level in the memory hierarchy – so slow! – or can use a write buffer, so only have to

Read / Write Strategies Read Through: Word read from memory No Read Through: Word

Read / Write Strategies Read Through: Word read from memory No Read Through: Word word from cache after block is read from memory Write Through: Word written to both Cache and Memory Write Back: Word written only to Cache Write Allocate: Block is loaded on a write miss, followed by a write hit Write No Allocate: Block is modified on a write miss but not loaded Write Hit Policy Write Through Write Miss Policy Write Allocate Write Through * Write No Allocate * Write Back * Write Allocate * Write Back No Write Allocate

Write Buffer for Write-Through Caching Cache Processor DRAM write buffer q q q Write

Write Buffer for Write-Through Caching Cache Processor DRAM write buffer q q q Write buffer between the cache and main memory l Processor: writes data into the cache and the write buffer l Memory controller: writes contents of the write buffer to memory The write buffer is just a FIFO l Typical number of entries: 4 l Works fine if store frequency (w. r. t. time) << 1 / DRAM write cycle Memory system designer’s nightmare l When the store frequency (w. r. t. time) → 1 / DRAM write cycle leading to write buffer saturation - One solution is to use a write-back cache; another is to use an “L 2” cache

Another Reference String Mapping q Consider the main memory word reference string Start with

Another Reference String Mapping q Consider the main memory word reference string Start with an empty cache - all blocks initially marked as not valid 0 miss 00 00 01 l q Mem(0) 0 miss Mem(4) 0 01 00 0 4 0 4 4 miss Mem(0) 4 00 01 0 miss 0 01 00 0 miss 0 Mem(4) 01 00 Mem(4) 4 miss Mem(0)4 4 miss Mem(0) 8 requests, 8 misses Ping pong effect due to conflict misses - two memory locations that map into the same cache block 4

Sources of Cache Misses q Compulsory (cold start or process migration, first reference): l

Sources of Cache Misses q Compulsory (cold start or process migration, first reference): l l q q First access to a block, “cold” fact of life, not a whole lot you can do about it If you are going to run “millions” of instruction, compulsory misses are insignificant Conflict (collision): l Multiple memory locations mapped to the same cache location l Solution 1: increase cache size or block length l Solution 2: increase associativity Capacity: l Cache cannot contain all blocks accessed by the program l Solution: increase cache size What about the relationship between cache size and block length?

Handling Cache Misses q Read misses (I$ and D$) l q stall the entire

Handling Cache Misses q Read misses (I$ and D$) l q stall the entire pipeline, fetch the block from the next level in the memory hierarchy, install it in the cache and send the requested word to the processor, then let the pipeline resume Write misses (D$ only) 1. stall the pipeline, fetch the block from next level in the memory hierarchy, install it in the cache (which may involve having to evict a dirty block if using a write-back cache), write the word from the processor to the cache, then let the pipeline resume or (normally used in write-back caches) 2. Write allocate – just write the word into the cache updating both the tag and data, no need to check for cache hit, no need to stall or (normally used in write-through caches with a write buffer) 3. No-write allocate – skip the cache write and just write the word to the write buffer (and eventually to the next memory level), no need to stall if the write buffer isn’t full; must invalidate the cache block since it will be inconsistent (now holding stale data)

Multiword Block Direct Mapped Cache q Four words/block, cache size = 1 K words

Multiword Block Direct Mapped Cache q Four words/block, cache size = 1 K words 31 30. . . Hit Tag 13 12 11 20 . . . 4 32 10 Byte offset 8 Block offset Index Valid Tag 0 1 2. . . 253 254 255 Data 20 32 What kind of locality are we taking advantage of?

Taking Advantage of Spatial Locality q Let cache block hold more than one word

Taking Advantage of Spatial Locality q Let cache block hold more than one word 0 1 2 3 4 15 Start with an empty cache - all blocks initially marked as not valid 0 miss 00 Mem(1) 1 hit Mem(0) 00 Mem(0) Mem(2) 01 00 00 3 hit 00 00 Mem(1) Mem(3) Mem(1) Mem(0) 4 miss 5 4 Mem(1) Mem(0) Mem(3) Mem(2) 4 hit 01 00 l Mem(5) Mem(3) 00 00 2 miss Mem(1) Mem(0) Mem(3) Mem(2) 3 hit 01 00 Mem(5) Mem(3) 15 miss Mem(4) Mem(2) 8 requests, 4 misses 1101 00 Mem(5) Mem(4) 15 14 Mem(3) Mem(2) Mem(4) Mem(2)

Miss Rate vs Block Size vs Cache Size q Miss rate goes up if

Miss Rate vs Block Size vs Cache Size q Miss rate goes up if the block size becomes a significant fraction of the cache size because the number of blocks that can be held in the same size cache is smaller (increasing capacity misses)

Block Size Tradeoff q Larger block sizes take advantage of spatial locality but l

Block Size Tradeoff q Larger block sizes take advantage of spatial locality but l l If the block size is too big relative to the cache size, the miss rate will go up Larger block size means larger miss penalty - Latency to first word in block + transfer time for remaining words Miss Exploits Spatial Locality Rate Average Access Time Miss Penalty Increased Miss Penalty & Miss Rate Fewer blocks compromises Temporal Locality Block Size q Block Size In general, Average Memory Access Time = Hit Time + Miss Penalty x Miss Rate Block Size

Multiword Block Considerations q Read misses (I$ and D$) l l Processed the same

Multiword Block Considerations q Read misses (I$ and D$) l l Processed the same as for single word blocks – a miss returns the entire block from memory Miss penalty grows as block size grows - Early restart – datapath resumes execution as soon as the requested word of the block is returned - Requested word first – requested word is transferred from the memory to the cache (and datapath) first l q Nonblocking cache – allows the datapath to continue to access the cache while the cache is handling an earlier miss Write misses (D$) l Can’t use write allocate or will end up with a “garbled” block in the cache (e. g. , for 4 word blocks, a new tag, one word of data from the new block, and three words of data from the old block), so must fetch the block from memory first and pay the stall time

Cache Summary q The Principle of Locality: l Program likely to access a relatively

Cache Summary q The Principle of Locality: l Program likely to access a relatively small portion of the address space at any instant of time - Temporal Locality: Locality in Time - Spatial Locality: Locality in Space q Three major categories of cache misses: l l l q Compulsory misses: sad facts of life. Example: cold start misses Conflict misses: increase cache size and/or associativity Nightmare Scenario: ping pong effect! Capacity misses: increase cache size Cache design space l total size, block size, associativity (replacement policy) l write-hit policy (write-through, write-back) l write-miss policy (write allocate, write buffers)

Measuring Cache Performance q Assuming cache hit costs are included as part of the

Measuring Cache Performance q Assuming cache hit costs are included as part of the normal CPU execution cycle, then CPU time = IC × CPI × CC = IC × (CPIideal + Memory-stall cycles) × CC CPIstall q Memory-stall cycles come from cache misses (a sum of readstalls and write-stalls) Read-stall cycles = reads/program × read miss rate × read miss penalty Write-stall cycles = (writes/program × write miss rate × write miss penalty) + write buffer stalls q For write-through caches, we can simplify this to Memory-stall cycles = miss rate × miss penalty

Impacts of Cache Performance q Relative cache penalty increases as processor performance improves (faster

Impacts of Cache Performance q Relative cache penalty increases as processor performance improves (faster clock rate and/or lower CPI) l l q The memory speed is unlikely to improve as fast as processor cycle time. When calculating CPIstall, the cache miss penalty is measured in processor clock cycles needed to handle a miss The lower the CPIideal, the more pronounced the impact of stalls A processor with a CPIideal of 2, a 100 cycle miss penalty, 36% load/store instr’s, and 2% I$ and 4% D$ miss rates Memory-stall cycles = 2% × 100 + 36% × 4% × 100 = 3. 44 So q q CPIstalls = 2 + 3. 44 = 5. 44 What if the CPIideal is reduced to 1? 0. 5? 0. 25? What if the processor clock rate is doubled (doubling the miss penalty)?

Reducing Cache Miss Rates #1 q q Allow more flexible block placement In a

Reducing Cache Miss Rates #1 q q Allow more flexible block placement In a direct mapped cache a memory block maps to exactly one cache block At the other extreme, could allow a memory block to be mapped to any cache block – fully associative cache A compromise is to divide the cache into sets each of which consists of n “ways” (n-way set associative). A memory block maps to a unique set (specified by the index field) and can be placed in any way of that set (so there are n choices) (block address) modulo (# sets in the cache)