CSE351 Autumn 2019 Caches IV Summary

l19 caches iv n.w
1 / 30
Embed
Share

Explore cache management concepts in CSE351 Autumn 2019, including write-through vs. write-back policies, write hits, write misses, and examples illustrating write-back, write allocate strategies and their impact on cache performance.

  • Cache Management
  • CSE351
  • Write Policies
  • Memory Hierarchy
  • Cache Performance

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. L19: Caches IV CSE351, Autumn 2019 Caches IV CSE 351 Autumn 2019 Instructor: Justin Hsia Teaching Assistants: Andrew Hu Diya Joy Maurice Montag Suraj Jagadeesh Antonio Castelli Ivy Yu Melissa Birchfield Cosmo Wang Kaelin Laundry Millicent Li http://xkcd.com/908/

  2. L19: Caches IV CSE351, Autumn 2019 Administrivia Lab 3 due tonight Late days count as normal: Sunday is 1 day, Monday is 2 No lecture Monday Veteran s Day! Lab 4 released over the long weekend Cache parameter puzzles and code optimizations hw16 due Wednesday (11/13) 2

  3. L19: Caches IV CSE351, Autumn 2019 What about writes? Multiple copies of data may exist: multiple levels of cache and main memory What to do on a write-hit? Write-through: write immediately to next level Write-back: defer write to next level until line is evicted (replaced) Must track which cache lines have been modified ( dirty bit ) What to do on a write-miss? Write allocate: ( fetch on write ) load into cache, then execute the write-hit policy Good if more writes or reads to the location follow No-write allocate: ( write around ) just write immediately to next level Typical caches: Write-back + Write allocate, usually Write-through + No-write allocate, occasionally 3

  4. L19: Caches IV CSE351, Autumn 2019 Write-back, Write Allocate Example Note: While unrealistic, this example assumes that all requests have offset 0 and are for a block s worth of data. Valid Dirty Tag Block Contents 0xBEEF Cache: 1 0 G There is only one set in this tiny cache, so the tag is the entire block number! Block Num Memory: 0xCAFE F 0xBEEF G 4

  5. L19: Caches IV CSE351, Autumn 2019 Write-back, Write Allocate Example 1) mov 0xFACE, F Write Miss! Valid Dirty Tag Block Contents 0xBEEF Cache: 1 0 G Step 1: Bring F into cache Block Num Memory: 0xCAFE F 0xBEEF G 5

  6. L19: Caches IV CSE351, Autumn 2019 Write-back, Write Allocate Example 1) mov 0xFACE, F Write Miss Valid Dirty Tag Block Contents 0xCAFE Cache: 1 0 F Step 1: Bring F into cache Step 2: Write 0xFACE to cache only and set the dirty bit Block Num Memory: 0xCAFE F 0xBEEF G 6

  7. L19: Caches IV CSE351, Autumn 2019 Write-back, Write Allocate Example 1) mov 0xFACE, F Write Miss Valid Dirty Tag Block Contents 0xFACE Cache: 1 1 F Step 1: Bring F into cache Step 2: Write 0xFACE to cache only and set the dirty bit Block Num Memory: 0xCAFE F 0xBEEF G 7

  8. L19: Caches IV CSE351, Autumn 2019 Write-back, Write Allocate Example 1) mov 0xFACE, F Write Miss 2) mov 0xFEED, F Write Hit! Valid Dirty Tag Block Contents 0xFACE Cache: 1 1 F Step: Write 0xFEED to cache only (and set the dirty bit) Block Num Memory: 0xCAFE F 0xBEEF G 8

  9. L19: Caches IV CSE351, Autumn 2019 Write-back, Write Allocate Example 1) mov 0xFACE, F Write Miss 2) mov 0xFEED, F Write Hit Valid Dirty Tag Block Contents 0xFEED Cache: 1 1 F Block Num Memory: 0xCAFE F 0xBEEF G 9

  10. L19: Caches IV CSE351, Autumn 2019 Write-back, Write Allocate Example 1) mov 0xFACE, F Write Miss 2) mov 0xFEED, F Write Hit 3) mov G,%ax Read Miss! Valid Dirty Tag Block Contents 0xFEED Cache: 1 1 F Step 1: Write F back to memory since it is dirty Block Num Memory: 0xCAFE F 0xBEEF G 10

  11. L19: Caches IV CSE351, Autumn 2019 Write-back, Write Allocate Example 1) mov 0xFACE, F Write Miss 2) mov 0xFEED, F Write Hit 3) mov G,%ax Read Miss Valid Dirty Tag Block Contents 0xFEED 0xBEEF Cache: 1 1 0 F G Step 1: Write F back to memory since it is dirty Block Num Step 2: Bring G into the cache so that we can copy it into %ax Memory: 0xFEED F 0xBEEF G 11

  12. L19: Caches IV CSE351, Autumn 2019 Cache Simulator Want to play around with cache parameters and policies? Check out our cache simulator! https://courses.cs.washington.edu/courses/cse351/cachesim/ Way to use: Take advantage of explain mode and navigable history to test your own hypotheses and answer your own questions Self-guided Cache Sim Demo posted along with Section 7 Will be used in hw17 Lab 4 Preparation 12

  13. L19: Caches IV CSE351, Autumn 2019 Polling Question Which of the following cache statements is FALSE? Vote at http://PollEv.com/justinh 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

  14. L19: Caches IV CSE351, Autumn 2019 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 bothhow caches work as well as your system s parameters Proper choice of algorithm Loop transformations 14

  15. L19: Caches IV CSE351, Autumn 2019 Example: Matrix Multiplication C A B cij = ai* b*j 15

  16. L19: Caches IV CSE351, Autumn 2019 Matrices in Memory How do cache blocks fit into this scheme? Row major matrix in memory: Cache blocks COLUMN of matrix (blue) is spread among cache blocks shown in red 16

  17. L19: Caches IV CSE351, Autumn 2019 Na ve Matrix Multiply # move along rows of A for (i = 0; i < n; i++) # move along columns of B for (j = 0; j < n; j++) # EACH k loop reads row of A, col of B # Also read & write c(i,j) n times for (k = 0; k < n; k++) c[i*n+j] += a[i*n+k] * b[k*n+j]; C(i,j) C(i,j) A(i,:) = + B(:,j) 17

  18. L19: Caches IV CSE351, Autumn 2019 Ignoring matrix c 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: = ? 8+ ? =9? 8 misses 18

  19. L19: Caches IV CSE351, Autumn 2019 Ignoring matrix c 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: = ? 8+ ? =9? 8 misses Afterwards in cache: (schematic) = 8 doubles wide 19

  20. L19: Caches IV CSE351, Autumn 2019 Ignoring matrix c 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: = ? 8+ ? =9? 8 misses Total misses: 9? 8 ?2=9 8?3 once per product matrix element 20

  21. L19: Caches IV CSE351, Autumn 2019 This is extra (non-testable) material 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: 21

  22. L19: Caches IV CSE351, Autumn 2019 This is extra (non-testable) material Linear Algebra to the Rescue (2) C11 C12 C13 C14 A11 A12 A13 A14 B11 B12 B13 B14 C21 C22 C23 C24 A21 A22 A23 A24 B21 B22 B23 B24 C31 C32 C43 C34 A31 A32 A33 A34 B32 B32 B33 B34 C41 C42 C43 C44 A41 A42 A43 A144 B41 B42 B43 B44 Matrices of size ? ?, split into 4 blocks of size ? (?=4?) 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 22

  23. L19: Caches IV CSE351, Autumn 2019 Blocked Matrix Multiply Blocked version of the na ve algorithm: # move by rxr BLOCKS now for (i = 0; i < n; i += r) for (j = 0; j < n; j += r) for (k = 0; k < n; k += r) # block matrix multiplication for (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]; ? = block matrix size (assume ? divides ? evenly) 23

  24. L19: Caches IV CSE351, Autumn 2019 Ignoring matrix c 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< ? ?/? blocks ?2elements per block, 8 per cache block Each block iteration: ?2/8 misses per block 2?/? ?2/8 = ??/4 = ?/? blocks in row and column 24

  25. L19: Caches IV CSE351, Autumn 2019 Ignoring matrix c 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< ? ?/? blocks ?2elements per block, 8 per cache block Each block iteration: ?2/8 misses per block 2?/? ?2/8 = ??/4 = ?/? blocks in row and column Afterwards in cache (schematic) = 25

  26. L19: Caches IV CSE351, Autumn 2019 Ignoring matrix c 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< ? ?/? blocks ?2elements per block, 8 per cache block Each block iteration: ?2/8 misses per block 2?/? ?2/8 = ??/4 = ?/? blocks in row and column Total misses: ??/4 (?/?)2 = ?3/(4?) 26

  27. L19: Caches IV CSE351, Autumn 2019 Matrix Multiply Visualization Here ? = 100, ? = 32 KiB, ? = 30 Na ve: Blocked: 1,020,000 cache misses 90,000 cache misses 27

  28. L19: Caches IV CSE351, Autumn 2019 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 28

  29. L19: Caches IV CSE351, Autumn 2019 Core i7 Haswell 2.1 GHz 32 KB L1 d-cache 256 KB L2 cache 8 MB L3 cache 64 B block size The Memory Mountain Aggressive prefetching 16000 14000 L1 Read throughput (MB/s) 12000 10000 8000 Ridges of temporal locality L2 6000 4000 L3 2000 Slopes of spatial locality 0 32k s1 128k s3 Mem 512k s5 2m s7 8m s9 Stride (x8 bytes) Size (bytes) 32m s11 128m 29

  30. L19: Caches IV CSE351, Autumn 2019 Learning About Your Machine Linux: lscpu ls /sys/devices/system/cpu/cpu0/cache/index0/ Example: cat /sys/devices/system/cpu/cpu0/cache/index*/size Windows: wmic memcache get <query> (all values in KB) Example: wmic memcache get MaxCacheSize Modern processor specs: http://www.7-cpu.com/ 30

More Related Content