340 likes | 350 Views
Understand memory hierarchy layers, gaps in performance, and the importance of locality in optimized data access. Explore SRAM, DRAM, disk storage trends, and CPU clock rates evolution. Learn about memory caching and the analogy of a library in the context of computer architecture.
E N D
CS-447– Computer Architecture Lecture 19Memory Hierarchy October 27th, 2008 Majd F. Sakrmsakr@qatar.cmu.edu www.qatar.cmu.edu/~msakr/15447-f08/
During This Lecture • Introduction to the Memory Hierarchy • Processor Memory Gap • Locality • Latency Hiding
Keyboard, Mouse Computer Processor (active) Devices Memory (passive) (where programs, data live when running) Input Disk, Network Control (“brain”) Output Datapath (“brawn”) Display, Printer The Big Picture
Processor-DRAM Memory Gap (latency) µProc 60%/yr. (2X/1.5yr) 1000 CPU “Moore’s Law” 100 Processor-Memory Performance Gap:(grows 50% / year) 10 DRAM 9%/yr. (2X/10 yrs) Performance DRAM 1 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 Time
Memories: • SRAM: • value is stored on a pair of inverting gates • very fast but takes up more space than DRAM (4 to 6 transistors)
Memories: DRAM: • value is stored as a charge on capacitor (must be refreshed) • very small but slower than SRAM (factor of 5 to 10) Word line Pass Transistor Capacitor Bit line
Memory • Users want large and fast memories! • SRAM access times are .5 – 5ns at cost of $4000 to $10,000 per GB. • DRAM access times are 50-70ns at cost of $100 to $200 per GB. • Disk access times are 5 to 20 million ns at cost of $.50 to $2 per GB. 2004
Storage Trends SRAM metric 1980 1985 1990 1995 2000 2005 2005:1980 $/MB 19,200 2,900 320 256 100 75 256 access (ns) 300 150 35 15 12 10 30 DRAM metric 1980 1985 1990 1995 2000 2005 2005:1980 $/MB 8,000 880 100 30 1 0.20 40,000 access (ns) 375 200 100 70 60 50 8 typical size(MB) 0.064 0.256 4 16 64 1,000 15,000 Disk metric 1980 1985 1990 1995 2000 2005 2005:1980 $/MB 500 100 8 0.30 0.05 0.001 10,000 access (ms) 87 75 28 10 8 4 22 typical size(MB) 1 10 160 1,000 9,000 400,000 400,000
CPU Clock Rates 1980 1985 1990 1995 2000 2005 2005:1980 processor 8080 286 386 Pentium P-III P-4 clock rate(MHz) 1 6 20 150 750 3,000 3,000 cycle time(ns) 1,000 166 50 6 1.3 0.3 3,333
The CPU-Memory Gap The gap widens between DRAM, disk, and CPU speeds.
Locality • Principle of Locality: • Programs tend to reuse data and instructions near those they have used recently, or that were recently referenced themselves. • Temporal locality: Recently referenced items are likely to be referenced in the near future. • Spatial locality: Items with nearby addresses tend to be referenced close together in time. • Locality Example: • Data • Reference array elements in succession (stride-1 reference pattern): • Reference sum each iteration: • Instructions • Reference instructions in sequence: • Cycle through loop repeatedly: sum = 0; for (i = 0; i < n; i++) sum += a[i]; return sum; Spatial locality Temporal locality Spatial locality Temporal locality
Locality Example • Claim: Being able to look at code and get a qualitative sense of its locality is a key skill for a professional programmer. • Question: Does this function have good locality? int sum_array_rows(int a[M][N]) { int i, j, sum = 0; for (i = 0; i < M; i++) for (j = 0; j < N; j++) sum += a[i][j]; return sum; }
Locality Example • Question: Does this function have good locality? int sum_array_cols(int a[M][N]) { int i, j, sum = 0; for (j = 0; j < N; j++) for (i = 0; i < M; i++) sum += a[i][j]; return sum; }
Memory Hierarchy (1/3) • Processor • executes instructions on order of nanoseconds to picoseconds • holds a small amount of code and data in registers • Memory • More capacity than registers, still limited • Access time ~50-100 ns • Disk • HUGE capacity (virtually limitless) • VERY slow: runs ~milliseconds
Processor Increasing Distance from Proc.,Decreasing speed Levels in memory hierarchy Level 1 Level 2 Level 3 . . . Level n Size of memory at each level Memory Hierarchy (2/3) Higher Lower As we move to deeper levels the latency goes up and price per bit goes down.
Memory Hierarchy (3/3) • If level closer to Processor, it must be: • smaller • faster • subset of lower levels (contains most recently used data) • Lowest Level (usually disk) contains all available data • Other levels?
Memory Caching • We’ve discussed three levels in the hierarchy: processor, memory, disk • Mismatch between processor and memory speeds leads us to add a new level: a memory cache • Implemented with SRAM technology
Memory Hierarchy Analogy: Library (1/2) • You’re writing a term paper (Processor) at a table in Library • Library is equivalent to disk • essentially limitless capacity • very slow to retrieve a book • Table is memory • smaller capacity: means you must return book when table fills up • easier and faster to find a book there once you’ve already retrieved it
Memory Hierarchy Analogy: Library (2/2) • Open books on table are cache • smaller capacity: can have very few open books fit on table; again, when table fills up, you must close a book • much, much faster to retrieve data • Illusion created: whole library open on the tabletop • Keep as many recently used books open on table as possible since likely to use again • Also keep as many books on table as possible, since faster than going to library
Memory Hierarchy Basis • Disk contains everything. • When Processor needs something, bring it into to all higher levels of memory. • Cache contains copies of data in memory that are being used. • Memory contains copies of data on disk that are being used. • Entire idea is based on Temporal Locality: if we use it now, we’ll want to use it again soon (a Big Idea)
Locality • A principle that makes having a memory hierarchy a good idea • If an item is referenced,temporal locality: it will tend to be referenced again soon spatial locality: nearby items will tend to be referenced soon.
A View of the Memory Hierarchy Regs Upper Level Instr. Operands Faster Cache Blocks L2 Cache Blocks Memory Pages Disk Files Larger Tape Lower Level
Cache • Our initial focus: two levels (upper, lower) • block: minimum unit of data • hit: data requested is in the upper level • miss: data requested is not in the upper level
Cache Design • How do we organize cache? • Where does each memory address map to? (Remember that cache is subset of memory, so multiple memory addresses map to the same cache location.) • How do we know which elements are in cache? • How do we quickly locate them?
Direct Mapped Cache • Mapping: address is modulo the number of blocks in the cache
Direct-Mapped Cache (1/2) • In a direct-mapped cache, each memory address is associated with one possible block within the cache • Therefore, we only need to look in a single location in the cache for the data if it exists in the cache • Block is the unit of transfer between cache and memory
4 Byte Direct Mapped Cache Cache Index Memory Address Memory 0 0 1 1 2 2 3 3 4 5 6 7 8 9 A B C D E F Direct-Mapped Cache (2/2) • Cache Location 0 can be occupied by data from: • Memory location 0, 4, 8, ... • 4 blocks => any memory location that is multiple of 4
TagIndexOffset tttttttttttttttttiiiiiiiiiioooo tagindexbyteto checktooffsetif haveselect withincorrect blockblockblock Issues with Direct-Mapped • Since multiple memory addresses map to same cache index, how do we tell which one is in there? • What if we have a block size > 1 byte? • Answer: divide memory address into three fields HEIGHT WIDTH
Direct-Mapped Cache Terminology • All fields are read as unsigned integers. • Index: specifies the cache index (which “row” of the cache we should look in) • Offset: once we’ve found correct block, specifies which byte within the block we want -- I.e., which “column” • Tag: the remaining bits after offset and index are determined; these are used to distinguish between all the memory addresses that map to the same location
Direct-Mapped Cache Example (1/3) • Suppose we have a 16KB of data in a direct-mapped cache with 4 word blocks • Determine the size of the tag, index and offset fields if we’re using a 32-bit architecture • Offset • need to specify correct byte within a block • block contains 4 words = 16 bytes = 24 bytes • need 4 bits to specify correct byte
Direct-Mapped Cache Example (2/3) • Index: (~index into an “array of blocks”) • need to specify correct row in cache • cache contains 16 KB = 214 bytes • block contains 24 bytes (4 words) • # blocks/cache = bytes/cache bytes/block = 214 bytes/cache 24 bytes/block = 210 blocks/cache • need 10 bits to specify this many rows
Direct-Mapped Cache Example (3/3) • Tag: use remaining bits as tag • tag length = addr length - offset - index = 32 - 4 - 10 bits = 18 bits • so tag is leftmost 18 bits of memory address • Why not full 32 bit address as tag? • All bytes within block need same address (4b) • Index must be same for every address within a block, so its redundant in tag check, thus can leave off to save memory (10 bits in this example)
TagIndexOffset TIOcache mnemonic 2(H+W) = 2H * 2W AREA (cache size, B)= HEIGHT (# of blocks) * WIDTH (size of one block, B/block) WIDTH (size of one block, B/block) HEIGHT(# of blocks) AREA(cache size, B)