Algorithm Search⌘K

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 S(i⃗)S(\vec{i}) to T(j⃗)T(\vec{j}), where i⃗\vec{i} and j⃗\vec{j} are the iteration vectors of the kk loops common to SS and TT. Its distance vector is d⃗=j⃗−i⃗\vec{d} = \vec{j} - \vec{i}. Because the source runs first, i⃗⪯j⃗\vec{i} \preceq \vec{j}, so d⃗\vec{d} is lexicographically positive or zero.

  • The dependence is carried at level ll, or carried by the loop of index ili_l, when d1=⋯=dl−1=0d_1 = \dots = d_{l-1} = 0 and dl≠0d_l \ne 0. Then dl>0d_l > 0, because d⃗≻0⃗\vec{d} \succ \vec{0}.
  • The dependence is loop independent when d⃗=0⃗\vec{d} = \vec{0}. Then S(i⃗)S(\vec{i}) and T(i⃗)T(\vec{i}) run in the same iteration, and SS comes before TT in the loop body. A dependence between statements that share no loop (k=0k = 0) is also loop independent.

The level of the dependence is ll in the first case and ∞\infty in the second. In the notation of direction vectors (Wolfe's <,=,><, =, >, where << at position ll means il<jli_l < j_l, that is, dl>0d_l > 0), a dependence is carried at level ll when its direction vector has == in positions 11 to l−1l-1 and << at position ll, 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 S→TS \to T 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 xx inside one instance, and that order is fixed. So a dependence of SS on itself always has d⃗≠0⃗\vec{d} \ne \vec{0} and is loop-carried.

Which loops can run in parallel

Run the nest with the loops of levels 11 to l−1l - 1 in order, run the iterations of the level-ll 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 ll:

  • carried at a level m<lm < l: source and sink lie in different iterations of the outer loop mm, which still runs in order;
  • carried at a level m>lm > l or loop independent: source and sink lie in the same iteration of the level-ll loop, which still runs in order.

So by the legality theorem, the level-ll loop can run in parallel if no dependence is carried at level ll. 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-kk loop keeps all level-kk dependences.

Examples and non-examples

One loop

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

S1(i)S1(i) writes A[i]A[i] and S1(i+1)S1(i+1) reads it. The flow dependence has distance 11 and is carried by the ii 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

S1(i)S1(i) writes A[i]A[i] and S2(i)S2(i) reads it in the same iteration: distance 00, loop independent. The loop carries no dependence, so its iterations can run in parallel, as long as each iteration runs S1S1 before S2S2.

Two loops

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

S(i,j)S(i, j) writes A[i][j]A[i][j], and S(i+1,j−1)S(i+1, j-1) reads it. The distance is (1,−1)(1, -1) and the direction vector is (<,>)(<, >). The ii loop carries the dependence. The jj loop carries nothing, so for a fixed ii all iterations of jj 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 (0,1)(0, 1): carried at level 2. The ii 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 (0,1)(0, 1), (1,0)(1, 0) and (2,−1)(2, -1); the output dependences have (1,−1)(1, -1) and (2,−2)(2, -2); the anti dependence has (1,−2)(1, -2). The flow dependence S→SS \to S is carried at level 2 by (0,1)(0,1) and at level 1 by the others. Neither loop is parallel.

Non-examples

  • A dependence with distance (0,0)(0, 0) 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 * t inside a loop, is carried by the loop through the output and anti dependences on t. Giving each iteration its own copy of t removes them. A loop that carries only such dependences becomes parallel after privatization.
  • 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 (1,−1)(1, -1) above becomes (−1,1)(-1, 1), which is not lexicographically positive, so that interchange is illegal.

References

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

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/AR27GT/text.yaml