Carnegie Mellon
1
Cache Memories 15-213: Introduc0on to Computer Systems 10 th - - PowerPoint PPT Presentation
Carnegie Mellon Cache Memories 15-213: Introduc0on to Computer Systems 10 th Lecture, Sep. 23, 2010. Instructors: Randy Bryant and Dave OHallaron 1 Carnegie
Carnegie Mellon
1
Carnegie Mellon
2
Cache ¡memory ¡organiza7on ¡and ¡opera7on ¡ Performance ¡impact ¡of ¡caches ¡
Carnegie Mellon
3
Cache ¡memories ¡are ¡small, ¡fast ¡SRAM-‑based ¡memories ¡
CPU ¡looks ¡first ¡for ¡data ¡in ¡caches ¡(e.g., ¡L1, ¡L2, ¡and ¡L3), ¡
Typical ¡system ¡structure: ¡
Main memory I/O bridge Bus interface ALU Register file CPU chip System bus Memory bus Cache memories
Carnegie Mellon
4
E ¡= ¡2e ¡lines ¡per ¡set ¡ S ¡= ¡2s ¡sets ¡ set ¡ line ¡
0 ¡ 1 ¡ 2 ¡
B-‑1 ¡
tag ¡ v ¡
B ¡= ¡2b ¡bytes ¡per ¡cache ¡block ¡(the ¡data) ¡
valid ¡bit ¡
Carnegie Mellon
5
E ¡= ¡2e ¡lines ¡per ¡set ¡ S ¡= ¡2s ¡sets ¡
0 ¡ 1 ¡ 2 ¡
B-‑1 ¡ tag ¡
v ¡
valid ¡bit ¡ B ¡= ¡2b ¡bytes ¡per ¡cache ¡block ¡(the ¡data) ¡
t ¡bits ¡ s ¡bits ¡ b ¡bits ¡
Address ¡of ¡word: ¡ tag ¡ set ¡ index ¡ block ¡
data ¡begins ¡at ¡this ¡offset ¡
has ¡matching ¡tag ¡
at ¡offset ¡
Carnegie Mellon
6
S ¡= ¡2s ¡sets ¡ Direct ¡mapped: ¡One ¡line ¡per ¡set ¡ Assume: ¡cache ¡block ¡size ¡8 ¡bytes ¡
t ¡bits ¡ 0…01 ¡ 100 ¡
Address ¡of ¡int: ¡
0 ¡ 1 ¡ 2 ¡ 7 ¡ tag ¡ v ¡ 3 ¡ 6 ¡ 5 ¡ 4 ¡ 0 ¡ 1 ¡ 2 ¡ 7 ¡ tag ¡ v ¡ 3 ¡ 6 ¡ 5 ¡ 4 ¡ 0 ¡ 1 ¡ 2 ¡ 7 ¡ tag ¡ v ¡ 3 ¡ 6 ¡ 5 ¡ 4 ¡ 0 ¡ 1 ¡ 2 ¡ 7 ¡
tag ¡
v ¡ 3 ¡ 6 ¡ 5 ¡ 4 ¡
find ¡set ¡
Carnegie Mellon
7
Direct ¡mapped: ¡One ¡line ¡per ¡set ¡ Assume: ¡cache ¡block ¡size ¡8 ¡bytes ¡
t ¡bits ¡ 0…01 ¡ 100 ¡
Address ¡of ¡int: ¡
0 ¡ 1 ¡ 2 ¡ 7 ¡ tag ¡ v ¡ 3 ¡ 6 ¡ 5 ¡ 4 ¡
match: ¡assume ¡yes ¡= ¡hit ¡ valid? ¡ ¡ ¡+ ¡ block ¡offset ¡
tag ¡
Carnegie Mellon
8
Direct ¡mapped: ¡One ¡line ¡per ¡set ¡ Assume: ¡cache ¡block ¡size ¡8 ¡bytes ¡
t ¡bits ¡ 0…01 ¡ 100 ¡
Address ¡of ¡int: ¡
0 ¡ 1 ¡ 2 ¡ 7 ¡ tag ¡ v ¡ 3 ¡ 6 ¡ 5 ¡ 4 ¡
match: ¡assume ¡yes ¡= ¡hit ¡ valid? ¡ ¡ ¡+ ¡ int ¡(4 ¡Bytes) ¡is ¡here ¡ block ¡offset ¡
Carnegie Mellon
9
Set ¡0 ¡ Set ¡1 ¡ Set ¡2 ¡ Set ¡3 ¡
Carnegie Mellon
10
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; }
Ignore ¡the ¡variables ¡sum, ¡i, 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¡per ¡set ¡ Assume: ¡cache ¡block ¡size ¡8 ¡bytes ¡
t ¡bits ¡ 0…01 ¡ 100 ¡
Address ¡of ¡short ¡int: ¡
0 ¡ 1 ¡ 2 ¡ 7 ¡ tag ¡ v ¡ 3 ¡ 6 ¡ 5 ¡ 4 ¡ 0 ¡ 1 ¡ 2 ¡ 7 ¡ tag ¡ v ¡ 3 ¡ 6 ¡ 5 ¡ 4 ¡
compare ¡both ¡ valid? ¡ ¡+ ¡ ¡ match: ¡yes ¡= ¡hit ¡ block ¡offset ¡ short ¡int ¡(2 ¡Bytes) ¡is ¡here ¡
Carnegie Mellon
14
Set ¡0 ¡ Set ¡1 ¡
Carnegie Mellon
15
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_rows(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; }
Ignore ¡the ¡variables ¡sum, ¡i, ¡j ¡
Carnegie Mellon
16
Mul7ple ¡copies ¡of ¡data ¡exist: ¡
What ¡to ¡do ¡on ¡a ¡write-‑hit? ¡
What ¡to ¡do ¡on ¡a ¡write-‑miss? ¡
Typical ¡
Carnegie Mellon
17
Regs L1 d-cache L1 i-cache L2 unified cache Core 0 Regs L1 d-cache L1 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. ¡ ¡
Carnegie Mellon
18
Miss ¡Rate ¡
Hit ¡Time ¡
Miss ¡Penalty ¡
Carnegie Mellon
19
Huge ¡difference ¡between ¡a ¡hit ¡and ¡a ¡miss ¡
Would ¡you ¡believe ¡99% ¡hits ¡is ¡twice ¡as ¡good ¡as ¡97%? ¡
cache ¡hit ¡0me ¡of ¡1 ¡cycle ¡ miss ¡penalty ¡of ¡100 ¡cycles ¡
¡ ¡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” ¡
Carnegie Mellon
20
Make ¡the ¡common ¡case ¡go ¡fast ¡
Minimize ¡the ¡misses ¡in ¡the ¡inner ¡loops ¡
Carnegie Mellon
21
Cache ¡organiza7on ¡and ¡opera7on ¡ Performance ¡impact ¡of ¡caches ¡
Carnegie Mellon
22
Read ¡throughput ¡(read ¡bandwidth) ¡
Memory ¡mountain: ¡Measured ¡read ¡throughput ¡as ¡a ¡
Carnegie Mellon
23
/* The test function */ void test(int elems, int stride) { int i, result = 0; volatile int sink; for (i = 0; i < elems; i += stride) result += data[i]; sink = result; /* So compiler doesn't optimize away the loop */ } /* Run test(elems, stride) and return read throughput (MB/s) */ double run(int size, int stride, double Mhz) { double cycles; int elems = size / sizeof(int); test(elems, stride); /* warm up the cache */ cycles = fcyc2(test, elems, stride, 0); /* call test(elems,stride) */ return (size / stride) / (cycles / Mhz); /* convert cycles to MB/s */ }
Carnegie Mellon
24
64M 8M 1M 128K 16K 2K 1000 2000 3000 4000 5000 6000 7000 s1 s3 s5 s7 s9 s11 s13 s15 s32 Working set size (bytes) Read throughput (MB/s) Stride (x8 bytes)
Intel ¡Core ¡i7 ¡ 32 ¡KB ¡L1 ¡ ¡i-‑cache ¡ 32 ¡KB ¡L1 ¡d-‑cache ¡ 256 ¡KB ¡unified ¡L2 ¡cache ¡ 8M ¡unified ¡L3 ¡cache ¡ All ¡caches ¡on-‑chip ¡
Carnegie Mellon
25
64M 8M 1M 128K 16K 2K 1000 2000 3000 4000 5000 6000 7000 s1 s3 s5 s7 s9 s11 s13 s15 s32 Working set size (bytes) Read throughput (MB/s) Stride (x8 bytes)
Intel ¡Core ¡i7 ¡ 32 ¡KB ¡L1 ¡ ¡i-‑cache ¡ 32 ¡KB ¡L1 ¡d-‑cache ¡ 256 ¡KB ¡unified ¡L2 ¡cache ¡ 8M ¡unified ¡L3 ¡cache ¡ All ¡caches ¡on-‑chip ¡ Slopes ¡of ¡ spa@al ¡ ¡ locality ¡
Carnegie Mellon
26
64M 8M 1M 128K 16K 2K 1000 2000 3000 4000 5000 6000 7000 s1 s3 s5 s7 s9 s11 s13 s15 s32 Working set size (bytes) Read throughput (MB/s) Stride (x8 bytes) L1 L2 Mem L3
Intel ¡Core ¡i7 ¡ 32 ¡KB ¡L1 ¡ ¡i-‑cache ¡ 32 ¡KB ¡L1 ¡d-‑cache ¡ 256 ¡KB ¡unified ¡L2 ¡cache ¡ 8M ¡unified ¡L3 ¡cache ¡ All ¡caches ¡on-‑chip ¡ Slopes ¡of ¡ spa@al ¡ ¡ locality ¡ Ridges ¡of ¡ ¡ ¡ Temporal ¡ ¡locality ¡
Carnegie Mellon
27
Cache ¡organiza7on ¡and ¡opera7on ¡ Performance ¡impact ¡of ¡caches ¡
Carnegie Mellon
28
Assume: ¡
Analysis ¡Method: ¡
k i
k j
i j
Carnegie Mellon
29
Descrip7on: ¡
/* ijk */ for (i=0; i<n; i++) { for (j=0; j<n; j++) { sum = 0.0; for (k=0; k<n; k++) sum += a[i][k] * b[k][j]; c[i][j] = sum; } } Variable sum held in register
Carnegie Mellon
30
C ¡arrays ¡allocated ¡in ¡row-‑major ¡order ¡
Stepping ¡through ¡columns ¡in ¡one ¡row: ¡
Stepping ¡through ¡rows ¡in ¡one ¡column: ¡
Carnegie Mellon
31
/* ijk */ for (i=0; i<n; i++) { for (j=0; j<n; j++) { sum = 0.0; for (k=0; k<n; k++) sum += a[i][k] * b[k][j]; c[i][j] = sum; } }
Carnegie Mellon
32
/* jik */ for (j=0; j<n; j++) { for (i=0; i<n; i++) { sum = 0.0; for (k=0; k<n; k++) sum += a[i][k] * b[k][j]; c[i][j] = sum } }
Carnegie Mellon
33
/* kij */ for (k=0; k<n; k++) { for (i=0; i<n; i++) { r = a[i][k]; for (j=0; j<n; j++) c[i][j] += r * b[k][j]; } }
Carnegie Mellon
34
/* ikj */ for (i=0; i<n; i++) { for (k=0; k<n; k++) { r = a[i][k]; for (j=0; j<n; j++) c[i][j] += r * b[k][j]; } }
Carnegie Mellon
35
/* jki */ for (j=0; j<n; j++) { for (k=0; k<n; k++) { r = b[k][j]; for (i=0; i<n; i++) c[i][j] += a[i][k] * r; } }
Carnegie Mellon
36
/* kji */ for (k=0; k<n; k++) { for (j=0; j<n; j++) { r = b[k][j]; for (i=0; i<n; i++) c[i][j] += a[i][k] * r; } }
Carnegie Mellon
37
for (i=0; i<n; i++) { for (j=0; j<n; j++) { sum = 0.0; for (k=0; k<n; k++) sum += a[i][k] * b[k][j]; c[i][j] = sum; } } for (k=0; k<n; k++) { for (i=0; i<n; i++) { r = a[i][k]; for (j=0; j<n; j++) c[i][j] += r * b[k][j]; } } for (j=0; j<n; j++) { for (k=0; k<n; k++) { r = b[k][j]; for (i=0; i<n; i++) c[i][j] += a[i][k] * r; } }
Carnegie Mellon
38
10 20 30 40 50 60 50 100 150 200 250 300 350 400 450 500 550 600 650 700 750
Cycles per inner loop iteration Array size (n) jki kji ijk jik kij ikj
Carnegie Mellon
39
Cache ¡organiza7on ¡and ¡opera7on ¡ Performance ¡impact ¡of ¡caches ¡
Carnegie Mellon
40
i ¡ j ¡
c = (double *) calloc(sizeof(double), n*n); /* Multiply n x n matrices a and b */ void mmm(double *a, double *b, double *c, int n) { int i, j, k; for (i = 0; i < n; i++) for (j = 0; j < n; j++) for (k = 0; k < n; k++) c[i*n+j] += a[i*n + k]*b[k*n + j]; }
Carnegie Mellon
41
Assume: ¡ ¡
First ¡itera7on: ¡
n ¡
8 ¡wide ¡
Carnegie Mellon
42
Assume: ¡ ¡
Second ¡itera7on: ¡
Total ¡misses: ¡
n ¡
8 ¡wide ¡
Carnegie Mellon
43
c = (double *) calloc(sizeof(double), n*n); /* Multiply n x n matrices a and b */ void mmm(double *a, double *b, double *c, int n) { int i, j, k; for (i = 0; i < n; i+=B) for (j = 0; j < n; j+=B) for (k = 0; k < n; k+=B) /* B x B mini matrix multiplications */ for (i1 = i; i1 < i+B; i++) for (j1 = j; j1 < j+B; j++) for (k1 = k; k1 < k+B; k++) c[i1*n+j1] += a[i1*n + k1]*b[k1*n + j1]; }
i1 ¡ j1 ¡
Block ¡size ¡B ¡x ¡B ¡
Carnegie Mellon
44
Assume: ¡ ¡
First ¡(block) ¡itera7on: ¡
Block ¡size ¡B ¡x ¡B ¡ n/B ¡blocks ¡
Carnegie Mellon
45
Assume: ¡ ¡
Second ¡(block) ¡itera7on: ¡
Total ¡misses: ¡
Block ¡size ¡B ¡x ¡B ¡ n/B ¡blocks ¡
Carnegie Mellon
46
No ¡blocking: ¡(9/8) ¡* ¡n3 ¡ Blocking: ¡1/(4B) ¡* ¡n3 ¡ Suggest ¡largest ¡possible ¡block ¡size ¡B, ¡but ¡limit ¡3B2 ¡< ¡C! ¡ Reason ¡for ¡drama7c ¡difference: ¡
Carnegie Mellon
47
Programmer ¡can ¡op7mize ¡for ¡cache ¡performance ¡
All ¡systems ¡favor ¡“cache ¡friendly ¡code” ¡