Cache Memory and Performance

Slides:



Advertisements
Similar presentations
CS492B Analysis of Concurrent Programs Memory Hierarchy Jaehyuk Huh Computer Science, KAIST Part of slides are based on CS:App from CMU.
Advertisements

Example How are these parameters decided?. Row-Order storage main() { int i, j, a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12}; for (i=0; i
Faculty of Computer Science © 2006 CMPUT 229 Cache Performance Analysis Hitting for performance.
Carnegie Mellon 1 Cache Memories : Introduction to Computer Systems 10 th Lecture, Sep. 23, Instructors: Randy Bryant and Dave O’Hallaron.
Memory System Performance October 29, 1998 Topics Impact of cache parameters Impact of memory reference patterns –matrix multiply –transpose –memory mountain.
Cache Memories May 5, 2008 Topics Generic cache memory organization Direct mapped caches Set associative caches Impact of caches on performance EECS213.
Cache Memories February 24, 2004 Topics Generic cache memory organization Direct mapped caches Set associative caches Impact of caches on performance class13.ppt.
CS 3214 Computer Systems Godmar Back Lecture 11. Announcements Stay tuned for Exercise 5 Project 2 due Sep 30 Auto-fail rule 2: –Need at least Firecracker.
CPSC 312 Cache Memories Slides Source: Bryant Topics Generic cache memory organization Direct mapped caches Set associative caches Impact of caches on.
Fast matrix multiplication; Cache usage
Cache Memories Topics Generic cache memory organization Direct mapped caches Set associative caches Impact of caches on performance CS213.
Systems I Locality and Caching
Computer Science and Engineering Parallel and Distributed Processing CSE 8380 February 8, 2005 Session 8.
1 Cache Memories Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
Recitation 7: 10/21/02 Outline Program Optimization –Machine Independent –Machine Dependent Loop Unrolling Blocking Annie Luo
– 1 – , F’02 Caching in a Memory Hierarchy Larger, slower, cheaper storage device at level k+1 is partitioned into blocks.
Lecture 13: Caching EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr. Rozier.
Lecture 20: Locality and Caching CS 2011 Fall 2014, Dr. Rozier.
ECE 454 Computer Systems Programming Memory performance (Part II: Optimizing for caches) Ding Yuan ECE Dept., University of Toronto
Code and Caches 1 Computer Organization II © CS:APP & McQuain Cache Memory and Performance Many of the following slides are taken with permission.
1 Seoul National University Cache Memories. 2 Seoul National University Cache Memories Cache memory organization and operation Performance impact of caches.
Memory Hierarchy II. – 2 – Last class Caches Direct mapped E=1 (One cache line per set) Each main memory address can be placed in exactly one place in.
1 Cache Memory. 2 Outline Cache mountain Matrix multiplication Suggested Reading: 6.6, 6.7.
Cache Memories February 28, 2002 Topics Generic cache memory organization Direct mapped caches Set associative caches Impact of caches on performance Reading:
Systems I Cache Organization
Cache Memories Topics Generic cache-memory organization Direct-mapped caches Set-associative caches Impact of caches on performance CS 105 Tour of the.
1 Cache Memories. 2 Today Cache memory organization and operation Performance impact of caches  The memory mountain  Rearranging loops to improve spatial.
Lecture 5: Memory Performance. Types of Memory Registers L1 cache L2 cache L3 cache Main Memory Local Secondary Storage (local disks) Remote Secondary.
Cache Memories Topics Generic cache-memory organization Direct-mapped caches Set-associative caches Impact of caches on performance CS 105 Tour of the.
Cache Memories Topics Generic cache memory organization Direct mapped caches Set associative caches Impact of caches on performance cache.ppt CS 105 Tour.
Memory Hierarchy Computer Organization and Assembly Languages Yung-Yu Chuang 2007/01/08 with slides by CMU
Optimizing for the Memory Hierarchy Topics Impact of caches on performance Memory hierarchy considerations Systems I.
Cache Memories February 26, 2008 Topics Generic cache memory organization Direct mapped caches Set associative caches Impact of caches on performance The.
1 Writing Cache Friendly Code Make the common case go fast  Focus on the inner loops of the core functions Minimize the misses in the inner loops  Repeated.
Vassar College 1 Jason Waterman, CMPU 224: Computer Organization, Fall 2015 Cache Memories CMPU 224: Computer Organization Nov 19 th Fall 2015.
DEPENDENCE-DRIVEN LOOP MANIPULATION Based on notes by David Padua University of Illinois at Urbana-Champaign 1.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Cache Memories CENG331 - Computer Organization Instructors:
Programming for Cache Performance Topics Impact of caches on performance Blocking Loop reordering.
Carnegie Mellon 1 Cache Memories Authors: Adapted from slides by Randy Bryant and Dave O’Hallaron.
Cache Memories.
CSE 351 Section 9 3/1/12.
Cache Memories CSE 238/2038/2138: Systems Programming
The Hardware/Software Interface CSE351 Winter 2013
Section 7: Memory and Caches
CS 105 Tour of the Black Holes of Computing
CS 105 Tour of the Black Holes of Computing
The Memory Hierarchy : Memory Hierarchy - Cache
CS 213: Data Structures and Algorithms
Authors: Adapted from slides by Randy Bryant and Dave O’Hallaron
Cache Memories September 30, 2008
Memory Hierarchies.
Cache Memories Topics Cache memory organization Direct mapped caches
“The course that gives CMU its Zip!”
Memory Hierarchy II.
CS 105 Tour of the Black Holes of Computing
November 14 6 classes to go! Read
Introduction to Computer Systems
Memory Hierarchy and Cache Memories CENG331 - Computer Organization
Cache Memories Professor Hugh C. Lauer CS-2011, Machine Organization and Assembly Language (Slides include copyright materials from Computer Systems:
Cache Performance October 3, 2007
Cache Memories Lecture, Oct. 30, 2018
Computer Organization and Assembly Languages Yung-Yu Chuang 2006/01/05
Matrix Addition, C = A + B Add corresponding elements of each matrix to form elements of result matrix. Given elements of A as ai,j and elements of B as.
Cache Memories.
Cache Memory and Performance
Cache Memory and Performance
ENERGY 211 / CME 211 Lecture 11 October 15, 2008.
Applied Discrete Mathematics Week 4: Functions
Writing Cache Friendly Code

Presentation transcript:

Cache Memory and Performance Many of the following slides are taken with permission from Complete Powerpoint Lecture Notes for Computer Systems: A Programmer's Perspective (CS:APP) Randal E. Bryant and David R. O'Hallaron http://csapp.cs.cmu.edu/public/lectures.html The book is used explicitly in CS 2505 and CS 3214 and as a reference in CS 2506.

Locality Example (1) Claim: Being able to look at code and get a qualitative sense of its locality is a key skill for a professional programmer. Question: Which of these functions has good locality? int sumarrayrows(int a[M][N]) { int i, j, sum = 0; for (i = 0; i < M; i++) for (j = 0; j < N; j++) sum += a[i][j]; return sum; } int sumarraycols(int a[M][N]) { int i, j, sum = 0; for (j = 0; j < N; j++) for (i = 0; i < M; i++) sum += a[i][j]; return sum; }

Layout of C Arrays in Memory C arrays allocated in contiguous memory locations with addresses ascending with the array index: 7FFF99702320 7FFF99702324 1 7FFF99702328 2 7FFF9970232C 3 7FFF99702330 4 ... 7FFF99702340 8 7FFF99702344 9 int32_t A[10] = {0, 1, 2, 3, 4, ..., 8, 9};

Two-dimensional Arrays in C In C, a two-dimensional array is an array of arrays: int32_t A[3][5] = { { 0, 1, 2, 3, 4}, {10, 11, 12, 13, 14}, {20, 21, 22, 23, 24} }; A[0] A[1] A[2] In fact, if we print the values as pointers, we see something like this: A: 0x7fff22e41d30 A[0]: 0x7fff22e41d30 0x14 A[1]: 0x7fff22e41d44 A[2]: 0x7fff22e41d58

Layout of C Arrays in Memory Two-dimensional C arrays allocated in row-major order - each row in contiguous memory locations: 7FFF22E41D30 7FFF22E41D34 1 7FFF22E41D38 2 7FFF22E41D3C 3 7FFF22E41D40 4 7FFF22E41D44 10 7FFF22E41D48 11 7FFF22E41D4C 12 7FFF22E41D50 13 7FFF22E41D54 14 7FFF22E41D58 20 7FFF22E41D5C 21 7FFF22E41D60 22 7FFF22E41D64 23 7FFF22E41D68 24 int32_t A[3][5] = { { 0, 1, 2, 3, 4}, {10, 11, 12, 13, 14}, {20, 21, 22, 23, 24} };

Layout of C Arrays in Memory int32_t A[3][5] = { { 0, 1, 2, 3, 4}, {10, 11, 12, 13, 14}, {20, 21, 22, 23, 24}, }; 7FFF22E41D30 7FFF22E41D34 1 7FFF22E41D38 2 7FFF22E41D3C 3 7FFF22E41D40 4 7FFF22E41D44 10 7FFF22E41D48 11 7FFF22E41D4C 12 7FFF22E41D50 13 7FFF22E41D54 14 7FFF22E41D58 20 7FFF22E41D5C 21 7FFF22E41D60 22 7FFF22E41D64 23 7FFF22E41D68 24 i = 0 Stepping through columns in one row: for (i = 0; i < 3; i++) for (j = 0; j < 5; j++) sum += A[i][j]; - accesses successive elements in memory - if cache block size B > 4 bytes, exploit spatial locality compulsory miss rate = 4 bytes / B i = 1 i = 2

Layout of C Arrays in Memory int32_t A[3][5] = { { 0, 1, 2, 3, 4}, {10, 11, 12, 13, 14}, {20, 21, 22, 23, 24}, }; j = 0 7FFF22E41D30 7FFF22E41D34 1 7FFF22E41D38 2 7FFF22E41D3C 3 7FFF22E41D40 4 7FFF22E41D44 10 7FFF22E41D48 11 7FFF22E41D4C 12 7FFF22E41D50 13 7FFF22E41D54 14 7FFF22E41D58 20 7FFF22E41D5C 21 7FFF22E41D60 22 7FFF22E41D64 23 7FFF22E41D68 24 j = 1 Stepping through rows in one column: for (j = 0; i < 5; i++) for (i = 0; i < 3; i++) sum += a[i][j]; accesses distant elements no spatial locality! compulsory miss rate = 1 (i.e. 100%)

Stride and Array Accesses 7FFF22E41D30 7FFF22E41D34 1 7FFF22E41D38 2 7FFF22E41D3C 3 7FFF22E41D40 4 7FFF22E41D44 10 7FFF22E41D48 11 7FFF22E41D4C 12 7FFF22E41D50 13 7FFF22E41D54 14 7FFF22E41D58 20 7FFF22E41D5C 21 7FFF22E41D60 22 7FFF22E41D64 23 7FFF22E41D68 24 Stride 1 Stride 4

Writing Cache Friendly Code Repeated references to variables are good (temporal locality) Stride-1 reference patterns are good (spatial locality) Assume an initially-empty cache with 16-byte cache blocks. 1 2 3 4 5 6 7 i = 0, j = 0 to i = 0, j = 3 int sumarrayrows(int a[M][N]) { int row, col, sum = 0; for (row = 0; row < M; row++) for (col = 0; col < N; col++) sum += a[row][col]; return sum; } i = 0, j = 4 to i = 1, j = 2 Miss rate = 1/4 = 25%

Writing Cache Friendly Code Consider the previous slide, but assume that the cache uses a block size of 64 bytes instead of 16 bytes.. 1 2 3 4 5 6 7 8 9 10 . . . 15 16 int sumarrayrows(int a[M][N]) { int row, col, sum = 0; for (row = 0; row < M; row++) for (col = 0; col < N; col++) sum += a[row][col]; return sum; } i = 0, j = 0 to i = 3, j = 1 Miss rate = 1/16 = 6.25%

Writing Cache Friendly Code "Skipping" accesses down the rows of a column do not provide good locality: int sumarraycols(int a[M][N]) { int row, col, sum = 0; for (col = 0; col < N; col++) for (row = 0; row < M; row++) sum += a[row][col]; return sum; } Miss rate = 100% (That's actually somewhat pessimistic... depending on cache geometry.)

Layout of C Arrays in Memory It's easy to write an array traversal and see the addresses at which the array elements are stored: int A[5] = {0, 1, 2, 3, 4}; for (i = 0; i < 5; i++) printf("%d: %p\n", i, &A[i]); We see there that for a 1D array, the index varies in a stride-1 pattern. i address ----------- 0: 28ABE0 1: 28ABE4 2: 28ABE8 3: 28ABEC 4: 28ABF0 stride-1 : addresses differ by the size of an array cell (4 bytes, here)

Layout of C Arrays in Memory int B[3][5] = { ... }; for (i = 0; i < 3; i++) for (j = 0; j < 5; j++) printf("%d %3d: %p\n", i, j, &B[i][j]); We see that for a 2D array, the second index varies in a stride-1 pattern. But the first index does not vary in a stride-1 pattern. j-i order: i j address ---------------- 0 0: 28CC9C 1 0: 28CCB0 2 0: 28CCC4 0 1: 28CCA0 1 1: 28CCB4 2 1: 28CCC8 0 2: 28CCA4 1 2: 28CCB8 i-j order: i j address ---------------- 0 0: 28ABA4 0 1: 28ABA8 0 2: 28ABAC 0 3: 28ABB0 0 4: 28ABB4 1 0: 28ABB8 1 1: 28ABBC 1 2: 28ABC0 stride-5 (0x14/4) stride-1

3D Arrays in C int32_t A[2][3][5] = { { { 0, 1, 2, 3, 4}, { { 0, 1, 2, 3, 4}, { 10, 11, 12, 13, 14}, { 20, 21, 22, 23, 24}}, {110, 111, 112, 113, 114}, {220, 221, 222, 223, 224}} };

Locality Example (2) Question: Can you permute the loops so that the function scans the 3D array a[][][] with a stride-1 reference pattern (and thus has good spatial locality)? int sumarray3d(int a[N][N][N]) { int row, col, page, sum = 0; for (row = 0; row < N; row++) for (col = 0; col < N; col++) for (page = 0; page < N; page++) sum += a[page][row][col]; return sum; }

Layout of C Arrays in Memory int C[2][3][5] = { ... }; for (i = 0; i < 2; i++) for (j = 0; j < 3; j++) for (k = 0; k < 5; k++) printf("%3d %3d %3d: %p\n", i, j, k, &C[i][j][k]); We see that for a 3D array, the third index varies in a stride-1 pattern: But… if we change the order of access, we no longer have a stride-1 pattern: i-j-k order: i j k address ------------------ 0 0 0: 28CC1C 0 0 1: 28CC20 0 0 2: 28CC24 0 0 3: 28CC28 0 0 4: 28CC2C 0 1 0: 28CC30 0 1 1: 28CC34 0 1 2: 28CC38 k-j-i order: i j k address ------------------ 0 0 0: 28CC24 1 0 0: 28CC60 0 1 0: 28CC38 1 1 0: 28CC74 0 2 0: 28CC4C 1 2 0: 28CC88 0 0 1: 28CC28 1 0 1: 28CC64 i-k-j order: i j k address ------------------ 0 0 0: 28CC24 0 1 0: 28CC38 0 2 0: 28CC4C 0 0 1: 28CC28 0 1 1: 28CC3C 0 2 1: 28CC50 0 0 2: 28CC2C 0 1 2: 28CC40 0x4 0x3C 0x14 0x4 0x28 0x14 0x4 0x3C 0x14

Locality Example (2) Question: Can you permute the loops so that the function scans the 3D array a[] with a stride-1 reference pattern (and thus has good spatial locality)? int sumarray3d(int a[N][N][N]) { int i, j, k, sum = 0; for (i = 0; i < N; i++) for (j = 0; j < N; j++) for (k = 0; k < N; k++) sum += a[k][i][j]; return sum; } This code does not yield good locality at all. The inner loop is varying the first index, worst case!

Locality Example (3) Question: Which of these two exhibits better spatial locality? // struct of arrays struct soa { float *x; float *y; float *z; float *r; }; compute_r(struct soa s) { for (i = 0; …) { s.r[i] = s.x[i] * s.x[i] + s.y[i] * s.y[i] + s.z[i] * s.z[i]; } // array of structs struct aos { float x; float y; float z; float r; }; compute_r(struct aos *s) { for (i = 0; …) { s[i].r = s[i].x * s[i].x + s[i].y * s[i].y + s[i].z * s[i].z; } For the following discussions assume a cache block size of 32 bytes, and that the cache is not capable of holding all the blocks of the relevant structure at once.

Locality Example (3) // struct of arrays struct soa { float *x; float *y; float *z; float *r; }; struct soa s; s.x = malloc(1000 * sizeof(float)); ... x y r z … … … … 16 bytes 4 bytes per cell, 1000 cells per array

Locality Example (3) … // array of structs struct aos { float x; float y; float z; float r; }; struct aos s[1000]; … x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z 16 bytes per cell, 1000 cells

Locality Example (3) Describe the locality exhibited by this algorithm: s.x[0] miss s.y[0] miss s.z[0] miss s.r[0] miss // struct of arrays compute_r(struct soa s) { for (int i = 0; i < 1000; i++) { s.r[i] = s.x[i] * s.x[i] + s.y[i] * s.y[i] + s.z[i] * s.z[i]; } s.x[1] hit s.y[1] hit s.z[1] hit s.r[1] hit 8 cells . . . s.x[7] hit s.y[7] hit s.z[7] hit s.r[7] hit x y r z … … s.x[8] miss! … … 32 bytes 4 bytes per cell, 1000 cells per array

Locality Example (3) Describe the locality exhibited by this algorithm: s.x[8] miss s.y[8] miss s.z[8] miss s.r[8] miss // struct of arrays compute_r(struct soa s) { for (int i = 0; i < 1000; i++) { s.r[i] = s.x[i] * s.x[i] + s.y[i] * s.y[i] + s.z[i] * s.z[i]; } 8 cells s.x[9] hit s.y[9] hit s.z[9] hit s.r[9] hit . . . 8 cells For the arrays: Misses = 4*1*125 Hits = 4*7*125 Hit rate = 87.5% x y r z … … … … 32 bytes 4 bytes per cell, 1000 cells per array

Locality Example (3) Describe the locality exhibited by this algorithm: s[0].x miss // array of structs compute_r(struct aos *s) { for (int i = 0; i < 1000; i++) { s[i].r = s[i].x * s[i].x + s[i].y * s[i].y + s[i].z * s[i].z; } s[0].y hit s[0].z hit s[0].r hit s[1].x hit s[1].y hit s[1].z hit s[1].r hit . . . … x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z Hit rate: 7/8 or 87.5%

Locality Example (4) Describe the locality exhibited by this algorithm: // struct of arrays sum_r(struct soa s) { sum = 0; for (int i = 0; i < 1000; i++) { sum += s.r[i]; } x y r z … … … …

Locality Example (4) Describe the locality exhibited by this algorithm: // array of structs sum_r(struct aos *s) { sum = 0; for (int i = 0; i < 1000; i++) { sum += s[i].r; } … x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z x y r z

Locality Example (5) QTP: How would this compare to the previous two? // array of pointers to structs struct aops { float x; float y; float z; float r; }; struct *aops apos[1000]; for (i = 0; i < 1000; i++) apos[i] = malloc(sizeof(struct aops));

Writing Cache Friendly Code Make the common case go fast Focus on the inner loops of the core functions Minimize the misses in the inner loops Repeated references to variables are good (temporal locality) Stride-1 reference patterns are good (spatial locality) Key idea: Our qualitative notion of locality is quantified through our understanding of cache memories.

Miss Rate Analysis for Matrix Multiply Assume: Line size = 32B (big enough for four 64-bit words) Matrix dimension (N) is very large Approximate 1/N as 0.0 Cache is not even big enough to hold multiple rows Analysis Method: Look at access pattern of inner loop A k i B k j C i j

Matrix Multiplication Example Description: Multiply N x N matrices O(N3) total operations N reads per source element N values summed per destination Variable sum held in register /* ijk */ 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; }

Matrix Multiplication (ijk) 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; } Inner loop: B (*,j) A (i,*) C (i,j) Row-wise Column- wise Fixed Misses per inner loop iteration: A B C 0.25 1.0 0.0

Matrix Multiplication (kij) 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]; } Inner loop: A (i,k) B (k,*) C (i,*) Fixed Row-wise Row-wise Misses per inner loop iteration: A B C 0.0 0.25 0.25

Matrix Multiplication (jki) Inner loop: /* jki */ 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; } A (*,k) C (*,j) B (k,j) Column- wise Fixed Column- wise Misses per inner loop iteration: A B C 1.0 0.0 1.0

Summary of Matrix Multiplication 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; } ijk (& jik): 2 loads, 0 stores misses/iter = 1.25 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]; } kij (& ikj): 2 loads, 1 store misses/iter = 0.5 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; } jki (& kji): 2 loads, 1 store misses/iter = 2.0

Core i7 Matrix Multiply Performance jki / kji ijk / jik kij / ikj

Concluding Observations 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 sizes, line sizes, associativities, etc. Can get most of the advantage with generic code Keep working set reasonably small (temporal locality) Use small strides (spatial locality)