Algorithm Search⌘K

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 S1,…,SmS_1, \dots, S_m in this textual order, and build the dependence graph GG: a node per statement, and an edge Sa→SbS_a \to S_b for every dependence from an instance of SaS_a to an instance of SbS_b, 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 GG, flow, anti and output alike. A test that cannot decide gives an edge.

In a nest, distributing the loop at depth ll uses only the dependences carried at depth ll 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

  1. DISTRIBUTE(for i = L to U: S_1; ...; S_m)
  2. 1build the dependence graph G on S_1, ..., S_m
  3. 2find the strongly connected components C_1, ..., C_r of G
  4. 3order the components in a topological order of the component graph
  5. 4for each component C in that order do
  6. 5emit for i = L to U: the statements of C in their original textual order
  7. 6if no edge inside C is carried by the loop then
  8. 7mark 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 G1,…,GrG_1, \dots, G_r 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 GG goes from a statement of a later group to a statement of an earlier group.

Proof. In the original loop the instance Sa(i)S_a(i) runs before Sb(j)S_b(j) exactly when i<ji < j, or i=ji = j and a<ba < b. Take a dependence from Sa(i)S_a(i) to Sb(j)S_b(j), so Sa(i)S_a(i) 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 ii first and textual order second. The pair keeps its order.
  • Group of SaS_a before the group of SbS_b. Every instance of the earlier loop runs before every instance of the later loop. The pair keeps its order.
  • Group of SaS_a after the group of SbS_b. Then Sb(j)S_b(j) 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. □\square

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: S1→S2S_1 \to S_2 on A[i], loop-independent; S2→S1S_2 \to S_1 on B, since S2(i)S_2(i) writes B[i] and S1(i+1)S_1(i+1) reads it, carried with distance 1; S2→S3S_2 \to S_3 on B[i], loop-independent; S3→S3S_3 \to S_3 on C, carried with distance 1; S1→S4S_1 \to S_4 on A[i], loop-independent. The components are {S1,S2}\{S_1, S_2\}, a cycle; {S3}\{S_3\}, with a self loop; and {S4}\{S_4\}. Both S3S_3 and S4S_4 come after {S1,S2}\{S_1, S_2\}, 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 n=8n = 8, the original and this distribution give the same arrays, and so does the order with the D loop before the C loop. Splitting S1S_1 from S2S_2 is illegal: the loop of S1S_1 would read every B[i-1] before the loop of S2S_2 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 S1→S2S_1 \to S_2, and also the carried anti-dependence S2→S1S_2 \to S_1, because S1(i+1)S_1(i+1) overwrites the t that S2(i)S_2(i) read, and a carried output dependence S1→S1S_1 \to S_1. 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 S1→S2S_1 \to S_2 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 if across 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

  1. Distribute S1: A[i] = A[i-1] + B[i]; S2: C[i] = A[i] * 3. Answer. Edges: S1→S1S_1 \to S_1 carried, S1→S2S_1 \to S_2 loop-independent. Two components; the loop of S1S_1 comes first and is sequential, the loop of S2S_2 is parallel.
  2. Distribute S1: A[i] = C[i-1] + 1; S2: C[i] = A[i] * 2. Answer. S1→S2S_1 \to S_2 loop-independent and S2→S1S_2 \to S_1 carried: one cycle, no distribution.
  3. Distribute S1: B[i] = A[i] + 1; S2: A[i+1] = C[i] * 2. Answer. S2(i)S_2(i) writes A[i+1], which S1(i+1)S_1(i+1) reads: an edge S2→S1S_2 \to S_1, carried. There is no edge from S1S_1 to S2S_2. The topological order puts S2S_2 first: for i: A[i+1] = C[i] * 2 then for i: B[i] = A[i] + 1, both parallel.
  4. 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

  1. 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
  2. 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
  3. 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.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • three classic examples