CSE 351: Week 7 Tom Bergan, TA 1 Today Cache geometries Lab 4 - - PowerPoint PPT Presentation

cse 351 week 7
SMART_READER_LITE
LIVE PREVIEW

CSE 351: Week 7 Tom Bergan, TA 1 Today Cache geometries Lab 4 - - PowerPoint PPT Presentation

CSE 351: Week 7 Tom Bergan, TA 1 Today Cache geometries Lab 4 2 Caches they make memory faster Main CPU Reg Cache Memory fast slow Tradeoff: caches are smaller than main memory 3 Why do caches work? Temporal locality: int


slide-1
SLIDE 1

CSE 351: Week 7

Tom Bergan, TA

1

slide-2
SLIDE 2

Today

  • Cache geometries
  • Lab 4

2

slide-3
SLIDE 3

Caches

they make memory faster

3

CPU Reg Cache Main Memory

fast slow

Tradeoff: caches are smaller than main memory

slide-4
SLIDE 4

Why do caches work?

4

Temporal locality:

int global; ... for (...) { global++;

same variable accessed in each loop iteration

Spatial locality:

struct Point p; p.x = 5; p.y = 6;

fields of same struct accessed together

int a[10]; for (i=0; i<10; ++i) a[i] = i*2;

adjacent elements accessed consecutively

slide-5
SLIDE 5

5

What a cache looks like

D!F!<% 5#"%'!1%&!'%( '%( B!F!<' '%(' 5#"%

; = < E"= (-/ $

$-5#6!G#( E!F!<G G)(%'!6-(-!G5*3H!1%&!3-3.%!5#"%!7(.%!6-(-:

!"!#$!%&'$( )!*!+!*!,!!-"."!/0.$%

>< 4-) <;=< 4%8*&) ?&/-"#@-(#*"

(figure from lecture slides)

slide-6
SLIDE 6

6

Let’s backup and see how they came up with that ...

slide-7
SLIDE 7

What data structure should we use for a cache?

7

CPU Reg Cache Main Memory

fast slow

Important: caches must be fast Answer: a hash table!

slide-8
SLIDE 8

8

A cache is just a fixed-size hash table! key: address value: data at that address

What a cache looks like

data

...

data data data

cache

Size of a data block is configurable

hash(addr)

data blocks

slide-9
SLIDE 9

9

A cache is just a fixed-size hash table! key: address value: data at that address

What a cache looks like

data

...

data data data

hash(addr)

cache

tag tag tag tag

...

used to check for hash collisions data blocks

slide-10
SLIDE 10

10

A cache is just a fixed-size hash table! key: address value: data at that address

What a cache looks like

data

...

data data data

hash(addr)

tag tag tag tag

...

v v v v

used to check for hash collisions valid bit

cache

data blocks

...

What hash function should we use?

slide-11
SLIDE 11

11

tag index offset

What a cache looks like

(direct mapped cache)

tag v tag v tag v tag v

... ... ...

address cache

1 2 3

...

B-1 1 2 3

...

B-1

B bytes per data block

What happens

  • n a hash collision?

select byte(s)

1 2 3

...

B-1 1 2 3

...

B-1

  • select row
  • check for matching tag
slide-12
SLIDE 12

12

Pathological Case

(direct mapped cache)

int a[64]; int b[64]; for (i=0; i<64; ++i) b[i] = a[i];

A simple program: What if:

000A 020 00

&a = 0x

000B 020 00

&b = 0x

tag index offset

There will be a cache miss on every access!

000A 020 00

&a[0] = 0x

000B 020 00

&b[0] = 0x

000A 020 01

&a[1] = 0x

000B 020 01

&b[1] = 0x

Note the alternating tags

Solution: associative sets

slide-13
SLIDE 13

13

tag index offset

What a cache looks like

(set associative cache)

... ... ...

address cache

tag v tag v tag v tag v

1 2 3

...

B-1 1 2 3

...

B-1

tag v

1 2 3

...

B-1

B bytes per data block select byte(s)

1 2 3

...

B-1 1 2 3

...

B-1

  • select set
  • find matching tag
slide-14
SLIDE 14

14

What a cache looks like

(set associative cache)

(figure from lecture slides)

4!5!6% 7#"%'!8%&!'%( )$'!0$%#)+-1!%$1

  • 2&'!3!/+-&!4$/+56!)+%
  • !"#$%&!5$%$!'%$7%+-1

$%!",,'&% 9!5!6' '%('

(!:#(' '!:#(' :!:#('

;33&%''!*+!<*&3= (-/ '%( #"3%> :7*1? *++'%(

@ A 6 B"A (-/ $

$-7#3!:#( B!5!6: :)(%'!3-(-!:7*1?!8%&!1-1.%!7#"%!C(.%!3-(-D

3-(-!:%/#"'!-(!(.#'!*++'%(

slide-15
SLIDE 15

15

Lab 4: You measure the geometry

  • We give you:
  • flush_cache()
  • access_cache(addr)
  • You measure:

B: bytes per block E: lines per set S×E×B: total size of the cache

slide-16
SLIDE 16

16

Lab 4: You measure B, E, and S×E×B

4!5!6% 7#"%'!8%&!'%( )$'!0$%#)+-1!%$1

  • 2&'!3!/+-&!4$/+56!)+%
  • !"#$%&!5$%$!'%$7%+-1

$%!",,'&% 9!5!6' '%('

(!:#(' '!:#(' :!:#('

;33&%''!*+!<*&3= (-/ '%( #"3%> :7*1? *++'%(

@ A 6 B"A (-/ $

$-7#3!:#( B!5!6: :)(%'!3-(-!:7*1?!8%&!1-1.%!7#"%!C(.%!3-(-D

3-(-!:%/#"'!-(!(.#'!*++'%(

(figure from lecture slides)

slide-17
SLIDE 17

17

What does a cache-unfriendly program look like?

int a[64][64]; for (i=0; i<64; ++i) for (k=0; k<64; ++k) a[i][k]++;

Friendly: Unfriendly:

int a[64][64]; for (i=0; i<64; ++i) for (k=0; k<64; ++k) a[k][i]++;

slide-18
SLIDE 18

18

What does a cache-unfriendly program look like?

struct Point { int x; int y; }; struct Point a[64]; for (i=0; i<64; ++i) a[i].x += a[i].y;

Friendly: Unfriendly:

struct Points { int x[64]; int y[64]; }; struct Points a; for (i=0; i<64; ++i) a.x[i] += a.y[i];

When might this be better?

slide-19
SLIDE 19

19

What does a cache-unfriendly program look like?

struct Stuff { int x; char str[64]; int y; };

Which one is friendly depends on access patterns

struct Stuff { int x; char *str; int y; };

Good when str accessed frequently Good when str accessed rarely