Techniquecore
Loop distribution
Also called loop fission.
Splits a loop into several loops over the same range, each with part of the body, by the strongly connected components of dependences.
Loop distribution splits one loop into several loops over the same range, each running a part of the original body. The statements are grouped by the strongly connected components of the dependence graph of the body: statements on a common dependence cycle stay in one loop, and the loops follow a topological order of the components. With this rule the distribution is always legal. The point is that the cycles are now separate. A statement that is not on any cycle carried by the loop lands in a loop of its own with no carried dependence, and that loop can run in parallel or as vector code, while the recurrences stay in small sequential loops. Distribution is the inverse of loop fusion, and it is the core step of the Allen-Kennedy algorithm.
In plain words
A loop body often does several jobs at once. Some jobs depend on what they did in the last turn of the loop, and others do not. Distribution pulls the jobs apart into separate loops, so that each loop does one group of jobs over the whole range. Jobs that feed each other in a circle must stay together, because neither can finish first. All other jobs can go in their own loops, placed so that each loop comes after the loops whose results it needs. A loop whose job never needs its own earlier turns can then run all its turns at the same time.
When it applies
Take a loop for i = L to U whose body is the statements in this textual order, and build the dependence graph : a node per statement, and an edge for every dependence from an instance of to an instance of , either carried by this loop or loop-independent. The loop must satisfy:
- (D1) A straight-line body. The statements form a sequence. A conditional is first turned into a guarded statement, with the condition kept in a temporary if the statements split.
- (D2) Known dependences. Every dependence of the body is an edge of , flow, anti and output alike. A test that cannot decide gives an edge.
In a nest, distributing the loop at depth uses only the dependences carried at depth or deeper, and the loop-independent ones. The dependences carried by an outer loop stay satisfied, because the outer loop still runs its iterations in order around the new loops.
The template
DISTRIBUTE(for i = L to U: S_1; ...; S_m)- 1
build the dependence graph G on S_1, ..., S_m - 2
find the strongly connected components C_1, ..., C_r of G - 3
order the components in a topological order of the component graph - 4
for each component C in that order do - 5
emit for i = L to U: the statements of C in their original textual order - 6
if no edge inside C is carried by the loop then - 7
mark this loop as parallel
The holes are the body and its graph. Line L2 is Tarjan's algorithm, which also returns the components in reverse topological order, so line L3 costs nothing more. The result is the finest distribution, with one loop per component. Any coarser one, which merges components that are next to each other in the order, is also legal, and the pieces can be fused back with loop fusion to recover reuse.
Theorem. Split the statements into groups and emit one loop per group in this order, each with its statements in textual order. This keeps every dependence pair in order if and only if no edge of goes from a statement of a later group to a statement of an earlier group.
Proof. In the original loop the instance runs before exactly when , or and . Take a dependence from to , so runs first in the original.
- Same group. The loop for this group runs its instances in the same order as the original, restricted to its statements: by first and textual order second. The pair keeps its order.
- Group of before the group of . Every instance of the earlier loop runs before every instance of the later loop. The pair keeps its order.
- Group of after the group of . Then runs first, and the pair is reversed. This is the case the condition forbids.
So all pairs keep their order exactly when no edge goes backward between groups, and Theorem 1 of the legality page gives the same result.
Corollary. The SCC grouping in topological order is legal. An edge inside one component stays inside one group. An edge between two components goes from an earlier to a later one in any topological order of the component graph. Conversely, two statements on a common cycle cannot be put in different loops: whichever loop comes first, one edge of the cycle goes backward.
Profit. A component with no carried edge gives a parallel loop. Distribution can also make each loop touch fewer arrays, which helps when the body uses more arrays than the cache or the registers can hold, and it isolates a loop for interchange when only some statements block the interchange. The cost is that values used by two loops must now go through memory, and that the loop control runs once per new loop.
Classic examples
Four statements, three loops.
for i = 1 to n:
S1: A[i] = B[i-1] + 1
S2: B[i] = A[i] * 2
S3: C[i] = C[i-1] + B[i]
S4: D[i] = A[i] + 5
The edges are: on A[i], loop-independent; on B, since writes B[i] and reads it, carried with distance 1; on B[i], loop-independent; on C, carried with distance 1; on A[i], loop-independent. The components are , a cycle; , with a self loop; and . Both and come after , and either order of the two is a topological order:
for i = 1 to n: ▷ sequential: the recurrence through A and B
A[i] = B[i-1] + 1
B[i] = A[i] * 2
for i = 1 to n: ▷ sequential: the recurrence on C
C[i] = C[i-1] + B[i]
for i = 1 to n: ▷ parallel
D[i] = A[i] + 5
The last loop is a DOALL loop. Run on random start values with , the original and this distribution give the same arrays, and so does the order with the D loop before the C loop. Splitting from is illegal: the loop of would read every B[i-1] before the loop of writes it, and the arrays differ.
Scalar expansion. In
for i = 1 to n:
S1: t = A[i] + B[i]
S2: C[i] = t * t
the scalar t gives the loop-independent flow edge , and also the carried anti-dependence , because overwrites the t that read, and a carried output dependence . The two statements form one component, and nothing can be split. Scalar expansion replaces t by an array element T[i], one per iteration. Then only the edge is left, and the loop splits into two parallel loops. If the last value of t is used after the loop, the code sets t = T[n]. When the loop is to stay one parallel loop, array privatization gives each thread its own copy of t, which removes the same edges with less memory.
Vectorization. A vectorizing compiler uses distribution to turn the acyclic part of a loop into vector statements: the third loop above becomes the array statement D[1:n] = A[1:n] + 5. The Allen-Kennedy algorithm does this recursively over a nest, distributing at each level and moving to the next level inside each cyclic component.
Variations
- Partial distribution. Split only off the statements that block an interchange or a parallel loop, and keep the rest together for reuse.
- Distribution plus fusion. Kennedy and McKinley first distribute to the finest legal partition and then fuse the parallel pieces back, as far as the fusion-preventing edges allow, so that the parallel loops are as large as possible.
- Distribution in a nest. Distributing the outer loop of a nest gives several nests, which is how imperfect nests are made perfect before loop interchange or tiling.
- Node splitting. A cycle that consists of a loop-independent flow edge and a carried anti edge can sometimes be broken by copying the old value into a temporary array before the loop, which turns the anti edge into a read of the copy.
Pitfalls
- Ignoring anti and output dependences. A cycle through an anti-dependence is a real cycle, as the scalar example shows. Leaving those edges out of the graph splits statements that must stay together.
- Reordering by text. Placing the loops in textual order instead of a topological order of the components is wrong when a later statement feeds an earlier one through a carried edge, as in Exercise 3. Always use the component order.
- Scalars that live past the loop. After scalar expansion, the value of the scalar after the loop must be restored from the last element.
- Conditionals. Splitting the statements of one
ifacross loops evaluates the condition twice, and the second evaluation can see changed data. Save the condition in an array, one value per iteration. - Forgetting the cost. Finest distribution can turn one pass over the data into many, and a temporary that was a register becomes memory traffic.
Exercises
- Distribute
S1: A[i] = A[i-1] + B[i]; S2: C[i] = A[i] * 3. Answer. Edges: carried, loop-independent. Two components; the loop of comes first and is sequential, the loop of is parallel. - Distribute
S1: A[i] = C[i-1] + 1; S2: C[i] = A[i] * 2. Answer. loop-independent and carried: one cycle, no distribution. - Distribute
S1: B[i] = A[i] + 1; S2: A[i+1] = C[i] * 2. Answer. writesA[i+1], which reads: an edge , carried. There is no edge from to . The topological order puts first:for i: A[i+1] = C[i] * 2thenfor i: B[i] = A[i] + 1, both parallel. - Why can a component with only loop-independent edges inside it run in parallel? Answer. Its loop carries no dependence, so no iteration uses a value from another iteration of the same loop.
References
- David J. Kuck, Robert H. Kuhn, David A. Padua, Bruce Leasure and Michael Wolfe, Dependence graphs and compiler optimizations, Proceedings of the 8th ACM Symposium on Principles of Programming Languages (POPL), 1981, 207-218, the dependence graph of a loop body, its strongly connected components (pi-blocks), and loop distribution over them
- Randy Allen and Ken Kennedy, Optimizing Compilers for Modern Architectures, a Dependence-Based Approach, Morgan Kaufmann, 2002 (copyright 2001), Chapter 2 (Dependence, Theory and Practice), loop distribution by strongly connected components in the vectorization algorithm; Chapter 6 (Creating Coarse-Grained Parallelism), distribution and scalar expansion for parallel loops
- Ken Kennedy and Kathryn S. McKinley, Maximizing loop parallelism and improving data locality via loop fusion and distribution, Languages and Compilers for Parallel Computing (LCPC 1993), Lecture Notes in Computer Science 768, Springer, 1994, 301-320, distribution into the finest legal partition and fusion of the parallel pieces back together
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 JEA9PQ Edit this page History Report an error Markdown