Algorithm Search⌘K

Problemcore

Dependence testing

Also called array dependence analysis.

Decide whether two array references in a loop nest can touch the same element, an integer programming problem over the iteration space.

In plain words

Two statements in a loop nest touch an array, and at least one of them writes it. Can some run of the first statement and some run of the second touch the same element? If never, the compiler may reorder, run in parallel or vectorize them freely. If yes, it must keep their order. Each loop index takes integer values between its bounds, so the question is whether a small system of integer equations and inequalities has a solution. That is integer programming, which is hard in general. Compilers therefore run a mix of fast tests that are exact on the common cases and careful on the rest: when a test is not sure, it must answer "maybe", never "independent". Tests also report the distances or the directions of the dependence, because the transformations need them.

Definition

Input. Two references to the same array of mm dimensions, with affine subscripts:

  • a reference X[Fi⃗+f⃗]X[F\vec{i} + \vec{f}] in statement SS with iteration domain DS={i⃗∈ZkS:Ai⃗+a⃗≥0}D_S = \{\vec{i} \in \mathbb{Z}^{k_S} : A\vec{i} + \vec{a} \ge 0\},
  • a reference X[Gj⃗+g⃗]X[G\vec{j} + \vec{g}] in statement TT with iteration domain DT={j⃗∈ZkT:Bj⃗+b⃗≥0}D_T = \{\vec{j} \in \mathbb{Z}^{k_T} : B\vec{j} + \vec{b} \ge 0\},

where F,G,A,BF, G, A, B are integer matrices and f⃗,g⃗,a⃗,b⃗\vec{f}, \vec{g}, \vec{a}, \vec{b} integer vectors. The two statements have kk common loops, the first kk entries of both vectors. Optionally, a direction vector ψ⃗∈{<,=,>,∗}k\vec{\psi} \in \{<, =, >, *\}^k.

Question. Is there a pair (i⃗,j⃗)(\vec{i}, \vec{j}) with

i⃗∈DS,j⃗∈DT,Fi⃗+f⃗=Gj⃗+g⃗,il  ψl  jl for l=1,…,k  ?\vec{i} \in D_S, \quad \vec{j} \in D_T, \quad F\vec{i} + \vec{f} = G\vec{j} + \vec{g}, \quad i_l \;\psi_l\; j_l \text{ for } l = 1, \dots, k \; ?

The equations are the dependence equations, one per dimension. The relation ilψljli_l \psi_l j_l is il<jli_l < j_l, il=jli_l = j_l, il>jli_l > j_l or no constraint, in the notation of direction vectors used here (Wolfe's; << means that the second reference runs in a later iteration of loop ll). The set of solutions is a set of integer points of a polyhedron, in the isl notation { S[i] -> T[j] : F i + f = G j + g and ... }.

Outputs that tests give.

  • Decision: "independent" if there is no solution, else "dependent". A conservative test may answer "maybe" in place of either; it must never answer "independent" when a solution exists.
  • Directions: the set of plain direction vectors ψ⃗∈{<,=,>}k\vec{\psi} \in \{<, =, >\}^k for which a solution exists.
  • Distances: the set {j⃗−i⃗}\{\vec{j} - \vec{i}\} over the solutions on the common loops, or a summary of it.

From solutions to dependences. A solution with i⃗≺j⃗\vec{i} \prec \vec{j} on the common loops is a dependence from SS to TT; a solution with j⃗≺i⃗\vec{j} \prec \vec{i} is a dependence from TT to SS; a solution with i⃗=j⃗\vec{i} = \vec{j} is a loop-independent dependence in the direction of the order of SS and TT in the body (none if S=TS = T and the two references are the same access). Its kind (flow, anti, output) follows from which access writes. See data dependence.

Variants

  • Constant bounds. All loops are rectangular with known integer bounds. This is the setting of the GCD test, the Banerjee test and the practical tests.
  • Affine bounds. Bounds of inner loops depend on outer indices (triangular loops). The question is still integer feasibility of a polyhedron. Banerjee's bounds were extended to trapezoidal domains; the omega test handles any affine constraints.
  • Symbolic bounds. A bound is a parameter nn not known at compile time. The question becomes "for some nn" or "for all nn", a formula of Presburger arithmetic.
  • Memory-based against value-based. The question above asks whether two accesses touch the same element at all (memory-based). The value-based question asks whether the value read was last written by the given write; it removes dependences through overwritten values. Feautrier's array dataflow analysis answers it exactly with parametric integer programming.
  • Nonaffine subscripts. Subscripts such as A[i*j] or A[B[i]] are outside the affine model. Compilers assume a dependence, or test at run time.

Examples

A subscript that the GCD decides

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

The equation 2i=2j+12i = 2j + 1 has no integer solution, since its left side is even and its right side is odd. The GCD test proves independence without the bounds.

A real solution but no integer solution in the bounds

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

The equation is 2i−3j=22i - 3j = 2. The gcd of 22 and 33 is 11, which divides 22, so the GCD test says "maybe". Over the box 1≤i,j≤31 \le i, j \le 3 the value of 2i−3j2i - 3j runs over the real interval [−7,3][-7, 3], which holds 22, so the Banerjee test also says "maybe". But the loop writes A[2],A[4],A[6]A[2], A[4], A[6] and reads A[5],A[8],A[11]A[5], A[8], A[11]: there is no dependence. The integer solutions (i,j)=(1+3t,2t)(i, j) = (1 + 3t, 2t) need t≥1t \ge 1 for j≥1j \ge 1 and t≤0t \le 0 for i≤3i \le 3. Only an exact test, such as the exact SIV test of the practical tests, finds this.

A coupled pair

for i = 1 to 8:
  A[i][i] = A[i][i+1] + 1

Each dimension alone has solutions: i=ji = j for the first, and i=j+1i = j + 1 for the second. Together they have none. A test that treats each dimension on its own says "maybe"; a test that joins the constraints, such as the delta test, says "independent".

A dependence with a known distance

for i = 1 to 4:
  for j = 1 to 4:
    A[i+1][j] = A[i][j] + 1

The equations are i+1=i′i + 1 = i' and j=j′j = j'. Every solution has the distance (1,0)(1, 0), so there is one flow dependence with direction vector (<,=)(<, =), carried by the outer loop. The inner loop is parallel, and interchange is legal.

Known algorithms

test handles exact? cost
GCD test one equation, no bounds exact for one equation without bounds O(klog⁡C)O(k \log C) per equation
Banerjee test one equation, bounds, directions exact over the reals, not over the integers O(k)O(k) per equation and direction vector
practical tests (ZIV, SIV, MIV, delta) all subscripts, constant bounds exact when every coupled group reduces to single-index subscripts near linear in the number of subscripts in practice
Fourier-Motzkin elimination any affine constraints exact over the reals can grow doubly exponentially
omega test any affine constraints, symbolic bounds exact over the integers exponential in the worst case, fast in practice
Feautrier's array dataflow value-based, parametric exact parametric integer programming

All of them except the omega test and Feautrier's method are conservative. Their answer "independent" is a proof. Their answer "maybe" says only that they could not find a proof. Other tests in the literature, such as the lambda test of Li, Yew and Zhu, the I test of Kong, Klappholz and Psarris, and the power test of Wolfe and Tseng, sit between Banerjee and the omega test.

Most compilers use a cascade: cheap exact tests on simple subscripts first, a general test only on the rest. Goff, Kennedy and Tseng measured that most subscript pairs in scientific Fortran code are ZIV or SIV, where cheap exact tests exist.

Directions. Any of the tests can take a direction vector as extra constraints. To find the set of direction vectors, test (∗,…,∗)(*, \dots, *); if it is not independent, split the first ∗* into <<, == and >> and test each, and go on down. A branch that is proved independent is cut. The search tests at most 3k+3k−1+⋯+13^k + 3^{k-1} + \dots + 1 vectors, and far fewer when most branches are cut early.

AlgorithmTimeSpacePropertiesCode
Banerjee testO(m k) worstO(1) extradeterministiciterative1 language
GCD testO(m k log C) worstO(1) extradeterministiciterative1 language
Subscript-wise dependence testsO(m^2 k + m log C + 3^k m k) worstO(m k) extradeterministiciterative1 language

Bounds

Theorem. Dependence testing with affine subscripts and affine bounds is NP-complete. It is NP-hard already for one subscript and loops that run from 00 to 11.

Membership. The question is the feasibility of an integer program with the variables i⃗\vec{i} and j⃗\vec{j}: the equations, the domain inequalities and the direction constraints (a strict il<jli_l < j_l is il+1≤jli_l + 1 \le j_l over the integers). Integer feasibility is in NP: if a system has an integer solution, it has one whose size in bits is polynomial in the size of the system (Papadimitriou 1981). That solution is the certificate.

Hardness. Reduce from subset sum. Take positive integers w1,…,wkw_1, \dots, w_k and a target TT. Build the nest

for i1 = 0 to 1:
  ...
    for ik = 0 to 1:
      S: A[w1*i1 + ... + wk*ik] = 0
R: x = A[T]

SS writes A[∑lwlil]A[\sum_l w_l i_l] for each (i1,…,ik)∈{0,1}k(i_1, \dots, i_k) \in \{0, 1\}^k, and RR runs after the nest. There is a flow dependence from SS to RR if and only if some 0-1 vector has ∑lwlil=T\sum_l w_l i_l = T, that is, if and only if some subset of the wlw_l sums to TT. The nest has size polynomial in the input. So a polynomial algorithm for dependence testing would decide subset sum. For w=(3,5,7)w = (3, 5, 7) and T=12T = 12, the iteration (0,1,1)(0, 1, 1) writes A[12]A[12]; for T=11T = 11 no iteration does.

Subset sum is solvable in pseudo-polynomial time, so this shows only weak NP-hardness. An array with many dimensions gives strong NP-hardness from exact cover: one 0-1 loop per set, one dimension per element of the universe with subscript ∑s∋eis\sum_{s \ni e} i_s, and a read of A[1][1]…[1]A[1][1]\dots[1]. All coefficients are 00 or 11.

Fixed depth. For a fixed number of loops, the integer program has a fixed number of variables, and integer programming in fixed dimension is polynomial (Lenstra 1983). Real loop nests are shallow, which is one reason why exact tests are fast in practice.

Beyond affine. With polynomial subscripts and unbounded loop indices, dependence testing is undecidable. A write A[p(i1, ..., ik)] in a nest whose indices range over all natural numbers, and a read of A[0], are dependent if and only if the polynomial pp has a root in natural numbers, which is Hilbert's tenth problem, undecidable by Matiyasevich's theorem.

Reductions

  • To integer feasibility. The definition is an instance of integer feasibility with kS+kTk_S + k_T variables and mm equations. Every exact test solves this integer program, and every conservative test solves a relaxation of it: the GCD test drops the bounds, the Banerjee test drops integrality.
  • To linear Diophantine equations. Without bounds, each equation is a linear Diophantine equation, and a system of them is solved exactly with the Hermite normal form. This is the generalized GCD test.
  • To Presburger arithmetic. With symbolic bounds, the question with its quantifiers over parameters is a sentence of Presburger arithmetic, which the omega test decides.
  • From subset sum and exact cover, as in the bounds section.

Applications

References

  1. Randy Allen and Ken Kennedy, Optimizing Compilers for Modern Architectures, a Dependence-Based Approach, Morgan Kaufmann, 2002 (copyright 2001), Chapter 3, Dependence Testing (subscript classification, the GCD and Banerjee tests, the SIV and MIV tests and the delta test)
  2. Utpal Banerjee, Dependence Analysis for Supercomputing, Kluwer Academic Publishers, 1988, the dependence equations of a pair of affine references and the GCD and bounds tests
  3. Gina Goff, Ken Kennedy and Chau-Wen Tseng, Practical dependence testing, Proceedings of the ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI), 1991, the empirical study of subscripts in real programs, the ZIV, SIV and MIV classes and the delta test
  4. William Pugh, A practical algorithm for exact array dependence analysis, Communications of the ACM 35(8), 1992, pages 102 to 114, exact dependence testing with the omega test and its cost in practice
  5. 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 as integer linear programming, the GCD test and the heuristics for the general case)
  6. Christos H. Papadimitriou, On the complexity of integer programming, Journal of the ACM 28(4), 1981, the bound on the size of an integer solution, which puts integer feasibility in NP

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)
WritingW0 stub
  • the lead paragraph