The. Memory Hierarchy. Chapter 6

Similar documents
CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 26, SPRING 2013

Random Access Memory (RAM)

The Memory Hierarchy /18-213/15-513: Introduction to Computer Systems 11 th Lecture, October 3, Today s Instructor: Phil Gibbons

Foundations of Computer Systems

Storage Technologies and the Memory Hierarchy

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 26, FALL 2012

Computer Systems. Memory Hierarchy. Han, Hwansoo

Giving credit where credit is due

The Memory Hierarchy Sept 29, 2006

CISC 360. The Memory Hierarchy Nov 13, 2008

CS 33. Memory Hierarchy I. CS33 Intro to Computer Systems XVI 1 Copyright 2016 Thomas W. Doeppner. All rights reserved.

Random-Access Memory (RAM) Lecture 13 The Memory Hierarchy. Conventional DRAM Organization. SRAM vs DRAM Summary. Topics. d x w DRAM: Key features

Today. The Memory Hierarchy. Random-Access Memory (RAM) Nonvolatile Memories. Traditional Bus Structure Connecting CPU and Memory

CS429: Computer Organization and Architecture

CS429: Computer Organization and Architecture

Key features. ! RAM is packaged as a chip.! Basic storage unit is a cell (one bit per cell).! Multiple RAM chips form a memory.

CS 201 The Memory Hierarchy. Gerson Robboy Portland State University

+ Random-Access Memory (RAM)

The Memory Hierarchy / : Introduction to Computer Systems 10 th Lecture, Feb 12, 2015

Computer Organization: A Programmer's Perspective

Read-only memory (ROM): programmed during production Programmable ROM (PROM): can be programmed once SRAM (Static RAM)

Today. The Memory Hierarchy. Byte Oriented Memory Organization. Simple Memory Addressing Modes

Carnegie Mellon. Carnegie Mellon

Random-Access Memory (RAM) CS429: Computer Organization and Architecture. SRAM and DRAM. Flash / RAM Summary. Storage Technologies

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

Random-Access Memory (RAM) CISC 360. The Memory Hierarchy Nov 24, Conventional DRAM Organization. SRAM vs DRAM Summary.

CSE 153 Design of Operating Systems

The Memory Hierarchy. Computer Organization 2/12/2015. CSC252 - Spring Memory. Conventional DRAM Organization. Reading DRAM Supercell (2,1)

Announcements. Outline

CSE 153 Design of Operating Systems Fall 2018

Problem: Processor- Memory Bo<leneck

Review: Assembly Programmer s View. The Memory Hierarchy. Random- Access Memory (RAM) Today. NonvolaHle Memories. SRAM vs DRAM Summary

Large and Fast: Exploiting Memory Hierarchy

Giving credit where credit is due

CS 33. Architecture and Optimization (3) CS33 Intro to Computer Systems XVI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

CS 153 Design of Operating Systems

Locality. CS429: Computer Organization and Architecture. Locality Example 2. Locality Example

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

Memory Hierarchy. Instructor: Adam C. Champion, Ph.D. CSE 2431: Introduction to Operating Systems Reading: Chap. 6, [CSAPP]

CS3350B Computer Architecture

A Computer. Computer organization - Recap. The Memory Hierarchy... Brief Overview of Memory Design. CPU has two components: Memory

Random-Access Memory (RAM) Systemprogrammering 2007 Föreläsning 4 Virtual Memory. Locality. The CPU-Memory Gap. Topics

Random-Access Memory (RAM) Systemprogrammering 2009 Föreläsning 4 Virtual Memory. Locality. The CPU-Memory Gap. Topics! The memory hierarchy

Computer Architecture and System Software Lecture 08: Assembly Language Programming + Memory Hierarchy

Computer Organization: A Programmer's Perspective

CS 261 Fall Mike Lam, Professor. Memory

Problem: Processor Memory BoJleneck

Lecture 15: Caches and Optimization Computer Architecture and Systems Programming ( )

The Memory Hierarchy 10/25/16

CS 2461: Computer Architecture 1 The Memory Hierarchy and impact on program performance

CSCI-UA.0201 Computer Systems Organization Memory Hierarchy

The Memory Hierarchy. Fall Instructors: Aykut & Erkut Erdem

CS 261 Fall Mike Lam, Professor. Memory

Computer Systems CSE 410 Autumn Memory Organiza:on and Caches

Computer Architecture and System Software Lecture 09: Memory Hierarchy. Instructor: Rob Bergen Applied Computer Science University of Winnipeg

F28HS Hardware-Software Interface: Systems Programming

CS 31: Intro to Systems Storage and Memory. Kevin Webb Swarthmore College March 17, 2015

211: Computer Architecture Summer 2016

Systems Programming and Computer Architecture ( ) Timothy Roscoe

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

Contents. Memory System Overview Cache Memory. Internal Memory. Virtual Memory. Memory Hierarchy. Registers In CPU Internal or Main memory

Lecture 18: Memory Systems. Spring 2018 Jason Tang

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

CS 240 Stage 3 Abstractions for Practical Systems

Where Have We Been? Ch. 6 Memory Technology

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 13

Memory Hierarchy. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

EEM 486: Computer Architecture. Lecture 9. Memory

Show how to connect three Full Adders to implement a 3-bit ripple-carry adder

UNIT-V MEMORY ORGANIZATION

Computer Organization

Internal Memory. Computer Architecture. Outline. Memory Hierarchy. Semiconductor Memory Types. Copyright 2000 N. AYDIN. All rights reserved.

UNIT 2 Data Center Environment

Organization. 5.1 Semiconductor Main Memory. William Stallings Computer Organization and Architecture 6th Edition

William Stallings Computer Organization and Architecture 6th Edition. Chapter 5 Internal Memory

Main Memory (RAM) Organisation

Chapter 5 Internal Memory

Computer Organization and Assembly Language (CS-506)

CENG3420 Lecture 08: Memory Organization

Lecture 10: Memory System -- Memory Technology CSE 564 Computer Architecture Summer 2017

General Cache Mechanics. Memory Hierarchy: Cache. Cache Hit. Cache Miss CPU. Cache. Memory CPU CPU. Cache. Cache. Memory. Memory

Module 1: Basics and Background Lecture 4: Memory and Disk Accesses. The Lecture Contains: Memory organisation. Memory hierarchy. Disks.

Memory Hierarchy. 2/18/2016 CS 152 Sec6on 5 Colin Schmidt

Semiconductor Memory Types Microprocessor Design & Organisation HCA2102

Computer Organization. 8th Edition. Chapter 5 Internal Memory

Memory Management! Goals of this Lecture!

Computer Architecture A Quantitative Approach, Fifth Edition. Chapter 2. Memory Hierarchy Design. Copyright 2012, Elsevier Inc. All rights reserved.

Secondary storage. CS 537 Lecture 11 Secondary Storage. Disk trends. Another trip down memory lane

CS241 Computer Organization Spring Principle of Locality

Chapter 5. Large and Fast: Exploiting Memory Hierarchy

CMPSC 311- Introduction to Systems Programming Module: Caching

ECE 341. Lecture # 16

Advanced Parallel Architecture Lesson 4 bis. Annalisa Massini /2015

k -bit address bus n-bit data bus Control lines ( R W, MFC, etc.)

Memory systems. Memory technology. Memory technology Memory hierarchy Virtual memory

Computer Memory. Textbook: Chapter 1

Memory Hierarchy Y. K. Malaiya

Memory Management. Goals of this Lecture. Motivation for Memory Hierarchy

Overview. Memory Classification Read-Only Memory (ROM) Random Access Memory (RAM) Functional Behavior of RAM. Implementing Static RAM

Copyright 2012, Elsevier Inc. All rights reserved.

Transcription:

The Memory Hierarchy Chapter 6 1

Outline! Storage technologies and trends! Locality of reference! Caching in the memory hierarchy 2

Random- Access Memory (RAM)! Key features! RAM is tradi+onally packaged as a chip.! Basic storage unit is normally a cell (one bit per cell).! Mul+ple RAM chips form a memory.! Sta@c RAM (SRAM)! Each cell stores a bit with a four or six- transistor circuit.! Retains value indefinitely, as long as it is kept powered.! Rela+vely insensi+ve to electrical noise (EMI), radia+on, etc.! Faster and more expensive than DRAM.! Dynamic RAM (DRAM)! Each cell stores bit with a capacitor. One transistor is used for access! Value must be refreshed every 10-100 ms.! More sensi+ve to disturbances (EMI, radia+on, ) than SRAM.! Slower and cheaper than SRAM. 3

SRAM vs DRAM Summary Transistors Access Needs Needs per bit time refresh? EDC? Cost Applications SRAM 4 or 6 1X No Maybe 100x Cache memories DRAM 1 10X Yes Yes 1X Main memories, frame buffers 4

Enhanced DRAMs! Basic DRAM cell has not changed since its inven@on in 1966.! Commercialized by Intel in 1970.! DRAM cores with beker interface logic and faster I/O :! Synchronous DRAM (SDRAM)! Uses a conven+onal clock signal instead of asynchronous control! Allows reuse of the row addresses (e.g., RAS, CAS, CAS, CAS)! Double data- rate synchronous DRAM (DDR SDRAM)! Double edge clocking sends two bits per cycle per pin! Different types dis+nguished by size of small prefetch buffer: DDR (2 bits), DDR2 (4 bits), DDR4 (8 bits)! By 2010, standard for most server and desktop systems! Intel Core i7 supports only DDR3 SDRAM 5

anima@on Memory Modules addr (row = i, col = j) DRAM 7 DRAM 0 : supercell (i,j) 64 MB memory module consisting of eight 8Mx8 DRAMs bits 56-63 bits 48-55 bits 40-47 bits 32-39 bits 24-31 bits 16-23 bits 8-15 bits 0-7 63 56 55 48 47 40 39 32 31 24 23 16 15 8 7 64-bit doubleword at main memory address A 0 Memory controller 64-bit doubleword 6

Op@onal Slides Conven@onal DRAM Organiza@on! d w DRAM: (e.g. 16 8 = 128 bits) Each supercell has w bits (e.g., w=8) Number of supercells is rows cols = d (e.g., 4 4 = 16) 16 x 8 DRAM chip cols 0 1 2 3 (to/from CPU) Memory controller 2 bits / addr 8 bits / data rows 0 1 2 3 supercell (2,1) Internal row buffer 7

Op@onal Slides anima@on Reading DRAM Supercell (2,1) 16 x 8 DRAM chip Memory controller RAS = 2 2 / addr Rows 0 1 2 Cols 0 1 2 3 8 / data 3 Internal row buffer 8

Op@onal Slides anima@on Reading DRAM Supercell (2,1) 16 x 8 DRAM chip To CPU Memory controller CAS = 1 2 / addr Rows 0 1 2 Cols 0 1 2 3 supercell (2,1) 8 / data 3 supercell (2,1) Internal row buffer 9

Nonvola@le Memories! DRAM and SRAM are vola@le memories! Lose informa+on if powered off.! Nonvola@le memories retain value even if powered off! Read- only memory (ROM): programmed during produc+on! Programmable ROM (PROM): can be programmed once! Eraseable PROM (EPROM): can be bulk erased (UV, X- Ray)! Electrically eraseable PROM (EEPROM): electronic erase capability! Flash memory: EEPROMs with par+al (sector) erase capability! Wears out aber about 100,000 erasings.! Uses for Nonvola@le Memories! Firmware programs stored in a ROM (BIOS, controllers for disks, network cards, graphics accelerators, security subsystems, )! Solid state disks (replace rota+ng disks in thumb drives, smart phones, mp3 players, tablets, laptops, )! Disk caches 10

Tradi@onal Bus Structure A bus is a collec@on of parallel wires that carry address, data, and control signals. Buses are typically shared by mul@ple devices. CPU chip Register file ALU System bus Memory bus Bus interface I/O bridge Main memory 11

Memory Read Transac@on The CPU places address A on the memory bus. CPU chip Register file Load operation: movq A,%rax %rax ALU I/O bridge Main memory 0 Bus interface x A 12

Memory Read Transac@on The CPU places address A on the memory bus. CPU chip Register file Load operation: movq A,%rax %rax ALU Bus interface I/O bridge Main memory Addr A 0 x A 13

Memory Read Transac@on Main memory reads A from the memory bus, retrieves word x, and places it on the bus. CPU chip Register file Load operation: movq A,%rax %rax ALU Bus interface I/O bridge Main memory X 0 x A 14

Memory Read Transac@on The CPU reads word x from the bus and copies it into register %rax. CPU chip Register file Load operation: movq A,%rax %rax X ALU I/O bridge Main memory 0 Bus interface x A 15

Memory Write Transac@on The CPU places address A on bus. Main memory reads it and waits for the corresponding data word to arrive. CPU chip Register file Store operation: movq %rax,a %rax Y ALU I/O bridge Addr A Main memory 0 Bus interface A 16

Memory Write Transac@on The CPU places data word Y on the bus. CPU chip Register file Store operation: movq %rax,a %rax Y Bus interface ALU I/O bridge Y Main memory 0 A 17

Memory Write Transac@on Main memory reads data word Y from the bus and stores it at address A. CPU chip register file Store operation: movq %rax,a %rax Y ALU I/O bridge main memory 0 bus interface Y A 18

What s Inside A Disk Drive? Arm Spindle Actuator Platters Read/Write head SCSI connector Electronics (including a processor and memory!) Image courtesy of Seagate Technology 19

Disk Geometry Track Sector Bits Recording Surface Spindle 20

Disk Geometry A sector is a sequence of bytes (usually 512 bytes). Each track consists of sectors separated by gaps. Each surface consists of many tracks. Disks consist of plakers, each with two surfaces. Tracks Surface Track k Gaps Spindle Sector 21

Disk Capacity Capacity: maximum number of bits that can be stored. Vendors express capacity in units of gigabytes (GB) Note: 1 GB = 10 9 Bytes, not 2 30 Bytes Capacity is determined by these technology factors: Recording density (bits/in): number of bits that can be squeezed into a 1 inch segment of a track. Track density (tracks/in): number of tracks that can be squeezed into a 1 inch radial segment. Areal density (bits/in 2 ): product of recording and track density. 22

Disk Geometry (Muliple- PlaKer View) Surface 0 Surface 1 Surface 2 Surface 3 Surface 4 Surface 5 Platter 0 Platter 1 Platter 2 Spindle 23

Disk Geometry (Muliple- PlaKer View) Aligned tracks form a cylinder. Cylinder k Surface 0 Surface 1 Surface 2 Surface 3 Surface 4 Surface 5 Platter 0 Platter 1 Platter 2 Spindle 24

Recording Zones Want each sector to be about the same size. Sector Outer tracks have lots of sectors Spindle Inner tracks have fewer sectors 25

Recording Zones Modern disks par@@on tracks into disjoint subsets called recording zones Each track in a zone has the same number of sectors. (Determined by the circumference of innermost track) Each zone has a different number of sectors/track.! Outer zones have more sectors/track! Inner zones have fewer sectors/track Use average number of sectors/track when compu+ng capacity. 26

Compu@ng Disk Capacity Capacity = (# bytes/sector) (average # sectors/track) (# tracks/surface) (# surfaces/plaker) (# plakers/disk) Example:! 512 bytes/sector! 300 sectors/track (on average)! 20,000 tracks/surface! 2 surfaces/plaher! 5 plahers/disk Capacity = 512 300 20000 2 5 = 30,720,000,000 = 30.72 GB 27

Disk Opera@on (Single- PlaKer View) The disk surface spins at a fixed rotational rate The read/write head is attached to the end of the arm and flies over the disk surface on a thin cushion of air. spindle spindle spindle spindle By moving radially, the arm can position the read/write head over any track. 28

Disk Opera@on (Mul@- PlaKer View) Read/write heads move in unison from cylinder to cylinder Arm Spindle 29

Disk Structure - top view of single plaker anima@on Surface organized into tracks Tracks divided into sectors 30

Disk Access Head in position above a track 31

Disk Access Rotation is counter-clockwise 32

Disk Access Read About to read blue sector 33

Disk Access Read After BLUE read After reading blue sector 34

Disk Access Read After BLUE read Red request scheduled next 35

Disk Access Seek After BLUE read Seek for RED Seek to red s track 36

Disk Access Rota@onal Latency After BLUE read Seek for RED Rotational latency Wait for red sector to rotate around 37

Disk Access Read After BLUE read Seek for RED Rotational latency After RED read Complete read of red 38

Disk Access Service Time Components After BLUE read Seek for RED Rotational latency After RED read Data transfer Seek Rota@onal latency Data transfer 39

Disk Access Time Average @me to access some target sector: T access = T avg seek + T avg rota@on + T avg transfer Seek @me (T avg seek ) Time to posi@on heads over cylinder containing target sector. Typical: 3 9 ms Rota@onal latency (T avg rota@on ) Time wai@ng for first bit of target sector to pass under r/w head. T avg rota@on = 1/2 1/RPM 60,000 ms/min Typical: 1/2 1/7200 60,000 ms/min = 4 ms Transfer @me (T avg transfer ) Time to read the bits in the target sector. T avg transfer = 1/RPM 1/(avg # sectors/track) 60,000 ms/min Typical: 1/7200 1/400 sectors 60,000ms/min = 0.02 ms 40

Disk Access Time Example Given: Rota+onal rate = 7,200 RPM Average seek +me = 9 ms. Avg # sectors/track = 400. Derived: T avg rota+on = 1/2 1/7200 RPM 60,000 ms/min= 4 ms. T avg transfer = 1/7200 RPM 1/400 secs/track 60,000 ms/min= 0.02 ms T access = 9 ms + 4 ms + 0.02 ms = 13.02 ms Important points:! Access @me dominated by seek @me and rota@onal latency.! First bit in a sector is the most expensive, the rest are free.! SRAM access @me is about 4 ns/doubleword, DRAM about 60 ns! Disk is about 40,000 @mes slower than SRAM,! 2,500 @mes slower then DRAM. 41

Logical Disk Blocks Modern disks present a simpler abstract view of the complex sector geometry:! The set of available sectors is modeled as a sequence of b- sized logical blocks (0, 1, 2,...) Mapping between logical blocks and actual (physical) sectors! Maintained by hardware/firmware device called disk controller.! Converts requests for logical blocks into (surface,track,sector) triples. Allows controller to set aside spare cylinders for each zone.! Accounts for the difference in formahed capacity and maximum capacity. 42

I/O Bus CPU chip Register file ALU Bus interface I/O bridge Main memory I/O bus System bus Memory bus USB controller Graphics adapter Disk controller Expansion slots for other devices such as network adapters. Mouse Keyboard Monitor Disk 43

Reading a Disk Sector (1) CPU chip Register file ALU CPU initiates a disk read by writing a command, logical block number, and destination memory address to a port (address) associated with disk controller. Bus interface I/O bridge Main memory I/O bus System bus Memory bus USB controller Graphics adapter Disk controller Expansion slots for other devices such as network adapters. Mouse Keyboard Monitor Disk 44

Reading a Disk Sector (2) CPU chip Register file ALU Disk controller reads the sector and performs a direct memory access (DMA) transfer into main memory. Bus interface I/O bridge Main memory I/O bus System bus Memory bus USB controller Graphics adapter Disk controller Expansion slots for other devices such as network adapters. Mouse Keyboard Monitor Disk 45

Reading a Disk Sector (3) CPU chip Register file ALU When the DMA transfer completes, the disk controller notifies the CPU with an interrupt (i.e., asserts a special interrupt pin on the CPU) Bus interface I/O bridge Main memory I/O bus System bus Memory bus USB controller Graphics adapter Disk controller Expansion slots for other devices such as network adapters. Mouse Keyboard Monitor Disk 46

Flash Memory: Solid State Disk (SSD) Non- vola@le: Retains data without power! Fast (like DRAM)! More rugged than disks.! More expensive than disks. 47

Solid State Disks (SSDs) I/O bus Solid State Disk (SSD) Flash translation layer Requests to read and write logical disk blocks Flash memory Block 0 Page 0 Page 1 Page P-1 Block B-1 Page 0 Page 1 Page P-1 Pages: 512KB to 4KB, Blocks: 32 to 128 pages! Data is read/wriken in units of pages.! Page can only be wriken aper its block has been erased! A block wears out aper 100,000 repeated writes. 48

SSD Performance Characteris@cs Sequen@al read thruput 550 MB/s Random read thruput 365 MB/s Avg sequen@al read @me 50 us Reading Sequen@al write thruput 470 MB/s Random write thruput 303 MB/s Avg sequen@al write @me 60 us Wri@ng Sequen@al access is faster than random access. Random writes are slow.! Erasing a block takes a long +me (~1 ms)! Modifying a block page requires all other pages to be copied to new block Source: Intel SSD 730 product specifica@on. 49

SSD Tradeoffs vs Rota@ng Disks Advantages to SSD No moving parts " faster, less power, more rugged Disadvantages Have the poten+al to wear out Mi+gated by wear leveling logic in flash transla+on layer! Intel SSD 730 guarantees 128 petabyte (128 x 10 15 bytes) of writes before they wear out In 2015, about 6 +mes more expensive per byte * * Best Buy: $13/32GB flash, $60/1TB External Disk Applica@ons Phones, laptops, portable electronics Beginning to appear in desktops and servers 50

SRAM Storage Trends Metric 1985 1990 1995 2000 2005 2010 2015 2015:1985 $/MB 2,900 320 256 100 75 60 320 116 access (ns) 150 35 15 3 2 1.5 200 115 DRAM Metric 1985 1990 1995 2000 2005 2010 2015 2015:1985 $/MB 880 100 30 1 0.1 0.06 0.02 44,000 access (ns) 200 100 70 60 50 40 20 10 typical size (MB) 0.256 4 16 64 2,000 8,000 16.000 62,500 Disk Metric 1985 1990 1995 2000 2005 2010 2015 2015:1985 $/GB 100,000 8,000 300 10 5 0.3 0.03 3,333,333 access (ms) 75 28 10 8 5 3 3 25 typical size (GB) 0.01 0.16 1 20 160 1,500 3,000 300,000 51

CPU Clock Rates Inflec@on point in computer history when designers hit the Power Wall 1985 1990 1995 2003 2005 2010 2015 2015:1985 CPU 80286 80386 Pentium P-4 Core 2 Core i7(n) Core i7(h) Clock rate (MHz) 6 20 150 3,300 2,000 2,500 3,000 500 Cycle time (ns) 166 50 6 0.30 0.50 0.4 0.33 500 Cores 1 1 1 1 2 4 4 4 Effective cycle 166 50 6 0.30 0.25 0.10 0.08 2,075 time (ns) (n) Nehalem processor (h) Haswell processor 52

The CPU- Memory Gap 100,000,000.0 10,000,000.0 1,000,000.0 Time (ns) 100,000.0 10,000.0 1,000.0 100.0 10.0 1.0 0.1 0.0 1985 1990 1995 2000 2003 2005 2010 2015 Year Disk Flash/SSD DRAM SRAM CPU Disk seek time SSD access time DRAM access time SRAM access time CPU cycle time Effective CPU cycle time 53

Locality to the Rescue! The key to bridging this CPU- Memory gap is a fundamental property of computer programs known as locality 54

anima@on Locality Principle of Locality: Programs tend to use data and instruc@ons with addresses near or equal to those they have used recently Temporal locality: Recently referenced items are likely to be referenced again in the near future Spa@al locality: Items with nearby addresses tend to be referenced close together in +me 55

anima@on Locality Example sum = 0; for (i = 0; i < n; i++) sum += a[i]; return sum; Data references! Reference array elements in succession (stride- 1 reference pahern).! Reference variable sum each itera+on. Instruc@on references! Reference instruc+ons in sequence.! Cycle through loop repeatedly. Spa@al locality Temporal locality Spa@al locality Temporal locality 56

Qualita@ve Es@mates of Locality Claim: Being able to look at code and get a qualita@ve sense of its locality is a key skill for a professional programmer. Ques@on: Does this func@on have good locality with respect to array a? 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; 57

Qualita@ve Es@mates of Locality Claim: Being able to look at code and get a qualita@ve sense of its locality is a key skill for a professional programmer. Ques@on: Does this func@on have good locality with respect to array a? 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; 58

Locality Example Ques@on: Can you permute the loops so that the func@on scans the 3- d array a with a stride- 1 reference pakern (and thus has good spa@al locality)? int sum_array_3d(int a[m][n][n]) { int i, j, k, sum = 0; } for (i = 0; i < M; i++) for (j = 0; j < N; j++) for (k = 0; k < N; k++) sum += a[k][i][j]; return sum; 59

Memory Hierarchies Some fundamental and enduring proper@es of hardware and sopware:! Fast storage technologies cost more per byte, have less capacity, and require more power (heat!).! The gap between CPU and main memory speed is widening.! Well- wrihen programs tend to exhibit good locality. These fundamental proper@es complement each other beau@fully. They suggest an approach for organizing memory and storage systems known as a memory hierarchy. 60

Example Memory Hierarchy Smaller, faster, and costlier (per byte) storage devices L3: L2: L1: L0: Regs L1 cache (SRAM) L2 cache (SRAM) L3 cache (SRAM) CPU registers hold words retrieved from the L1 cache. L1 cache holds cache lines retrieved from the L2 cache. L2 cache holds cache lines retrieved from L3 cache L3 cache holds cache lines retrieved from main memory. Larger, slower, and cheaper (per byte) storage L5: devices L4: Main memory (DRAM) Local secondary storage (local disks) Main memory holds disk blocks retrieved from local disks. Local disks hold files retrieved from disks on remote servers L6: Remote secondary storage (e.g., Web servers) 61

Caches Cache: A smaller, faster storage device that acts as a staging area for a subset of the data in a larger, slower device. Fundamental idea of a memory hierarchy:! For each k, the faster, smaller device at level k serves as a cache for the larger, slower device at level k+1. Why do memory hierarchies work?! Because of locality, programs tend to access the data at level k more oben than they access the data at level k+1.! Thus, the storage at level k+1 can be slower, and thus larger and cheaper per bit. Big Idea: The memory hierarchy creates a large pool of storage that costs as much as the cheap storage near the bokom, but that serves data to programs at the rate of the fast storage near the top. 62

General Cache Concepts CPU Memory 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 63

General Cache Concepts CPU Cache 84 9 10 14 3 Smaller, faster, more expensive memory caches a subset of the blocks Memory 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Larger, slower, cheaper memory viewed as par@@oned into blocks 64

Intel Core i7 Cache Hierarchy Processor package Core 0 Regs Core 3 Regs L1 i- cache and d- cache: 32 KB Access: 4 cycles L1 d-cache L1 i-cache L1 d-cache L1 i-cache L2 cache: 256 KB Access: 10 cycles L2 cache L2 cache L3 cache: 8 MB Access: 40-75 cycles L3 cache (shared by all cores) Block size: 64 bytes for all caches. Main memory 65

General Cache Concepts anima@on CPU Cache 84 9 10 14 3 Smaller, faster, more expensive memory caches a subset of the blocks Data is copied in block- sized transfer units Memory 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Larger, slower, cheaper memory viewed as par@@oned into blocks Typical block size: 64 bytes 66

General Cache Concepts: Placement anima@on Cache Memory CPU 8 5 14 3 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 The cache has a limited size. There are constraints on where data can be placed. We cannot put any block any place! 67

General Cache Concepts: Hit anima@on Cache CPU Request: 10 4 9 10 3 Data in block b is needed Block b is in cache: Hit! Memory 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 68

General Cache Concepts: Miss anima@on CPU Request: 12 Data in block b is needed Cache 12 4 9 10 3 Request: 12 Block b is NOT in cache! Block is fetched from memory Memory 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Block is stored in cache. Request is sahsfied. Block now available for future accesses. 69

Ques@ons A block of data (b) is loaded into cache Where do you put it? Placement policy: Where can you place b in the cache? Not every block can go into every place in cache. Why? Efficiency. Where will you place b? Some other block will get evicted. Replacement policy 70

General Caching Concepts: Types of Cache Misses Cold (compulsory) miss Cold misses occur because the cache is empty. Conflict miss Most caches limit blocks at level k+1 to a small subset (some+mes a singleton) of the block posi+ons at level k.! E.g. Block i at level k+1 must be placed in block (i mod 4) at level k. Conflict misses occur when the level k cache is large enough, but mul+ple data objects all map to the same level k block. Capacity miss! E.g. Referencing blocks 0, 8, 0, 8, 0, 8,... would miss every +me. Occurs when the set of ac+ve cache blocks (working set) is larger than the cache. 71

Examples of Caching in the Hierarchy Cache Type What is Cached? Where is it Cached? Latency (cycles) Managed By Registers 4-8 bytes words CPU core 0 Compiler TLB Address transla@ons On- Chip TLB 0 Hardware L1 cache L2 cache Virtual Memory Buffer cache Network buffer cache Browser cache 64- bytes block 64- bytes block 4- KB page Parts of files Parts of files Web pages On- Chip L1 On/Off- Chip L2 Main memory Main memory Local disk Local disk 1 10 100 10,000,000 10,000,000 Hardware Hardware Hardware + OS Disk cache Disk sectors Disk controller 100,000 Disk firmware 100 OS AFS/NFS client Web browser Web cache Web pages Remote server disks 1,000,000,000 Web proxy server 72

Summary! The speed gap between CPU, memory and mass storage con@nues to widen.! Well- wriken programs exhibit a property called locality.! Memory hierarchies based on caching close the gap by exploi@ng locality. 73