Algorithm Search⌘K

Definitioncore

DOALL loop

A loop that carries no data dependence at its own level, so its iterations may run in any order or in parallel with the same result.

A DOALL loop is a loop that carries no data dependence at its own level: no dependence goes from one of its iterations to another iteration of the same loop instance. So its iterations may run in any order, or at the same time, and every statement instance still reads the same values as in the sequential run. A compiler marks such a loop as a parallel for loop. The test is the absence of a loop-carried dependence at the level of the loop, which comes from dependence testing.

Definition

Take a loop nest in the notation of the loop pages. A statement instance is S(i⃗)S(\vec{i}), the iteration vector i⃗=(i1,…,ik)\vec{i} = (i_1, \dots, i_k) runs in lexicographic order ≺\prec, and a dependence from S(i⃗)S(\vec{i}) to T(j⃗)T(\vec{j}) exists when both touch the same memory cell, at least one writes it, and S(i⃗)S(\vec{i}) runs first. For a dependence between two statements with cc common loops, the distance is d⃗=(j1−i1,…,jc−ic)\vec{d} = (j_1 - i_1, \dots, j_c - i_c). The dependence is carried at level ll if d1=⋯=dl−1=0d_1 = \dots = d_{l-1} = 0 and dl>0d_l > 0. It is loop independent if d⃗=0⃗\vec{d} = \vec{0}.

Definition. The loop at level ll of a nest is a DOALL loop if no dependence of the nest is carried at level ll.

Parallel execution. Run the loop at level ll as a DOALL loop means this. The outer loops at levels 11 to l−1l - 1 stay sequential. For each fixed outer iteration (i1,…,il−1)(i_1, \dots, i_{l-1}), every iteration ili_l of the loop runs its body, with the inner loops, in the original order. Across different values of ili_l, the statement instances may interleave in any way. Each statement instance runs as one atomic step. The loop ends when all its iterations end, which is a barrier.

A reordering of the instances is called legal if it keeps the order of every dependent pair (the condition of Theorem 1 on the legality page). Then every instance reads the same values and the final memory is the same, for every initial memory.

Theorem. Running the loop at level ll as a DOALL loop is legal for every interleaving if and only if no dependence is carried at level ll.

Proof. Take a dependent pair, from S(i⃗)S(\vec{i}) to T(j⃗)T(\vec{j}), with distance d⃗\vec{d} and level of carry mm (or loop independent).

  • If m<lm < l, the two instances have different values of i1,…,imi_1, \dots, i_m, with im<jmi_m < j_m. The loop at level mm is still sequential, so all instances with the smaller value of imi_m end before any instance with the larger value starts. The order is kept.
  • If m>lm > l or the dependence is loop independent, then i1=j1,…,il=jli_1 = j_1, \dots, i_l = j_l. So both instances are in the same iteration of the loop at level ll, which runs its body in the original order. The order is kept.
  • If m=lm = l, the two instances are in different iterations il<jli_l < j_l of the same instance of the loop. The interleaving that runs all of iteration jlj_l before iteration ili_l reverses the pair.

So all pairs keep their order for every interleaving exactly when no pair has m=lm = l. With the reordering theorem of the legality page, this gives the claim. □\square

The converse is about dependences, not about values. A reversed flow or anti dependence changes a value that some instance reads, when each statement stores a value that is a one to one function of its inputs and of its own identity. A reversed output dependence alone may leave the result the same, for example if a later statement overwrites the cell. Compilers do not use that freedom: they treat every carried dependence as a reason to keep the loop sequential.

A dependence test is conservative: it may report a dependence that does not exist, and it never misses one that exists. A loop that the compiler proves DOALL is a DOALL loop. A loop that the compiler does not prove DOALL may still be one.

Examples and non-examples

A plain DOALL loop.

for i = 1 to n:
  A[i] = B[i] + C[i]

Each iteration writes its own cell of AA and reads only BB and CC. No dependence exists, so the loop is DOALL.

A recurrence is not DOALL.

for i = 1 to 6:
  A[i] = A[i-1] + B[i]

The write A[i]A[i] and the read A[i−1]A[i-1] in iteration i+1i + 1 touch the same cell, so there is a flow dependence with distance 11, carried at level 1. With A[0]=1A[0] = 1, the other cells of AA zero and B[i]=iB[i] = i, the sequential run gives A=[1,2,4,7,11,16,22]A = [1, 2, 4, 7, 11, 16, 22], the prefix sums. The interleaving that runs the iterations from i=6i = 6 down to 11 gives A=[1,2,2,3,4,5,6]A = [1, 2, 2, 3, 4, 5, 6], because each iteration reads A[i−1]A[i-1] before it is written. The recurrence has a parallel algorithm (a parallel prefix sum), but that changes the order of the additions, so it is not a DOALL run of this loop.

One loop of a nest.

for i = 1 to n:
  for j = 1 to m:
    A[i][j] = A[i-1][j] + 1

The only dependence has distance (1,0)(1, 0), carried at level 1. The jj loop is DOALL and the ii loop is not. Each column is a chain, so after loop interchange, which is legal here because (0,1)(0, 1) stays lexicographically positive, the outer loop over jj is DOALL and runs mm independent chains.

An exact test helps.

for i = 0 to n:
  A[2*i] = A[2*i+1] + 1

A dependence needs 2i=2i′+12 i = 2 i' + 1, which has no integer solution because gcd⁡(2,2)=2\gcd(2, 2) = 2 does not divide 11. The GCD test proves this, so the loop is DOALL. A test that ignored integrality would report a possible dependence and lose the parallel loop.

A scalar temporary.

for i = 1 to n:
  t = A[i]
  B[i] = t * t

All iterations write the same cell tt. This gives an output dependence and an anti dependence, from the read of tt in iteration ii to the write of tt in iteration i+1i + 1, both carried at level 1. So the loop is not DOALL as written. Each read of tt follows a write of tt in the same iteration, so tt can be made private to each iteration (array privatization, which also covers scalars). After privatization no dependence is carried, and the loop is DOALL.

A reduction is not DOALL.

for i = 1 to n:
  s = s + A[i]

The flow dependence on ss has distance 1. Because ++ is associative and commutative on integers, a compiler can run the loop as a parallel reduction: each thread sums a part, and the parts are added at the end. That is a different program, not a DOALL run of this one. For floating point numbers it can change the result.

Indirect subscripts. In A[idx[i]] = B[i], the loop is DOALL exactly when idx has no repeated value among the iterations. The compiler cannot know this from the text. A run-time check (an inspector that tests the index array before the loop) can decide it.

  • Loop-carried dependence and dependence distance vectors give the level of a dependence, which is the only fact the definition uses.
  • A DOACROSS loop runs the iterations of a loop with carried dependences in parallel, with post and wait synchronization that keeps each carried pair in order.
  • Vectorization needs less than DOALL for one statement: the statement must not be on a dependence cycle at its level. The Allen-Kennedy algorithm splits a loop into DOALL pieces and sequential pieces by the strongly connected components of the dependence graph, the same way as loop distribution.
  • The hyperplane method and wavefront parallelism build new loops, of which all but the outer one are DOALL, when no original loop is.
  • In a parallel language, the parallel for loop is the construct. A DOALL loop is the property that makes a loop safe to write as a parallel for loop.

References

  1. Randy Allen and Ken Kennedy, Optimizing Compilers for Modern Architectures, a Dependence-Based Approach, Morgan Kaufmann, 2002 (copyright 2001), Chapter 2 (the section on parallelization and vectorization, where a loop that carries no dependence can run in parallel) and Chapter 6 (creating coarse-grained parallel loops by privatization, distribution, alignment and interchange)
  2. David A. Padua and Michael J. Wolfe, Advanced compiler optimizations for supercomputers, Communications of the ACM 29(12), 1986, 1184-1201, the sections on DOALL loops, DOACROSS loops and the dependence conditions for each
  3. Alfred V. Aho, Monica S. Lam, Ravi Sethi and Jeffrey D. Ullman, Compilers, Principles, Techniques, and Tools, second edition, Addison-Wesley, 2007, Chapter 11 (Section 11.7 on synchronization-free parallelism and Section 11.8 on synchronization between parallel loops)
  4. Michael Wolfe, High Performance Compilers for Parallel Computing, Addison-Wesley, 1996, the chapters on data dependence and on parallel loops (DOALL and DOACROSS execution)

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)
WritingW4 complete
  • a review of the text: no review record reviews/EWRA39/text.yaml