Techniqueadvanced
Unimodular loop transformations
Also called unimodular framework.
Models interchange, reversal and skewing as integer matrices of determinant plus or minus 1 on the iteration space, composed by product.
A unimodular transformation of a perfect loop nest of depth is a integer matrix with determinant . The new nest visits the same iterations, but it names the iteration by and runs the points in lexicographic order of . Interchange, reversal and skewing are the elementary cases, and a sequence of them is the product of their matrices, so one test covers every combination: is legal if and only if for every dependence distance . The determinant makes the map a bijection of the integer points, so the new loops visit exactly the old iterations, with bounds computed by Fourier-Motzkin elimination. The framework, due to Banerjee for double loops and to Wolf and Lam in general, turns the choice of a loop order into the choice of a matrix.
In plain words
Think of the iterations of a nest as points on a grid. Swapping two loops, running a loop backward, or sliding the rows of the grid are all ways of looking at the same points along new directions. Each of these views can be written as a small table of whole numbers, and doing two of them in a row is the same as multiplying their tables. A table of the right kind loses no point and adds no point. To know whether a new view is safe, take every arrow between two points that depend on each other, look at it through the table, and check that it still points forward. If all arrows point forward, the program gives the same result in the new order.
When it applies
- (T1) A perfect nest. All statements are inside the innermost loop, and all of them are mapped by the same .
- (T2) Affine bounds. The iteration space is for an integer matrix and a vector that may contain symbolic sizes.
- (T3) Known distances. Every dependence has a constant distance vector, or a direction vector whose entries are handled with intervals as Wolf and Lam do.
- (T4) Unimodular. is an integer matrix with .
The elementary matrices for two loops are:
| transformation | matrix | |
|---|---|---|
| interchange | ||
| reversal of the inner loop | ||
| skewing the inner loop by |
Every unimodular integer matrix is a product of such elementary matrices, so the framework covers exactly the sequences of interchanges, reversals and skews.
The template
UNIMODULAR(nest with space {i : A i <= b}, body BODY(i), distances D, matrix T)- 1
if det T is not 1 or -1 then - 2
return "not unimodular" - 3
for each d in D do - 4
if T d is not lexicographically positive then - 5
return "illegal: T reverses a dependence" - 6
S <- inverse of T ▷ an integer matrix, since det T = +-1 - 7
the new space is {y : A S y <= b} - 8
eliminate y_k, ..., y_2 by Fourier-Motzkin to get the bounds of each y_l - 9
emit the nest over y_1, ..., y_k with body BODY(S y)
The holes are the nest, its distances and the matrix . Choosing is the job of an algorithm on top of the framework: the hyperplane method builds a whose first row is a time function, the Wolf-Lam algorithm builds one for locality, and Wolf and Lam's parallelization algorithm builds one with as many parallel loops as possible.
Theorem. The transformed nest keeps every dependence pair in order if and only if for every dependence distance .
Proof. The new nest runs before exactly when . Lexicographic order is compatible with addition, so holds exactly when . A dependence goes from to , and . So the source runs first exactly when . If this holds for every , every pair keeps its order, and Theorem 1 of the legality page gives the same result. If for some (it is never , because is invertible and ), every pair with this distance has its sink run first.
Why the determinant must be . If is an integer matrix with an integer inverse , then is an integer vector for every integer , and is an integer vector for every integer . So the integer points of the new space are exactly the images of the old iterations, one for one, and a nest that scans the integer points of the new space runs each old iteration once. An integer matrix has an integer inverse exactly when its determinant is , because with both determinants integers. With , for example, half of the integer points of the new space have no preimage, and the code needs extra steps or guards.
Composition. If and are legal one after the other, the combined map is , and its legality is again the single test . An intermediate step need not be legal for the product to be legal, which is why the framework finds sequences that the step-by-step tests reject.
Classic examples
A wavefront. The nest for i = 1 to n: for j = 1 to m: A[i][j] = A[i-1][j] + A[i][j-1] + A[i-1][j+1] has the distances , and . The test for three matrices:
| result | ||||
|---|---|---|---|---|
| illegal | ||||
| legal, inner loop carries | ||||
| legal, inner loop parallel |
The third matrix is skewing by 2 followed by interchange. Its outer loop runs over , and every transformed distance has a positive first entry, so the inner loop is a DOALL loop.
Bounds by elimination. Take , so and . The inverse gives and . The old bounds and become and . The inner bounds are . Eliminating leaves :
for y1 = 2 to n + m:
for y2 = max(1, y1 - m) to min(n, y1 - 1):
BODY(i = y2, j = y1 - y2)
Reversal makes interchange legal. A nest with the single distance cannot be interchanged. The product of the inner reversal and the interchange, , maps to , which is legal. The new outer loop runs , that is, over backward.
Completing a row. For a parallel inner loop, an algorithm often fixes only the first row of , a time function with for all . If the entries of have greatest common divisor 1, the extended Euclidean algorithm, or the Hermite normal form in higher dimensions, completes to a unimodular matrix. For , the rows and give determinant .
Variations
- Direction vectors. With entries like (any positive value) or (any value), is an interval vector. Wolf and Lam test that it is lexicographically positive for every choice inside the intervals.
- Non-unimodular maps. An integer matrix with can still be legal, but the new space has holes. The generated code steps by more than 1 or uses guards; the Hermite normal form of gives the steps.
- Tiling on top. A unimodular that makes a band fully permutable, usually a skew, is followed by tiling, which is not a unimodular map.
- One map per statement. For imperfect nests and different maps for different statements, the polyhedral model replaces by one affine schedule per statement, found by solving the affine scheduling problem.
Pitfalls
- Testing the factors and not the product. A sequence can be legal as a whole even when one step alone is illegal. Test the product.
- Forgetting the determinant. A matrix with determinant 2 passes the dependence test but makes the new nest scan points that are not iterations.
- Copying the bounds. The new bounds come from the inverse and elimination, not from the old bounds. They often contain
maxandminof several terms, and the outer bounds can needceilandfloorwhen a coefficient is not . - Statements outside the inner loop. The framework maps one perfect nest. A statement between the loop headers has no place in space; distribute it out first.
- Equating a legal with a good one. The test says nothing about locality or parallelism. The second matrix in the table above is legal, but its inner loop is still sequential.
Exercises
- Is legal for the distances and ? Answer. and , both positive, so yes.
- Compute the product of skewing by 1 and then interchange, and apply it to . Answer. , and maps to .
- Why is not used, though it keeps every distance positive? Answer. . The new outer index takes only even values, so a loop over all integers runs points that are not iterations.
- Complete the row to a unimodular matrix. Answer. Find with , for example , : has determinant .
References
- Utpal Banerjee, Unimodular transformations of double loops, Proceedings of the Third Workshop on Languages and Compilers for Parallel Computing, Irvine, 1990; reprinted in Advances in Languages and Compilers for Parallel Computing (A. Nicolau, D. Gelernter, T. Gross and D. Padua, editors), MIT Press, 1991, 192-219, a double loop transformed by a 2 x 2 unimodular matrix, the legality condition on the distance vectors, and the new loop bounds
- Michael E. Wolf and Monica S. Lam, A loop transformation theory and an algorithm to maximize parallelism, IEEE Transactions on Parallel and Distributed Systems 2(4), 1991, 452-471, interchange, reversal and skewing as unimodular matrices, legality as the lexicographic positivity of the transformed dependence vectors, distance and direction entries, and the code generation of the bounds
- Michael Wolfe, High Performance Compilers for Parallel Computing, Addison-Wesley, 1996, the chapter on loop restructuring, the unimodular matrix view of loop interchange, reversal and skewing
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 2X9ZFB Edit this page History Report an error Markdown