Skip to content

Commit d6cb99d

Browse files
committed
add some tests for dependencies
1 parent 7d3e749 commit d6cb99d

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

test/test_dependencies.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import sys
2+
3+
import numpy as np
4+
import pytest
5+
6+
import pyopencl as cl
7+
from pyopencl.tools import (
8+
pytest_generate_tests_for_pyopencl as pytest_generate_tests, # noqa
9+
)
10+
11+
import loopy as lp
12+
from loopy.kernel.dependency import (
13+
add_lexicographic_happens_after,
14+
reduce_strict_ordering,
15+
)
16+
17+
18+
def test_no_dependency():
19+
t_unit = lp.make_kernel(
20+
"{ [i,j] : 0 <= i, j < n}",
21+
"""
22+
a[i,j] = 2*i {id=source}
23+
b[i,j] = a[i,j] {id=sink}
24+
""",
25+
)
26+
27+
t_unit = add_lexicographic_happens_after(t_unit)
28+
t_unit = reduce_strict_ordering(t_unit)
29+
knl = t_unit.default_entrypoint
30+
31+
assert len(knl.id_to_insn["sink"].happens_after) == 0
32+
33+
34+
@pytest.mark.parametrize("img_size", [(512, 512), (1920, 1080)])
35+
def test_3x3_blur(ctx_factory, img_size):
36+
ctx = ctx_factory()
37+
queue = cl.CommandQueue(ctx)
38+
39+
hx, hy = img_size
40+
img = np.random.default_rng(seed=42).random(size=(hx, hy))
41+
42+
knl = lp.make_kernel(
43+
"{ [x, y]: 0 <= x < hx and 0 <= y < hy }",
44+
"""
45+
img_(i, j) := img[i+1, j+1]
46+
blurx(i, j) := img_(i-1, j) + img_(i, j) + img_(i+1, j)
47+
48+
out[x, y] = blurx(x, y-1) + blurx(x, y) + blurx(x, y+1)
49+
""",
50+
[
51+
lp.GlobalArg("out",
52+
dtype=np.float64,
53+
shape=(hx, hy),
54+
is_output=True),
55+
lp.GlobalArg("img",
56+
dtype=np.float64,
57+
shape=(hx, hy))
58+
]
59+
)
60+
61+
knl = lp.fix_parameters(knl, hx=hx-2, hy=hy-2)
62+
63+
bsize = 4
64+
knl = lp.split_iname(knl, "x", bsize, inner_tag="vec", outer_tag="for")
65+
knl = lp.split_iname(knl, "y", bsize, inner_tag="for", outer_tag="g.0")
66+
knl = lp.precompute(
67+
knl,
68+
"blurx",
69+
sweep_inames="x_inner, y_inner",
70+
precompute_outer_inames="x_outer, y_outer",
71+
precompute_inames="bx, by"
72+
)
73+
74+
knl = lp.prioritize_loops(knl, "y_outer, x_outer, y_inner, x_inner")
75+
knl = lp.expand_subst(knl)
76+
77+
knl = add_lexicographic_happens_after(knl)
78+
knl = reduce_strict_ordering(knl)
79+
80+
_, out = knl(queue, img=img)
81+
blurx = np.zeros_like(img)
82+
out_np = np.zeros_like(img)
83+
for x in range(hx-2):
84+
blurx[x, :] = img[x, :] + img[x+1, :] + img[x+2, :]
85+
for y in range(hy-2):
86+
out_np[:, y] = blurx[:, y] + blurx[:, y+1] + blurx[:, y+2]
87+
88+
import numpy.linalg as la
89+
assert (la.norm(out[0] - out_np) / la.norm(out_np)) <= 1e-14
90+
91+
92+
if __name__ == "__main__":
93+
if len(sys.argv) > 1:
94+
exec(sys.argv[1])
95+
else:
96+
from pytest import main
97+
98+
main([__file__])

0 commit comments

Comments
 (0)