Definitioncore
Loop-carried dependence
Also called cross-iteration dependence.
A dependence between different iterations of a loop, carried by the outermost loop whose distance component is not zero.
A loop-carried dependence is a data dependence whose source and sink run in different iterations of a loop. In a loop nest it is carried by one loop, the outermost loop in which the two iterations differ. A dependence whose source and sink run in the same iteration of every common loop is loop independent. The level that carries a dependence decides which loops can run their iterations in parallel: a loop can be a DOALL loop exactly when it carries no dependence.
Definition
Take a dependence from to , where and are the iteration vectors of the loops common to and . Its distance vector is . Because the source runs first, , so is lexicographically positive or zero.
- The dependence is carried at level , or carried by the loop of index , when and . Then , because .
- The dependence is loop independent when . Then and run in the same iteration, and comes before in the loop body. A dependence between statements that share no loop () is also loop independent.
The level of the dependence is in the first case and in the second. In the notation of direction vectors (Wolfe's , where at position means , that is, ), a dependence is carried at level when its direction vector has in positions to and at position , and it is loop independent when all entries are .
Between two statements, the set of instance pairs can have many distance vectors, so the dependence can be carried at several levels and also be loop independent. Each level that occurs is recorded on its own edge of the dependence graph.
A dependence of an instance on itself does not exist: a statement such as x = x + 1 reads and then writes inside one instance, and that order is fixed. So a dependence of on itself always has and is loop-carried.
Which loops can run in parallel
Run the nest with the loops of levels to in order, run the iterations of the level- loop for each fixed outer iteration in any order or at the same time, and run each such iteration in order inside. This keeps the order of each dependence that is not carried at level :
- carried at a level : source and sink lie in different iterations of the outer loop , which still runs in order;
- carried at a level or loop independent: source and sink lie in the same iteration of the level- loop, which still runs in order.
So by the legality theorem, the level- loop can run in parallel if no dependence is carried at level . If one is, the parallel run does not fix the order of its source and sink, and the result can change. Allen and Kennedy state the general form as their Theorem 2.4: a reordering that keeps the order of the loops and the iteration order of the level- loop keeps all level- dependences.
Examples and non-examples
One loop
for i = 1 to n:
S1: A[i] = A[i-1] + 1
writes and reads it. The flow dependence has distance and is carried by the loop. The loop is a recurrence, and its iterations cannot run in parallel as written.
for i = 1 to n:
S1: A[i] = B[i] + 1
S2: C[i] = A[i] * 2
writes and reads it in the same iteration: distance , loop independent. The loop carries no dependence, so its iterations can run in parallel, as long as each iteration runs before .
Two loops
for i = 1 to n:
for j = 1 to n:
S: A[i][j] = A[i-1][j+1] + 1
writes , and reads it. The distance is and the direction vector is . The loop carries the dependence. The loop carries nothing, so for a fixed all iterations of can run in parallel, even though the second entry of the distance is negative.
for i = 1 to n:
for j = 1 to n:
S: B[i][j] = B[i][j-1] + 1
The distance is : carried at level 2. The loop carries nothing, so the outer loop is parallel and each row runs its recurrence in order.
One dependence carried at two levels
for i = 1 to 3:
for j = 1 to 3:
S: X[i+j] = X[i+j-1] + 1
All instance pairs, enumerated: the flow dependences have the distances , and ; the output dependences have and ; the anti dependence has . The flow dependence is carried at level 2 by and at level 1 by the others. Neither loop is parallel.
Non-examples
- A dependence with distance is not carried by any loop. It is loop independent, even inside two loops. A transformation that changes only the order of iterations keeps it, if it keeps the order of the statements in the body.
- A dependence whose only cause is a temporary scalar, such as
t = A[i]; B[i] = t * tinside a loop, is carried by the loop through the output and anti dependences ont. Giving each iteration its own copy oftremoves them. A loop that carries only such dependences becomes parallel after privatization.
Related definitions
- Distance and direction vectors. The distance vector gives the level directly; when the distance is not known, the direction vector still gives it.
- DOALL and DOACROSS. A loop that carries no dependence is a DOALL loop and can run as a parallel for. A loop that carries dependences can still overlap its iterations with synchronization (DOACROSS).
- Vectorization. A statement can be turned into vector operations at a level if it is not on a cycle of dependences carried at that level or deeper; see vectorization and the Allen-Kennedy algorithm.
- Interchange. Loop interchange moves a carried dependence to another level. The dependence above becomes , which is not lexicographically positive, so that interchange is illegal.
References
- Randy Allen and Ken Kennedy, Optimizing Compilers for Modern Architectures, a Dependence-Based Approach, Morgan Kaufmann, 2002 (copyright 2001), Chapter 2 (loop-carried and loop-independent dependences, Definition 2.11, the level of a dependence, and Theorem 2.4 on transformations that keep all level-k dependences)
- Randy Allen and Ken Kennedy, Automatic translation of FORTRAN programs to vector form, ACM Transactions on Programming Languages and Systems 9(4), 1987, the sections on loop-carried and loop-independent dependence and on the level of a dependence, used by the vector code generation algorithm
- Alfred V. Aho, Monica S. Lam, Ravi Sethi and Jeffrey D. Ullman, Compilers, Principles, Techniques, and Tools, second edition, Addison-Wesley, 2007, Section 11.6 (array data-dependence analysis) and Section 11.7 (synchronization-free parallelism)
- Michael Wolfe, High Performance Compilers for Parallel Computing, Addison-Wesley, 1996, the chapter on data dependence (dependence in loops, carried dependences and the level of a dependence)
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 AR27GT Edit this page History Report an error Markdown