Module 18: Loop Optimizations
  Lecture 36: Cycle Shrinking
 


Cycle Shrinking …

  • Dependence cycle with distance > 1
  • Transform a serial loop into two nested loops (outer serial and inner parallel)
  • Consider the loop
for I = 1,n
A[i+k] = B[i] –1
B[i+k] = A[i] + C[i]
endfor
for i=1, n, k
forall j=1, i+k-1
A[j+k]=B[j]-1
B[j+k]=A[j]+C[j]
endforall
endfor
   
For I = 3,n
A[i]=B[i-2]-1
B[i]=A[i-3]*k
Endfor

For j =3, n, 2
forall I = j, j+1
A[i]=B[i-2]-1
B[i]=A[i-3]*k
endforall
Endfor

A3 = B1 -1
B3 = A0 * k
A4 = B2 -1
B4 = A1 * k
A5 = B3 -1
B5 = A2 * k
A6 = B4 -1
B6 = A3 * k
A7 = B5 -1
B7 = A4 * k
A8 = B6 -1
B8 = A5 * k

Cycle Shrinking in Distance Varying Loops

  • The distance may not be constant
  • Cycle may be reduced by the minimum distance
For I = 1,n
X[i]=Y[i]+Z[i]
Y[i+3]=X[i-4]*W[i]
Endfor
for j=1, n, 3
forall I = j, j+2
X[i]=Y[i]+Z[i]
Y[i+3]=X[i-4]*W[i]
endforall
endfor