What is Cache Memory? EE 352 Unit 11. Motivation for Cache Memory. Memory Hierarchy. Cache Definitions Cache Address Mapping Cache Performance

Similar documents
7.1. CS356 Unit 8. Memory

EE 457 Unit 7a. Cache and Memory Hierarchy

LECTURE 11. Memory Hierarchy

CS 61C: Great Ideas in Computer Architecture (Machine Structures) Caches Part 1

Chapter 5A. Large and Fast: Exploiting Memory Hierarchy

CSE 431 Computer Architecture Fall Chapter 5A: Exploiting the Memory Hierarchy, Part 1

ECE 552 / CPS 550 Advanced Computer Architecture I. Lecture 13 Memory Part 2

CS3350B Computer Architecture

Memory Technology. Caches 1. Static RAM (SRAM) Dynamic RAM (DRAM) Magnetic disk. Ideal memory. 0.5ns 2.5ns, $2000 $5000 per GB

Lecture 7 - Memory Hierarchy-II

EEC 170 Computer Architecture Fall Cache Introduction Review. Review: The Memory Hierarchy. The Memory Hierarchy: Why Does it Work?

CSF Cache Introduction. [Adapted from Computer Organization and Design, Patterson & Hennessy, 2005]

CS3350B Computer Architecture

10/16/2017. Miss Rate: ABC. Classifying Misses: 3C Model (Hill) Reducing Conflict Misses: Victim Buffer. Overlapping Misses: Lockup Free Cache

Page 1. Multilevel Memories (Improving performance using a little cash )

Why memory hierarchy? Memory hierarchy. Memory hierarchy goals. CS2410: Computer Architecture. L1 cache design. Sangyeun Cho

Systems Programming and Computer Architecture ( ) Timothy Roscoe

Donn Morrison Department of Computer Science. TDT4255 Memory hierarchies

Memory Hierarchy Computing Systems & Performance MSc Informatics Eng. Memory Hierarchy (most slides are borrowed)

Chapter 5. Large and Fast: Exploiting Memory Hierarchy

EE 4683/5683: COMPUTER ARCHITECTURE

Memory Hierarchy Computing Systems & Performance MSc Informatics Eng. Memory Hierarchy (most slides are borrowed)

Inside out of your computer memories (III) Hung-Wei Tseng

Memory Hierarchy. ENG3380 Computer Organization and Architecture Cache Memory Part II. Topics. References. Memory Hierarchy

Memory Hierarchy. Mehran Rezaei

Last class. Caches. Direct mapped

Chapter 5. Large and Fast: Exploiting Memory Hierarchy

Caches Part 1. Instructor: Sören Schwertfeger. School of Information Science and Technology SIST

ECE 252 / CPS 220 Advanced Computer Architecture I. Lecture 13 Memory Part 2

CS 152 Computer Architecture and Engineering. Lecture 8 - Memory Hierarchy-III

CHAPTER 6 Memory. CMPS375 Class Notes (Chap06) Page 1 / 20 Dr. Kuo-pao Yang

EE 660: Computer Architecture Advanced Caches

Chapter 2: Memory Hierarchy Design Part 2

CS/ECE 250 Computer Architecture

The Memory Hierarchy. Cache, Main Memory, and Virtual Memory (Part 2)

EITF20: Computer Architecture Part 5.1.1: Virtual Memory

Chapter 2: Memory Hierarchy Design Part 2

ECE260: Fundamentals of Computer Engineering

CS 61C: Great Ideas in Computer Architecture. Direct Mapped Caches

211: Computer Architecture Summer 2016

Lecture notes for CS Chapter 2, part 1 10/23/18

CENG 3420 Computer Organization and Design. Lecture 08: Cache Review. Bei Yu

Cache performance Outline

CS161 Design and Architecture of Computer Systems. Cache $$$$$

Chapter 5. Large and Fast: Exploiting Memory Hierarchy

Page 1. Memory Hierarchies (Part 2)

CS 152 Computer Architecture and Engineering. Lecture 8 - Memory Hierarchy-III

CS356: Discussion #9 Memory Hierarchy and Caches. Marco Paolieri Illustrations from CS:APP3e textbook

V. Primary & Secondary Memory!

Chapter Seven. Large & Fast: Exploring Memory Hierarchy

CS152 Computer Architecture and Engineering Lecture 17: Cache System

Computer Architecture Computer Science & Engineering. Chapter 5. Memory Hierachy BK TP.HCM

Chapter 5. Large and Fast: Exploiting Memory Hierarchy

Lecture 12. Memory Design & Caches, part 2. Christos Kozyrakis Stanford University

Chapter 5. Large and Fast: Exploiting Memory Hierarchy

CS 61C: Great Ideas in Computer Architecture. The Memory Hierarchy, Fully Associative Caches

Lecture 11. Virtual Memory Review: Memory Hierarchy

Cache Memory: Instruction Cache, HW/SW Interaction. Admin

EITF20: Computer Architecture Part4.1.1: Cache - 2

Cache Performance (H&P 5.3; 5.5; 5.6)

Computer Architecture Spring 2016

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

CSF Improving Cache Performance. [Adapted from Computer Organization and Design, Patterson & Hennessy, 2005]

COSC 6385 Computer Architecture. - Memory Hierarchies (II)

Memory Hierarchy. Reading. Sections 5.1, 5.2, 5.3, 5.4, 5.8 (some elements), 5.9 (2) Lecture notes from MKP, H. H. Lee and S.

CENG 3420 Computer Organization and Design. Lecture 08: Memory - I. Bei Yu

LECTURE 4: LARGE AND FAST: EXPLOITING MEMORY HIERARCHY

CS 152 Computer Architecture and Engineering. Lecture 7 - Memory Hierarchy-II

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2015 Lecture 15

EN1640: Design of Computing Systems Topic 06: Memory System

Cache Memories. Topics. Next time. Generic cache memory organization Direct mapped caches Set associative caches Impact of caches on performance

Key Point. What are Cache lines

Announcements. ! Previous lecture. Caches. Inf3 Computer Architecture

Textbook: Burdea and Coiffet, Virtual Reality Technology, 2 nd Edition, Wiley, Textbook web site:

Digital Logic & Computer Design CS Professor Dan Moldovan Spring Copyright 2007 Elsevier 8-<1>

Fundamentals of Computer Systems

Cache Optimisation. sometime he thought that there must be a better way

Advanced Memory Organizations

1. Memory technology & Hierarchy

CHAPTER 4 MEMORY HIERARCHIES TYPICAL MEMORY HIERARCHY TYPICAL MEMORY HIERARCHY: THE PYRAMID CACHE PERFORMANCE MEMORY HIERARCHIES CACHE DESIGN

CACHE MEMORIES ADVANCED COMPUTER ARCHITECTURES. Slides by: Pedro Tomás

Caches and Memory Hierarchy: Review. UCSB CS240A, Fall 2017

Caches. Han Wang CS 3410, Spring 2012 Computer Science Cornell University. See P&H 5.1, 5.2 (except writes)

CS 152 Computer Architecture and Engineering. Lecture 7 - Memory Hierarchy-II

CPU issues address (and data for write) Memory returns data (or acknowledgment for write)

Chapter 8 :: Topics. Chapter 8 :: Memory Systems. Introduction Memory System Performance Analysis Caches Virtual Memory Memory-Mapped I/O Summary

Agenda. EE 260: Introduction to Digital Design Memory. Naive Register File. Agenda. Memory Arrays: SRAM. Memory Arrays: Register File

Chapter 5. Topics in Memory Hierachy. Computer Architectures. Tien-Fu Chen. National Chung Cheng Univ.

Lecture 9: Improving Cache Performance: Reduce miss rate Reduce miss penalty Reduce hit time

Agenda. Cache-Memory Consistency? (1/2) 7/14/2011. New-School Machine Structures (It s a bit more complicated!)

CS 61C: Great Ideas in Computer Architecture (Machine Structures) Caches Part 2

Cache Memories /18-213/15-513: Introduction to Computer Systems 12 th Lecture, October 5, Today s Instructor: Phil Gibbons


Improving Cache Performance. Reducing Misses. How To Reduce Misses? 3Cs Absolute Miss Rate. 1. Reduce the miss rate, Classifying Misses: 3 Cs

Cache and Memory. CS230 Tutorial 07

Memory Hierarchy, Fully Associative Caches. Instructor: Nick Riasanovsky

Agenda Cache memory organization and operation Chapter 6 Performance impact of caches Cache Memories

CS/ECE 3330 Computer Architecture. Chapter 5 Memory

CS 152 Computer Architecture and Engineering. Lecture 8 - Memory Hierarchy-III

Denison University. Cache Memories. CS-281: Introduction to Computer Systems. Instructor: Thomas C. Bressoud

Transcription:

What is EE 352 Unit 11 Definitions Address Mapping Performance memory is a small, fast memory used to hold of data that the processor will likely need to access in the near future sits between the processor and main memory (MM) and is usually Read and write requests will hopefully be satisfied by the fast cache rather than the slow main memory Processor Core Main Usual chip boundary Motivation for Hierarchy Large memories are inherently slow We need a large memory to hold code and data from multiple applications Small memory is inherently faster Important act: Processor is only accessing in any short time period Use both! Large memory as a global store and cache as a smaller working-set store Larger hierarchy provides ability to access data quickly from lower levels while still providing large memory size Size Speed aster More Expensive Cost Higher Levels Lower Levels Registers L1 (SRAM) L2 (SRAM) Main (DRAM) Backing Store (Magnetic or LASH memory)

Principle of Locality 2 dimensions of this principle: Locality uture accesses will likely Instructions and data arrays are sequential (they are all one after the next) Locality Recent accesses will likely be Same code and data are repeatedly accessed (loops, subroutines, etc.) 9/1 rule: Analysis shows that usually 1% of the written instructions account for 9% of the executed instructions and Locality s exploit locality Spatial Locality s do not store individual words but s always bring in a group of sequential words because if we access one, we are likely to access the next Bringing in blocks of sequential words takes advantage of Temporal Locality Leave data in the cache because it will likely be accessed again Blocks/Lines Processor Read/Write Sequencing is broken into blocks or Any time data is brought in, it will bring in the entire block of data Blocks start on addresses multiples of their size Usually block size matches x4 x44 x48 x4c x41 x414 Narrow (Word) bus Wide (multi-word) SB 128B [4 blocks (lines) of 8-words (32-bytes)] Main Whenever the processor generates a read or a write, it will first check the cache memory to see if it contains the desired data If so, it can get the data quickly from cache Otherwise, it must go to the slow main memory to get the data 4 x4 x44 x48 x4c x41 x414 forward desired word 3 responds 1 Request word @ x428 does not 2 have the data and requests whole cache line 42-43f

Definitions Hit: if requested data present in cache Read Hit: Desired read data resides in cache Write Hit: Desired write data resides in cache Miss: if requested data not present in cache Read Miss: Desired read data does not reside in cache Write Miss: Desired write data does not reside in cache Read-Miss Policies When desired read data is not in the cache, the entire block must be read in to the cache Different policies for when the desired read data is returned to the processor : As a block is being fetched the desired word will be as soon as it is loaded into the cache. : Variant of early-restart where the desired word in the block is brought in from memory first, then the rest of the block (i.e. if the 2 nd word in a 4-word block is desired, the memory will supply word 2 then 3,, 1 using modulo addressing) : Entire block is fetched before returning the desired word to the processor Write Miss Policies When desired write data is not in cache, what should be done etch-on-miss ( ): Bring block from MM to cache and then perform write w/ given write-hit policy (chances are you will so bring in the block) Write Around ( ): Just update the word in MM and do not bring the block into cache (write around the cache going straight to MM) Write (Hit) Policies If desired write data is already in cache or once it is brought in after a miss, what should be done : Update the desired word in MM as well as the cached version. This way the MM and cache versions are always : Update the cached version of the word and NOT the MM copy. Requires that the block be NOTE: or our class we will assume that Write-Through and No-Write Allocate are always used together and that Write-Back and Write-Allocate are used together.

Early-Restart Example code snippet: (recall $t,$t1 are aliases for registers $8 and $9) MM x4 Early-Restart cache returns the requested data to the processor as soon as it gets it.text $t,$,2 LOOP: $t,$t,-1 bne $t,$zero,loop lui $t1,x11 ori $t1,$t1,x4 sw $t,($t1) Assumptions: 1.) access requires 1ns 2.) MM access requires 1ns 3.) Processor time is negligible bne lui ori sw x44 x48 x4c x41 x414 x418 x41c Address Start Time Complete Time x4 x44 x48 x44 x48 x4c Block bne lui ori sw Assumptions: @ 1 ns @ 2 ns bne @ 3 ns lui @ 4 ns ori @ 5 ns sw @ 6 ns @ 7 ns @ 8 ns 1.) access requires 1ns 2.) MM access requires 1ns 3.) Processor time is negligible MM Block bne lui ori sw x4 x44 x48 x4c x41 x414 x418 x41c Critical Word irst Example If processor requests a word in the middle of the block, MM will return that word first and then the subsequent words, wrapping around the block address (i.e. addr n mod block_size) x414 sw Block sw @ 4 ns @ 5 ns bne @ 6 ns lui @ 7 ns ori @ 8 ns sw @ 1 ns @ 2 ns @ 3 ns MM Block bne lui ori sw x4 x44 x48 x4c x41 x414 x418 x41c Early-Restart Summary As soon as the cache gets the requested word it will return it to the processor and the processor can continue while the cache brings in the rest of the block If the processor requests a new word that the cache does not have yet, it must wait until the cache gets that word (the cache may be busy bringing in the rest of the block) Critical word first starts with the desired word and brings in the rest of the block after that Time: 11 ns Processor waits

No Early-Restart No Early-Restart cache fetches entire block before returning desired word to processor No Early-Restart Summary The cache will always bring in the entire block before returning the requested word Req. Time Address ns x4 x44 x48 Complete Time Block bne lui ori sw @ 1 ns @ 2 ns bne @ 3 ns lui @ 4 ns ori @ 5 ns sw @ 6 ns @ 7 ns @ 8 ns MM Block bne lui ori sw x4 x44 x48 x4c x41 x414 x418 x41c Then each subsequent access to that block can be serviced in the cache access time x4 x42 Handling Writes Write-Back cache (using Write-Allocate) Write-Through cache (using No-Write- Allocate) Write-Back Word is only written in the cache (not MM) On replacement, entire block must be written back to MM

On write-hit Update only cached copy Processor can continue quickly (e.g. 1 ns) Later when block is evicted, entire block is written back (because bookkeeping is kept on a per block basis) Ex: 8 words @ 1 ns per word for writing mem. = 8 ns Write Back x4 x44 x48 x4c x41 x414 1 3 Write word (hit) 2 updates value & signals processor to continue On eviction, entire block written back Write-Through Word is written in both cache and MM On replacement, block does not have to be written back since MM already has updated values Write Through On write-hit Update both cached and main memory version Processor may have to wait for memory to complete (e.g. 1 ns) Later when block is evicted, no writeback is needed 1 Write word (hit) 2 and memory copies are updated Example Assume we execute a loop 4 times, and each iteration writes to address x118 3 On eviction, entire block written back x4 x44 x48 x4c x41 x414

Write-Back Iteration 1: Processor writes data to address x118 which is in cache Write-Back Iteration 1 completes after 1 ns. Block MM Block Block MM Block 1 1 x1 1 1 x1 A: x118 2 2 x14 A: x118 2 2 x14 D: x12345678 3 4 3 4 x18 x1c D: x12345678 1234 5678 4 3 4 x18 x1c 5 5 x11 5 5 x11 6 6 x114 6 6 x114 7 7 x118 7 7 x118 8 8 x11c 8 8 x11c Time: ns Time: 1 ns Processor can continue after 1 ns it takes to write the cache only Write-Back Iteration 4: Processor writes data to address x118 which is in cache Write-Back On replacement, the entire block must be written back (8 words * 1 ns) A: x118 D: x5678e Block 1 2 5678 E 4 5 6 7 8 MM Block 1 2 3 4 5 6 7 8 x1 x14 x18 x1c x11 x114 x118 x11c Block 1 2 5678 E 4 5 6 7 8 @ 5 ns @ 6 ns @ 7 ns @ 8 ns @ 9 ns @ 1 ns @ 11 ns @ 12 ns MM Block 1 x1 2 x14 3 x18 4 x1c 5 x11 6 x114 7 x118 8 x11c Time: 4 ns Time: 12 ns

Write-Through Iteration 1: Processor writes data to address x118 which is in cache Write-Through Iteration 4: Processor writes data ABCD to address 8 which is in cache Block MM Block Block MM Block 1 1 x1 1 1 x1 A: x118 2 A: x118 2 x14 A: x118 2 A: x118 2 x14 D: x12345678 3 3 4 D: x12345678 4 x18 x1c D: x5678e 1234 5678 1234 5678 4 D: x5678e 4 x18 x1c 5 5 x11 5 5 x11 6 6 x114 6 6 x114 7 7 x118 7 7 x118 8 8 x11c 8 8 x11c Time: 1 ns Time: 4 ns Write-Through Analysis On replacement, the block in cache need not be written back they are the same Block 1 2 4 5 6 7 8 MM Block 1 2 5678 E 5678 E 4 5 6 7 8 x1 x14 x18 x1c x11 x114 x118 x11c Write-back Best results when (i.e. writes in loops) Write-through Best results when (no need for writeback) Time: 4 ns

Replacement Policies On a read- or write-miss, a new block must be brought in This requires evicting a current block residing in the cache Replacement policies Blocking vs. Non-blocking A cache has an interface between itself and the processor and itself and main memory. Can the cache satisfy requests from the processor at the same time it is bringing in a block from MM Blocking : A blocking cache cannot satisfy other requests from the processor while it is fetching or writing blocks from/to main memory. Non-blocking : A non-blocking cache can satisfy processor requests at the same time it accesses main memory. Example: In an out-of-order, superscalar processor an instruction might cause a miss and stall but the processor will continue executing the following instructions. Implementation Assume a cache of 4 blocks of 4 words (16-bytes) each What other bookkeeping and identification info is needed Has the block been modified Is the block empty or full Address range of the data Mapping Schemes CACHE IMPLEMENTATION Data of xac-ac (unmodified) Data of x47-47 (modified) empty empty

Identifying Blocks via Address Range Implementation Possible methods Store start and end address (requires multiple comparisons) Ensure block ranges sit on binary boundaries (upper address bits identify the block with a single value) 1 11 12 13 14 15 16 17 18 19 1 st loor Analogy: Hotel room layout/addressing 12 121 122 123 124 125 126 127 128 129 2 21 22 23 24 25 26 27 28 29 2 nd loor Analogy: Hotel Rooms 22 221 222 223 224 225 226 227 228 229 1 st Digit = loor 2 nd Digit = Aisle 3 rd Digit = Room w/in aisle To refer to the range of rooms on the second floor, left aisle we would just say rooms 2x 4 word (16-byte) blocks: Addr. Range 8 word (32-byte) blocks: Binary -f -1111 1-1f 1-1111 Addr. Range Binary -1f -11111 2-3f 1-11111 Assume 12-bit addresses and 4-word blocks Block addresses will range from xx-xx Address can be broken down as follows = unused (always access 32-bit word) = selects word within the block = identifies block. Addr. = x124 Word w/in block 12-12 Word Addr. = xacc Word w/in block AC-AC Implementation Terminology What bookkeeping values must be stored with the cache in tion to the block data Portion of the block s address range used to identify the MM block residing in the cache from other MM blocks. bit Indicates the block is occupied with valid data (i.e. not empty or invalid) bit Indicates the cache and MM copies are inconsistent (i.e. a write has been done to the cached copy but not the main memory copy) Used for write-back caches Implementation To identify which MM block resides in each cache block, the tags need to be stored along with the Dirty and Valid bits 11 11 V= D= 1 111 V= D= 1 111 V= D= V= D= Data of xac-ac (unmodified) Data of x47-47 (modified) empty empty

Content-Addressable Comparison memory is one form of what is known as contentaddressable memory This means data can be in memory and does not have one particular address Additional information is saved with the data and is used to address /find the desired data (this is the tag in this case) via a search on each access This search can be very!! Processor 1 Read x47c 11 11 V=1 D= 1 111 V=1 D=1 V= D= V= D= Data of xac-ac (unmodified) Data of x47-47 (modified) empty empty 2 Is block x47- x47f here or here or here or here When caches have many blocks (> 16 or 32) it can be expensive (hardware-wise) to check all tags Hit Address = A[11:2] = = = = = A[11:4] 11 11 V=1 D= 1 111 V=1 V= V= D=1 1 111 D= D= Word = A[3:2] $AC-AC (unmodified) $47-47 (modified) empty empty Comparison Example portion of desired address is check against all the tags and qualified with the valid bits to determine a hit Hit Address = $47C 1 1 = = = = = A[11:4] = 11 11 V=1 D= 1 111 V=1 V= V= D=1 D= D= Word = A[3:2] = $AC-AC (unmodified) $47-47 (modified) empty empty Mapping Techniques Determines where blocks can be placed in the cache By reducing number of possible MM blocks that map to a cache block, hit logic (searches) can be done faster 3 Primary Methods Mapping Mapping Mapping

Mapping techniques Example cache w/ only 4 blocks MM has many blocks Block Block Block 3 Block 4 Block 3 Block 5 Block 6 Block 7 Block 8 ully Associative Mapping Any block from memory can be put in any cache block (i.e. no restriction) Block Block 3... Block Block 6 Block 7 Block 8 Direct Mapping Each block from memory can only be put in one location Given n cache blocks, MM block i maps to cache block Block Block 3 Block Block 3 Block 4 Block 5 Block 6 Block 7 Block 8 K-way Set-Associative Mapping Given S sets, block i of MM maps to set Within the set, block can be put Let k = number of cache blocks in a set = K comparisons required for search Set Usually given k & need to solve for s Block Block Block 3 Block 3 Block 4 Block 5 Block 6 Block 7 Block 8 Set Set Set Set Set Set Set Set Set

Mapping Implementation Q: How to implement mapping schemes Direct Mapping ully Associative Mapping Set-Associative Mapping A: By using the Assumptions: Only access words 12-bit MM addresses (496 Bytes) 4-word blocks ully Associative Mapping Any block from memory can be put in any cache block (i.e. no mapping) Block Block 3... Block Block 6 Block 7 Block 8 ully Associative Implementation ully Associative Address Scheme 12-bit address: 4 = 2 2 words per block => 2 LSB s above A[1:] used to determine the desired word w/in the block = Block # = Upper bits used to identify the block in the cache Block Block 3 Word A[1:] Address = x8 1... Block Block 6 Block 7 Block 8 A[1:] unused (word access only) Word bits = log 2 B bits (B=Block Size in units of words) = Remaining bits

ully Associative Mapping Direct Mapping Any block from memory can be put in any cache block (i.e. no mapping scheme) Completely flexible Block Block 3 Access Trace x4 x1 xfec xff8 xfcc Word A[1:] 1 1111 111 1111 1111 1111 11 1 11 1 11 Block Block 3 Block C Block D Block E Block Each block from memory can only be put in one location MM block i maps to cache block i mod n Block Block 3 Block Block 3 Block 4 Block 5 Block 6 Block 7 Block 8 = mod 4 = 1 mod 4 = 2 mod 4 = 3 mod 4 = mod 4 = 1 mod 4 = 2 mod 4 = 3 mod 4 = mod 4 Direct Mapping Implementation Direct Mapping Address Scheme 12-bit address: 4 = 2 2 words per block => 2 bits of address used to determine the desired word w/in the block 4 = 2 2 possible cache blocks => bits to determine block next bits of address = Upper bits used to identify the block in the cache (identifies between block (,4,8,xC,x1, etc.) Block Word A[1:] Address = 8 Address = A8 8 Block 8 = mod 4 8 9 Block Block 9 = 1 mod 4 9 A Block A = 2 mod 4 A B Block B = 3 mod 4 B C Block 3 Block C = mod 4 C A[1:] unused (word access only) Word bits = log 2 B bits (B=Block Size) Block bits = = Remaining bits

Direct Mapping Set-Associative Mapping Each block from memory can only be put in one location MM block i maps to cache block i mod n Block Block 3 Access Trace x4 x1 xfc8 x28 xfec 1111 11 1111 11 Block Word A[1:] Block Block 3 Block C Block D Block E 1 1 Block 1 1 1 1 11 Blocks from set i can map into any cache block from set i Set Block Block 3 Block Block 3 Block 4 Block 5 Block 6 Block 7 Block 8 Set Set Set Set Set Set-Associative Mapping 12-bit address: 4 = 2 2 words per block => 2 bits of address used to determine the desired word w/in the block 2 = sets => next bit used to determine which set = Upper bits used to identify the block in the cache Set Address = 8 Block Block 3 Set Word A[1:] Block Block 3 Block 4 Block 5 Block 6 Block 7 Block 8 Set Set Set Set Set K-way Set Associative Address Scheme A[1:] unused (word access only) Word bits = log 2 B bits (B=Block Size) Set bits = = Remaining bits

Set Set-Associative Mapping Blocks from set i can map into any cache block from set i Block Block 3 Access Trace x4 x1 xfc8 xfec 1111 11 1111 111 Set 1 Word A[1:] 1 1 11 Block Block 3 Block C Block D Block E Block Set Set Set Set Address Mapping Examples 16-bit addresses, 1 KB cache, 8 words/block ind address mapping for: ully Associative Direct Mapping 4-way Set Associative 8-way Set Associative Address Mapping Examples irst find parameters: B = Block size N = blocks S = Sets for 4-way and 8-way B is given as N depends on cache size and block size N = S for 4-way & 8-way S = S = ully Associative A1-A = unused (only word accesses) Word = = Word Parameters: B = N = S 4-way = S 8-way =

Direct Mapping A1-A = unused (only word accesses) Word = Block = = Parameters: B = 8 N = 32 S4-way = 8 S8-way = 4 4-Way Set Assoc. Mapping A1-A = unused (only word accesses) Word = log 2 8 = 3 bits (A4-A2) Set = = Parameters: B = 8 N = 32 S4-way = 8 S8-way = 4 Block Word Set Word 8-Way Set Assoc. Mapping A1-A = unused (only word accesses) Word = log 2 8 = 3 bits (A4-A2) Set = = Set Word Parameters: B = 8 N = 32 S4-way = 8 S8-way = 4 Operation Example Address Trace R: xa W: xf4 R: xb W: x2a2c Operations Hit etch block XX Evict block XX (w/ or w/o WB) inal WB of block XX) Perform address breakdown and apply address trace 2-Way Set-Assoc, N=4, B=8 words Address Set Word Unused xa xf4 xb x2a2c Processor Access R: xa W: xf4 R: xb Operation W: x2a2c Done!

Levels of If one cache is good, 2 or more might be better L1 cache is closest to processor with increasing numbers progressing outwards L1 is smallest and fastest cache with higher levels being slower but bigger to hold more data On a read or write, check L1 cache. On a miss, check L2. If it hits in L2 bring the block into L1. If L2 misses then get the data from memory Principle of Inclusion When a block is brought in from memory or an upper level, it is brought into This implies that lower levels always contains a of higher levels L1 contains most recently used data L2 contains that data + data used earlier MM contains all data Processor L1 L2 Main Processor L1 L2 Main Pentium 4 Pentium 4 L1 Broken into separate caches to hold instructions or data I- (Instruction) 12K Entries D- (Data) 16 KB 4-Way Set Assoc. L2 Both I & D together 1-2 MB 8-Way Set Associative L1 I- L1 D- Processor Logic Unified L2 Pentium 4

Intel Nehalem Quad Core Average Access Time Define parameters H i = (Note that = Miss rate) T i = R i = Burst rate per word of level i ( ) B = Block Size We will find T AVE = average access time Assume No-Load-Through cache Whether hit or miss, you must spend at least T1 time to get the value from the L1 cache T ave without L2 cache 2 possible cases: Either we have a hit and pay only the L1 cache hit time Or we have a miss and read in the whole block to L1 and then read from L1 to the processor T ave = T 1 + (Miss Rate)*(Miss Penalty) or T 1 =1ns, H 1 =.9, B=8, T MM =1ns, R MM =25ns T ave = 1 + T ave with L2 cache 3 possible cases: Either we have a hit and pay the L1 cache hit time Or we miss L1 but hit L2 and read in the block from L2 Or we miss L1 and L2 and read in the block from MM T ave = T 1 + + L1 miss / L2 Hit L1 miss / L2 Miss or T 1 = 1ns, H 1 =.9, T 2 = 2ns, R 2 = 1ns, H 2 =.98, B=8, T MM =1ns, R MM =25 ns T ave = 1 + = 1 +

Configurations Miss Rate AMD Opteron Intel P4 PPC 7447a Clock rate (24) 2. GHz 3.2 GHz 1.5 2 GHz Instruction 64KB, 2-way SA 96 KB 32 KB, 8-way SA Latency (clocks) 3 4 1 Data cache 64 KB, 2-way SA 8 KB, 4-way SA 32 KB, 8-way SA Latency (clocks) 3 2 1 L1 Write Policy Write-back Write-through Programmable On-chip L2 1 MB, 16-way SA 512 KB, 8-way SA 512 KB, 8-way SA L2 Latency 6 5 9 Block size (L1/L2) 64 64/128 32/64 L2 Write-Policy Write-back Write-back Programmable Sources: H&P, CO&D, 3 rd ed., reescale.com, Reducing Miss Rate means lower T AVE To analyze miss rate categorize them based on why they occur Compulsory Misses Capacity Misses Conflict Misses Miss Rate & Block Size Miss Rate & Associativity Graph used courtesy Computer Architecture: AQA, 3 rd ed., Hennessey and Patterson Graph used courtesy Computer Architecture: AQA, 3 rd ed., Hennessey and Patterson

Prefetching -Conscious Programming Hardware Prefetching On miss of block i, fetch block i and i+1 Advanced speculation / Runahead execution While processor is waiting for a cache block, try to continue executing future instructions looking only for memory references and not storing results of instructions Software Prefetching Special Prefetch Instructions Compiler inserts these instructions to give hints ahead of time as to the upcoming access pattern Order of array indexing Row major vs. column major ordering Blocking (keeps working set small) Pointer-chasing Linked lists, graphs, tree data structures that use pointers do not exhibit good spatial locality General Principles Keep working set reasonably small (temporal locality) Use small strides (spatial locality) Static structures usually better than dynamic ones for(i=; i<size; i++) { for(j=; j<size; j++) { // Row-major A[i][j] = A[i][j]*2; // Column-major A[j][i] = A[j][i]*2; } } Example of row vs. column major ordering Original Matrix Linked Lists Row Major Col. Major Layout of matrix A Blocked Matrix Layout of Linked List Blocked Matrix Multiply Blocked Multiply Results Traditional working set 1 row of C, 1 row of A, NxN matrix B Break NxN matrix into smaller BxB matrices Perform matrix multiply on blocks Sum results of block multiplies to produce overall multiply result Blocked multiply working set Three BxB matrices for(i = ; i < N; i+=b) { for(j = ; j < N; j+=b) { for(k = ; k < N; k+=b) { for(ii = i; ii < i+b; ii++) { for(jj = j; jj < j+b; jj++) { for(kk = k; kk < k+b; kk++) { Cb[ii][jj] += Ab[ii][kk] * Bb[kk][jj]; } } } } } } = C A B * Traditional Multiply C A B + += = + C A B * * Blocked Multiply * Intel Nehalem processor L1D = 32 KB, L2 = 256KB, L3 = 8 MB Time (sec) 12 1 8 6 4 2 Blocked Matrix Multiply (N=248) 95.98 96.95 78.31 25.6 17.37 18.9 18.8 13.27 12.1 18.78 4 8 16 32 64 128 256 512 124 248 Block Dimension (B)