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 global; ... same variable accessed in each loop iteration for (...) { global++; Spatial locality: struct Point p; int a[10]; p.x = 5; for (i=0; i<10; ++i) p.y = 6; a[i] = i*2; fields of same struct adjacent elements accessed together accessed consecutively 4
What a cache looks like D ! F ! < % 5#"%' ! 1%& ! '%( '%( 5#"% B ! F ! < ' '%(' !"!#$ ! %&'$( ) ! * ! + ! * ! , !! -"." ! /0.$% $ (-/ ; = < E " = $-5#6 ! G#( E ! F ! < G G)(%' ! 6-(- ! G5*3H ! 1%& ! 3-3.% ! 5#"% ! 7(.% ! 6-(-: 4-) <;=< 4%8*&) ?&/-"#@-(#*" >< (figure from lecture slides) 5
Let’s backup and see how they came up with that ... 6
What data structure should we use for a cache? Important: caches must be fast Answer: a hash table! Main CPU Reg Cache Memory fast slow 7
What a cache looks like A cache is just a fixed-size hash table! key : address value : data at that address data blocks cache Size of a data block is data configurable data data hash(addr) ... data 8
What a cache looks like A cache is just a fixed-size hash table! key : address value : data at that address used to check for hash collisions data blocks cache tag data tag data tag data hash(addr) ... ... tag data 9
What a cache looks like A cache is just a fixed-size hash table! key : address value : data at that address used to check for hash collisions valid bit data blocks cache What hash function v tag data should we use? v tag data v tag data hash(addr) ... ... ... v tag data 10
What a cache looks like (direct mapped cache) address select byte(s) cache tag index offset v tag ... 0 1 2 3 B-1 v tag ... 0 1 2 3 B-1 v tag ... 0 1 2 3 B-1 - select row - check for matching tag ... ... ... v tag ... 0 1 2 3 B-1 What happens on a hash collision? B bytes per data block 11
Pathological Case (direct mapped cache) A simple program: What if: &a = 0x 000A 020 00 int a[64]; int b[64]; &b = 0x 000B 020 00 for (i=0; i<64; ++i) tag index offset b[i] = a[i]; There will be a cache miss on every access! &a[0] = 0x 000A 020 00 &b[0] = 0x 000B 020 00 Note the alternating tags &a[1] = 0x 000A 020 01 &b[1] = 0x 000B 020 01 Solution: associative sets 12
What a cache looks like (set associative cache) address select byte(s) cache tag index offset v tag ... 0 1 2 3 B-1 v tag ... 0 1 2 3 B-1 v tag ... 0 1 2 3 B-1 - select set v tag ... 0 1 2 3 B-1 - find matching tag ... ... ... v tag ... 0 1 2 3 B-1 B bytes per data block 13
What a cache looks like (set associative cache) )$' ! 0$%#)+-1 ! %$1 4 ! 5 ! 6 % 7#"%' ! 8%& ! '%( • 2&' ! 3 ! /+-& ! 4$/+56 ! )+% • !"#$%& ! 5$%$ ! '%$7%+-1 $% ! ",,'&% ;33&%'' ! *+ ! <*&3= ( ! :#(' ' ! :#(' : ! :#(' 9 ! 5 ! 6 ' '%(' (-/ '%( :7*1? #"3%> *++'%( 3-(- ! :%/#"' ! -( ! (.#' ! *++'%( $ (-/ @ A 6 B " A $-7#3 ! :#( B ! 5 ! 6 : :)(%' ! 3-(- ! :7*1? ! 8%& ! 1-1.% ! 7#"% ! C(.% ! 3-(-D (figure from lecture slides) 14
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 15
Lab 4: You measure B, E, and S × E × B )$' ! 0$%#)+-1 ! %$1 4 ! 5 ! 6 % 7#"%' ! 8%& ! '%( • 2&' ! 3 ! /+-& ! 4$/+56 ! )+% • !"#$%& ! 5$%$ ! '%$7%+-1 $% ! ",,'&% ;33&%'' ! *+ ! <*&3= ( ! :#(' ' ! :#(' : ! :#(' 9 ! 5 ! 6 ' '%(' (-/ '%( :7*1? #"3%> *++'%( 3-(- ! :%/#"' ! -( ! (.#' ! *++'%( $ (-/ @ A 6 B " A $-7#3 ! :#( B ! 5 ! 6 : :)(%' ! 3-(- ! :7*1? ! 8%& ! 1-1.% ! 7#"% ! C(.% ! 3-(-D (figure from lecture slides) 16
What does a cache-unfriendly program look like? Friendly: Unfriendly: int a[64][64]; int a[64][64]; for (i=0; i<64; ++i) for (i=0; i<64; ++i) for (k=0; k<64; ++k) for (k=0; k<64; ++k) a[i][k]++; a[k][i]++; 17
What does a cache-unfriendly program look like? Friendly: Unfriendly: struct Point { struct Points { int x; int x[64]; int y; int y[64]; }; }; struct Point a[64]; struct Points a; for (i=0; i<64; ++i) for (i=0; i<64; ++i) a[i].x += a[i].y; a.x[i] += a.y[i]; When might this be better? 18
What does a cache-unfriendly program look like? Which one is friendly depends on access patterns struct Stuff { struct Stuff { int x; int x; char str[64]; char *str; int y; int y; }; }; Good when str accessed Good when str accessed frequently rarely 19
Recommend
More recommend