Skip to content

Commit 9956264

Browse files
committed
tests: Add to specialization tests
1 parent 32d75cd commit 9956264

2 files changed

Lines changed: 41 additions & 9 deletions

File tree

tests/test_pickle.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import cloudpickle as pickle1
55
import numpy as np
66
import pytest
7+
import sympy
78
from sympy import Symbol
89

910
from devito import (
@@ -14,6 +15,7 @@
1415
from devito.data import LEFT, OWNED
1516
from devito.finite_differences.tools import centered, direct, left, right, transpose
1617
from devito.ir import Backward, Forward, GuardBound, GuardBoundNext
18+
from devito.ir.iet.visitors import Specializer
1719
from devito.ir.support.guards import GuardFactorEq
1820
from devito.mpi.halo_scheme import Halo
1921
from devito.mpi.routines import (
@@ -756,6 +758,28 @@ def test_unjitted_operator(self, pickle):
756758

757759
assert str(op) == str(new_op)
758760

761+
def test_specialized_operator(self, pickle):
762+
grid = Grid(shape=(3, 3))
763+
x_m, y_m = [d.symbolic_min for d in grid.dimensions]
764+
765+
f = Function(name='f', grid=grid)
766+
767+
op = Operator(Eq(f, f + 1))
768+
769+
pkl_op = pickle.dumps(op)
770+
new_op = pickle.loads(pkl_op)
771+
772+
mapper = {x_m: sympy.S.Zero, y_m: sympy.S.Zero}
773+
774+
# Check pickled operators can be specialized
775+
op0 = Specializer(mapper).visit(new_op)
776+
777+
# Check that specialized operators can be pickled
778+
pkl_op0 = pickle.dumps(op0)
779+
new_op0 = pickle.loads(pkl_op0)
780+
781+
assert str(op0) == str(new_op0)
782+
759783
def test_operator_function(self, pickle):
760784
grid = Grid(shape=(3, 3, 3))
761785
f = Function(name='f', grid=grid)

tests/test_specialization.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,20 @@ def test_sizes(self):
161161
assert "const int x_fsz0 = 11;" in str(op1.ccode)
162162
assert "const int y_fsz0 = 11;" in str(op1.ccode)
163163

164-
# TODO: Should strides get specialized? If so, how?
164+
def test_blocking(self):
165+
grid = Grid(shape=(11, 11, 11))
166+
f = TimeFunction(name='f', grid=grid, space_order=4)
167+
168+
op0 = Operator(Eq(f.forward, f.laplace))
169+
170+
block_params = [p for p in op0.parameters if 'blk' in str(p)]
171+
mapper = {p: sympy.Integer(32) for p in block_params}
172+
173+
op1 = Specializer(mapper).visit(op0)
174+
175+
assert "blk0_size" not in str(op1.ccode)
176+
assert "x0_blk0 += 32" in str(op1.ccode)
177+
assert "y0_blk0 += 32" in str(op1.ccode)
165178

166179
def test_apply_basic(self):
167180
"""
@@ -190,11 +203,6 @@ def test_apply_basic(self):
190203

191204
assert np.all(check == f.data)
192205

193-
# TODO: Need a test to check that block sizes can be specialized
194-
# TODO: Need to test that tile sizes can be specialized
195-
# TODO: Test pickling followed by specialization
196-
# TODO: Test specialized operators can be pickled
197-
198206

199207
class TestApply:
200208
"""Tests for specialization of operators at apply time"""
@@ -311,9 +319,9 @@ def gaussian_bump(x, y, x0=0, y0=0, sigma=1):
311319
assert np.all(np.isclose(check, u.data))
312320

313321
if source:
314-
assert np.isclose(np.linalg.norm(u.data), 21.698477)
322+
assert np.isclose(np.linalg.norm(u.data), 21.698477, atol=1e-4, rtol=0)
315323
else:
316-
assert np.isclose(np.linalg.norm(u.data), 10.793053)
324+
assert np.isclose(np.linalg.norm(u.data), 10.793053, atol=1e-4, rtol=0)
317325

318326
@pytest.mark.parametrize('specialize',
319327
[('x_m',),
@@ -374,4 +382,4 @@ def test_elastic(self, specialize):
374382
norms = (1.333946, 0.4931774, 1.333946, 1.1619346, 1.1619346)
375383

376384
for field, norm in zip(fields, norms, strict=True):
377-
assert np.isclose(np.linalg.norm(field.data), norm)
385+
assert np.isclose(np.linalg.norm(field.data), norm, atol=1e-4, rtol=0)

0 commit comments

Comments
 (0)