Cache Management: Allen and Kennedy, Chapter 9
Cache Management: Allen and Kennedy, Chapter 9
Cache Management: Allen and Kennedy, Chapter 9
Introduction
Register
One word storage Temporal reuse Direct store Asynchronous
Cache
Multiple words Spatial reuse Load before store Synchronous
Spatial Reuse
Permits high reuse when accessing closely located data DO I = 1, M DO J = 1, N
Spatial Reuse
ENDDO
ENDDO Iterates over columns
Temporal Reuse
Reuse limited by cache size, LRU replacement strategy DO I = 1, M DO J = 1, N
Temporal Reuse
DO jj = J, MIN(N, J+S-1)
A(I) = A(I) + B(jj) ENDDO
ENDDO
ENDDO
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
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
Loop Reordering
Once the cost is established, reorder the loop from cheapest innermost loop to high cost outermost loop
Blocking
ENDDO
ENDDO 2NM/b misses
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
Blocking
DO J = 1, M, T DO I = 1, N DO jj = J, MIN(J+T-1, M)
Unaligned Data
DO I = 1, N, S DO J = 1, M DO ii = I, MIN(I+S-1,N)
Unaligned Data
DO J = 1, M, T DO I = 1, N DO jj = J, MIN(J+T-1, M)
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
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
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
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
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
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
ENDDO
ENDDO
DO J = 1, ii 1
A(ii, J) = A(ii, I) + A(ii, J) ENDDO ENDDO ENDDO
Software Prefetching
Prefetching disadvantages
Increases number of executions May result in premature eviction of useful cache May bring in data evicted before use or never used
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
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
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
Prefetch Analysis
ENDDO
ENDDO
Prefetch Analysis
ENDDO
ENDDO
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
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
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
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
ENDDO
ENDDO
ENDDO
DO J = 1, M prefetch(A(2,J)) DO I = 2, 4 A(I, J) = A(I, J) * B(I) ENDDO
prefetch(A(33, J))
A(33, J) = A(33, J) * B(33) ENDDO
ENDDO
ENDDO
ENDDO
. . .
Effectiveness
Summary
Software prefetching