Carnegie Mellon
1
Cache Memories Thanks to Randal E. Bryant and David R. - - PowerPoint PPT Presentation
Carnegie Mellon Cache Memories Thanks to Randal E. Bryant and David R. OHallaron from CMU Reading Assignment: Computer Systems: A Programmers Perspec4ve,
Carnegie Mellon
1
Carnegie Mellon
2
¢ Cache ¡memory ¡organiza7on ¡and ¡opera7on ¡ ¢ Performance ¡impact ¡of ¡caches ¡
Carnegie Mellon
3
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
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
¢ 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
0 ¡ 1 ¡ 2 ¡
B-‑1 ¡
tag ¡ v ¡
Carnegie Mellon
7
0 ¡ 1 ¡ 2 ¡
B-‑1 ¡ tag ¡
v ¡
t ¡bits ¡ s ¡bits ¡ b ¡bits ¡
data ¡begins ¡at ¡this ¡offset ¡
Carnegie Mellon
8
t ¡bits ¡ 0…01 ¡ 100 ¡
0 ¡ 1 ¡ 2 ¡ 7 ¡ tag ¡ v ¡ 3 ¡ 6 ¡ 5 ¡ 4 ¡ 0 ¡ 1 ¡ 2 ¡ 7 ¡ tag ¡ v ¡ 3 ¡ 6 ¡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 3 ¡ 6 ¡ 5 ¡ 4 ¡ 0 ¡ 1 ¡ 2 ¡ 7 ¡ tag ¡ v ¡ 3 ¡ 6 ¡ 5 ¡ 4 ¡
tag ¡
Carnegie Mellon
14
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
Carnegie Mellon
16
¢ 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 ¡loca4on ¡follow ¡
¢ Typical ¡
Carnegie Mellon
17
Carnegie Mellon
18
¢ Miss ¡Rate ¡
§ 3-‑10% ¡for ¡L1 ¡ § can ¡be ¡quite ¡small ¡(e.g., ¡< ¡1%) ¡for ¡L2, ¡depending ¡on ¡size, ¡etc. ¡
¢ Hit ¡Time ¡
§ includes ¡4me ¡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
¢ 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
¢ 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
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
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
¢ Cache ¡organiza7on ¡and ¡opera7on ¡ ¢ Performance ¡impact ¡of ¡caches ¡
Carnegie Mellon
26
¢ Descrip7on: ¡
§ but ¡may ¡be ¡able ¡to ¡
Carnegie Mellon
27
¢ Assume: ¡
§ Approximate ¡1/N ¡as ¡0.0 ¡
¢ Analysis ¡Method: ¡
Carnegie Mellon
28
¢ 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
Carnegie Mellon
30
Carnegie Mellon
31
Carnegie Mellon
32
Carnegie Mellon
33
Carnegie Mellon
34
Carnegie Mellon
35
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
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
¢ Cache ¡organiza7on ¡and ¡opera7on ¡ ¢ Performance ¡impact ¡of ¡caches ¡
Carnegie Mellon
38
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
¢ Assume: ¡ ¡
¢ First ¡itera7on: ¡
8 ¡wide ¡
Carnegie Mellon
40
¢ Assume: ¡ ¡
¢ Second ¡itera7on: ¡
¢ Total ¡misses: ¡
8 ¡wide ¡
Carnegie Mellon
41
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
¢ Assume: ¡ ¡
¢ First ¡(block) ¡itera7on: ¡
Carnegie Mellon
43
¢ Assume: ¡ ¡
¢ Second ¡(block) ¡itera7on: ¡
¢ Total ¡misses: ¡
Carnegie Mellon
44
¢ 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, ¡computa4on ¡2n3 ¡ § Every ¡array ¡elements ¡used ¡O(n) ¡4mes! ¡
Carnegie Mellon
45
¢ Cache ¡memories ¡can ¡have ¡significant ¡performance ¡impact ¡ ¢ You ¡can ¡write ¡your ¡programs ¡to ¡exploit ¡this! ¡