Cache Memories. EL2010 Organisasi dan Arsitektur Sistem Komputer Sekolah Teknik Elektro dan Informatika ITB 2010

Similar documents
CISC 360. Cache Memories Nov 25, 2008

Giving credit where credit is due

Cache Memories October 8, 2007

Systems I. Optimizing for the Memory Hierarchy. Topics Impact of caches on performance Memory hierarchy considerations

Cache Memories. Cache Memories Oct. 10, Inserting an L1 Cache Between the CPU and Main Memory. General Org of a Cache Memory

ν Hold frequently accessed blocks of main memory 2 CISC 360, Fa09 Cache is an array of sets. Each set contains one or more lines.

Cache memories The course that gives CMU its Zip! Cache Memories Oct 11, General organization of a cache memory

Cache Memories. Topics. Next time. Generic cache memory organization Direct mapped caches Set associative caches Impact of caches on performance

211: Computer Architecture Summer 2016

Last class. Caches. Direct mapped

Cache memories are small, fast SRAM based memories managed automatically in hardware.

Today Cache memory organization and operation Performance impact of caches

Memory Hierarchy. Announcement. Computer system model. Reference

The course that gives CMU its Zip! Memory System Performance. March 22, 2001

Example. How are these parameters decided?

Agenda Cache memory organization and operation Chapter 6 Performance impact of caches Cache Memories

Denison University. Cache Memories. CS-281: Introduction to Computer Systems. Instructor: Thomas C. Bressoud

Today. Cache Memories. General Cache Concept. General Cache Organization (S, E, B) Cache Memories. Example Memory Hierarchy Smaller, faster,

Computer Organization: A Programmer's Perspective

Carnegie Mellon. Cache Memories. Computer Architecture. Instructor: Norbert Lu1enberger. based on the book by Randy Bryant and Dave O Hallaron

Memory Hierarchy. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Cache Memories /18-213/15-513: Introduction to Computer Systems 12 th Lecture, October 5, Today s Instructor: Phil Gibbons

CS 33. Caches. CS33 Intro to Computer Systems XVIII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Carnegie Mellon. Cache Memories

Memory Hierarchy. Computer Systems Organization (Spring 2017) CSCI-UA 201, Section 3. Instructor: Joanna Klukowska

Memory Hierarchy. Cache Memory Organization and Access. General Cache Concept. Example Memory Hierarchy Smaller, faster,

High-Performance Parallel Computing

Cache Memories. Andrew Case. Slides adapted from Jinyang Li, Randy Bryant and Dave O Hallaron

Cache Memories. From Bryant and O Hallaron, Computer Systems. A Programmer s Perspective. Chapter 6.

Cache Memories : Introduc on to Computer Systems 12 th Lecture, October 6th, Instructor: Randy Bryant.

Cache memories are small, fast SRAM-based memories managed automatically in hardware. Hold frequently accessed blocks of main memory

Memory Hierarchy. Instructor: Adam C. Champion, Ph.D. CSE 2431: Introduction to Operating Systems Reading: Chap. 6, [CSAPP]

Lecture 12. Memory Design & Caches, part 2. Christos Kozyrakis Stanford University

CSCI-UA.0201 Computer Systems Organization Memory Hierarchy

CISC 360. Cache Memories Exercises Dec 3, 2009

Cache Memories. Lecture, Oct. 30, Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Systems Programming and Computer Architecture ( ) Timothy Roscoe

Chapter 6 Caches. Computer System. Alpha Chip Photo. Topics. Memory Hierarchy Locality of Reference SRAM Caches Direct Mapped Associative

Lecture 15: Caches and Optimization Computer Architecture and Systems Programming ( )

211: Computer Architecture Summer 2016

CSCI 402: Computer Architectures. Performance of Multilevel Cache

CS429: Computer Organization and Architecture

CS429: Computer Organization and Architecture

CS 240 Stage 3 Abstractions for Practical Systems

Cache Performance II 1

EE108B Lecture 13. Caches Wrap Up Processes, Interrupts, and Exceptions. Christos Kozyrakis Stanford University

Lecture 17: Memory Hierarchy and Cache Coherence Concurrent and Mul7core Programming

CS 261 Fall Caching. Mike Lam, Professor. (get it??)

Locality. CS429: Computer Organization and Architecture. Locality Example 2. Locality Example

Memory hierarchies: caches and their impact on the running time

Announcements. EE108B Lecture 13. Caches Wrap Up Processes, Interrupts, and Exceptions. Measuring Performance. Memory Performance

write-through v. write-back write-through v. write-back write-through v. write-back option 1: write-through write 10 to 0xABCD CPU RAM Cache ABCD: FF

CS3350B Computer Architecture

Problem: Processor Memory BoJleneck

Computer Organization - Overview

Memory Hierarchy & Caching. CS 351: Systems Programming Michael Saelee

Computer Systems. Memory Hierarchy. Han, Hwansoo

Caches Spring 2016 May 4: Caches 1

Problem: Processor- Memory Bo<leneck

Measurement tools and techniques

+ Random-Access Memory (RAM)

SE-292 High Performance Computing. Memory Hierarchy. R. Govindarajan Memory Hierarchy

Introduction to Computer Systems

Introduction to Computer Systems

Memories. CPE480/CS480/EE480, Spring Hank Dietz.

Caches Concepts Review

Computer Architecture and System Software Lecture 09: Memory Hierarchy. Instructor: Rob Bergen Applied Computer Science University of Winnipeg

Advanced Caching Techniques

How to Write Fast Numerical Code

L2 cache provides additional on-chip caching space. L2 cache captures misses from L1 cache. Summary

CS/ECE 250 Computer Architecture

CS356: Discussion #9 Memory Hierarchy and Caches. Marco Paolieri Illustrations from CS:APP3e textbook

Copyright 2012, Elsevier Inc. All rights reserved.

Donn Morrison Department of Computer Science. TDT4255 Memory hierarchies

211: Computer Architecture Summer 2016

Advanced Caching Techniques

Caches III. CSE 351 Spring Instructor: Ruth Anderson

How to Write Fast Numerical Code

CS 33. Architecture and Optimization (3) CS33 Intro to Computer Systems XVI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

CS377P Programming for Performance Single Thread Performance Caches I

CS241 Computer Organization Spring Principle of Locality

Advanced Memory Organizations

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2014 Lecture 14

F28HS Hardware-Software Interface: Systems Programming

Advanced Architecture

Cache Memory and Performance

Cache Impact on Program Performance. T. Yang. UCSB CS240A. 2017

Computer Systems CSE 410 Autumn Memory Organiza:on and Caches

Random-Access Memory (RAM) CISC 360. The Memory Hierarchy Nov 24, Conventional DRAM Organization. SRAM vs DRAM Summary.

A Crash Course in Compilers for Parallel Computing. Mary Hall Fall, L2: Transforms, Reuse, Locality

Kathryn Chan, Kevin Bi, Ryan Wong, Waylon Huang, Xinyu Sui

CS3350B Computer Architecture

Caches. Han Wang CS 3410, Spring 2012 Computer Science Cornell University. See P&H 5.1, 5.2 (except writes)

Lecture 2. Memory locality optimizations Address space organization

LECTURE 10: Improving Memory Access: Direct and Spatial caches

LECTURE 4: LARGE AND FAST: EXPLOITING MEMORY HIERARCHY

Cache Memory: Instruction Cache, HW/SW Interaction. Admin

14:332:331. Week 13 Basics of Cache

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 15

Transcription:

Cache Memories EL21 Organisasi dan Arsitektur Sistem Komputer Sekolah Teknik Elektro dan Informatika ITB 21

Topics Generic cache memory organization Direct mapped caches Set associative caches Impact of caches on performance

Cache Memories Cache memories are small, fast SRAM-based memories managed automatically in hardware. Hold frequently accessed blocks of main memory CPU looks first for data in L1, then in L2, then in main memory. Typical bus structure: CPU chip register file cache bus L1 cache ALU system bus memory bus L2 cache bus interface I/O bridge main memory

Inserting an L1 Cache Between the CPU and Main Memory The transfer unit between the CPU register file and the cache is a 4-byte block. The transfer unit between the cache and main memory is a 4-word block (16 bytes). line line 1 block 1 block 21 block 3 a b c d... p q r s... w x y z... The tiny, very fast CPU register file has room for four 4-byte words. The small fast L1 cache has room for two 4-word blocks. The big slow main memory has room for many 4-word blocks.

General Org of a Cache Memory Cache is an array of sets. 1 valid bit per line t bits per line B = 2 b bytes per cache block Each set contains one or more lines. set : valid valid 1 1 B 1 B 1 E lines per set Each line holds a block of data. valid 1 B 1 S = 2 s sets set 1: valid 1 B 1 valid 1 B 1 set S-1: valid 1 B 1 Cache size: C = B x E x S data bytes

Addressing Caches Address A: t bits s bits b bits set : v v 1 1 B 1 B 1 m-1 <> <set index> <block offset> set 1: set S-1: v v v v 1 1 1 1 B 1 B 1 B 1 B 1 The word at address A is in the cache if the bits in one of the <valid> lines in set <set index> match <>. The word contents begin at offset <block offset> bytes from the beginning of the block.

Direct-Mapped Cache Simplest kind of cache Characterized by exactly one line per set. set : valid cache block E=1 lines per set set 1: valid cache block set S-1: valid cache block

Accessing Direct-Mapped Caches Set selection Use the set index bits to determine the set of interest. set : valid cache block selected set set 1: valid cache block m-1 t bits s bits 1 b bits set index block offset set S-1: valid cache block

Accessing Direct-Mapped Caches Line matching and word selection Line matching: Find a valid line in the selected set with a matching Word selection: Then extract the word =1? (1) The valid bit must be set selected set (i): 1 1 2 3 4 5 6 7 11 w w 1 w 2 w 3 (2) The bits in the cache line must match the bits in the address m-1 =? t bits 11 s bits b bits i 1 set index block offset (3) If (1) and (2), then cache hit, and block offset selects starting byte.

Direct-Mapped Cache Simulation t=1 s=2 b=1 x xx x M=16 byte addresses, B=2 bytes/block, S=4 sets, E=1 entry/set Address trace (reads): [ 2 ], 1 [1 2 ], 13 [111 2 ], 8 [1 2 ], [ 2 ] [ 2 ] (miss) v data 1 m[1] M[-1] m[] 13 [111 2 ] (miss) v data 1 m[1] M[-1] m[] (1) (3) 1 1 m[13] M[12-13] m[12] 8 [1 2 ] (miss) v data 1 1 m[9] M[8-9] m[8] [ 2 ] (miss) v data 1 m[1] M[-1] m[] (4) 1 1 M[12-13] (5) 1 1 m[13] M[12-13] m[12]

Why Use Middle Bits as Index? High-Order Bit Indexing Adjacent memory lines would map to same cache entry Poor use of spatial locality Middle-Order Bit Indexing 1 1 11 4-line Cache Consecutive memory lines map to different cache lines Can hold C-byte region of address space in cache at one time 1 1 11 1 11 11 111 1 11 11 111 11 111 111 1111 High-Order Bit Indexing 1 1 11 1 11 11 111 1 11 11 111 11 111 111 1111 Middle-Order Bit Indexing

Cache Example Problem 32 bit address 4K cache sets 1 word per line (32 bit data) Byte addressable Memory address structure? Cache line structure? Cache size?

Cache Example Memory address 4K sets requires 12 bits (496 = 2 s ) s = 12 1 word 32 bit = 4 bytes (4 = 2 b ) b = 2 Tag = 32 12 2 = 18 Cache line structure 1 valid bit 18 bit 1 word : 32 bit Total 51 bit Cache size V (1) Tag (18) Tag (18) C = S x Line = 496 x 51 bit = 28896 bit Set (12) Block (2) Word (32) (8) (8) (8) (8)

Cache access problem Cache: 8 bit address and 1 word block Direct Mapped : 16 sets each line contains one word Show the final content of the cache given 2,3,11,16,21,13,64,48,19,11,3,22,4,27,6,11 memory access

2 1 miss 3 11 hit 11 1 11 miss 16 1 miss 21 11 1 miss 13 11 1 miss 64 1 miss 48 11 miss 19 1 11 hit 11 1 11 hit 3 11 miss 22 11 1 hit 4 1 miss 27 11 11 miss 6 1 1 hit 11 1 11 hit Set V Tag B B1 B2 B3 1 M[] M[1] M[2] M[3] 1 1 M[4] M[5] M[6] M[7] 2 1 M[8] M[9] M[1] M[11] 3 1 M[12] M[13] M[14] M[15] 4 1 M[16] M[17] M[18] M[19] 5 1 M[2] M[21] M[22] M[23] 6 1 M[24] M[25] M[26] M[27] 7 8 9 1 11 12 1 M[48] M[49] M[5] M[51] 13 14 15

Cache access problem Cache: 8 bit address and 1 word block Direct Mapped : 16 sets each line contains one word s = 4, b = 2, and = 2 Access 2 [ 1]: Set : miss 3 [ 11]: Set : hit 11 [ 1 11]: Set 2 : miss 16 [ 1 ]: set 4 : miss 21 [ 11 1]: Set 5 : miss 13 [ 11 1]: Set 3 : miss 64 [1 ]: Set : miss 48 [ 11 ]: Set 12 : miss 19 [ 1 11]: Set 4 : hit 11 [ 1 11]: Set 2 : hit 3 [ 11]: Set : miss 22 [ 11 1]: Set 5 : hit 4 [ 1 ]: Set 1 : miss 27 [ 11 11]: Set 6 : miss 6 [ 1 1]: Set 1 : hit 11 [ 1 11]: Set 2 : hit Set Valid Tag Block 1 M[-3] 1 2 1 M[8-11] 3 4 5 6 7 8 9 A B C D E F

Set Associative Caches Characterized by more than one line per set set : valid cache block valid cache block E=2 lines per set set 1: set S-1: valid cache block valid cache block valid cache block valid cache block

Accessing Set Associative Caches Set selection identical to direct-mapped cache set : valid valid cache block cache block Selected set set 1: valid valid cache block cache block m-1 t bits s bits 1 b bits set index block offset set S-1: valid valid cache block cache block

Accessing Set Associative Caches Line matching and word selection must compare the in each valid line in the selected set. =1? (1) The valid bit must be set. 1 2 3 4 5 6 7 selected set (i): 1 11 1 11 w w 1 w 2 w 3 (2) The bits in one of the cache lines must match the bits in the address m-1 =? t bits 11 s bits b bits i 1 set index block offset (3) If (1) and (2), then cache hit, and block offset selects starting byte.

Cache Example Problem 32 bit address 2K cache sets (2 way set associative) 1 word per line (32 bit data) Byte addressable Memory address structure? Cache line structure? Cache size?

Cache access problem Cache: 8 bit address and 1 word block Two-way Set Associative : 8 sets each line contains one word Show the final content of the cache given 2,3,11,16,21,13,64,48,19,11,3,22,4,27,6,11 memory access

2 1 miss 3 11 hit 11 1 11 miss 16 1 miss 21 11 1 miss 13 11 1 miss 64 1 miss 48 1 1 miss 19 1 11 hit 11 1 11 hit 3 11 hit 22 11 1 hit 4 1 miss 27 11 11 miss 6 1 1 hit 11 1 11 hit Set V Tag B B1 B2 B3 1 M[] M[1] M[2] M[3] 1 1 M[64] M[65] M[66] M[67] 1 M[4] M[5] M[6] M[7] 2 1 M[8] M[9] M[1] M[11] 3 1 M[12] M[13] M[14] M[15] 4 1 M[16] M[17] M[18] M[19] 1 1 M[48] M[49] M[5] M[51] 5 1 M[2] M[21] M[22] M[23] 6 M[24] M[25] M[26] M[27] 7

Fully Set Associative Caches Characterized by only one set valid cache block set : valid cache block valid cache block valid cache block valid cache block valid cache block E= n lines per set

Accessing Fully Set Associative Caches No Set selection set : valid valid cache block cache block valid cache block t bits b bits valid cache block m-1 block offset valid cache block valid cache block

Accessing Fully Set Associative Caches Line matching and word selection must compare the in each valid. =1? (1) The valid bit must be set. 1 2 3 4 5 6 7 set : 1 11 1 11 w w 1 w 2 w 3 (2) The bits in one of the cache lines must match the bits in the address m-1 =? t bits 11 b bits 1 block offset (3) If (1) and (2), then cache hit, and block offset selects starting byte.

Cache Example Problem 32 bit address 1K cache lines (Fully associative) 1 word per line (32 bit data) Byte addressable Memory address structure? Cache line structure? Cache size?

Cache access problem Cache: 8 bit address and 1 word block Fully Associative : 8 lines contains one word Show the final content of the cache given 2,3,11,16,21,13,64,48,19,11,3,22,4,27,6,11 memory access

2 1 miss 3 11 hit 11 1 11 miss 16 1 miss 21 11 1 miss 13 11 1 miss 64 1 miss 48 1 1 miss 19 1 11 hit 11 1 11 hit 3 11 hit 22 11 1 hit 4 1 miss 27 11 11 miss 6 1 1 hit 11 1 11 hit Set V Tag B B1 B2 B3 1 11 M[24] M[25] M[26] M[27] 1 1 M[8] M[9] M[1] M[11] 1 1 M[16] M[17] M[18] M[19] 1 11 M[2] M[21] M[22] M[23] 1 11 M[12] M[13] M[14] M[15] 1 1 M[64] M[65] M[66] M[67] 1 11 M[48] M[49] M[5] M[51] 1 1 M[4] M[5] M[6] M[7]

Multi-Level Caches Options: separate data and instruction caches, or a unified cache Processor Regs L1 d-cache L1 i-cache Unified L2 Cache Memory disk size: speed: $/Mbyte: line size: 2 B 3 ns 8-64 KB 3 ns 8 B 32 B larger, slower, cheaper 1-4MB SRAM 6 ns $1/MB 32 B 128 MB DRAM 6 ns $1.5/MB 8 KB 3 GB 8 ms $.5/MB

Intel Pentium Cache Hierarchy Regs. L1 Data 1 cycle latency 16 KB 4-way assoc Write-through 32B lines L1 Instruction 16 KB, 4-way 32B lines L2 Unified 128KB--2 MB 4-way assoc Write-back Write allocate 32B lines Main Memory Up to 4GB Processor Chip

Cache Performance Metrics Miss Rate Fraction of memory references not found in cache (misses/references) Typical numbers: 3-1% for L1 can be quite small (e.g., < 1%) for L2, depending on size, etc. Hit Time Time to deliver a line in the cache to the processor (includes time to determine whether the line is in the cache) Typical numbers: 1 clock cycle for L1 3-8 clock cycles for L2 Miss Penalty Additional time required because of a miss Typically 25-1 cycles for main memory

Writing Cache Friendly Code Repeated references to variables are good (temporal locality) Stride-1 reference patterns are good (spatial locality) Examples: cold cache, 4-byte words, 4-word cache blocks int sumarrayrows(int a[m][n]) { int i, j, sum = ; int sumarraycols(int a[m][n]) { int i, j, sum = ; for (i = ; i < M; i++) for (j = ; j < N; j++) sum += a[i][j]; return sum; for (j = ; j < N; j++) for (i = ; i < M; i++) sum += a[i][j]; return sum; Miss rate = 1/4 = 25% Miss rate = 1%

The Memory Mountain Read throughput (read bandwidth) Number of bytes read from memory per second (MB/s) Memory mountain Measured read throughput as a function of spatial and temporal locality. Compact way to characterize memory system performance.

Memory Mountain Test Function /* The test function */ void test(int elems, int stride) { int i, result = ; volatile int sink; for (i = ; i < elems; i += stride) result += data[i]; sink = result; /* So compiler doesn't optimize away the loop */ /* Run test(elems, stride) and return read throughput (MB/s) */ double run(int size, int stride, double Mhz) { double cycles; int elems = size / sizeof(int); test(elems, stride); /* warm up the cache */ cycles = fcyc2(test, elems, stride, ); /* call test(elems,stride) */ return (size / stride) / (cycles / Mhz); /* convert cycles to MB/s */

Memory Mountain Main Routine /* mountain.c - Generate the memory mountain. */ #define MINBYTES (1 << 1) /* Working set size ranges from 1 KB */ #define MAXBYTES (1 << 23) /*... up to 8 MB */ #define MAXSTRIDE 16 /* Strides range from 1 to 16 */ #define MAXELEMS MAXBYTES/sizeof(int) int data[maxelems]; /* The array we'll be traversing */ int main() { int size; /* Working set size (in bytes) */ int stride; /* Stride (in array elements) */ double Mhz; /* Clock frequency */ init_data(data, MAXELEMS); /* Initialize each element in data to 1 */ Mhz = mhz(); /* Estimate the clock frequency */ for (size = MAXBYTES; size >= MINBYTES; size >>= 1) { for (stride = 1; stride <= MAXSTRIDE; stride++) printf("%.1f\t", run(size, stride, Mhz)); printf("\n"); exit();

The Memory Mountain read throughput (MB/s) Slopes of Spatial Locality 12 1 8 6 4 2 xe L2 L1 Pentium III Xeon 55 MHz 16 KB on-chip L1 d-cache 16 KB on-chip L1 i-cache 512 KB off-chip unified L2 cache Ridges of Temporal Locality s1 s3 s5 mem 8k 2k stride (words) s7 s9 s11 s13 s15 8m 2m 512k 128k 32k working set size (bytes)

Ridges of Temporal Locality Slice through the memory mountain with stride=1 illuminates read throughputs of different caches and memory 12 main memory region L2 cache region L1 cache region 1 8 6 4 2 8m 4m 2m 124k 512k 256k 128k 64k 32k 16k 8k 4k 2k 1k read througput (MB/s) working set size (bytes)

A Slope of Spatial Locality Slice through memory mountain with size=256kb shows cache block size.

Matrix Multiplication Example Major Cache Effects to Consider Total cache size Exploit temporal locality and keep the working set small (e.g., by using blocking) Block size Exploit spatial locality Description: Multiply N x N matrices O(N3) total operations Accesses N reads per source element N values summed per destination but may be able to hold in register /* ijk */ for (i=; i<n; i++) { for (j=; j<n; j++) { sum =.; for (k=; k<n; k++) sum += a[i][k] * b[k][j]; c[i][j] = sum; Variable sum held in register

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

Layout of C Arrays in Memory (review) C arrays allocated in row-major order each row in contiguous memory locations Stepping through columns in one row: for (i = ; i < N; i++) sum += a[][i]; accesses successive elements if block size (B) > 4 bytes, exploit spatial locality compulsory miss rate = 4 bytes / B Stepping through rows in one column: for (i = ; i < n; i++) sum += a[i][]; accesses distant elements no spatial locality! compulsory miss rate = 1 (i.e. 1%)

Matrix Multiplication (ijk) /* ijk */ for (i=; i<n; i++) { for (j=; j<n; j++) { sum =.; for (k=; k<n; k++) sum += a[i][k] * b[k][j]; c[i][j] = sum; Inner loop: (i,*) (*,j) (i,j) A B C Row-wise Columnwise Fixed Misses per Inner Loop Iteration: A B C.25 1..

Matrix Multiplication (jik) /* jik */ for (j=; j<n; j++) { for (i=; i<n; i++) { sum =.; for (k=; k<n; k++) sum += a[i][k] * b[k][j]; c[i][j] = sum Misses per Inner Loop Iteration: A B C.25 1.. Inner loop: (i,*) (*,j) (i,j) A B C Row-wise Columnwise Fixed

Matrix Multiplication (kij) /* kij */ for (k=; k<n; k++) { for (i=; i<n; i++) { r = a[i][k]; for (j=; j<n; j++) c[i][j] += r * b[k][j]; Inner loop: (i,k) (k,*) (i,*) A B C Fixed Row-wise Row-wise Misses per Inner Loop Iteration: A B C..25.25

Matrix Multiplication (ikj) /* ikj */ for (i=; i<n; i++) { for (k=; k<n; k++) { r = a[i][k]; for (j=; j<n; j++) c[i][j] += r * b[k][j]; Inner loop: (i,k) (k,*) (i,*) A B C Fixed Row-wise Row-wise Misses per Inner Loop Iteration: A B C..25.25

Matrix Multiplication (jki) /* jki */ for (j=; j<n; j++) { for (k=; k<n; k++) { r = b[k][j]; for (i=; i<n; i++) c[i][j] += a[i][k] * r; Inner loop: (*,k) (*,j) (k,j) A B C Misses per Inner Loop Iteration: A B C 1.. 1. Column - wise Fixed Columnwise

Matrix Multiplication (kji) /* kji */ for (k=; k<n; k++) { for (j=; j<n; j++) { r = b[k][j]; for (i=; i<n; i++) c[i][j] += a[i][k] * r; Misses per Inner Loop Iteration: A B C 1.. 1. Inner loop: (*,k) (k,j) (*,j) A B C Columnwise Fixed Columnwise

Summary of Matrix Multiplication ijk (& jik): 2 loads, stores misses/iter = 1.25 kij (& ikj): 2 loads, 1 store misses/iter =.5 jki (& kji): 2 loads, 1 store misses/iter = 2. for (i=; i<n; i++) { for (j=; j<n; j++) { sum =.; for (k=; k<n; k++) sum += a[i][k] * b[k][j]; c[i][j] = sum; for (k=; k<n; k++) { for (i=; i<n; i++) { r = a[i][k]; for (j=; j<n; j++) c[i][j] += r * b[k][j]; for (j=; j<n; j++) { for (k=; k<n; k++) { r = b[k][j]; for (i=; i<n; i++) c[i][j] += a[i][k] * r;

Pentium Matrix Multiply Performance Miss rates are helpful but not perfect predictors. Code scheduling matters, too. 6 5 Cycles/iteration 4 3 2 kji jki kij ikj jik ijk 1 25 5 75 1 125 15 175 2 225 25 275 3 325 35 375 4 Array size (n)

Improving Temporal Locality by Blocking Example: Blocked matrix multiplication block (in this context) does not mean cache block. Instead, it mean a sub-block within the matrix. Example: N = 8; sub-block size = 4 A 11 A 12 A 21 A 22 X B 11 B 12 B 21 B 22 = C 11 C 12 C 21 C 22 Key idea: Sub-blocks (i.e., A xy ) can be treated just like scalars. C 11 = A 11 B 11 + A 12 B 21 C 12 = A 11 B 12 + A 12 B 22 C 21 = A 21 B 11 + A 22 B 21 C 22 = A 21 B 12 + A 22 B 22

Blocked Matrix Multiply (bijk) for (jj=; jj<n; jj+=bsize) { for (i=; i<n; i++) for (j=jj; j < min(jj+bsize,n); j++) c[i][j] =.; for (kk=; kk<n; kk+=bsize) { for (i=; i<n; i++) { for (j=jj; j < min(jj+bsize,n); j++) { sum =. for (k=kk; k < min(kk+bsize,n); k++) { sum += a[i][k] * b[k][j]; c[i][j] += sum;

Blocked Matrix Multiply Analysis Innermost loop pair multiplies a 1 X bsize sliver of A by a bsize X bsize block of B and accumulates into 1 X bsize sliver of C Loop over i steps through n row slivers of A & C, using same B Innermost Loop Pair for (i=; i<n; i++) { for (j=jj; j < min(jj+bsize,n); j++) { sum =. for (k=kk; k < min(kk+bsize,n); k++) { sum += a[i][k] * b[k][j]; c[i][j] += sum; kk jj jj i kk i A B C row sliver accessed bsize times block reused n times in succession Update successive elements of sliver

Pentium Blocked Matrix Multiply Performance Blocking (bijk and bikj) improves performance by a factor of two over unblocked versions (ijk and jik) relatively insensitive to array size. 6 5 Cycles/iteration 4 3 2 1 25 5 75 1 125 15 175 2 Array size (n) 225 25 275 3 325 35 375 4 kji jki kij ikj jik ijk bijk (bsize = 25) bikj (bsize = 25)

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 advane with generic code Keep working set reasonably small (temporal locality) Use small strides (spatial locality)