6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully...

48
6 . 1 7 2 P e r f o r m a n c e E n g i n e e r i n g o f S o f t w a r e S y s t e m s L E C T U R E 1 4 C a c h i n g a n d C a c h e - E f f i c i e n t A l g o r i t h m s J u l i a n S h u n !"##$ %&'&( ! "#) +)$#) +, -./01 © 2008-2018 by the MIT 6.172 Lecturers 1

Transcript of 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully...

Page 1: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

6.172 Performance Engineering of Software Systems

LECTURE 14 Caching and Cache-Efficient Algorithms

Julian Shun

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

© 2008-2018 by the MIT 6.172 Lecturers 1

Page 2: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

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

© 2008-2018 by the MIT 6.172 Lecturers

CACHE HARDWARE

2

Page 3: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Multicore Cache Hierarchy

Level Size Assoc. Latency (ns)

Main 128GB 50LLC 30MB 20 6L2 256KB 8 4

L1-d 32KB 8 2L1-i 32KB 8 2

64 B cache blocks

DRAM

L1 data

L1 inst

L1 data

L1 inst

L1 data

L1 inst

L2 L2 L2

LLC (L3)

P P

!P

MemoryController

Net-work

DRAM DRAM

! ! !

© 2008-2018 by the MIT 6.172 Lecturers 3

Page 4: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Fully Associative Cache 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014

0x0040 0x0018

w-bit 0x001C 0x0024 0x0020 0x0014 tag address 0x0024 0x003C 0x0028 0x0030 space 0x002C

0x0030 0x0008 0x0034

0x0038 0x003C 0x0040 0x0044 0x0048

A cache block can reside anywhere in the cache.

Cache size M = 32. Line/block size B = 4.

To find a block in the cache, the entire cache must be searched for the tag. When the cache becomes full, a block must be evicted to make room for a new block. The replacement policy determines which block to evict.

© 2008-2018 by the MIT 6.172 Lecturers 4

Page 5: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Direct-Mapped Cache

w-bitaddressspace

0x00000x00040x00080x000C0x00100x00140x00180x001C0x00200x00240x00280x002C0x00300x00340x00380x003C0x00400x00440x0048

0x0008

0x0014

0x0024

0x0030

0x003C

0x0040

tag

A cache block’s set determines its location in the cache.

Cache size M = 32. Line/block size B = 4.

tag set offset w – lg M lg(M/B) lg Bbits

© 2008-2018 by the MIT 6.172 Lecturers

address To find a block in the cache, only a single location in the cache need be searched.

5

Page 6: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Set-Associative Cache

w-bitaddressspace

0x00000x00040x00080x000C0x00100x00140x00180x001C0x00200x00240x00280x002C0x00300x00340x00380x003C0x00400x00440x0048

address

bits tag set offset

w – lg(M/k) lg(M/kB) lg B

© 2008-2018 by the MIT 6.172 Lecturers

0x0008

0x00140x0024

0x0030

0x003C

0x0040

tag

Cache size M = 32. Line/block size B = 4. k=2-way associativity.

A cache block’s set determines k possible cache locations.

To find a block in the cache, only the k locations of its set must be searched.

6

Page 7: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Taxonomy of Cache Misses

Cold miss ∙ The first time the cache block is accessed.

Capacity miss ∙ The previous cached copy would have been evicted even

with a fully associative cache.Conflict miss ∙ Too many blocks from the same set in the cache. The block

would not have been evicted with a fully associative cache.Sharing miss ∙ Another processor acquired exclusive access to the cache

block.∙ True-sharing miss: The two processors are accessing the

same data on the cache line.∙ False-sharing miss: The two processors are accessing

different data that happen to reside on the same cache line.

© 2008-2018 by the MIT 6.172 Lecturers 7

Page 8: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Conflict Misses for Submatrices

4096 columns of doubles = 215 bytes

A32

Assume: ! Word width w = 64!! Cache size M = 32K!! Line (block) size B = 64!! k=4-way associativity.

4096rows

32

Conflict misses can be problematic for caches with limited associativity.

address

bits

tag set offset tag w – lg(M/k) lg(M/kB) lg B

51 7 6

Analysis Look at a column of submatrix A. The addresses of the elements are x, x+215, x+2·215, …, x+31·215. They all fall into the same set!

Solutions Copy A into a temporary 32!32matrix, or pad rows.

© 2008-2018 by the MIT 6.172 Lecturers 8

Page 9: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

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

© 2008-2018 by the MIT 6.172 Lecturers

IDEAL-CACHE MODEL

9

Page 10: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Ideal-Cache Model

Parameters ! Two-level hierarchy.! Cache size of M

bytes.! Cache-line length

of B bytes. ! Fully associative.! Optimal, omniscient

replacement.

P

cache

memory

BM/Bcache lines

© 2008-2018 by the MIT 6.172 Lecturers

Performance Measures !work W (ordinary running time)! cache misses Q

10

Page 11: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

How Reasonable Are Ideal Caches? “LRU” Lemma [ST85]. Suppose that an algorithm incurs Q cache misses on an ideal cache of size M. Then on a fully associative cache of size 2M that uses the least-recently used (LRU) replacement policy, it incurs at most 2Q cache misses. ∎ImplicationFor asymptotic analyses, one can assume optimal or LRU replacement, as convenient.

Software Engineering ∙Design a theoretically good algorithm.∙ Engineer for detailed performance.

• Real caches are not fully associative.• Loads and stores have different costs

with respect to bandwidth and latency.© 2008-2018 by the MIT 6.172 Lecturers 11

Page 12: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Cache-Miss Lemma Lemma. Suppose that a program reads a set of r data segments, where the ith segment consists of sibytes, and suppose that

Tం UK � 0 � ෬�� �N0 0�T ଯ �K��

Then all the segments fit into cache, and the number of misses to read them all is at most 3N/B. Proof. A single segment si incurs at most si/B + 2 misses, and hence we have ం UK� � � 0� �T

K�T � � 0� �T�B B B B B

si ମ 0� �0�� �0� � !© 2008-2018 by the MIT 6.172 Lecturers 12

Page 13: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Tall Caches

P

cache

memory

BM/Bcache lines

Tall-cache assumption B 2 < cM for some sufficiently small constant c " 1.

Example: Intel Xeon E5-2666 v3 ! Cache-line length = 64 bytes.! L1-cache size = 32 Kbytes.

© 2008-2018 by the MIT 6.172 Lecturers 13

Page 14: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

What’s Wrong with Short Caches?

Tall-cache assumption B 2 < cM for some sufficiently small constant c ! 1.

short cache

B

M/B

nn

An n"n submatrix stored in row-major order may not fit in a short cache even if n2 < cM !

© 2008-2018 by the MIT 6.172 Lecturers 14

Page 15: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Submatrix Caching Lemma

A

n

n

Lemma. Suppose that an n×n submatrix A is read into a tall cache satisfying B 2 < cM , where c ≤ 1 is constant, and suppose that cM ≤ n2 < M /3. Then Afits into cache, and the number of misses to read all A’s elements is at most 3n2/B.

Proof. We have N = n2, n = r = si, B ≤ n = N/r, and N< M /3. Thus, the Cache-Miss Lemma applies. ∎

© 2008-2018 by the MIT 6.172 Lecturers 15

Page 16: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

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

© 2008-2018 by the MIT 6.172 Lecturers

CACHE ANALYSIS OF MATRIX MULTIPLICATION

16

Page 17: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Multiply Square Matrices

void Mult(double *C, double *A, double *B, int64_t n) { for (int64_t i=0; i < n; i++) for (int64_t j=0; j < n; j++) for (int64_t k=0; k < n; k++) C[i*n+j] += A[i*n+k] * B[k*n+j];

}

Analysis of work W(n) = !(n3).

© 2008-2018 by the MIT 6.172 Lecturers 17

Page 18: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Analysis of Cache Misses

void Mult(double *C, double *A, double *B, int64_t n) { for (int64_t i=0; i < n; i++) for (int64_t j=0; j < n; j++) for (int64_t k=0; k < n; k++) C[i*n+j] += A[i*n+k] * B[k*n+j];

}

Assume row major and tall cache

A B © 2008-2018 by the MIT 6.172 Lecturers

Case 1 n > cM/B. Analyze matrix B. Assume LRU. Q(n) = !(n3), since matrix B misses on every access.

18

Page 19: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Analysis of Cache Misses

void Mult(double *C, double *A, double *B, int64_t n) { for (int64_t i=0; i < n; i++) for (int64_t j=0; j < n; j++) for (int64_t k=0; k < n; k++) C[i*n+j] += A[i*n+k] * B[k*n+j];

}

Assume row major and tall cache

A B

Case 2 c’M1/2< n < cM/B. Analyze matrix B. Assume LRU. Q(n) = n·!(n2/B) = !(n3/B), since matrix B can exploitspatial locality.

© 2008-2018 by the MIT 6.172 Lecturers 19

Page 20: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Analysis of Cache Misses

void Mult(double *C, double *A, double *B, int64_t n) { for (int64_t i=0; i < n; i++) for (int64_t j=0; j < n; j++) for (int64_t k=0; k < n; k++) C[i*n+j] += A[i*n+k] * B[k*n+j];

}

Assume row major and tall cache

A B © 2008-2018 by the MIT 6.172 Lecturers

Case 3 n !"#$M1/2. Analyze matrix B. Assume LRU. Q(n) = !(n2/B), since everything fits in cache!

20

Page 21: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Swapping Inner Loop Order

void Mult(double *C, double *A, double *B, int64_t n) { for (int64_t i=0; i < n; i++) for (int64_t k=0; k < n; k++) for (int64_t j=0; j < n; j++) C[i*n+j] += A[i*n+k] * B[k*n+j];

}

Assume row major and tall cache Analyze matrix B.Assume LRU.

Q(n) = n·!(n2/B) = !(n3/B), since matrix B can exploitspatial locality.

C B © 2008-2018 by the MIT 6.172 Lecturers 21

Page 22: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

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

© 2008-2018 by the MIT 6.172 Lecturers

TILING

22

Page 23: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Tiled Matrix Multiplication

s

void Tiled_Mult(double *C, double *A, double *B, int64_t n) { for (int64_t i1=0; i1<n/s; i1+=s) for (int64_t j1=0; j1<n/s; j1+=s) for (int64_t k1=0; k1<n/s; k1+=s) for (int64_t i=i1; i<i1+s && i<n; i++) for (int64_t j=j1; j<j1+s && j<n; j++) for (int64_t k=k1; k<k1+s && k<n; k++) C[i*n+j] += A[i*n+k] * B[k*n+j];

}

Analysis of work ! Work W(n) = "((n/s)3(s3))

= "(n3).

s

n

n © 2008-2018 by the MIT 6.172 Lecturers 23

Page 24: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Tiled Matrix Multiplication void Tiled_Mult(double *C, double *A, double *B, int64_t n) { for (int64_t i1=0; i1<n; i1+=s) for (int64_t j1=0; j1<n; j1+=s) for (int64_t k1=0; k1<n; k1+=s) for (int64_t i=i1; i<i1+s && i<n; i++) for (int64_t j=j1; j<j1+s && j<n; j++) for (int64_t k=k1; k<k1+s && k<n; k++) C[i*n+j] += A[i*n+k] * B[k*n+j];

}

s Analysis of cache misses s ! Tune s so that the submatrices

just fit into cache ! s = "(M1/2).! Submatrix Caching Lemma implies n "(s2/B) misses per submatrix.

! Q(n) = = "(n3/(BM1/2)). "((n/s)3(s2/B))

Remember n ! Optimal [HK81]. this!

© 2008-2018 by the MIT 6.172 Lecturers 24

Page 25: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

for (int64_t i1=0; i1<n; i1+=s) for (int64_t j1=0; j1<n; j1+=s) for (int64_t k1=0; k1<n; k1+=s) for (int64_t i=i1; i<i1+s && i<n; i++) for (int64_t j=j1; j<j1+s && j<n; j++) for (int64_t k=k1; k<k1+s && k<n; k++) C[i*n+j] += A[i*n+k] * B[k*n+j]; C[i*n+j] += A[i*n+k] * B[k*n+j]; C[i*n+j] += A[i*n+k] * B[k*n+j]; C[i*n+j] += A[i*n+k] * B[k*n+j];

Tiled Matrix Multiplication void Tiled_Mult(double *C, double *A, double *B, int64_t n) {

Voodoo! }

s Analysis of cache misses ! Tune s so that the submatrices s

just fit into cache ! s = "(M1/2). ! Submatrix Caching Lemma implies n "(s2/B) misses per submatrix. ! Q(n) =

= "(n3/(BM1/2)). "((n/s)3(s2/B))

Remember ! Optimal [HK81]. this! n © 2008-2018 by the MIT 6.172 Lecturers 25

Page 26: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Two-Level Cache n

t

s

n

s

∙ Two “voodoo” tuningparameters s and t.

∙Multidimensionaltuning optimizationcannot be done withbinary search.

t

© 2008-2018 by the MIT 6.172 Lecturers 26

Page 27: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

! Two “voodoo” tuningparameters s and t.

!Multidimensionaltuning optimization cannot be done with binary search.

Two-Level Cache n

s t

t s

n

void Tiled_Mult2(double *C, double *A, double *B, int64_t n) { for (int64_t i2=0; i2<n; i2+=s) for (int64_t j2=0; j2<n; j2+=s) for (int64_t k2=0; k2<n; k2+=s) for (int64_t i1=i2; i1<i2+s && i1<n; i1+=t) for (int64_t j1=j2; j1<j2+s && j1<n; j1+=t) for (int64_t k1=k2; k1<k2+s && k1<n; k1+=t) for (int64_t i=i1; i<i1+s && i<i2+t && i<n; i++) for (int64_t j=j1; j<j1+s && j<j2+t && j<n; j++) for (int64_t k=k1; k1<k1+s && k<k2+t && k<n; k++)

C[i*n+j] += A[i*n+k] * B[k*n+j]; }

© 2008-2018 by the MIT 6.172 Lecturers 27

Page 28: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Three-Level Cache n

∙Three “voodoo”tuning parameters.

∙Twelve nested for loops.∙Multiprogrammed environment:

Don’t know the effective cache sizewhen other jobs are running ⇒easy to mistune the parameters!

uut

n

s

t s

© 2008-2018 by the MIT 6.172 Lecturers 28

Page 29: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

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

© 2008-2018 by the MIT 6.172 Lecturers

DIVIDE & CONQUER

29

Page 30: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Recursive Matrix Multiplication Divide-and-conquer on n × n matrices.

C11 C12

C21 C22=

A11 A12

A21 A22×

B11 B12

B21 B22

= + A11B11 A11B12

A21B11 A21B12

A12B21 A12B22

A22B21 A22B22

8 multiply-adds of (n/2) × (n/2) matrices.

© 2008-2018 by the MIT 6.172 Lecturers 30

Page 31: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Recursive Code

// Assume that n is an exact power of 2. void Rec_Mult(double *C, double *A, double *B,

int64_t n, int64_t rowsize) { if (n == 1) C[0] += A[0] * B[0];

else { int64_t d11 = 0; int64_t d12 = n/2; int64_t d21 = (n/2) * rowsize; int64_t d22 = (n/2) * (rowsize+1);

Rec_Mult(C+d11, A+d11, B+d11, n/2, rowsize); Rec_Mult(C+d11, A+d12, B+d21, n/2, rowsize); Rec_Mult(C+d12, A+d11, B+d12, n/2, rowsize); Rec_Mult(C+d12, A+d12, B+d22, n/2, rowsize); Rec_Mult(C+d21, A+d21, B+d11, n/2, rowsize); Rec_Mult(C+d21, A+d22, B+d21, n/2, rowsize); Rec_Mult(C+d22, A+d21, B+d12, n/2, rowsize); Rec_Mult(C+d22, A+d22, B+d22, n/2, rowsize);

} }

Coarsen base case to overcome function-

call overheads.

© 2008-2018 by the MIT 6.172 Lecturers 31

Page 32: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Recursive Code

// Assume that n is an exact power of 2. void Rec_Mult(double *C, double *A, double *B,

int64_t n, int64_t rowsize) { if (n == 1) C[0] += A[0] * B[0];

else { int64_t d11 = 0; int64_t d12 = n/2; int64_t d21 = (n/2) * rowsize; int64_t d22 = (n/2) * (rowsize+1);

Rec_Mult(C+d11, A+d11, B+d11, n/2, rowsize); Rec_Mult(C+d11, A+d12, B+d21, n/2, rowsize); Rec_Mult(C+d12, A+d11, B+d12, n/2, rowsize); Rec_Mult(C+d12, A+d12, B+d22, n/2, rowsize); Rec_Mult(C+d21, A+d21, B+d11, n/2, rowsize); Rec_Mult(C+d21, A+d22, B+d21, n/2, rowsize); Rec_Mult(C+d22, A+d21, B+d12, n/2, rowsize); Rec_Mult(C+d22, A+d22, B+d22, n/2, rowsize);

} }

11 12

21 22

rowsize

n

© 2008-2018 by the MIT 6.172 Lecturers 32

Page 33: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Analysis of Work

W(n) = =

// Assume that n is an exact power of 2. void Rec_Mult(double *C, double *A, double *B,

int64_t n, int64_t rowsize) { if (n == 1) C[0] += A[0] * B[0];

else { int64_t d11 = 0; int64_t d12 = n/2; int64_t d21 = (n/2) * rowsize; int64_t d22 = (n/2) * (rowsize+1);

Rec_Mult(C+d11, A+d11, B+d11, n/2, rowsize); Rec_Mult(C+d11, A+d12, B+d21, n/2, rowsize); Rec_Mult(C+d12, A+d11, B+d12, n/2, rowsize); Rec_Mult(C+d12, A+d12, B+d22, n/2, rowsize); Rec_Mult(C+d21, A+d21, B+d11, n/2, rowsize); Rec_Mult(C+d21, A+d22, B+d21, n/2, rowsize); Rec_Mult(C+d22, A+d21, B+d12, n/2, rowsize); Rec_Mult(C+d22, A+d22, B+d22, n/2, rowsize);

} }

8W(n/2) + !(1) !(n3)

© 2008-2018 by the MIT 6.172 Lecturers 33

Page 34: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Analysis of Work

W(n) = 8W(n/2) + Θ(1)

recursion tree W(n)

© 2008-2018 by the MIT 6.172 Lecturers 34

Page 35: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

W(n)

Analysis of Work

W(n) = 8W(n/2) + Θ(1)

recursion tree 1 8

W(n/2) W(n/2) ⋯ W(n/2)

© 2008-2018 by the MIT 6.172 Lecturers 35

Page 36: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

W(n/2) W(n/2) W(n/2)

Analysis of Work

W(n) = 8W(n/2) + Θ(1)

recursion tree 1 8

1 1 ⋯8

W(n/4) W(n/4) ⋯ W(n/4)

© 2008-2018 by the MIT 6.172 Lecturers 36

1

Page 37: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Analysis of Work

W(n) = 8W(n/2) + #(1)

1

W(n/2) W(n/2) W(n/2)!8

1

W(n/4) W(n/4) W(n/4) !

1 1 8

"

1 1 1

#(1)

8

1

64

$

#(n3)

#leaves = 8lg n = nlg 8 = n3

lg n

W(n) = #(n3)

recursion tree

Note: Same work as looping versions.

#

Geom

etric

© 2008-2018 by the MIT 6.172 Lecturers 37

Page 38: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Analysis of Cache Misses

Submatrix Caching Lemma

// Assume that n is an exact power of 2. void Rec_Mult(double *C, double *A, double *B,

int64_t n, int64_t rowsize) { if (n == 1) C[0] += A[0] * B[0];

else { int64_t d11 = 0; int64_t d12 = n/2; int64_t d21 = (n/2) * rowsize; int64_t d22 = (n/2) * (rowsize+1);

Rec_Mult(C+d11, A+d11, B+d11, n/2, rowsize); Rec_Mult(C+d11, A+d12, B+d21, n/2, rowsize); Rec_Mult(C+d12, A+d11, B+d12, n/2, rowsize); Rec_Mult(C+d12, A+d12, B+d22, n/2, rowsize); Rec_Mult(C+d21, A+d21, B+d11, n/2, rowsize); Rec_Mult(C+d21, A+d22, B+d21, n/2, rowsize); Rec_Mult(C+d22, A+d21, B+d12, n/2, rowsize); Rec_Mult(C+d22, A+d22, B+d22, n/2, rowsize);

} }

Q(n) = !(n2/B) if n2<cM for suff. small const c"1,8Q(n/2) + !(1) otherwise.

© 2008-2018 by the MIT 6.172 Lecturers 38

Page 39: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Analysis of Cache Misses

Q(n) = !(n2/B) if n2<cM for suff. small const c"1,8Q(n/2) + !(1) otherwise.

recursion tree Q(n)

© 2008-2018 by the MIT 6.172 Lecturers 39

Page 40: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Analysis of Cache Misses

Q(n) = !(n2/B) if n2<cM for suff. small const c"1,8Q(n/2) + !(1) otherwise.

recursion tree 18

Q(n/2) Q(n/2) # Q(n/2)

© 2008-2018 by the MIT 6.172 Lecturers 40

Page 41: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Analysis of Cache Misses

Q(n) = !(n2/B) if n2<cM for suff. small const c"1,8Q(n/2) + !(1) otherwise.

recursion tree 18

1 1 # 18

Q(n/4) Q(n/4) # Q(n/4)

© 2008-2018 by the MIT 6.172 Lecturers 41

Page 42: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Analysis of Cache Misses

Q(n) = #(n2/B) if n2<cM for suff. small const c&1, 8Q(n/2) + #(1) otherwise.

1

8

64

"

Same cache misses as with tiling!

lg n-!lg(cM)

1

Q(n/2) Q(n/2) Q(n/2) !

8

1

Q(n/4) Q(n/4) Q(n/4) !

1 1 8

#(n3/BM1/2) #(cM/B)

$

1 1 1

#(

Geom

etric

Q(n) = #(n3/BM1/2)

recursion tree

#leaves = 8lg n – %lg(cM)

= #(n3/M3/2).

© 2008-2018 by the MIT 6.172 Lecturers 42

Page 43: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Efficient Cache-Oblivious Algorithms

• No voodoo tuning parameters.• No explicit knowledge of caches.• Passively autotune.• Handle multilevel caches automatically.• Good in multiprogrammed environments.

Matrix multiplication The best cache-oblivious codes to date work on arbitrary rectangular matrices and perform binary splitting (instead of 8-way) on the largest of i, j, and k.

© 2008-2018 by the MIT 6.172 Lecturers 43

Page 44: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Recursive Parallel Matrix Multiply // Assume that n is an exact power of 2. void Rec_Mult(double *C, double *A, double *B,

int64_t n, int64_t rowsize) { if (n == 1) C[0] += A[0] * B[0];

else { int64_t d11 = 0; int64_t d12 = n/2; int64_t d21 = (n/2) * rowsize; int64_t d22 = (n/2) * (rowsize+1);

cilk_spawn Rec_Mult(C+d11, A+d11, B+d11, n/2, rowsize); cilk_spawn Rec_Mult(C+d21, A+d22, B+d21, n/2, rowsize); cilk_spawn Rec_Mult(C+d12, A+d11, B+d12, n/2, rowsize); Rec_Mult(C+d22, A+d22, B+d22, n/2, rowsize); cilk_sync; cilk_spawn Rec_Mult(C+d11, A+d12, B+d21, n/2, rowsize); cilk_spawn Rec_Mult(C+d21, A+d21, B+d11, n/2, rowsize); cilk_spawn Rec_Mult(C+d12, A+d12, B+d22, n/2, rowsize); Rec_Mult(C+d22, A+d21, B+d12, n/2, rowsize); cilk_sync;

} }

© 2008-2018 by the MIT 6.172 Lecturers 44

Page 45: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Cilk and Caching Theorem. Let QP be the number of cache misses in a deterministic Cilk computation when run on Pprocessors, each with a private cache of size M, and let S

P be the number of successful steals during the

computation. In the ideal-cache model, we have QP

= Q1

+ O(SPM/B) ,

where M is the cache size and B is the size of a cache block.Proof. After a worker steals a continuation, its cache is completely cold in the worst case. But after M/B (cold) cache misses, its cache is identical to that in the serial execution. The same is true when a worker resumes a stolen subcomputation after a cilk_sync. The number of times these two situations can occur is at most 2S

P. ∎ S

P = O(PT∞) in expectation

MORAL: Minimizing cache misses in the serial elision essentially minimizes them in parallel executions.

© 2008-2018 by the MIT 6.172 Lecturers 45

Page 46: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Recursive Parallel Matrix Multiply // Assume that n is an exact power of 2. void Rec_Mult(double *C, double *A, double *B,

int64_t n, int64_t rowsize) { if (n == 1) C[0] += A[0] * B[0];

else { int64_t d11 = 0; int64_t d12 = n/2; int64_t d21 = (n/2) * rowsize; int64_t d22 = (n/2) * (rowsize+1);

cilk_spawn Rec_Mult(C+d11, A+d11, B+d11, n/2, rowsize); cilk_spawn Rec_Mult(C+d21, A+d22, B+d21, n/2, rowsize); cilk_spawn Rec_Mult(C+d12, A+d11, B+d12, n/2, rowsize); Rec_Mult(C+d22, A+d22, B+d22, n/2, rowsize); cilk_sync; cilk_spawn Rec_Mult(C+d11, A+d12, B+d21, n/2, rowsize); cilk_spawn Rec_Mult(C+d21, A+d21, B+d11, n/2, rowsize); cilk_spawn Rec_Mult(C+d12, A+d12, B+d22, n/2, rowsize); Rec_Mult(C+d22, A+d21, B+d12, n/2, rowsize); cilk_sync;

} }

Span: T!(n) = 2!!(n/2) + "#$%&)&"#*%&

Cache misses: Qp = '1 + O(SPM(B)= "(n3/BM1/2) + O(PnM(B)

© 2008-2018 by the MIT 6.172 Lecturers 46

Page 47: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

Summary

● Associativity in caches● Ideal cache model● Cache-aware algorithms● Tiled matrix multiplication● Cache-oblivious algorithms● Divide-and-conquer matrix multiplication● Cache efficiency analysis in Homework 8

© 2008-2018 by the MIT 6.172 Lecturers 47

Page 48: 6.172 - MIT OpenCourseWare › courses › electrical-engineering-and-computer... · Fully Associative Cache . 0x0000 0x0004 0x0008 0x000C 0x0010 0x0014. 0x0018. 0x0040. w-bit. 0x001C

MIT OpenCourseWare https://ocw.mit.edu

6.172 Performance Engineering of Software Systems Fall 2018

For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms.

48