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 , the iteration vector runs in lexicographic order , and a dependence from to exists when both touch the same memory cell, at least one writes it, and runs first. For a dependence between two statements with common loops, the distance is . The dependence is carried at level if and . It is loop independent if .
Definition. The loop at level of a nest is a DOALL loop if no dependence of the nest is carried at level .
Parallel execution. Run the loop at level as a DOALL loop means this. The outer loops at levels to stay sequential. For each fixed outer iteration , every iteration of the loop runs its body, with the inner loops, in the original order. Across different values of , 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 as a DOALL loop is legal for every interleaving if and only if no dependence is carried at level .
Proof. Take a dependent pair, from to , with distance and level of carry (or loop independent).
- If , the two instances have different values of , with . The loop at level is still sequential, so all instances with the smaller value of end before any instance with the larger value starts. The order is kept.
- If or the dependence is loop independent, then . So both instances are in the same iteration of the loop at level , which runs its body in the original order. The order is kept.
- If , the two instances are in different iterations of the same instance of the loop. The interleaving that runs all of iteration before iteration reverses the pair.
So all pairs keep their order for every interleaving exactly when no pair has . With the reordering theorem of the legality page, this gives the claim.
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 and reads only and . 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 and the read in iteration touch the same cell, so there is a flow dependence with distance , carried at level 1. With , the other cells of zero and , the sequential run gives , the prefix sums. The interleaving that runs the iterations from down to gives , because each iteration reads 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 , carried at level 1. The loop is DOALL and the loop is not. Each column is a chain, so after loop interchange, which is legal here because stays lexicographically positive, the outer loop over is DOALL and runs independent chains.
An exact test helps.
for i = 0 to n:
A[2*i] = A[2*i+1] + 1
A dependence needs , which has no integer solution because does not divide . 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 . This gives an output dependence and an anti dependence, from the read of in iteration to the write of in iteration , both carried at level 1. So the loop is not DOALL as written. Each read of follows a write of in the same iteration, so 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 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.
Related definitions
- 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
- 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)
- 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
- 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)
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag EWRA39 Edit this page History Report an error Markdown