Skip to content

Commit be58bf3

Browse files
committed
compiler: Restrict SubGrid Functions to the SubDomain of their Eq
With subdomain=, a Function on a SubGrid kept iterating its whole subsampled Dimension around the SubDomain loop, so every point accumulated over the entire SubDomain. Subsample the SubDomain's Dimension instead, bound it by the parent's actual extent, thickness included, and localize the thicknesses that only bound such a Dimension. Also keep time guarded in the subsampling pass: it now runs before cluster specialization, where buffering and other time-based lowering rely upon the guard.
1 parent 4d7af0e commit be58bf3

5 files changed

Lines changed: 91 additions & 12 deletions

File tree

‎devito/ir/equations/algorithms.py‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,20 @@ def _make_dimension_map(expr):
186186
# Some Relationals may be pure SymPy objects, thus lacking the SubDomain
187187
dimension_map = {}
188188
else:
189-
functions = [f for f in retrieve_functions(expr) if f._is_on_subdomain]
189+
functions = retrieve_functions(expr)
190190
for f in functions:
191-
dimension_map.update({d: expr.subdomain.dimension_map[d.root]
192-
for d in f.space_dimensions if d.is_Sub})
191+
if f._is_on_subdomain:
192+
dimension_map.update({d: expr.subdomain.dimension_map[d.root]
193+
for d in f.space_dimensions if d.is_Sub})
194+
195+
# A subsampled Function, e.g. on a SubGrid, is subsampled from the
196+
# SubDomain's Dimensions rather than from the whole Grid's
197+
for f in functions:
198+
dimension_map.update({
199+
d: d._rebuild(parent=expr.subdomain.dimension_map[d.parent])
200+
for d in f.dimensions
201+
if d.is_Conditional and d.parent in expr.subdomain.dimension_map
202+
})
193203

194204
return frozendict(dimension_map)
195205

‎devito/operator/operator.py‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -695,10 +695,14 @@ def _prepare_arguments(self, autotune=None, estimate_memory=False, **kwargs):
695695
# No need to do anything more if only checking the memory
696696
return args
697697

698-
# Process the symbols localized by MPI
699-
for p in self.parameters:
700-
if isinstance(p, (SubsamplingOffset, Thickness)):
701-
args.update(p._arg_values(grid=grid, **kwargs))
698+
# Process the symbols localized by MPI. A SubDimension's thicknesses are
699+
# needed even when they only bound another Dimension, e.g. a subsampled one
700+
localized = OrderedSet(*[p for p in self.parameters
701+
if isinstance(p, (SubsamplingOffset, Thickness))])
702+
localized.update(t for d in toposort if isinstance(d, SubDimension)
703+
for t in d.thickness)
704+
for p in localized:
705+
args.update(p._arg_values(grid=grid, **kwargs))
702706

703707
# Process Dimensions
704708
for d in reversed(toposort):

‎devito/passes/clusters/subsampling.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ def _is_strideable(cluster, d, guard):
113113
if d not in cluster.ispace.itdims:
114114
return False
115115

116+
# Time keeps its guard, which later passes (e.g. buffering) rely upon
117+
if d.is_Time:
118+
return False
119+
116120
# Skipping points is only sound if no dependence is carried along `d`;
117121
# otherwise the discarded iterations may be doing work that `d`'s
118122
# remaining iterations rely upon

‎devito/types/dimension.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,17 +1231,22 @@ def _arg_values(self, interval, grid=None, args=None, **kwargs):
12311231
vals = {}
12321232
offset = runtime.get(self.offset.name, 0) if self.offset else 0
12331233

1234-
# The first subsampled point at or after the parent's rank-local min...
1234+
# The parent's bounds, e.g. including a SubDimension's thickness
1235+
bounds = (self.parent.symbolic_min, self.parent.symbolic_max)
1236+
values = {s: runtime[s.name] for b in bounds for s in b.free_symbols
1237+
if runtime.get(s.name) is not None}
1238+
parent_min, parent_max = (b.subs(values) for b in bounds)
1239+
1240+
# The first subsampled point at or after the parent's min...
12351241
with suppress(KeyError, TypeError):
1236-
parent_min = runtime[self.parent.min_name]
1237-
vals[self.min_name] = math.ceil((parent_min - offset) / fact)
1242+
vals[self.min_name] = math.ceil((int(parent_min) - offset) / fact)
12381243

12391244
# ...and the last one at or before its max. Note `floor` rather than
12401245
# `ceil`: with e.g. `factor=3` and `x_M=11` the last subsampled point
12411246
# is `x=9`, hence index 3, while `ceil(11/3) == 4` would overshoot the
12421247
# Function by one
12431248
with suppress(KeyError, TypeError):
1244-
vals[self.max_name] = (runtime[self.parent.max_name] - offset) // fact
1249+
vals[self.max_name] = (int(parent_max) - offset) // fact
12451250
vals[self.max_name] = min(vals[self.max_name], runtime[self.max_name])
12461251

12471252
vals[self.symbolic_factor.name] = fact

‎tests/test_subsampling.py‎

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from devito import (
99
CondEq, ConditionalDimension, Constant, Eq, Function, Grid, Inc, Operator,
10-
SpaceDimension, SubGrid, TimeFunction, switchconfig
10+
SpaceDimension, SubDomain, SubGrid, TimeFunction, switchconfig
1111
)
1212
from devito.ir.iet import Conditional, Expression, FindNodes, Iteration
1313
from devito.symbolics import retrieve_indexed
@@ -347,6 +347,11 @@ def test_time_subsampling(self):
347347

348348
Operator([Eq(u.forward, u + 1.), Eq(usave, u)]).apply(time_M=nt-2)
349349

350+
# Time keeps its guard even where it is parallel
351+
op = Operator(Eq(usave, 1.))
352+
assert FindNodes(Conditional).visit(op)
353+
assert all(i.dim is not ts for i in FindNodes(Iteration).visit(op))
354+
350355
# time runs 0..nt-2, so only the slots whose time index is reached
351356
# are written; `u` holds the step count at that point
352357
nwritten = (nt - 2)//factor + 1
@@ -578,6 +583,22 @@ def test_with_openmp(self):
578583
assert np.array_equal(g.data, data[::2])
579584

580585

586+
class Middle(SubDomain):
587+
588+
name = 'middle'
589+
590+
def define(self, dimensions):
591+
# Thicknesses deliberately not multiples of the SubGrid factors
592+
return {d: ('middle', 2, 3) for d in dimensions}
593+
594+
595+
def middle_of(data):
596+
"""`data` zeroed outside the `Middle` SubDomain."""
597+
ret = np.zeros_like(data)
598+
ret[2:-3, 2:-3] = data[2:-3, 2:-3]
599+
return ret
600+
601+
581602
class TestSubGrid:
582603

583604
"""The `SubGrid` API."""
@@ -640,6 +661,22 @@ def test_copy_from_parent(self, factor):
640661
fx, fy = sub.factor
641662
assert np.array_equal(f.data, data[::fx, ::fy])
642663

664+
@pytest.mark.parametrize('factor', [2, 3, (2, 1), (3, 2)])
665+
def test_copy_on_subdomain(self, factor):
666+
"""Within a SubDomain, only the SubGrid points inside it are written."""
667+
grid = Grid(shape=(12, 12))
668+
sub = SubGrid(grid, factor=factor)
669+
670+
u = Function(name='u', grid=grid)
671+
f = Function(name='f', grid=sub, space_order=0)
672+
data = np.arange(1, 145, dtype=u.dtype).reshape(12, 12)
673+
u.data[:] = data
674+
675+
Operator(Eq(f, u, subdomain=Middle(grid=grid))).apply()
676+
677+
fx, fy = sub.factor
678+
assert np.array_equal(f.data, middle_of(data)[::fx, ::fy])
679+
643680
def test_copy_is_strided(self):
644681
"""
645682
No guard is emitted, every loop keeps a unit stride, and the Operator
@@ -980,6 +1017,25 @@ def test_copy_from_parent(self, mode, factor):
9801017
expected = glob[::factor, ::factor][g.local_indices]
9811018
assert np.array_equal(np.asarray(g.data), expected)
9821019

1020+
@pytest.mark.parallel(mode=[2, 4])
1021+
@pytest.mark.parametrize('factor', [2, 3])
1022+
def test_copy_on_subdomain(self, mode, factor):
1023+
"""The SubDomain bounds the SubGrid on every rank."""
1024+
n = 24
1025+
grid = Grid(shape=(n, n))
1026+
sub = SubGrid(grid, factor=factor)
1027+
1028+
u = Function(name='u', grid=grid)
1029+
g = Function(name='g', grid=sub, space_order=0)
1030+
1031+
glob = np.arange(1, n*n + 1, dtype=u.dtype).reshape(n, n)
1032+
u.data[:] = glob
1033+
1034+
Operator(Eq(g, u, subdomain=Middle(grid=grid))).apply()
1035+
1036+
expected = middle_of(glob)[::factor, ::factor][g.local_indices]
1037+
assert np.array_equal(np.asarray(g.data), expected)
1038+
9831039
@pytest.mark.parallel(mode=[2, 4])
9841040
@pytest.mark.parametrize('factor', [2, 3, 4])
9851041
def test_rank_boundary_off_the_subgrid(self, mode, factor):

0 commit comments

Comments
 (0)