Algorithmcore
Banerjee test
Also called Banerjee inequalities.
Reports independence when the constant of a subscript equation lies outside the real bounds of the equation over the loop bounds.
- Solves
- Dependence testing
- Time
- O(m k) worstO(k) best
- Space
- O(1) extra
- Where
- k = the depth of the loop nest; m = the number of subscripts
- Model
- Word RAM
- Properties
- deterministiciterative
- Evidence
- Proof sketchCode in 1 languages
The Banerjee test is a dependence test that uses the loop bounds. For one subscript pair, it computes the smallest and the largest value that the left side of the dependence equation can take when every loop index runs over the real interval of its loop. If the constant of the equation lies outside that range, no pair of iterations can touch the same element, and the references are independent. The bounds have a closed form for each level and each direction (, , or ), so the test costs a few operations per loop and can refine a direction vector one level at a time. The range is exact over the reals but not over the integers, so the answer "maybe" can be wrong; the answer "independent" never is.
In plain words
The two accesses touch the same element when some expression in the loop indices equals some constant. Each index can only take values inside its loop. So the expression has a smallest and a largest possible value, and both are easy to compute: for each loop, push the index to the end of its range that makes the term smallest, or largest. If the constant is below the smallest value or above the largest, the two accesses can never meet. The test can also ask the same question for pairs where the second access comes in a later, the same or an earlier iteration of each loop, which tells the compiler which loops carry the dependence. It works with fractions, not whole numbers, so it sometimes says "maybe" when the indices could only meet between two whole values.
The problem
A rectangular loop nest of depth runs loop over . Two references to an array of dimensions have the subscripts (source) and (sink). A direction vector , in Wolfe's notation ( at level means , the sink runs in a later iteration of loop ), restricts the pairs. The question is whether there are integer , in the bounds with and, for each dimension ,
The test answers "independent" or "maybe"; it must never answer "independent" when such vectors exist. The search over direction vectors returns the set of plain vectors that the test cannot rule out.
The algorithm
Write and , so , and . For a strict direction the integer constraint becomes the real constraint .
BANERJEE-TEST(a, b, L, U, psi):- 1
for each dimension r do - 2
c <- b_r0 - a_r0 - 3
lo <- 0; hi <- 0 - 4
for l <- 1 to k do - 5
range <- LEVEL-BOUNDS(a_rl, b_rl, L_l, U_l, psi_l) - 6
if range is empty then return "independent" - 7
lo <- lo + min(range); hi <- hi + max(range) - 8
if c < lo or c > hi then return "independent" - 9
return "maybe" LEVEL-BOUNDS(a, b, L, U, d): bounds of a*x - b*y over the region of d- 10
if d = "*" then return [a+ L - a- U - b+ U + b- L, a+ U - a- L - b+ L + b- U] - 11
if d = "=" then e <- a - b; return [e+ L - e- U, e+ U - e- L] - 12
if d = "<" then (empty if N < 1); v0 <- (a - b) L - b return [v0 - (a- + b)+ (N - 1), v0 + (a+ - b)+ (N - 1)]- 13
if d = ">" then (empty if N < 1); v0 <- (a - b) L + a return [v0 - (b+ - a)+ (N - 1), v0 + (a + b-)+ (N - 1)]
The region of a level is a rectangle for , a diagonal segment for , and a triangle for and . The formulas are the extremes of over its corners: , and for , and , and for .
Search over direction vectors. Start with . If the test says "maybe", replace the first by each of , , and test the three vectors; go on until no is left. A vector that tests "independent" is cut with all its refinements. The leaves are the direction vectors that the test cannot rule out.
Example
The loop
for i = 1 to 4:
for j = 1 to 4:
X[i + 2*j] = X[i + 2*j - 1] + 1
has , for the write and , for the read, so . The left side is , where is the iteration of the read. The search over directions gives these ranges:
| range of the left side | inside? | |
|---|---|---|
| yes, refine | ||
| yes, refine | ||
| no | ||
| yes | ||
| yes | ||
| yes, refine | ||
| , , | , , | no |
| yes, refine | ||
| yes | ||
| , | , | no |
The result is , and enumeration of all pairs of iterations gives the same set, so here the test is exact. The vectors and are flow dependences from the write to the read, for example and . The vector has the read first: it is an anti dependence from the read to the write, with direction after reversal, for example the read in and the write in , both on . No vector starts with , so the inner loop carries nothing and is parallel. The direction makes loop interchange illegal.
Independent by the bounds only. For A[i] = A[i + 10] with , the GCD test says "maybe" (the gcd is ), but lies in and does not. The Banerjee test says "independent".
Maybe without a dependence. For A[2*i] = A[3*i + 2] with , the range for is , which holds . Refined, gives , gives and gives , so the test keeps . The point solves the equation in the real triangle, but no integer point does: the loop writes and reads .
Correctness
The full proof is in proofs/correctness.md. Its steps:
- The regions are convex hulls of their corners. Each region of a level (rectangle, segment, triangle) is the set of convex combinations of the corners listed above; the proof writes the weights out.
- The closed forms are the extremes over the corners. A linear function on a convex hull takes its minimum and maximum at corners, and the formulas of lines L10 to L13 evaluate them, using the positive and negative parts to choose the right corner.
- The sum of the extremes is the extreme of the sum. The left side is a sum of one term per level, and the region of a direction vector is a product of the level regions, so each term can be pushed to its extreme on its own. So and are the minimum and the maximum of the left side over the whole region.
- Exactness over the reals. The region is convex, so the segment from the minimizing point to the maximizing point stays in it, and the left side takes every value in along it. So the test answers "maybe" for one subscript exactly when a real solution exists in the region.
- Soundness. An integer dependence with direction is a real point of the region (an integer gives ) that solves each equation, so the test answers "maybe".
- The search loses nothing. The region of a refined vector is inside the region of the coarser one, so a cut vector would also fail every refinement.
Proof: The Banerjee bounds are the exact real range of a subscript equation, so the test is soundfull
Setting
The loop nest has depth , and loop runs over the integers , with . One subscript pair gives the equation
where stands for the source iteration and for the sink iteration. For write and , so and .
For a direction , the region of level is the set of real pairs
where is no condition, is , is and is . For integers, is the same as , so every integer pair with in the bounds lies in . The region of a direction vector is the product .
The test computes, for each level, the pair of line L5 (the function level_bounds, lines L10 to L13), adds them in line L7, and answers "independent" in line L8 if or , or in line L6 if some region is empty.
Lemma 1 (each region is the convex hull of its vertices)
Let be the vertex list of the code:
| , if | |
| , if |
(dropping the index ). Then is the set of convex combinations of . For and the region is empty.
Proof. Each vertex lies in the region, and the region is convex (it is cut out by linear inequalities), so every convex combination of vertices lies in it. For the other inclusion, take in the region.
- . Put and if (if the region is the one point ). Then and . The four weights are nonnegative and sum to .
- . Then and with , or the one point if .
- . The three numbers , and are nonnegative in the region, and . If , they cannot all be nonnegative, so the region is empty. If , all three are , and the region is the one point . If , the vertices have equal to , and , in the order of the table. The point has the same , , as , because , , are affine functions and the weights sum to . Two points with the same and are equal, so this is .
- . Swap the roles of and in the previous case.
Lemma 2 (a linear function on a convex hull)
Let be a finite set of points and a linear function (plus a constant). On the set of convex combinations of , the minimum of is and the maximum is .
Proof. For with and , , a weighted mean of the values at the vertices. A weighted mean lies between the smallest and the largest of the values. Both are reached, at the vertices themselves.
Lemma 3 (the closed forms)
For each level and each direction with a nonempty region, the pair returned by level_bounds equals the minimum and the maximum of over .
Proof. By Lemmas 1 and 2 it is enough to compute the extremes of over .
- . The term over has the minimum if and if , which is in both cases, and the maximum . The term over has the minimum and the maximum . The four vertices are all combinations of and in , so the two terms reach their extremes on their own. The sums are the bounds of line L10.
- . On and , , with extremes and , as in line L11.
- . Let . Direct computation gives and . As , the minimum is , and the maximum is . Here and . These are the bounds of line L12.
- . Let . Then and . The minimum is and the maximum is , as in line L13.
Theorem 1 (the Banerjee bounds)
Let be a direction vector whose regions are all nonempty, and let and as computed in line L7. Then
and every value between and is taken by on .
Proof. The function is a sum in which the term depends only on the pair of level , and the region is a product of the sets . So a point of is any choice of one pair from each , and each term can be made as small as possible on its own: by Lemma 3, and the same for the maximum. Both are reached: let be the point that takes, at each level, the vertex where is smallest, and the same with the largest.
For a value with , put if and otherwise, and . Each level pair of is a convex combination of two points of the convex set , so . As is linear, .
The function real_witness builds exactly this point , with rational coordinates, and the tests check that it lies in the region and solves the equation.
Theorem 2 (exact over the reals, for one subscript)
For one subscript pair and a direction vector , the test answers "maybe" if and only if the equation has a real solution in .
Proof. If some region is empty, is empty, there is no solution, and line L6 answers "independent". Otherwise, by Theorem 1, the set of values of on is exactly the interval . So a solution exists if and only if , which is the negation of the condition of line L8.
Theorem 3 (soundness)
If there are integer vectors and in the loop bounds with for all and with all subscript pairs equal, the test answers "maybe". So "independent" proves that there is no dependence with direction vector .
Proof. The point lies in (integer gives ), and it solves the equation of each subscript. By Theorem 2 each subscript gives "maybe" in line L8, and no region is empty, so line L6 never fires. Line L9 answers "maybe".
Theorem 4 (the direction search loses nothing)
The function direction_vectors returns a set that contains the direction vector of every integer solution in the bounds, and equals the set of plain vectors for which the test says "maybe".
Proof. Each plain is reached from by replacing the first with a symbol, one level at a time, and the search follows this path unless the test says "independent" at a vector on it. Every vector on the path keeps the symbols of where it has symbols and has elsewhere, so , because . A smaller set gives a smaller range of , so if is outside the range for , it is outside for , and the test also says "independent" for . So the cut removes only vectors that the test rules out, and the leaves are exactly the plain vectors with "maybe". By Theorem 3, the direction vector of an integer solution gives "maybe", so it is in the set.
An example where the test says "maybe" and there is no integer solution
Take A[2*i] = A[3*i + 2] + 1 for . The equation is on , with , , , and .
- For : and . The value is inside, so the test says "maybe". The GCD test also says "maybe", since .
- Refined: for , , so the range is ; for , and the range is ; for , , and the range is . Only stays "maybe". A real witness is : it has , and .
- There is no integer solution in the bounds: forces to be even, so and . The written elements are and the read elements are .
So Theorem 2 is the best the test can do: it is exact over the reals, and the gap between real and integer solutions is where it says "maybe" with no dependence.
Cost
level_bounds takes arithmetic operations, so one subscript and one direction vector cost , and the test of subscripts costs . The direction search visits at most vectors, so it costs in the worst case.
Complexity
One call of LEVEL-BOUNDS costs operations, so a direction vector costs per subscript and for the reference. The search tests at most vectors, so time in the worst case, with a recursion stack of depth . In practice most branches are cut near the root, and nests are shallow. The test needs extra space for one direction vector.
Properties
- Conservative in one direction only. "Independent" is a proof; "maybe" is not a proof of dependence.
- Exact over the reals. For one subscript and a rectangular region, "maybe" holds exactly when a real solution exists (Theorem 2 of the proof).
- Not exact over the integers. It ignores divisibility, which the GCD test checks; the two are often run together on the same equation.
- One subscript at a time. Like the GCD test, it does not join the equations of a multidimensional reference.
- Direction aware. It is the classic test for computing direction vectors, because each direction has its own closed form.
Variants and improvements
- Trapezoidal bounds. Banerjee's book extends the bounds to loops whose bounds are affine in outer indices, such as triangular loops. The closed forms then include the coefficients of the bounds.
- Combined GCD and Banerjee test. Run the GCD test first on the equation for each direction vector; it is cheap and catches divisibility that the real bounds miss.
- Exact tests for simple subscripts. For subscripts with one index, the exact SIV tests of the practical tests replace the Banerjee test, and the Banerjee test is kept for the remaining MIV subscripts.
- Real elimination of the whole system. Fourier-Motzkin elimination decides real feasibility of all subscripts and bounds together, which catches coupled subscripts; the omega test makes it exact over the integers.
- Symbolic bounds. When a bound is a parameter , the extremes are affine functions of , and the test asks whether can lie between them for some .
When to use it
Use it for subscripts with several loop indices in a nest with known bounds, after the exact tests for single-index subscripts, and use the hierarchical search when the transformations need direction vectors (interchange, parallelization, vectorization). For exact answers, or for coupled subscripts that matter, use the omega test or a polyhedral library.
Pitfalls
- Using over the reals. The strict real triangle has the same closure as and gives wider bounds. The test must use , which is valid for integers and gives the closed forms above.
- Forgetting empty regions. For a loop with one iteration (), the directions and have no pairs; the test must answer "independent" for them, not compute bounds from a formula with .
- Reading "maybe" as "dependent". The example
A[2*i] = A[3*i + 2]shows a "maybe" without a dependence. - Mixing up the notation. On this page means that the sink index is larger. In the plus-zero-minus notation of the dragon book the same direction is written .
- A direction vector with first. A leaf such as is not a dependence from the source to the sink. It is a dependence from the sink to the source, and its direction is the reversed vector.
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/banerjee-test/. The authors are listed in prov/PS0MQH.jsonl.
"""The Banerjee test with direction vectors, for rectangular loop nests.
A loop nest has depth k, and loop l runs over the integers L_l..U_l, given as
bounds = [(L_1, U_1), ..., (L_k, U_k)]. A subscript is a tuple
(c0, c1, ..., ck) for c0 + c1*i1 + ... + ck*ik. The source reference uses
the indices i and the sink reference the indices j; a reference to an array
of m dimensions is a list of m subscripts.
The direction psi_l relates i_l and j_l: "<" is i_l < j_l, "=" is i_l = j_l,
">" is i_l > j_l and "*" is no constraint (Wolfe's notation).
banerjee_test returns "independent" when, for some subscript, the equation
sum(a_l i_l) - sum(b_l j_l) = b0 - a0 has no real solution in the region of
the bounds and the directions, and "maybe" otherwise.
"""
from fractions import Fraction
def pos(x: int) -> int:
"""The positive part x+ = max(x, 0)."""
return x if x > 0 else 0
def neg(x: int) -> int:
"""The negative part x- = max(-x, 0); x = x+ - x-."""
return -x if x < 0 else 0
def level_bounds(a: int, b: int, lo: int, hi: int, d: str):
"""Min and max of a*x - b*y over the real region of one level, or None.
The region is lo <= x, y <= hi with x = y for "=", x + 1 <= y for "<",
y + 1 <= x for ">" and no relation for "*". It is empty for "<" and ">"
when hi - lo < 1.
"""
n = hi - lo
if d == "*":
return (pos(a) * lo - neg(a) * hi - pos(b) * hi + neg(b) * lo, # @line L10
pos(a) * hi - neg(a) * lo - pos(b) * lo + neg(b) * hi)
if d == "=":
c = a - b
return (pos(c) * lo - neg(c) * hi, pos(c) * hi - neg(c) * lo) # @line L11
if n < 1:
return None
if d == "<":
v0 = (a - b) * lo - b
return (v0 - pos(neg(a) + b) * (n - 1), v0 + pos(pos(a) - b) * (n - 1)) # @line L12
v0 = (a - b) * lo + a
return (v0 - pos(pos(b) - a) * (n - 1), v0 + pos(a + neg(b)) * (n - 1)) # @line L13
def subscript_bounds(src, snk, bounds, psi=None):
"""Min and max of the left side of the dependence equation, or None."""
lo_sum, hi_sum = 0, 0 # @line L3
for l, (lo, hi) in enumerate(bounds): # @line L4
d = "*" if psi is None else psi[l]
r = level_bounds(src[l + 1], snk[l + 1], lo, hi, d) # @line L5
if r is None:
return None # @line L6
lo_sum += r[0] # @line L7
hi_sum += r[1]
return lo_sum, hi_sum
def banerjee_test(src, snk, bounds, psi=None) -> str:
"""The Banerjee test for two references and an optional direction vector."""
for f, g in zip(src, snk): # @line L1
c = g[0] - f[0] # @line L2
r = subscript_bounds(f, g, bounds, psi)
if r is None or c < r[0] or c > r[1]: # @line L8
return "independent"
return "maybe" # @line L9
def direction_vectors(src, snk, bounds) -> set:
"""All plain direction vectors that the Banerjee test does not rule out.
The search refines one "*" at a time, from the outermost level, and cuts
a branch as soon as the test says "independent".
"""
k = len(bounds)
out = set()
def refine(psi: tuple) -> None:
if banerjee_test(src, snk, bounds, psi) == "independent":
return
if "*" not in psi:
out.add(psi)
return
l = psi.index("*")
for d in "<=>":
refine(psi[:l] + (d,) + psi[l + 1:])
refine(("*",) * k)
return out
def level_vertices(lo: int, hi: int, d: str) -> list:
"""The vertices of the real region of one level, as (x, y) pairs."""
if d == "*":
return [(lo, lo), (lo, hi), (hi, lo), (hi, hi)]
if d == "=":
return [(lo, lo), (hi, hi)]
if hi - lo < 1:
return []
if d == "<":
return [(lo, lo + 1), (lo, hi), (hi - 1, hi)]
return [(lo + 1, lo), (hi, lo), (hi, hi - 1)]
def real_witness(src, snk, bounds, psi=None):
"""A real solution (x, y) of one subscript equation in the region, or None.
It joins the vertex that gives the minimum with the vertex that gives the
maximum at each level; the segment between the two points stays in the
region, and the left side runs over [lo, hi] along it.
"""
k = len(bounds)
c = snk[0] - src[0]
pmin, pmax = [], []
for l, (lo, hi) in enumerate(bounds):
d = "*" if psi is None else psi[l]
vs = level_vertices(lo, hi, d)
if not vs:
return None
val = lambda p: src[l + 1] * p[0] - snk[l + 1] * p[1] # noqa: E731
pmin.append(min(vs, key=val))
pmax.append(max(vs, key=val))
left = lambda ps: sum(src[l + 1] * ps[l][0] - snk[l + 1] * ps[l][1] for l in range(k)) # noqa: E731
lo_v, hi_v = left(pmin), left(pmax)
if c < lo_v or c > hi_v:
return None
t = Fraction(0) if hi_v == lo_v else Fraction(c - lo_v, hi_v - lo_v)
x = [pmin[l][0] + t * (pmax[l][0] - pmin[l][0]) for l in range(k)]
y = [pmin[l][1] + t * (pmax[l][1] - pmin[l][1]) for l in range(k)]
return x, y
if __name__ == "__main__":
# for i = 1 to 5: A[i] = A[i + 10]: the range of i - j is [-4, 4].
print(banerjee_test([(0, 1)], [(10, 1)], [(1, 5)])) # independent
# for i = 1 to 3: A[2*i] = A[3*i + 2]: maybe, only with ">".
print(banerjee_test([(0, 2)], [(2, 3)], [(1, 3)])) # maybe
print(sorted(direction_vectors([(0, 2)], [(2, 3)], [(1, 3)]))) # [('>',)]
banerjee_test.py 22 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/banerjee-test/. The authors are listed in prov/PS0MQH.jsonl.
"""Entry point named after the slug. The code is in banerjee_bounds.py,
because the tools treat a file whose name ends in _test as a test file."""
from banerjee_bounds import (
banerjee_test,
direction_vectors,
level_bounds,
level_vertices,
real_witness,
subscript_bounds,
)
__all__ = [
"banerjee_test",
"direction_vectors",
"level_bounds",
"level_vertices",
"real_witness",
"subscript_bounds",
]
test_banerjee_test.py 205 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/banerjee-test/. The authors are listed in prov/PS0MQH.jsonl.
import random
from fractions import Fraction
from itertools import product
from banerjee_test import (
banerjee_test,
direction_vectors,
level_bounds,
level_vertices,
real_witness,
subscript_bounds,
)
DIRS = "<=>*"
def holds(rel, a, b) -> bool:
return rel == "*" or (rel == "<" and a < b) or (rel == "=" and a == b) or (rel == ">" and a > b)
def sign(a, b) -> str:
return "<" if a < b else ("=" if a == b else ">")
def value(sub, x):
return sub[0] + sum(c * v for c, v in zip(sub[1:], x))
def box(bounds):
return list(product(*[range(lo, hi + 1) for lo, hi in bounds]))
def brute_pairs(src, snk, bounds, psi=None):
"""All integer pairs (i, j) in the box with equal subscripts and directions psi."""
pts = box(bounds)
out = []
for i in pts:
for j in pts:
if psi is not None and not all(holds(r, a, b) for r, a, b in zip(psi, i, j)):
continue
if all(value(f, i) == value(g, j) for f, g in zip(src, snk)):
out.append((i, j))
return out
def random_bounds(rng, k):
top = {1: 8, 2: 6, 3: 4}[k]
out = []
for _ in range(k):
lo = rng.randint(0, 2)
out.append((lo, rng.randint(lo, top)))
return out
def random_case(rng):
k = rng.randint(1, 3)
bounds = random_bounds(rng, k)
m = rng.randint(1, 2)
src, snk = [], []
for _ in range(m):
f = [rng.randint(-6, 6)] + [rng.randint(-4, 4) for _ in range(k)]
g = [rng.randint(-6, 6)] + [rng.randint(-4, 4) for _ in range(k)]
if rng.random() < 0.5:
i = [rng.randint(lo, hi) for lo, hi in bounds]
j = [rng.randint(lo, hi) for lo, hi in bounds]
g[0] += value(f, i) - value(g, j)
src.append(tuple(f))
snk.append(tuple(g))
psi = None if rng.random() < 0.3 else tuple(rng.choice(DIRS) for _ in range(k))
return src, snk, bounds, psi
def in_region(x, y, bounds, psi):
for l, (lo, hi) in enumerate(bounds):
d = "*" if psi is None else psi[l]
if not (lo <= x[l] <= hi and lo <= y[l] <= hi):
return False
if d == "<" and not x[l] + 1 <= y[l]:
return False
if d == ">" and not y[l] + 1 <= x[l]:
return False
if d == "=" and x[l] != y[l]:
return False
return True
def test_level_bounds_are_the_extremes_over_integer_points():
# The vertices of each region are integer points, so the closed forms
# must equal the min and max of a*x - b*y over the integer points.
for a in range(-5, 6):
for b in range(-5, 6):
for lo in range(0, 3):
for hi in range(lo, 8):
for d in DIRS:
pts = [(x, y) for x in range(lo, hi + 1) for y in range(lo, hi + 1) if holds(d, x, y)]
r = level_bounds(a, b, lo, hi, d)
if not pts:
assert r is None
assert level_vertices(lo, hi, d) == []
continue
vals = [a * x - b * y for x, y in pts]
assert r == (min(vals), max(vals)), (a, b, lo, hi, d)
vs = level_vertices(lo, hi, d)
assert all(p in pts for p in vs)
assert min(a * x - b * y for x, y in vs) == r[0]
assert max(a * x - b * y for x, y in vs) == r[1]
def test_subscript_bounds_over_the_whole_box():
rng = random.Random(11)
for _ in range(400):
k = rng.randint(1, 3)
bounds = random_bounds(rng, k)
f = tuple(rng.randint(-5, 5) for _ in range(k + 1))
g = tuple(rng.randint(-5, 5) for _ in range(k + 1))
psi = tuple(rng.choice(DIRS) for _ in range(k))
r = subscript_bounds(f, g, bounds, psi)
vals = [
sum(f[l + 1] * i[l] - g[l + 1] * j[l] for l in range(k))
for i in box(bounds)
for j in box(bounds)
if all(holds(d, a, b) for d, a, b in zip(psi, i, j))
]
if not vals:
assert r is None
else:
assert r == (min(vals), max(vals))
def test_never_independent_when_dependent():
rng = random.Random(12)
dependent = 0
for _ in range(1500):
src, snk, bounds, psi = random_case(rng)
sols = brute_pairs(src, snk, bounds, psi)
answer = banerjee_test(src, snk, bounds, psi)
if sols:
dependent += 1
assert answer == "maybe", (src, snk, bounds, psi, sols[0])
assert dependent > 300
def test_exact_over_the_reals_for_one_subscript():
# "maybe" comes with a real witness in the region; "independent" means
# that c is outside [lo, hi], the exact range of the left side.
rng = random.Random(13)
maybes = 0
for _ in range(3000):
k = rng.randint(1, 3)
bounds = random_bounds(rng, k)
f = tuple(rng.randint(-7, 7) for _ in range(k + 1))
g = tuple(rng.randint(-7, 7) for _ in range(k + 1))
psi = tuple(rng.choice(DIRS) for _ in range(k))
answer = banerjee_test([f], [g], bounds, psi)
w = real_witness(f, g, bounds, psi)
assert (w is not None) == (answer == "maybe")
if w is None:
continue
maybes += 1
x, y = w
assert all(isinstance(v, (int, Fraction)) for v in x + y)
assert in_region(x, y, bounds, psi)
assert value(f, x) == value(g, y)
assert maybes > 500
def test_direction_vectors_contain_every_true_direction():
rng = random.Random(14)
for _ in range(600):
src, snk, bounds, _ = random_case(rng)
k = len(bounds)
found = direction_vectors(src, snk, bounds)
true = {tuple(sign(a, b) for a, b in zip(i, j)) for i, j in brute_pairs(src, snk, bounds)}
assert true <= found
# The pruned search loses nothing against testing every plain vector.
every = {p for p in product("<=>", repeat=k) if banerjee_test(src, snk, bounds, p) == "maybe"}
assert found == every
def test_page_examples():
# A[i] = A[i + 10] for i = 1..5: gcd 1, but i - j lies in [-4, 4].
assert banerjee_test([(0, 1)], [(10, 1)], [(1, 5)]) == "independent"
# A[2*i] = A[3*i + 2] for i = 1..3: real range [-7, 3] holds 2.
assert subscript_bounds((0, 2), (2, 3), [(1, 3)]) == (-7, 3)
assert banerjee_test([(0, 2)], [(2, 3)], [(1, 3)]) == "maybe"
assert level_bounds(2, 3, 1, 3, "<") == (-7, -4)
assert level_bounds(2, 3, 1, 3, "=") == (-3, -1)
assert level_bounds(2, 3, 1, 3, ">") == (0, 3)
assert direction_vectors([(0, 2)], [(2, 3)], [(1, 3)]) == {(">",)}
assert brute_pairs([(0, 2)], [(2, 3)], [(1, 3)]) == []
x, y = real_witness((0, 2), (2, 3), [(1, 3)], (">",))
assert 2 * x[0] == 3 * y[0] + 2 and y[0] + 1 <= x[0]
# X[i + 2*j] = X[i + 2*j - 1] in a 1..4 by 1..4 nest (page example).
src, snk, bounds = [(0, 1, 2)], [(-1, 1, 2)], [(1, 4), (1, 4)]
assert subscript_bounds(src[0], snk[0], bounds, ("=", "*")) == (-6, 6)
assert subscript_bounds(src[0], snk[0], bounds, ("=", "<")) == (-6, -2)
assert subscript_bounds(src[0], snk[0], bounds, ("=", "=")) == (0, 0)
assert subscript_bounds(src[0], snk[0], bounds, ("=", ">")) == (2, 6)
assert subscript_bounds(src[0], snk[0], bounds, ("<", ">")) == (-1, 5)
found = direction_vectors(src, snk, bounds)
true = {tuple(sign(a, b) for a, b in zip(i, j)) for i, j in brute_pairs(src, snk, bounds)}
assert found == true == {("<", "="), ("<", ">"), (">", "<")}
History
Utpal Banerjee developed the bounds test at the University of Illinois in the second half of the 1970s, in David Kuck's group, for the Parafrase parallelizing compiler. The form with direction vectors, where each level has its own bounds for , and , was worked out in the 1980s and is the form in Wolfe's book and in Allen and Kennedy's book, which also give the hierarchical search. Banerjee's 1988 book collects the tests with their proofs, including the bounds for trapezoidal loops.
References
- Utpal Banerjee, Dependence Analysis for Supercomputing, Kluwer Academic Publishers, 1988, the bounds of a linear function over a rectangle with direction constraints, and the test built on them
- Randy Allen and Ken Kennedy, Optimizing Compilers for Modern Architectures, a Dependence-Based Approach, Morgan Kaufmann, 2002 (copyright 2001), Chapter 3 (the Banerjee inequality with direction vectors, the positive and negative parts, and the hierarchical search for direction vectors)
- Michael Wolfe, High Performance Compilers for Parallel Computing, Addison-Wesley, 1996, the chapter on dependence testing (the Banerjee bounds for each direction, and the search over the direction vector hierarchy)
- 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 Banerjee test as the general test for MIV subscripts, used after the exact single-index tests
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 | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag PS0MQH Edit this page History Report an error Markdown