Skip to content

Commit a438cb4

Browse files
EdCauntclaude
andcommitted
dsl: Read the halo past the Grid edges with halo=0
A halo=0 derivative zeroed its argument outside the SubDomain, including the halo past an edge of the Grid that the SubDomain touches. That halo is read without halo=0, e.g. as written by a free-surface mirror, so the bound on a Grid edge is no longer tested. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 55fa9b8 commit a438cb4

4 files changed

Lines changed: 89 additions & 8 deletions

File tree

‎devito/finite_differences/derivative.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,8 @@ def transpose(self):
475475
def halo(self):
476476
"""
477477
None if the argument is read everywhere, 0 if it is treated as zero
478-
outside the SubDomain of the equation the Derivative belongs to.
478+
outside the SubDomain of the equation the Derivative belongs to, within
479+
the Grid. The halo past an edge of the Grid is read as with None.
479480
"""
480481
return self._halo
481482

‎devito/types/grid.py‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,9 @@ def _indicator(self, dim, offset):
948948
1 if the point `offset` points away from the current one along `dim` lies
949949
in this SubDomain, 0 otherwise, computed without branching. Always 1 if
950950
this SubDomain spans all of `dim`. With domain decomposition, the test
951-
is against the global bounds, which may lie beyond this rank.
951+
is against the global bounds, which may lie beyond this rank. A bound on
952+
an edge of the Grid is not tested, so that the halo past it is read as
953+
it would be without this SubDomain, e.g. as written by a mirror.
952954
953955
Parameters
954956
----------
@@ -962,8 +964,23 @@ def _indicator(self, dim, offset):
962964
return sympy.S.One
963965
point = dim + offset
964966
lower, upper = maybe_subdim._glb_interval
965-
return (Max(0, Min(1, point - lower + 1)) *
966-
Max(0, Min(1, upper - point + 1)))
967+
968+
# The points between this SubDomain and each Grid edge
969+
n = self.grid.shape[self.grid.dimensions.index(dim)]
970+
lgap, rgap = (i.value for i in maybe_subdim.thickness)
971+
if maybe_subdim.is_left:
972+
lgap, rgap = 0, n - lgap
973+
elif maybe_subdim.is_right:
974+
lgap, rgap = n - rgap, 0
975+
# Both bounds of an empty SubDomain are tested, so no point is in it
976+
empty = lgap + rgap >= n
977+
978+
indicator = sympy.S.One
979+
if lgap > 0 or empty:
980+
indicator *= Max(0, Min(1, point - lower + 1))
981+
if rgap > 0 or empty:
982+
indicator *= Max(0, Min(1, upper - point + 1))
983+
return indicator
967984

968985
def _restrict(self, expr, exclude=()):
969986
"""

‎tests/test_derivatives.py‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,6 +1888,59 @@ def test_forward(self):
18881888
assert np.linalg.norm(ref.data) > 0
18891889
assert np.allclose(out.data, ref.data, rtol=1e-12, atol=1e-12)
18901890

1891+
@pytest.mark.parametrize('region, inside', [
1892+
(('left', 6), slice(None, 6)), (('right', 6), slice(18, None)),
1893+
(('middle', 0, 6), slice(None, 18)), (('middle', 6, 0), slice(6, None)),
1894+
(('left', 24), slice(None)), (('right', 24), slice(None))
1895+
])
1896+
@pytest.mark.parametrize('transpose', [False, True])
1897+
def test_grid_edge(self, region, inside, transpose):
1898+
"""
1899+
The halo past an edge of the Grid is read as without `halo=0`, e.g. as
1900+
written by a mirror, while the argument is still zero past the other bound.
1901+
"""
1902+
grid = Grid(shape=(24,), extent=(23.,), dtype=np.float64)
1903+
subdomain = Region('s', {'x': region}, grid=grid)
1904+
kwargs = {'grid': grid, 'space_order': 4, 'dtype': np.float64}
1905+
g = Function(name='g', **kwargs)
1906+
g_zero = Function(name='g_zero', **kwargs)
1907+
out = Function(name='out', **kwargs)
1908+
ref = Function(name='ref', **kwargs)
1909+
1910+
# Non-zero everywhere, halo included. The reference keeps only S and the
1911+
# halo past the Grid edge it touches
1912+
g.data_with_halo[:] = np.random.default_rng(2).normal(size=g.shape_with_halo)
1913+
h = g._size_halo[grid.dimensions[0]].left
1914+
kept = slice(None if inside.start is None else inside.start + h,
1915+
None if inside.stop is None else inside.stop + h)
1916+
g_zero.data_with_halo[kept] = g.data_with_halo[kept]
1917+
1918+
deriv, deriv_ref = g.dx(halo=0), g_zero.dx
1919+
if transpose:
1920+
deriv, deriv_ref = deriv.T, deriv_ref.T
1921+
# The reference everywhere, as the `halo=0` derivative extends past S
1922+
Operator([Eq(out, deriv, subdomain=subdomain), Eq(ref, deriv_ref)]).apply()
1923+
1924+
assert np.linalg.norm(ref.data) > 0
1925+
assert np.allclose(out.data, ref.data, rtol=1e-12, atol=1e-12)
1926+
1927+
@pytest.mark.parametrize('region', [('left', 0), ('right', 0), ('middle', 0, 24),
1928+
('middle', 24, 0)])
1929+
@pytest.mark.parametrize('transpose', [False, True])
1930+
def test_empty_subdomain(self, region, transpose):
1931+
"""An empty SubDomain does not read the halo past the Grid edges"""
1932+
grid = Grid(shape=(24,), extent=(23.,), dtype=np.float64)
1933+
subdomain = Region('s', {'x': region}, grid=grid)
1934+
kwargs = {'grid': grid, 'space_order': 4, 'dtype': np.float64}
1935+
g = Function(name='g', **kwargs)
1936+
out = Function(name='out', **kwargs)
1937+
g.data_with_halo[:] = np.random.default_rng(2).normal(size=g.shape_with_halo)
1938+
1939+
deriv = g.dx(halo=0).T if transpose else g.dx(halo=0)
1940+
Operator(Eq(out, deriv, subdomain=subdomain)).apply()
1941+
1942+
assert np.all(out.data == 0)
1943+
18911944
def test_default_unchanged(self):
18921945
"""
18931946
Without `halo=0`, `.T` in a SubDomain equation is the full-grid transpose

‎tests/test_mpi.py‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3508,12 +3508,14 @@ def test_subdim_thickness_spanning_rank(self, mode):
35083508
@pytest.mark.parametrize('expand', [True, False])
35093509
@pytest.mark.parametrize('region', [('middle', 6, 6), ('middle', 16, 16),
35103510
('middle', 15, 17), ('left', 17),
3511-
('right', 18)])
3511+
('right', 18), ('middle', 0, 17),
3512+
('middle', 18, 0)])
35123513
def test_halo_zero_subdomain(self, region, expand, mode):
35133514
"""
35143515
Derivatives with `halo=0` treat their argument as zero outside the global
3515-
SubDomain, not outside its part on each rank. The rank edges lie at 16,
3516-
32 and 48, within the stencil radius of some of the SubDomain edges.
3516+
SubDomain, not outside its part on each rank, and read the halo past the
3517+
Grid edges. The rank edges lie at 16, 32 and 48, within the stencil radius
3518+
of some of the SubDomain edges.
35173519
"""
35183520
class Region(SubDomain):
35193521
name = 'region'
@@ -3532,7 +3534,15 @@ def functions(grid):
35323534

35333535
f0, o0, g0 = functions(serial)
35343536
f1, o1, g1 = functions(grid)
3535-
f0.data[:] = f1.data[:] = np.random.default_rng(0).normal(size=grid.shape)
3537+
rng = np.random.default_rng(0)
3538+
f0.data[:] = f1.data[:] = rng.normal(size=grid.shape)
3539+
3540+
# Non-zero halos past the Grid edges, read where the SubDomain touches them
3541+
h = f0._size_halo[grid.dimensions[0]].left
3542+
left, right = rng.normal(size=h), rng.normal(size=h)
3543+
for f in (f0, f1):
3544+
# Globally indexed, so on the edge ranks only
3545+
f.data_with_halo[:h], f.data_with_halo[-h:] = left, right
35363546

35373547
# Compiled for the serial Grid, applied to both
35383548
op = Operator([Eq(o0, f0.dx(halo=0), subdomain=subdomain),

0 commit comments

Comments
 (0)