Cache Management: Allen and Kennedy, Chapter 9

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 47

Cache Management

Allen and Kennedy, Chapter 9

Optimizing Compilers for Modern Architectures

Introduction

Register
One word storage Temporal reuse Direct store Asynchronous

Cache
Multiple words Spatial reuse Load before store Synchronous

Optimizing Compilers for Modern Architectures

Spatial Reuse

Permits high reuse when accessing closely located data DO I = 1, M DO J = 1, N

A(I, J) = A(I, J) + B(I, J)


ENDDO ENDDO doesnt support reuse

Optimizing Compilers for Modern Architectures

Spatial Reuse

DO J = 1, N DO I = 1, M A(I, J) = A(I, J) + B(I, J)

ENDDO
ENDDO Iterates over columns

Optimizing Compilers for Modern Architectures

Temporal Reuse

Reuse limited by cache size, LRU replacement strategy DO I = 1, M DO J = 1, N

A(I) = A(I) + B(J)


ENDDO ENDDO

Optimizing Compilers for Modern Architectures

Temporal Reuse

Strip mining to trick the replacement strategy


DO J = 1, N, S DO I = 1, M

DO jj = J, MIN(N, J+S-1)
A(I) = A(I) + B(jj) ENDDO

ENDDO
ENDDO

Optimizing Compilers for Modern Architectures

Loop Interchange

Which loop should be innermost ? Strives to reduce distances between memory accesses to increase locality Attaches cost function to the loop and computes for best loop ordering

Optimizing Compilers for Modern Architectures

Cost Assignment

Cost is 1 for references that do not depend on loop induction variables Cost is N for references based on induction variables over a non-contiguous space Cost is Ns/l for induction variables based references over contiguous space Multiply the cost by the loop trip count if the reference varies with the loop index

Optimizing Compilers for Modern Architectures

Loop Reordering

Once the cost is established, reorder the loop from cheapest innermost loop to high cost outermost loop

Optimizing Compilers for Modern Architectures

Blocking

DO J = 1, M DO I = 1, N D(I) = D(I) + B(I,J)

ENDDO
ENDDO 2NM/b misses

Optimizing Compilers for Modern Architectures

Blocking

After strip-mine-and-interchange DO I = 1, N, S DO J = 1, M

DO i = I, MIN(I+S-1, N)
D(i) = D(i) + B(I,J) ENDDO ENDDO ENDDO (1 + 1/M) NM / b misses

Optimizing Compilers for Modern Architectures

Blocking

DO J = 1, M, T DO I = 1, N DO jj = J, MIN(J+T-1, M)

D(I) = D(I) + B(I, jj)


ENDDO ENDDO ENDDO (1 + 1/T) NM/b misses

Optimizing Compilers for Modern Architectures

Unaligned Data

DO I = 1, N, S DO J = 1, M DO ii = I, MIN(I+S-1,N)

D(ii) = D(ii) + B(ii, J)


ENDDO ENDDO ENDDO (1 + 1/M + b/S) NM/b Misses

Optimizing Compilers for Modern Architectures

Unaligned Data

DO J = 1, M, T DO I = 1, N DO jj = J, MIN(J+T-1, M)

D(I) = D(I) + B(I, jj)


ENDDO ENDDO ENDDO (1+ 1/T + b/TN) NM/b Misses (1+ 1/T) NM/b Misses

Optimizing Compilers for Modern Architectures

Unaligned Data

First case, the cache must hold s/b different blocks of D Second case, the case must hold T different blocks of B s can be a factor b larger than T

(1 + 1/M + 1/T) NM/b Misses for the first case


(1+ 1/T) NM/b Misses for the second case

Optimizing Compilers for Modern Architectures

Legality of Blocking

Strip mining is always legal Loop interchange is not always legal
procedure StripMineAndInterchange (L, m, k, o, S) // L = {L1, L2, ..., Lm}is the loop nest to be transformed // Lk is the loop to be strip mined // Lo is the outer loop which is to be just inside the by-strip loop // after interchange // S is the variable to use as strip size; its value must be positive let the header of Lk be DO I = L, N, D; split the loop into two loops, a by-strip loop: DO I = L, N, S*D and a within-strip loop: DO i = I, MAX(I+S*D-D,N), D around the loop body; interchange the by-strip loop to the position just outside of Lo; end StripMineAndInterchange

Optimizing Compilers for Modern Architectures

Legality of Blocking

Every direction vector for a dependence carried by any of the loops L0Lk+1 has either an = or a < in the kth position Conservative testing

Optimizing Compilers for Modern Architectures

Profitability of Blocking

Profitable if there is reuse between iterations of a loop that is not the innermost loop Reuse occurs when:
Theres a small-threshold dependence of any type, including input, carried by the loop, or The loop index appears, with small stride, in the contiguous dimension of a multidimensional array and in no other dimension

Optimizing Compilers for Modern Architectures

Blocking with Skewing



For cases where interchange is not possible DO I = 1, M DO J = 1, N

A(J+1) = (A(J) + A(J+1))/2


ENDDO ENDDO

Optimizing Compilers for Modern Architectures

Blocking with Skewing

Optimizing Compilers for Modern Architectures

Blocking with Skewing

After Skewing DO I = 1, N DO j = I, M+I-1

A(j-I+2) = (A(j-I+1) + A(j-I+2))/2


ENDDO ENDDO

Optimizing Compilers for Modern Architectures

Blocking with Skewing

After strip-mine DO I = 1, N DO j = I, M+I-1, S

DO jj = j, MAX(j+S-1, M+I-1)
A(jj-I+2) = (A(jj-I+1) + A(jj-I+2))/2 ENDDO ENDDO ENDDO

Optimizing Compilers for Modern Architectures

Blocking with Skewing

Loop interchange DO j = 1, M+N-1, S DO I = MAX(1, j-M+1), MIN(j, N)

DO jj = j, MAX(j+S-1, M+I-1)
A(jj-I+2) = (A(jj-I+1) + A(jj-I+2))/2 ENDDO ENDDO ENDDO

Optimizing Compilers for Modern Architectures

Blocking with Skewing

Optimizing Compilers for Modern Architectures

Triangular Cache Blocking

DO I = 2, N DO J = 1, I-1 A(I, J) = A(I, I) + A(J, J)

ENDDO
ENDDO

Optimizing Compilers for Modern Architectures

Triangular Cache Blocking



Applying strip mining DO I = 2, N, K DO ii = I, I+K-1

DO J = 1, ii 1
A(ii, J) = A(ii, I) + A(ii, J) ENDDO ENDDO ENDDO

Optimizing Compilers for Modern Architectures

Triangular Cache Blocking



Applying triangular loop interchange DO I = 2, N, K DO J = 1, I+K-1

DO ii = MAX(J+1, I), I+K-1


A(ii, J) = A(ii, I) + A(ii, J) ENDDO ENDDO ENDDO

Optimizing Compilers for Modern Architectures

Software Prefetching

Program reorganization limitations


Cant eliminate first time misses Cant eliminate misses unknown at compile time

Prefetching disadvantages
Increases number of executions May result in premature eviction of useful cache May bring in data evicted before use or never used

Optimizing Compilers for Modern Architectures

Software Prefetching Algorithm

Critical steps in an effective prefetching algorithm


Accurate determination of the references requiring prefetching Insertion of prefetching instructions far enough in advance

Optimizing Compilers for Modern Architectures

Prefetch Analysis

Identify where misses may happen Make use of dependence analysis strategy First, ensure that every edge that is unlikely to correspond to reuse is eliminated from the graph Assume that the loop nest has been strip-mined and interchanged to increase locality Traverses the loop and mark ineffective for loops without reuse

Optimizing Compilers for Modern Architectures

Prefetch Analysis

Estimate amount of data used by each iteration, and determine the overflow iteration, which is one more than the number of iterations whose data can be accommodated in cache at the same time

Any dependence with a threshold equal to or greater than the overflow is considered ineffective for reuse

Optimizing Compilers for Modern Architectures

Prefetch Analysis

Identify where prefetching is required Two cases:
If the group generator is not contained in a dependence cycle, a miss is expected on each iteration unless references to the generator on subsequent iterations display temporal locality If the group generator is contained in a dependence cycle, then a miss is expected only on the first few iterations of the carrying loop, depending on the distance of the carrying dependence. In this case, a prefetch to the reference can be placed before the loop carrying the dependence

Optimizing Compilers for Modern Architectures

Prefetch Analysis

DO J = 1, M DO I = 1, 32 A(I+1, J) = A(I, J) + C(J)

ENDDO
ENDDO

Optimizing Compilers for Modern Architectures

Prefetch Analysis

DO J = 1, M DO I = 1, 32 A(I, J) = A(I, J) + B(I) * C(I, J)

ENDDO
ENDDO

Optimizing Compilers for Modern Architectures

Insertion for Acyclic Partitions



Assuming single name partition with single generator If there is no spatial reuse of the reference in the loop then insert a prefetch before each reference to the generator If the references have spatial locality within the loop, determine i0 of the first iteration after the initial iteration that causes a miss on the access to the generator and the iteration l between misses in the cache.

Optimizing Compilers for Modern Architectures

Insertion for Acyclic Partitions


1. 2. 3. 4.
Partition the loop into two parts;
initial subloop running from 1 to io-1 and remainder running from io to the end

Strip mine the second loop to have subloops of length l Insert all prefetches needed to avoid misses in the initial subloop prior to the loop Eliminate any very short loops by unrolling

Optimizing Compilers for Modern Architectures

Insertion for Acyclic Partitions

DO I = 1, M A(I, J) = A(I, J) + A(I-1, J) ENDDO Assuming cache line of length four, then io = 5 and l = 4

Optimizing Compilers for Modern Architectures

Insertion for Acyclic Partitions

DO I = 1, 3 A(I, J) = A(I, J) + A(I-1, J) ENDDO

DO I = 4, M, 4
IU = MIN(M, I+4) DO ii = I, IU A(I, J) = A(I, J) + A(I-1, J) ENDDO ENDDO

Optimizing Compilers for Modern Architectures

Insertion for Acyclic Partitions


prefetch(A(0,J)) DO I = 1, 3 A(I, J) = A(I, J) + A(I-1, J)

ENDDO
DO I = 4, M, 4 IU = MIN(M, I+3) prefetch(A(I, J))

DO ii = I, IU
A(ii, J) = A(ii, J) + A(ii-1, J) ENDDO ENDDO

Optimizing Compilers for Modern Architectures

Insertion for Cyclic Name Partitions



Insert prefetch instructions prior to the loop carrying the cycle In the case where loop carrying the dependence is an outer loop, the prefetch can be vectorized
Place prefetch loop nest outside the loop carrying the backward dependence of a cyclic name partition Rearrange the loop nest so that the loop iterating sequentially over cache lines is innermost Split the innermost loop into two Preloop to the first iteration of the innermost loop contaning a generator reference beginning on a new cache line and Main loop that begins with the iteration containing the new cache reference. Replace the preloop by a prefetch of the first generator reference. Set the stride of the main loop to the interval between new cache references.

Optimizing Compilers for Modern Architectures

Insertion for Cyclic Name Partitions

DO J = 1, M DO I = 2, 33 A(I, J) = A(I, J) * B(I)

ENDDO
ENDDO

Optimizing Compilers for Modern Architectures

Insertion for Cyclic Name Partitions


prefetch(B(2)) DO I = 5, 33, 4 prefetch(B(I))

ENDDO
DO J = 1, M prefetch(A(2,J)) DO I = 2, 4 A(I, J) = A(I, J) * B(I) ENDDO

Optimizing Compilers for Modern Architectures

Insertion for Cyclic Name Partitions


DO I = 5, 33, 4 prefetch(A(I, J)) A(I, J) = A(I, J) * B(I)

A(I+1, J) = A(I+1, J) * B(I+1)


A(I+2, J) = A(I+2, J) * B(I+2) A(I+3, J) = A(I+3, J) * B(I+3) ENDDO

prefetch(A(33, J))
A(33, J) = A(33, J) * B(33) ENDDO

Optimizing Compilers for Modern Architectures

Prefetching Irregular Accesses

DO J = 1, M DO I = 2, 33 A(I, J) = A(I, J) * B(IX(I), J)

ENDDO
ENDDO

Optimizing Compilers for Modern Architectures

Prefetching Irregular Accesses

prefetch(IX(2)) DO I = 5, 33, 4 prefetch(IX(I))

ENDDO
. . .

Optimizing Compilers for Modern Architectures

Effectiveness

Optimizing Compilers for Modern Architectures

Summary

Two different kind of reuse


Temporal reuse Spatial reuse

Strategies to increase the two reuse


Loop Interchange Cache Blocking

Software prefetching

Optimizing Compilers for Modern Architectures

You might also like