Docsity

Cache Memories, Lecture Slide - Computer Science, Slides of Computer System Design and Architecture

Partial preview of the text

Download Cache Memories, Lecture Slide - Computer Science and more Slides Computer System Design and Architecture in PDF only on Docsity!

Cache Memories

15-­‐213: Introduc0on to Computer Systems

th

Lecture, Sep. 23, 2010.

Instructors:

Randy Bryant and Dave O’Hallaron

Today

 Cache memory organiza7on and opera7on

 Performance impact of caches

 The memory mountain

 Rearranging loops to improve spa0al locality

 Using blocking to improve temporal locality

General Cache Organiza7on (S, E, B)

E = 2

e

lines per set

S = 2

s

sets

set

line

B-­‐ tag v

B = 2

b

bytes per cache block (the data)

Cache size:

C = S x E x B data bytes

valid bit

Cache Read

E = 2

e

lines per set

S = 2

s

sets

tag B-­‐ v

valid bit

B = 2

b

bytes per cache block (the data)

t bits s bits b bits

Address of word:

tag set

index

block

offset

data begins at this offset

  • Locate set
  • Check if any line in set

has matching tag

  • Yes + line valid: hit
  • Locate data star@ng

at offset

Example: Direct Mapped Cache (E = 1)

Direct mapped: One line per set

Assume: cache block size 8 bytes

t bits 0…01 100

Address of int:

v tag 0 1 2 3 4 5 6 7

valid? + match: assume yes = hit

block offset

tag

Example: Direct Mapped Cache (E = 1)

Direct mapped: One line per set

Assume: cache block size 8 bytes

t bits 0…01 100

Address of int:

v tag 0 1 2 3 4 5 6 7

valid? + match: assume yes = hit

int (4 Bytes) is here

block offset

No match: old line is evicted and replaced

A Higher Level Example

int sum_array_rows(double a[16][16])

int i, j;

double sum = 0;

for (i = 0; i < 16; i++)

for (j = 0; j < 16; j++)

sum += a[i][j];

return sum;

32 B = 4 doubles

assume: cold (empty) cache,

a[0][0] goes here

int sum_array_cols(double a[16][16])

int i, j;

double sum = 0;

for (j = 0; i < 16; i++)

for (i = 0; j < 16; j++)

sum += a[i][j];

return sum;

blackboard

Ignore the variables sum, i, j

E-­‐way Set Associa7ve Cache (Here: E = 2)

E = 2: Two lines per set

Assume: cache block size 8 bytes

t bits 0…01 100

Address of short int:

v tag 0 1 2 3 4 5 6 7 v tag 0 1 2 3 4 5 6 7

v tag 0 1 2 3 4 5 6 7 v tag 0 1 2 3 4 5 6 7

v tag 0 1 2 3 4 5 6 7 v tag 0 1 2 3 4 5 6 7

v tag 0 1 2 3 4 5 6 7 v tag 0 1 2 3 4 5 6 7

find set

E-­‐way Set Associa7ve Cache (Here: E = 2)

E = 2: Two lines per set

Assume: cache block size 8 bytes

t bits 0…01 100

Address of short int:

v tag 0 1 2 3 4 5 6 7 v tag 0 1 2 3 4 5 6 7

compare both

valid? + match: yes = hit

block offset

short int (2 Bytes) is here

No match:

• One line in set is selected for evic7on and replacement

• Replacement policies: random, least recently used (LRU), …

2-­‐Way Set Associa7ve Cache Simula7on

M=16 byte addresses, B=2 bytes/block,

S=2 sets, E=2 blocks/set

Address trace (reads, one byte per read):

0 [00 00

2

],

1 [00 01

2

],

7 [01 11

2

],

8 [10 00

2

],

0 [00 00

2

]

xx

t=2 s=1 b=

x x

v Tag Block

miss

1 00 M[0-­‐1]

hit

miss

1 01 M[6-­‐7]

miss

1 10 M[8-­‐9]

hit

Set 0

Set 1

What about writes?

 Mul7ple copies of data exist:

 L1, L2, Main Memory, Disk

 What to do on a write-­‐hit?

 Write-­‐through (write immediately to memory)

 Write-­‐back (defer write to memory un0l replacement of line)

 Need a dirty bit (line different from memory or not)

 What to do on a write-­‐miss?

 Write-­‐allocate (load into cache, update line in cache)

 Good if more writes to the loca0on follow

 No-­‐write-­‐allocate (writes immediately to memory)

 Typical

 Write-­‐through + No-­‐write-­‐allocate

 Write-­‐back + Write-­‐allocate

Intel Core i7 Cache Hierarchy

Regs

L

d-cache

L

i-cache

L2 unified cache

Core 0

Regs

L

d-cache

L

i-cache

L2 unified cache

Core 3

L3 unified cache

(shared by all cores)

Main memory

Processor package

L1 i-­‐cache and d-­‐cache:

32 KB, 8-­‐way,

Access: 4 cycles

L2 unified cache:

256 KB, 8-­‐way,

Access: 11 cycles

L3 unified cache:

8 MB, 16-­‐way,

Access: 30-­‐40 cycles

Block size: 64 bytes for

all caches.

Lets think about those numbers

 Huge difference between a hit and a miss

 Could be 100x, if just L1 and main memory

 Would you believe 99% hits is twice as good as 97%?

 Consider:

cache hit 0me of 1 cycle

miss penalty of 100 cycles

 Average access 0me:

97% hits: 1 cycle + 0.03 * 100 cycles = 4 cycles

99% hits: 1 cycle + 0.01 * 100 cycles = 2 cycles

 This is why “miss rate” is used instead of “hit rate”

Wri7ng Cache Friendly Code

 Make the common case go fast

 Focus on the inner loops of the core func0ons

 Minimize the misses in the inner loops

 Repeated references to variables are good (temporal locality)

 Stride-­‐1 reference pamerns are good (spa0al locality)

Key idea: Our qualita7ve no7on of locality is quan7fied

through our understanding of cache memories.