caches iv - courses.cs.washington.edu · l19: caches iv cse351, spring 2019 cache miss analysis...

27
CSE351, Spring 2019 L19: Caches IV Caches IV CSE 351 Spring 2019 Caches IV CSE 351 Spring 2019 Instructor: Teaching Assistants: Ruth Anderson Gavin Cai Jack Eggleston John Feltrup Britt Henderson Richard Jiang Jack Skalitzky Sophie Tian Connie Wang Sam Wolfson Casey Xing Chin Yeoh http://xkcd.com/908/

Upload: others

Post on 03-Oct-2019

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

Caches IVCSE 351 Spring 2019Caches IVCSE 351 Spring 2019

Instructor: Teaching Assistants:Ruth Anderson Gavin Cai Jack Eggleston John Feltrup

Britt Henderson Richard Jiang Jack SkalitzkySophie Tian Connie Wang Sam WolfsonCasey Xing  Chin Yeoh

http://xkcd.com/908/

Page 2: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

Administrivia

Lab 3, due TONIGHT, Wednesday (5/15) Homework 4 , due Wed (5/22) (Structs, Caches) Lab 4, Coming soon! Cache parameter puzzles and code optimizations

2

Page 3: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

Write‐back, write‐allocate example

3

0xBEEFCache

Memory

G

0xCAFE

0xBEEF

0

F

G

dirty bit

tag (there is only one set in this tiny cache, so the tag is the entire block address!)

In this example we are sort of ignoring block offsets. Here a blockholds 2 bytes (16 bits, 4 hex digits). 

Normally a block would be much bigger and thus there would be multiple items per block.  While only one item in that block would be written at a time, the entire line would be brought into cache.

Contents of memory stored at address G

Page 4: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

Write‐back, write‐allocate example

4

0xBEEFCache

Memory

G

0xCAFE

0xBEEF

0

F

G

mov 0xFACE, F

dirty bit

Page 5: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

0xBEEFU 0

Write‐back, write‐allocate example

5

0xCAFECache

Memory

F

0xCAFE

0xBEEF

F

G

dirty bit0xCAFE 0

Step 1: Bring F into cache

mov 0xFACE, F

Page 6: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

0xBEEFU 0

Write‐back, write‐allocate example

6

0xCAFECache

Memory

F

0xCAFE

0xBEEF

F

G

dirty bit0xFACE 1

Step 2: Write 0xFACEto cache only and setdirty bit

mov 0xFACE, F

Page 7: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

0xBEEFU 0

Write‐back, write‐allocate example

7

0xCAFECache

Memory

F

0xCAFE

0xBEEF

F

G

mov 0xFEED, F

dirty bit0xFACE 1

Write hit!Write 0xFEED to 

cache only

mov 0xFACE, F

Page 8: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

0xBEEFU 0

Write‐back, write‐allocate example

8

0xCAFECache

Memory

F

0xCAFE

0xBEEF

F

G

mov G, %rax

dirty bit0xFEED 1

mov 0xFEED, Fmov 0xFACE, F

Page 9: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

Write‐back, write‐allocate example

9

0xBEEFCache

Memory

G

0xFEED

0xBEEF

0

F

G

dirty bit

1. Write F back to memory since it is dirty

2. Bring G into the cache so we can copy it into %rax

mov G, %raxmov 0xFEED, Fmov 0xFACE, F

Page 10: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

Peer Instruction Question

Which of the following cache statements is FALSE? Vote at http://pollev.com/rea

A. We can reduce compulsory misses by decreasing our block size

B. We can reduce conflict misses by increasing associativity

C. A write‐back cache will save time for code with good temporal locality on writes

D. A write‐through cache will always match data with the memory hierarchy level below it

E. We’re lost…10

Page 11: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

Optimizations for the Memory Hierarchy

Write code that has locality! Spatial:  access data contiguously Temporal:  make sure access to the same data is not too far apart in time

How can you achieve locality? Adjust memory accesses in code (software) to improve miss rate (MR)• Requires knowledge of both how caches work as well as your system’s parameters

Proper choice of algorithm Loop transformations

11

Page 12: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

Example:  Matrix Multiplication

12

C

= ×

A B

ai* b*j

cij

Page 13: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

Matrices in Memory

How do cache blocks fit into this scheme? Row major matrix in memory:

13

Cache blocks

COLUMN of matrix (blue) is spread among cache blocks shown in red 

Page 14: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

Naïve Matrix Multiply

# move along rows of Afor (i = 0; i < n; i++)

# move along columns of Bfor (j = 0; j < n; j++)# EACH k loop reads row of A, col of B# Also read & write c(i,j) n timesfor (k = 0; k < n; k++)

c[i*n+j] += a[i*n+k] * b[k*n+j];

14

C(i,j) A(i,:)B(:,j)

C(i,j)

Page 15: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

Cache Miss Analysis (Naïve)

Scenario Parameters: Square matrix (𝑛 𝑛), elements are doubles Cache block size 𝐾 = 64 B = 8 doubles Cache size 𝐶 ≪ 𝑛 (much smaller than 𝑛)

Each iteration:

𝑛 misses

15

Ignoring matrix c

Page 16: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

Cache Miss Analysis (Naïve)

Scenario Parameters: Square matrix (𝑛 𝑛), elements are doubles Cache block size 𝐾 = 64 B = 8 doubles Cache size 𝐶 ≪ 𝑛 (much smaller than 𝑛)

Each iteration:

𝑛 misses

Afterwards in cache:(schematic)

168 doubles wide

Ignoring matrix c

Page 17: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

Cache Miss Analysis (Naïve)

Scenario Parameters: Square matrix (𝑛 𝑛), elements are doubles Cache block size 𝐾 = 64 B = 8 doubles Cache size 𝐶 ≪ 𝑛 (much smaller than 𝑛)

Each iteration:

𝑛 misses

Total misses:   2 3

17

Ignoring matrix c

once per element

Page 18: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

Linear Algebra to the Rescue (1)

Can get the same result of a matrix multiplication by splitting the matrices into smaller submatrices (matrix “blocks”)

For example, multiply two 4×4 matrices:

18

This is extra (non‐testable) 

material

Page 19: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

Linear Algebra to the Rescue (2)

19

Matrices of size  , split into 4 blocks of size  ( = )

C22 = A21B12 + A22B22 + A23B32 + A24B42  =  k A2k*Bk2

Multiplication operates on small “block” matrices Choose size so that they fit in the cache! This technique called “cache blocking”

C11 C12 C13 C14

C21 C22 C23 C24

C31 C32 C43 C34

C41 C42 C43 C44

A11 A12 A13 A14

A21 A22 A23 A24

A31 A32 A33 A34

A41 A42 A43 A144

B11 B12 B13 B14

B21 B22 B23 B24

B32 B32 B33 B34

B41 B42 B43 B44

This is extra (non‐testable) 

material

Page 20: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

Blocked Matrix Multiply

Blocked version of the naïve algorithm:

= block matrix size (assume 𝑟 divides 𝑛 evenly)

20

# move by rxr BLOCKS nowfor (i = 0; i < n; i += r)

for (j = 0; j < n; j += r)for (k = 0; k < n; k += r)

# block matrix multiplicationfor (ib = i; ib < i+r; ib++)

for (jb = j; jb < j+r; jb++)for (kb = k; kb < k+r; kb++)

c[ib*n+jb] += a[ib*n+kb]*b[kb*n+jb];

Page 21: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

Cache Miss Analysis (Blocked)

Scenario Parameters: Cache block size 𝐾 = 64 B = 8 doubles Cache size 𝐶 ≪ 𝑛 (much smaller than 𝑛) Three blocks      (𝑟 𝑟) fit into cache:  3𝑟2 𝐶

Each block iteration: 𝑟 /8misses per block 2𝑛/𝑟 𝑟2/8 𝑛𝑟/4

21

𝑛/𝑟 blocks𝑟2elements per block, 8 per cache block

𝑛/𝑟 blocks in row and column

Ignoring matrix c

Page 22: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

Cache Miss Analysis (Blocked)

Scenario Parameters: Cache block size 𝐾 = 64 B = 8 doubles Cache size 𝐶 ≪ 𝑛 (much smaller than 𝑛) Three blocks      (𝑟 𝑟) fit into cache:  3𝑟2 𝐶

Each block iteration: 𝑟 /8misses per block 2𝑛/𝑟 𝑟2/8 𝑛𝑟/4

Afterwards in cache(schematic)

22

𝑛/𝑟 blocks𝑟2elements per block, 8 per cache block

𝑛/𝑟 blocks in row and column

Ignoring matrix c

Page 23: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

Cache Miss Analysis (Blocked)

Scenario Parameters: Cache block size 𝐾 = 64 B = 8 doubles Cache size 𝐶 ≪ 𝑛 (much smaller than 𝑛) Three blocks      (𝑟 𝑟) fit into cache:  3𝑟2 𝐶

Each block iteration: 𝑟 /8misses per block 2𝑛/𝑟 𝑟2/8 𝑛𝑟/4

Total misses: 𝑛𝑟/4 𝑛/𝑟 2 𝑛3/ 4𝑟

23

𝑛/𝑟 blocks𝑟2elements per block, 8 per cache block

𝑛/𝑟 blocks in row and column

Ignoring matrix c

Page 24: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

Matrix Multiply Visualization

Here  = 100,  = 32 KiB,  = 30

24

Naïve:

Blocked:

≈ 1,020,000cache misses

≈ 90,000cache misses

Page 25: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

Cache‐Friendly Code

Programmer can optimize for cache performance How data structures are organized How data are accessed

• Nested loop structure• Blocking is a general technique

All systems favor “cache‐friendly code” Getting absolute optimum performance is very platform specific• Cache size, cache block size, associativity, etc.

Can get most of the advantage with generic code• Keep working set reasonably small (temporal locality)• Use small strides (spatial locality)• Focus on inner loop code

25

Page 26: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

The Memory Mountain

26

128m32m

8m2m

512k128k

32k0

2000

4000

6000

8000

10000

12000

14000

16000

s1s3

s5s7

s9s11

Size (bytes)

Read

 throug

hput (M

B/s)

Stride (x8 bytes)

Core i7 Haswell2.1 GHz32 KB L1 d‐cache256 KB L2 cache8 MB L3 cache64 B block size

Slopes of spatial locality

Ridges of temporal locality

L1

Mem

L2

L3

Aggressive prefetching

Page 27: Caches IV - courses.cs.washington.edu · L19: Caches IV CSE351, Spring 2019 Cache Miss Analysis (Blocked) Scenario Parameters: Cache block size 𝐾= 64 B = 8 doubles Cache size 𝐶≪𝑛(much

CSE351, Spring 2019L19: Caches IV

Learning About Your Machine

Linux: lscpu ls /sys/devices/system/cpu/cpu0/cache/index0/

• Ex:  cat /sys/devices/system/cpu/cpu0/cache/index*/size

Windows: wmic memcache get <query> (all values in KB) Ex:  wmic memcache get MaxCacheSize

Modern processor specs:  http://www.7‐cpu.com/

27