Docsity

Compiler Optimizations for Reducing Cache Misses: Techniques and Examples, Study notes of Computer Architecture and Organization

Toggle sidebar
bg1
1
Reducing Misses by Compiler Optimizations
•Instructions
–Reorder procedures in memory so as to reduce misses
–Profiling to look at conflicts
–McFarling [1989] reduced caches misses by 75% on 8KB direct
mapped cache with 4 byte blocks
•Data
–Merging Arrays: improve spatial locality by single array of
compound elements vs. 2 arrays
–Loop Interchange: change nesting of loops to access data in order
stored in memory
–Loop Fusion: Combine 2 independent loops that have same looping
and some variables overlap
–Blocking: Improve temporal locality by accessing “blocks” of data
repeatedly vs. going down whole columns or rows
Merging Arrays Example
/* Before */
int val[SIZE];
int key[SIZE];
/* After */
struct merge {
int val;
int key;
};
struct merge merged_array[SIZE];
Reducing conflicts between val& key (spatial locality)
Array Storage (Column order)
•Store first column, then second column, then next column, …
•Example
–[6x6] array A
–each element 1 word (32 bits)
–Block size = 4 words
–For i = 1 to 6; X += a[1,i]; How many blocks are loaded into cache?
–Misses: Compulsory? Conflict? Capacity?
A(4,5)
A(5,3)
A(6,1)
A(6,6)A(5,6)A(4,6)
A(3,6)A(2,6)A(1,6)A(6,5)A(5,5)A(3,5)A(2,5)A(1,5)A(6,4)A(5,4)
A(4,4)A(3,4)A(2,4)A(1,4)A(6,3)A(4,3)A(3,3)A(2,3)A(1,3)A(6,2)
A(5,2)A(4,2)A(3,2)A(2,2)A(1,2)A(5,1)A(4,1)A(3,1)A(2,1)A(1,1)
Memory Map
Array Storage (Row order)
•Store first row, then second row, then next row, …
•Example
–[6x6] array A
–each element 1 word (32 bits)
–Block size = 4 words
–For i = 1 to 6; X += a[1,i]; How many blocks are loaded into cache?
–Misses: Compulsory? Conflict? Capacity?
A(5,4)
A(3,5)
A(1,6)
A(6,6)A(6,5)A(6,4)
A(6,3)A(6,2)A(6,1)A(5,6)A(5,5)A(5,3)A(5,2)A(5,1)A(4,6)A(4,5)
A(4,4)A(4,3)A(4,2)A(4,1)A(3,6)A(3,4)A(3,3)A(3,2)A(3,1)A(2,6)
A(2,5)A(2,4)A(2,3)A(2,2)A(2,1)A(1,5)A(1,4)A(1,3)A(1,2)A(1,1)
Memory Map
Loop Interchange Example
/* Before */
for (j = 0; j < 100; j = j+1)
for (i = 0; i < 5000; i = i+1)
x[i][j] = 2 * x[i][j];
/* After */
for (i = 0; i < 5000; i = i+1)
for (j = 0; j < 100; j = j+1)
x[i][j] = 2 * x[i][j];
Sequential accesses (spatial locality)
-instead of striding through memory every 100 words
Fortran: column major
-row indices change
C: row major
-column indices change
Which is this?
A(3,5)
A(1,6)
A(4,4)A(4,3)A(4,2)A(4,1)A(3,6)A(3,4)A(3,3)A(3,2)A(3,1)A(2,6)
A(2,5)A(2,4)A(2,3)A(2,2)A(2,1)A(1,5)A(1,4)A(1,3)A(1,2)A(1,1)
Loop Fusion Example
/* Before */
for (i = 0; i < N; i = i+1)
for (j = 0; j < N; j = j+1)
a[i][j] = 1/b[i][j] * c[i][j];
for (i = 0; i < N; i = i+1)
for (j = 0; j < N; j = j+1)
d[i][j] = a[i][j] + c[i][j];
/* After */
for (i = 0; i < N; i = i+1)
for (j = 0; j < N; j = j+1)
{a[i][j] = 1/b[i][j] * c[i][j];
d[i][j] = a[i][j] + c[i][j];}
Temporal locality: 2 misses per access to a & c vs. one miss per
access
pf3

Partial preview of the text

Download Compiler Optimizations for Reducing Cache Misses: Techniques and Examples and more Study notes Computer Architecture and Organization in PDF only on Docsity!

Reducing Misses by Compiler Optimizations

  • Instructions
    • Reorder procedures in memory so as to reduce misses
    • Profiling to look at conflicts
    • McFarling [1989] reduced caches misses by 75% on 8KB direct

mapped cache with 4 byte blocks

  • Data
    • Merging Arrays : improve spatial locality by single array of

compound elements vs. 2 arrays

  • Loop Interchange : change nesting of loops to access data in order

stored in memory

  • Loop Fusion : Combine 2 independent loops that have same looping

and some variables overlap

  • Blocking : Improve temporal locality by accessing “blocks” of data

repeatedly vs. going down whole columns or rows

Merging Arrays Example

/ Before / int val[SIZE];

int key[SIZE];

/ After /

struct merge {

int val;

int key; };

struct merge merged_array[SIZE];

Reducing conflicts between val & key (spatial locality)

Array Storage (Column order)

  • Store first column, then second column, then next column, …
  • Example
    • [6x6] array A
    • each element 1 word (32 bits)
    • Block size = 4 words
    • For i = 1 to 6; X += a[1,i]; How many blocks are loaded into cache?
    • Misses: Compulsory? Conflict? Capacity?

A(4,5)

A(5,3)

A(6,1)

A(4,6) A(5,6) A(6,6)

A(5,4) A(6,4) A(1,5) A(2,5) A(3,5) A(5,5) A(6,5) A(1,6) A(2,6) A(3,6)

A(6,2) A(1,3) A(2,3) A(3,3) A(4,3) A(6,3) A(1,4) A(2,4) A(3,4) A(4,4)

A(1,1) A(2,1) A(3,1) A(4,1) A(5,1) A(1,2) A(2,2) A(3,2) A(4,2) A(5,2)

Memory Map

Array Storage (Row order)

  • Store first row, then second row, then next row, …
  • Example
    • [6x6] array A
    • each element 1 word (32 bits)
    • Block size = 4 words
    • For i = 1 to 6; X += a[1,i]; How many blocks are loaded into cache?
    • Misses: Compulsory? Conflict? Capacity?

A(5,4)

A(3,5)

A(1,6)

A(6,4) A(6,5) A(6,6)

A(4,5) A(4,6) A(5,1) A(5,2) A(5,3) A(5,5) A(5,6) A(6,1) A(6,2) A(6,3)

A(2,6) A(3,1) A(3,2) A(3,3) A(3,4) A(3,6) A(4,1) A(4,2) A(4,3) A(4,4)

A(1,1) A(1,2) A(1,3) A(1,4) A(1,5) A(2,1) A(2,2) A(2,3) A(2,4) A(2,5)

Memory Map

Loop Interchange Example

/ Before / for (j = 0; j < 100; j = j+1) for (i = 0; i < 5000; i = i+1) x[i][j] = 2 * x[i][j];

/ After / for (i = 0; i < 5000; i = i+1) for (j = 0; j < 100; j = j+1) x[i][j] = 2 * x[i][j];

Sequential accesses (spatial locality)

-instead of striding through memory every 100 words

Fortran: column major

-row indices change

C: row major

-column indices change

Which is this?

A(3,5)

A(1,6)

A(2,6)A(3,1) A(3,2) A(3,3) A(3,4) A(3,6) A(4,1) A(4,2) A(4,3) A(4,4)

A(1,1)A(1,2) A(1,3) A(1,4) A(1,5) A(2,1) A(2,2) A(2,3) A(2,4) A(2,5)

Loop Fusion Example

/* Before */

for (i = 0; i < N; i = i+1)

for (j = 0; j < N; j = j+1)

a[i][j] = 1/b[i][j] * c[i][j];

for (i = 0; i < N; i = i+1)

for (j = 0; j < N; j = j+1)

d[i][j] = a[i][j] + c[i][j];

/* After */

for (i = 0; i < N; i = i+1)

for (j = 0; j < N; j = j+1)

{ a[i][j] = 1/b[i][j] * c[i][j];

d[i][j] = a[i][j] + c[i][j];}

Temporal locality: 2 misses per access to a & c vs. one miss per access

Example: Matrix Multiply

6x16x26x36x46x5 6x

5x15x25x35x45x55x

4x14x24x34x44x54x

3x13x23x33x43x53x

2x12x22x32x42x52x

1x11x21x31x41x51x

Matrix X: i x j

i

j

Matrix Y: i x k

i

k

Matrix Z: k x j

k

j

/* Before */ X = Y x Z

for ( i = 0; i < N; i = i+1) for ( j = 0; j < N; j = j+1) {r = 0; for (k = 0; k < N; k = k+1){ r = r + y[i][ k]z[k][ j];}; x[i] [j] = r; };*

Read all N x N elements of z[ ] Read N elements of 1 row of y[ ] repeatedly

Write N elements of 1 row of x[ ]

To calculate one entry of X: Sum the multiplication of every element in a single row

of Y by every corresponding element in a single column of Z

Example: Matrix Multiply

6x16x26x36x46x5 6x

5x15x25x35x45x55x

4x14x24x34x44x54x

3x13x23x33x43x53x

2x12x22x32x42x52x

1x11x21x31x41x51x

Matrix X: i x j

i

j

Matrix Y: i x k

i

k

Matrix Z: k x j

k

j

X = Y x Z

Possibilities: Cache large enough to hold…

…all three N x N matrices (cold misses only if no conflicts)

…one N x N matrix, one row of N (limited capacity misses, no thrashing)

…neither row of N or one N x N matrix (capacity misses to compute single row of X)

Conclusion: Capacity Misses a function of N & Cache Size:

–When no capacity misses: O(3 NxN)

Blocking

• Problem:

  • Worst case capacity misses: 2N^3 + N^2
  • Working set of matrix elements is too large to fit in cache

• Solution:

  • Sub-divide matrices into smaller groups of working sets that will fit in cache, iterate through all subgroups (improves

temporal locality)

• Maximize accesses to the data loaded into the cache

before data is replaced

  • Can be done at block or register levels

Blocking Example: Matrix Multiply

6x 6

6x 5

6x 4

6x 3

6x 2

6x 1

5x 6

5x 5

5x 4

5x 3

5x 2

5x 1

4x 6

4x 5

4x 4

4x 3

4x 2

4x 1

3x 6

3x 5

3x 4

3x 3

3x 2

3x 1

2x 6

2x 5

2x 4

2x 3

2x 2

2x 1

1 x 6

1 x 5

1 x 4

1 x 3

1 x 2

1x

Matrix X: i x j

i

j

Matrix Y: i x k

i

k

Matrix Z: k x j

k

j

X = Y x Z

/ After / for ( jj = 0; jj < N; jj = jj+B) for ( kk = 0; kk < N; kk = kk+B) for ( i = 0; i < N; i = i+1) for (j = jj; j < m i n (jj+ B-1,N); j = j+ 1 ) {r = 0; for (k = kk; k < m i n (kk+ B-1,N); k = k+1) { r = r + y[i][ k]z[k][ j];}; x[i] [j] = x[ i][j ] + r; };*

jj

kk

jj

kk

jj

kk

jj

kk

jj jj kk kk

Blocking Example

• Capacity Misses from 2N^3 + N^2 to 2N^3 /B +N^2

• B called Blocking Factor

• Blocking traditionally aimed at capacity misses

  • Assumes conflict misses insignificant or addressed by associativity
  • Blocking ‡ smaller working set of blocks, therefore may

reduce conflicts

Reducing Conflict Misses by Blocking

  • Conflict misses in caches not FA vs. Blocking size
    • Lam et al [1991] a blocking factor of 24 had a fifth the misses vs.

48 despite both fit in cache

Blocking Factor

Fully Associative Cache

Direct Mapped Cache