Carnegie Mellon
1 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
Cache Memories 15-213: Introduc;on to Computer Systems 12 - - PowerPoint PPT Presentation
Carnegie Mellon Cache Memories 15-213: Introduc;on to Computer Systems 12 th Lecture, Oct. 8, 2015 Instructors: Randal E. Bryant and David R. OHallaron
Carnegie Mellon
1 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
Carnegie Mellon
2 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Cache ¡memory ¡organiza7on ¡and ¡opera7on ¡ ¢ Performance ¡impact ¡of ¡caches ¡
Carnegie Mellon
3 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
Larger, slower, and cheaper (per byte) storage devices
Local disks hold files retrieved from disks
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
Smaller, faster, and costlier (per byte) storage devices
L3 cache holds cache lines retrieved from main memory.
Main memory holds disk blocks retrieved from local disks.
Carnegie Mellon
4 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
Larger, ¡slower, ¡cheaper ¡memory ¡ viewed ¡as ¡par77oned ¡into ¡“blocks” ¡ Data ¡is ¡copied ¡in ¡block-‑sized ¡ transfer ¡units ¡ Smaller, ¡faster, ¡more ¡expensive ¡ memory ¡caches ¡a ¡ ¡subset ¡of ¡ the ¡blocks ¡
Carnegie Mellon
5 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Cache ¡memories ¡are ¡small, ¡fast ¡SRAM-‑based ¡memories ¡
¢ CPU ¡looks ¡first ¡for ¡data ¡in ¡cache ¡ ¢ Typical ¡system ¡structure: ¡
Main memory I/O bridge Bus interface ALU Register file CPU chip System bus Memory bus Cache memory
Carnegie Mellon
6 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
0 ¡ 1 ¡ 2 ¡
B-‑1 ¡
tag ¡ v ¡
Carnegie Mellon
7 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
0 ¡ 1 ¡ 2 ¡
B-‑1 ¡ tag ¡
v ¡
t ¡bits ¡ s ¡bits ¡ b ¡bits ¡
data ¡begins ¡at ¡this ¡offset ¡
Carnegie Mellon
8 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
t ¡bits ¡ 0…01 ¡ 100 ¡
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 ¡
Carnegie Mellon
9 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
t ¡bits ¡ 0…01 ¡ 100 ¡
0 ¡ 1 ¡ 2 ¡ 7 ¡ tag ¡ v ¡ 3 ¡ 6 ¡ 5 ¡ 4 ¡
tag ¡
Carnegie Mellon
10 Bryant ¡and ¡O’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 v ¡ 3 ¡ 6 ¡ 5 ¡ 4 ¡ 0 ¡ 1 ¡ 2 ¡ 7 ¡ tag ¡ v ¡ 3 ¡ 6 ¡ 5 ¡ 4 ¡
tag ¡
Carnegie Mellon
14 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
t ¡bits ¡ 0…01 ¡ 100 ¡
0 ¡ 1 ¡ 2 ¡ 7 ¡ tag ¡ v ¡ 3 ¡ 6 ¡ 5 ¡ 4 ¡ 0 ¡ 1 ¡ 2 ¡ 7 ¡ tag ¡ v ¡ 3 ¡ 6 ¡ 5 ¡ 4 ¡
Carnegie Mellon
15 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
Carnegie Mellon
16 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Mul7ple ¡copies ¡of ¡data ¡exist: ¡
¢ What ¡to ¡do ¡on ¡a ¡write-‑hit? ¡
§ Need ¡a ¡dirty ¡bit ¡(line ¡different ¡from ¡memory ¡or ¡not) ¡
¢ What ¡to ¡do ¡on ¡a ¡write-‑miss? ¡
§ Good ¡if ¡more ¡writes ¡to ¡the ¡loca;on ¡follow ¡
¢ Typical ¡
Carnegie Mellon
17 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
Carnegie Mellon
18 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Miss ¡Rate ¡
§ 3-‑10% ¡for ¡L1 ¡ § can ¡be ¡quite ¡small ¡(e.g., ¡< ¡1%) ¡for ¡L2, ¡depending ¡on ¡size, ¡etc. ¡
¢ Hit ¡Time ¡
§ includes ¡;me ¡to ¡determine ¡whether ¡the ¡line ¡is ¡in ¡the ¡cache ¡
§ 4 ¡clock ¡cycle ¡for ¡L1 ¡ § 10 ¡clock ¡cycles ¡for ¡L2 ¡
¢ Miss ¡Penalty ¡
§ typically ¡50-‑200 ¡cycles ¡for ¡main ¡memory ¡(Trend: ¡increasing!) ¡
Carnegie Mellon
19 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Huge ¡difference ¡between ¡a ¡hit ¡and ¡a ¡miss ¡
¢ Would ¡you ¡believe ¡99% ¡hits ¡is ¡twice ¡as ¡good ¡as ¡97%? ¡
¡
¢ This ¡is ¡why ¡“miss ¡rate” ¡is ¡used ¡instead ¡of ¡“hit ¡rate” ¡
Carnegie Mellon
20 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Make ¡the ¡common ¡case ¡go ¡fast ¡
¢ Minimize ¡the ¡misses ¡in ¡the ¡inner ¡loops ¡
Carnegie Mellon
21 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Cache ¡organiza7on ¡and ¡opera7on ¡ ¢ Performance ¡impact ¡of ¡caches ¡
Carnegie Mellon
22 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Read ¡throughput ¡(read ¡bandwidth) ¡
¢ Memory ¡mountain: ¡Measured ¡read ¡throughput ¡as ¡a ¡
Carnegie Mellon
23 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
long data[MAXELEMS]; /* Global array to traverse */
* array “data” with stride of "stride", using
int test(int elems, int stride) { long i, sx2=stride*2, sx3=stride*3, sx4=stride*4; long acc0 = 0, acc1 = 0, acc2 = 0, acc3 = 0; long length = elems, limit = length - sx4;
for (i = 0; i < limit; i += sx4) { acc0 = acc0 + data[i]; acc1 = acc1 + data[i+stride]; acc2 = acc2 + data[i+sx2]; acc3 = acc3 + data[i+sx3]; }
for (; i < length; i++) { acc0 = acc0 + data[i]; } return ((acc0 + acc1) + (acc2 + acc3)); }
Carnegie Mellon
24 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
128m 32m 8m 2m 512k 128k 32k 2000 4000 6000 8000 10000 12000 14000 16000 s1 s3 s5 s7 s9 s11 Size (bytes) Read throughput (MB/s) Stride (x8 bytes)
Slopes
locality Ridges
locality
Aggressive prefetching
Carnegie Mellon
25 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Cache ¡organiza7on ¡and ¡opera7on ¡ ¢ Performance ¡impact ¡of ¡caches ¡
Carnegie Mellon
26 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Descrip7on: ¡
§ but ¡may ¡be ¡able ¡to ¡
Carnegie Mellon
27 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Assume: ¡
§ Approximate ¡1/N ¡as ¡0.0 ¡
¢ Analysis ¡Method: ¡
Carnegie Mellon
28 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ C ¡arrays ¡allocated ¡in ¡row-‑major ¡order ¡
¢ Stepping ¡through ¡columns ¡in ¡one ¡row: ¡
§ miss ¡rate ¡= ¡sizeof(aij) ¡/ ¡B ¡
¢ Stepping ¡through ¡rows ¡in ¡one ¡column: ¡
§ miss ¡rate ¡= ¡1 ¡(i.e. ¡100%) ¡
Carnegie Mellon
29 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
Carnegie Mellon
30 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
Carnegie Mellon
31 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
Carnegie Mellon
32 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
Carnegie Mellon
33 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
Carnegie Mellon
34 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
Carnegie Mellon
35 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
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
36 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
1 10 100 50 100 150 200 250 300 350 400 450 500 550 600 650 700 Cycles per inner loop iteration Array size (n) jki kji ijk jik kij ikj
Carnegie Mellon
37 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Cache ¡organiza7on ¡and ¡opera7on ¡ ¢ Performance ¡impact ¡of ¡caches ¡
Carnegie Mellon
38 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
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
39 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Assume: ¡ ¡
¢ First ¡itera7on: ¡
8 ¡wide ¡
Carnegie Mellon
40 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Assume: ¡ ¡
¢ Second ¡itera7on: ¡
¢ Total ¡misses: ¡
8 ¡wide ¡
Carnegie Mellon
41 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
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]; }
Carnegie Mellon
42 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Assume: ¡ ¡
¢ First ¡(block) ¡itera7on: ¡
Carnegie Mellon
43 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Assume: ¡ ¡
¢ Second ¡(block) ¡itera7on: ¡
¢ Total ¡misses: ¡
Carnegie Mellon
44 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ No ¡blocking: ¡(9/8) ¡* ¡n3 ¡ ¢ Blocking: ¡1/(4B) ¡* ¡n3 ¡ ¢ Suggest ¡largest ¡possible ¡block ¡size ¡B, ¡but ¡limit ¡3B2 ¡< ¡C! ¡ ¢ Reason ¡for ¡drama7c ¡difference: ¡
§ Input ¡data: ¡3n2, ¡computa;on ¡2n3 ¡ § Every ¡array ¡elements ¡used ¡O(n) ¡;mes! ¡
Carnegie Mellon
45 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡
¢ Cache ¡memories ¡can ¡have ¡significant ¡performance ¡impact ¡ ¢ You ¡can ¡write ¡your ¡programs ¡to ¡exploit ¡this! ¡