+ All Categories
Home > Documents > The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted...

The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted...

Date post: 21-Jan-2016
Category:
Upload: suzan-jones
View: 217 times
Download: 0 times
Share this document with a friend
Popular Tags:
74
Carnegie Mellon 1 O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu (Section 1) Adapted from http ://csapp.cs.cmu.edu / and http ://inst.eecs.berkeley.edu/~cs152
Transcript
Page 1: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

The Memory Hierarchy

CENG331 - Computer Organization

Instructor: Murat Manguoglu (Section 1)

Adapted from http://csapp.cs.cmu.edu/ and http://inst.eecs.berkeley.edu/~cs152

Page 2: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Today Storage technologies and trends Locality of reference Caching in the memory hierarchy

Page 3: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Early Read-Only Memory Technologies

Punched cards, From early 1700s through Jaquard Loom, Babbage, and then IBM

Punched paper tape, instruction stream in Harvard Mk 1

IBM Card Capacitor ROS

IBM Balanced Capacitor ROSDiode Matrix,

EDSAC-2 µcode store

Page 4: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Early Read/Write Main Memory Technologies

Williams Tube, Manchester Mark 1, 1947

Babbage, 1800s: Digits stored on mechanical wheels

Mercury Delay Line, Univac 1, 1951

Also, regenerative capacitor memory on Atanasoff-Berry computer, and rotating magnetic drum memory on IBM 650

Page 5: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

MIT Whirlwind Core Memory

Page 6: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

6

Core Memory Core memory was first large scale reliable main memory

invented by Forrester in late 40s/early 50s at MIT for Whirlwind project Bits stored as magnetization polarity on small ferrite cores

threaded onto two-dimensional grid of wires Coincident current pulses on X and Y wires would write cell

and also sense original state (destructive reads)

DEC PDP-8/E Board, 4K words x 12 bits,

(1968)

• Robust, non-volatile storage• Used on space shuttle

computers until recently• Cores threaded onto wires by

hand (25 billion a year at peak production)

• Core access time ~ 1ms

Page 7: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

7

Semiconductor Memory Semiconductor memory began to be competitive in early

1970s Intel formed to exploit market for semiconductor memory Early semiconductor memory was Static RAM (SRAM). SRAM cell

internals similar to a latch (cross-coupled inverters).

First commercial Dynamic RAM (DRAM) was Intel 1103 1Kbit of storage on single chip charge on a capacitor used to hold value

Semiconductor memory quickly replaced core in ‘70s

Page 8: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Random-Access Memory (RAM) Key features

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

RAM comes in two varieties: SRAM (Static RAM) DRAM (Dynamic RAM)

Page 9: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

One Transistor Dynamic RAM [Dennard, IBM]

TiN top electrode (VREF)

Ta2O5 dielectric

W bottomelectrode

polywordline

access transistor

1-T DRAM Cell

word

bit

access transistor

Storagecapacitor (FET gate, trench, stack)

VREF

Page 10: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Modern DRAM Structure

[Samsung, sub-70nm DRAM, 2004]

Page 11: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

DRAM Architecture

Row

Ad

dre

ss

Decod

er

Col.1

Col.2M

Row 1

Row 2N

Column Decoder & Sense Amplifiers

M

N

N+M

bit linesword lines

Memory cell(one bit)

DData

• Bits stored in 2-dimensional arrays on chip• Modern chips have around 4-8 logical banks on each chip

– each logical bank physically implemented as many smaller arrays

Page 12: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

DRAM Packaging(Laptops/Desktops/Servers)

DIMM (Dual Inline Memory Module) contains multiple chips with clock/control/address signals connected in parallel (sometimes need buffers to drive signals to all chips)

Data pins work together to return wide word (e.g., 64-bit data bus using 16x4-bit parts)

Address lines multiplexed row/column address

Clock and control signals

Data bus(4b,8b,16b,32b)

DRAM chip

~12

~7

Page 13: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

DRAM Packaging, Mobile Devices

[ Apple A4 package cross-section, iFixit 2010 ]

Two stacked DRAM die

Processor plus logic die

[ Apple A4 package on circuit board]

Page 14: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

DRAM OperationThree steps in read/write access to a given bank Row access (RAS)

decode row address, enable addressed row (often multiple Kb in row) bitlines share charge with storage cell small change in voltage detected by sense amplifiers which latch whole row of bits sense amplifiers drive bitlines full rail to recharge storage cells

Column access (CAS) decode column address to select small number of sense amplifier latches (4, 8, 16,

or 32 bits depending on DRAM package) on read, send latched bits out to chip pins on write, change sense amplifier latches which then charge storage cells to required

value can perform multiple column accesses on same row without another row access

(burst mode) Precharge

charges bit lines to known value, required before next row accessEach step has a latency of around 15-20ns in modern DRAMsVarious DRAM standards (DDR, RDRAM) have different ways of encoding the signals for transmission

to the DRAM, but all share same core architecture

Page 15: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Double-Data Rate (DDR2) DRAM

[ Micron, 256Mb DDR2 SDRAM datasheet ]

Row Column Precharge Row’

Data

200MHz Clock

400Mb/s Data Rate

Page 16: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

CPU-Memory Bottleneck

MemoryCPU

Performance of high-speed computers is usuallylimited by memory bandwidth & latency

• Latency (time for a single access)Memory access time >> Processor cycle time

• Bandwidth (number of accesses per unit time)

Page 17: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Processor-DRAM Gap (latency)

Time

µProc 60%/year

DRAM7%/year

1

10

100

10001

98

0

19

81

19

83

19

84

19

85

19

86

19

87

19

88

19

89

19

90

19

91

19

92

19

93

19

94

19

95

19

96

19

97

19

98

19

99

20

00

DRAM

CPU

19

82

Processor-MemoryPerformance Gap:(growing 50%/yr)

Perf

orm

an

ce

Four-issue 3GHz superscalar accessing 100ns DRAM could execute 1,200 instructions during time for one memory access!

Page 18: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Physical Size Affects Latency

18

Small Memory

CPU

Big Memory

CPU

• Signals have further to travel

• Fan out to more locations

Page 19: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

19

Relative Memory Cell Sizes

[ Foss, “Implementing

Application-Specific Memory”,

ISSCC 1996 ]

DRAM on memory chip

On-Chip SRAM in

logic chip

Page 20: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

SRAM vs DRAM Summary

Trans. Access Needs Needsper 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

Page 21: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Nonvolatile Memories DRAM and SRAM are volatile memories

Lose information if powered off. Nonvolatile memories retain value even if powered off

Read-only memory (ROM): programmed during production 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 partial (block-level) erase capability

Wears out after about 100,000 erasings Uses for Nonvolatile Memories

Firmware programs stored in a ROM (BIOS, controllers for disks, network cards, graphics accelerators, security subsystems,…)

Solid state disks (replace rotating disks in thumb drives, smart phones, mp3 players, tablets, laptops,…)

Disk caches

Page 22: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Traditional Bus Structure Connecting CPU and Memory

A bus is a collection of parallel wires that carry address, data, and control signals.

Buses are typically shared by multiple devices.

Mainmemory

I/O bridge

Bus interface

ALU

Register file

CPU chip

System bus Memory bus

Page 23: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Memory Read Transaction (1) CPU places address A on the memory bus.

ALU

Register file

Bus interface

A0

Ax

Main memoryI/O bridge

%rax

Load operation: movq A, %rax

Page 24: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Memory Read Transaction (2) Main memory reads A from the memory bus, retrieves

word x, and places it on the bus.

ALU

Register file

Bus interface

x 0

Ax

Main memory

%rax

I/O bridge

Load operation: movq A, %rax

Page 25: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Memory Read Transaction (3) CPU read word x from the bus and copies it into register

%rax.

xALU

Register file

Bus interface x

Main memory0

A

%rax

I/O bridge

Load operation: movq A, %rax

Page 26: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Memory Write Transaction (1) CPU places address A on bus. Main memory reads it and

waits for the corresponding data word to arrive.

yALU

Register file

Bus interface

A

Main memory0

A

%rax

I/O bridge

Store operation: movq %rax, A

Page 27: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Memory Write Transaction (2) CPU places data word y on the bus.

yALU

Register file

Bus interface

y

Main memory0

A

%rax

I/O bridge

Store operation: movq %rax, A

Page 28: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Memory Write Transaction (3) Main memory reads data word y from the bus and stores

it at address A.

yALU

Register file

Bus interface y

main memory0

A

%rax

I/O bridge

Store operation: movq %rax, A

Page 29: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

What’s Inside A Disk Drive?SpindleArm

Actuator

Platters

Electronics(including a processor and memory!)SCSI

connector

Image courtesy of Seagate Technology

Page 30: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Disk Operation (Single-Platter View)

The disk surface spins at a fixedrotational rate

By moving radially, the arm can position the read/write head over any track.

The read/write headis attached to the endof the arm and flies over the disk surface ona thin cushion of air.

spindle

spindle

spin

dle

spindlespindle

Page 31: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Disk Operation (Multi-Platter View)

Arm

Read/write heads move in unison

from cylinder to cylinder

Spindle

Page 32: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Tracks divided into sectors

Disk Structure - top view of single platter

Surface organized into tracks

Page 33: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Disk Access

Head in position above a track

Page 34: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Disk Access

Rotation is counter-clockwise

Page 35: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Disk Access – Read

About to read blue sector

Page 36: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Disk Access – Read

After BLUE read

After reading blue sector

Page 37: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Disk Access – Read

After BLUE read

Red request scheduled next

Page 38: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Disk Access – Seek

After BLUE read Seek for RED

Seek to red’s track

Page 39: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Disk Access – Rotational Latency

After BLUE read Seek for RED Rotational latency

Wait for red sector to rotate around

Page 40: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Disk Access – Read

After BLUE read Seek for RED Rotational latency After RED read

Complete read of red

Page 41: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Disk Access – Service Time Components

After BLUE read Seek for RED Rotational latency After RED read

Data transfer Seek Rotational latency

Data transfer

Page 42: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Disk Access Time Average time to access some target sector approximated by :

Taccess = Tavg seek + Tavg rotation + Tavg transfer Seek time (Tavg seek)

Time to position heads over cylinder containing target sector. Typical Tavg seek is 3—9 ms

Rotational latency (Tavg rotation) Time waiting for first bit of target sector to pass under r/w head. Tavg rotation = 1/2 x 1/RPMs x 60 sec/1 min Typical Tavg rotation = 7200 RPMs

Transfer time (Tavg transfer) Time to read the bits in the target sector. Tavg transfer = 1/RPM x 1/(avg # sectors/track) x 60 secs/1 min.

Page 43: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Disk Access Time Example Given:

Rotational rate = 7,200 RPM Average seek time = 9 ms. Avg # sectors/track = 400.

Derived: Tavg rotation = 1/2 x (60 secs/7200 RPM) x 1000 ms/sec = 4 ms. Tavg transfer = 60/7200 RPM x 1/400 secs/track x 1000 ms/sec = 0.02 ms Taccess = 9 ms + 4 ms + 0.02 ms

Important points: Access time dominated by seek time and rotational latency. First bit in a sector is the most expensive, the rest are free. SRAM access time is about 4 ns/doubleword, DRAM about 60 ns

Disk is about 40,000 times slower than SRAM, 2,500 times slower then DRAM.

Page 44: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

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 “formatted capacity” and “maximum

capacity”.

Page 45: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

I/O Bus

Mainmemory

I/O bridge

Bus interface

ALU

Register file

CPU chip

System bus Memory bus

Disk controller

Graphicsadapter

USBcontroller

Mouse Keyboard Monitor

Disk

I/O bus Expansion slots forother devices suchas network adapters.

Page 46: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Reading a Disk Sector (1)

Mainmemory

ALU

Register file

CPU chip

Disk controller

Graphicsadapter

USBcontroller

mouse keyboard Monitor

Disk

I/O bus

Bus interface

CPU initiates a disk read by writing a command, logical block number, and destination memory address to a port (address) associated with disk controller.

Page 47: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Reading a Disk Sector (2)

Mainmemory

ALU

Register file

CPU chip

Disk controller

Graphicsadapter

USBcontroller

Mouse Keyboard Monitor

Disk

I/O bus

Bus interface

Disk controller reads the sector and performs a direct memory access (DMA) transfer into main memory.

Page 48: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Reading a Disk Sector (3)

Mainmemory

ALU

Register file

CPU chip

Disk controller

Graphicsadapter

USBcontroller

Mouse Keyboard Monitor

Disk

I/O bus

Bus interface

When the DMA transfer completes, the disk controller notifies the CPU with an interrupt (i.e., asserts a special “interrupt” pin on the CPU)

Page 49: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Solid State Disks (SSDs)

Pages: 512KB to 4KB, Blocks: 32 to 128 pages Data read/written in units of pages. Page can be written only after its block has been erased A block wears out after about 100,000 repeated writes.

Flash translation layer

I/O bus

Page 0 Page 1 Page P-1…Block 0

… Page 0 Page 1 Page P-1…Block B-1

Flash memory

Solid State Disk (SSD)

Requests to read and write logical disk blocks

Page 50: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

SSD Performance Characteristics

Sequential access faster than random access Common theme in the memory hierarchy

Random writes are somewhat slower Erasing a block takes a long time (~1 ms) Modifying a block page requires all other pages to be copied to

new block In earlier SSDs, the read/write gap was much larger.

Sequential read tput 550 MB/s Sequential write tput 470 MB/sRandom read tput 365 MB/s Random write tput 303 MB/sAvg seq read time 50 us Avg seq write time 60 us

Source: Intel SSD 730 product specification.

Page 51: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

SSD Tradeoffs vs Rotating Disks Advantages

No moving parts faster, less power, more rugged

Disadvantages Have the potential to wear out

Mitigated by “wear leveling logic” in flash translation layer E.g. Intel SSD 730 guarantees 128 petabyte (128 x 1015 bytes) of

writes before they wear out In 2015, about 30 times more expensive per byte

Applications MP3 players, smart phones, laptops Beginning to appear in desktops and servers

Page 52: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Today Storage technologies and trends Locality of reference Caching in the memory hierarchy

Page 53: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

The CPU-Memory GapThe gap widens between DRAM, disk, and CPU speeds.

1985 1990 1995 2000 2003 2005 2010 20150.0

0.1

1.0

10.0

100.0

1,000.0

10,000.0

100,000.0

1,000,000.0

10,000,000.0

100,000,000.0

Disk seek timeSSD access timeDRAM access timeSRAM access timeCPU cycle timeEffective CPU cycle time

Year

Tim

e (

ns

)

DRAM

CPU

SSD

Disk

Page 54: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Locality to the Rescue!

The key to bridging this CPU-Memory gap is a fundamental property of computer programs known as locality

Page 55: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Locality Principle of Locality: Programs tend to use data and

instructions 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

Spatial locality: Items with nearby addresses tend

to be referenced close together in time

Page 56: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Real Memory Reference Patterns

Donald J. Hatfield, Jeanette Gerald: Program Restructuring for Virtual Memory. IBM Systems Journal 10(3): 168-192 (1971)

Time

Mem

ory

Ad

dre

ss (

on

e d

ot

per

acc

ess)

Page 57: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Typical Memory Reference PatternsAddress

Time

Instruction fetches

Stackaccesses

Dataaccesses

n loop iterations

subroutine call

subroutine return

argument access

vector access

scalar accesses

Page 58: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Memory Reference Patterns

Donald J. Hatfield, Jeanette Gerald: Program Restructuring for Virtual Memory. IBM Systems Journal 10(3): 168-192 (1971)

Time

Mem

ory

Ad

dre

ss (

on

e d

ot

per

acc

ess)

SpatialLocality

Temporal Locality

Page 59: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Locality Example

Data references Reference array elements in succession

(stride-1 reference pattern). Reference variable sum each iteration.

Instruction references Reference instructions in sequence. Cycle through loop repeatedly.

sum = 0;for (i = 0; i < n; i++)

sum += a[i];return sum;

Spatial localityTemporal locality

Spatial localityTemporal locality

Page 60: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Locality Example Question: Does this function 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;}

Page 61: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Locality Example Question: Can you permute the loops so that the function

scans the 3-d array a with a stride-1 reference pattern (and thus has good spatial 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;}

Page 62: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Qualitative Estimates of Locality 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 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;}

Page 63: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Memory Hierarchies Some fundamental and enduring properties of hardware

and software: 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-written programs tend to exhibit good locality.

These fundamental properties complement each other beautifully.

They suggest an approach for organizing memory and storage systems known as a memory hierarchy.

Page 64: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Today Storage technologies and trends Locality of reference Caching in the memory hierarchy

Page 65: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Example Memory Hierarchy

Regs

L1 cache (SRAM)

Main memory(DRAM)

Local secondary storage(local disks)

Larger, slower, and cheaper (per byte)storagedevices

Remote secondary storage(e.g., Web servers)

Local disks hold files retrieved from disks on remote servers

L2 cache (SRAM)

L1 cache holds cache lines retrieved from the L2 cache.

CPU registers hold words retrieved from the L1 cache.

L2 cache holds cache lines retrieved from L3 cache

L0:

L1:

L2:

L3:

L4:

L5:

Smaller,faster,and costlier(per byte)storage devices

L3 cache (SRAM)

L3 cache holds cache lines retrieved from main memory.

L6:

Main memory holds disk blocks retrieved from local disks.

Page 66: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Management of Memory Hierarchy Small/fast storage, e.g., registers

Address usually specified in instruction Generally implemented directly as a register file

but hardware might do things behind software’s back, e.g., stack management, register renaming

Larger/slower storage, e.g., main memory Address usually computed from values in register Generally implemented as a hardware-managed cache

hierarchy hardware decides what is kept in fast memory but software may provide “hints”, e.g., don’t cache or prefetch

Page 67: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

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

often 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 bottom, but that serves data to programs at the rate of the fast storage near the top.

Page 68: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

General Cache Concepts

0 1 2 3

4 5 6 7

8 9 10 11

12 13 14 15

8 9 14 3Cache

MemoryLarger, slower, cheaper memoryviewed as partitioned into “blocks”

Data is copied in block-sized transfer units

Smaller, faster, more expensivememory caches a subset ofthe blocks

4

4

4

10

10

10

Page 69: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

General Cache Concepts: Hit

0 1 2 3

4 5 6 7

8 9 10 11

12 13 14 15

8 9 14 3Cache

Memory

Data in block b is neededRequest: 14

14Block b is in cache:Hit!

Page 70: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

General Cache Concepts: Miss

0 1 2 3

4 5 6 7

8 9 10 11

12 13 14 15

8 9 14 3Cache

Memory

Data in block b is neededRequest: 12

Block b is not in cache:Miss!

Block b is fetched frommemoryRequest: 12

12

12

12

Block b is stored in cache• Placement policy:

determines where b goes• Replacement policy:

determines which blockgets evicted (victim)

Page 71: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

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 (sometimes a

singleton) of the block positions 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 multiple data objects all map to the same level k block.

E.g. Referencing blocks 0, 8, 0, 8, 0, 8, ... would miss every time. Capacity miss

Occurs when the set of active cache blocks (working set) is larger than the cache.

Page 72: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Examples of Caching in the Mem. Hierarchy

Hardware MMU

0On-Chip TLBAddress translationsTLB

Web browser10,000,000Local diskWeb pagesBrowser cache

Web cache

Network buffer cache

Buffer cache

Virtual Memory

L2 cache

L1 cache

Registers

Cache Type

Web pages

Parts of files

Parts of files

4-KB pages

64-byte blocks

64-byte blocks

4-8 bytes words

What is Cached?

Web proxy server

1,000,000,000Remote server disks

OS100Main memory

Hardware4On-Chip L1

Hardware10On-Chip L2

NFS client10,000,000Local disk

Hardware + OS100Main memory

Compiler0 CPU core

Managed ByLatency (cycles)Where is it Cached?

Disk cache Disk sectors Disk controller 100,000 Disk firmware

Page 73: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

Summary The speed gap between CPU, memory and mass storage

continues to widen.

Well-written programs exhibit a property called locality.

Memory hierarchies based on caching close the gap by exploiting locality.

Page 74: The Memory Hierarchy CENG331 - Computer Organization Instructor: Murat Manguoglu(Section 1) Adapted from  and cs152

74

Acknowledgements

• These slides contain material developed and copyright by:

– Arvind (MIT)– Krste Asanovic (MIT/UCB)– Joel Emer (Intel/MIT)– James Hoe (CMU)– John Kubiatowicz (UCB)– David Patterson (UCB)

• MIT material derived from course 6.823• UCB material derived from course CS252


Recommended