Skip to content

Commit 4d7af0e

Browse files
committed
compiler: Lower Dimensions used as expressions to global indices
A bare Dimension in an expression compiled to the rank-local loop index, so Eq(u, x + y) restarted from zero on each MPI rank. Offset it by the new Grid.origin_isymbols, passed like origin_symbols. Dimensions indexing a Function stay rank-local, and the code generated without MPI is unchanged. Fixes #2214
1 parent a3362f4 commit 4d7af0e

4 files changed

Lines changed: 88 additions & 2 deletions

File tree

‎devito/ir/equations/algorithms.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ def _lower_exprs(expressions, subs):
156156
# Add dimensions map to the mapper in case dimensions are used
157157
# as an expression, i.e. Eq(u, x, subdomain=xleft)
158158
mapper.update(dimension_map)
159+
# Dimensions used as an expression are global indices, unlike loop indices
160+
mapper.update(_make_global_index_map(expr, dimension_map))
159161
# Add the user-supplied substitutions
160162
mapper.update(subs)
161163
# Apply mapper to expression
@@ -192,6 +194,21 @@ def _make_dimension_map(expr):
192194
return frozendict(dimension_map)
193195

194196

197+
def _make_global_index_map(expr, dimension_map):
198+
"""
199+
Map the Dimensions of a distributed Grid to the global indices they stand for
200+
when used as an expression, i.e. Eq(u, x + y) -> Eq(u[x, y], x + io_x + y + io_y).
201+
Dimensions indexing an Indexed are unaffected as the Indexed is replaced as a whole.
202+
"""
203+
if not isinstance(expr, Eq):
204+
return {}
205+
206+
grids = {f.grid.root for f in retrieve_functions(expr) if f.grid is not None}
207+
return {d: dimension_map.get(d, d) + o
208+
for grid in grids if grid.distributor.is_parallel
209+
for d, o in zip(grid.dimensions, grid.origin_isymbols, strict=True)}
210+
211+
195212
def concretize_subdims(exprs, **kwargs):
196213
"""
197214
Given a list of expressions, return a new list where all user-defined

‎devito/types/grid.py‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ def __init__(self, shape, extent=None, origin=None, dimensions=None,
212212
self._origin_symbols = tuple(Scalar(name=f'o_{d.name}', dtype=dtype,
213213
is_const=True)
214214
for d in self.dimensions)
215+
self._origin_isymbols = tuple(Scalar(name=f'io_{d.name}', dtype=np.int32,
216+
is_const=True)
217+
for d in self.dimensions)
215218

216219
# Sanity check
217220
assert (self.dim == len(self.origin) == len(self.extent) == len(self.spacing))
@@ -267,10 +270,20 @@ def origin_map(self):
267270
"""Map between origin symbols and their values."""
268271
return dict(zip(self.origin_symbols, self.origin, strict=True))
269272

273+
@property
274+
def origin_isymbols(self):
275+
"""Symbols representing the global index of the rank's first grid point."""
276+
return self._origin_isymbols
277+
278+
@property
279+
def origin_imap(self):
280+
"""Map between index origin symbols and their values."""
281+
return dict(zip(self.origin_isymbols, self.origin_ioffset, strict=True))
282+
270283
@property
271284
def origin_ioffset(self):
272285
"""Offset index of the local (per-process) origin from the domain origin."""
273-
grid_origin = [min(i) for i in self.distributor.glb_numb]
286+
grid_origin = [i.start for i in self.distributor.glb_slices.values()]
274287
assert len(grid_origin) == len(self.spacing)
275288
return DimensionTuple(*grid_origin, getters=self.dimensions)
276289

@@ -389,6 +402,7 @@ def _arg_names(self):
389402
ret = []
390403
ret.append(self.time_dim.spacing.name)
391404
ret.extend([i.name for i in self.origin_map])
405+
ret.extend([i.name for i in self.origin_imap])
392406
for i in self.spacing_map:
393407
try:
394408
ret.append(i.name)
@@ -410,6 +424,7 @@ def _arg_defaults(self):
410424

411425
# Grid origin
412426
args.update({k.name: v for k, v in self.origin_map.items()})
427+
args.update({k.name: v for k, v in self.origin_imap.items()})
413428

414429
# MPI-related objects
415430
if self.distributor.comm:

‎tests/test_caching.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ def test_solve(self, operate_on_empty_cache):
942942
# created by the finite difference (u.dt, u.dx2). We would have had
943943
# three extra references to u(t + dt), u(x - h_x) and u(x + h_x).
944944
# But this is not the case anymore!
945-
assert len(_SymbolCache) == 8
945+
assert len(_SymbolCache) == 9
946946
clear_cache()
947947
assert len(_SymbolCache) == 4
948948
clear_cache()

‎tests/test_mpi.py‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,60 @@ def check_halo_exchanges(op, exp0, exp1):
12021202
return calls, tloop
12031203

12041204

1205+
class TestGlobalIndex:
1206+
1207+
@pytest.mark.parallel(mode=[2, 4])
1208+
def test_dimension_as_expression(self, mode):
1209+
"""Dimensions in an expression are global indices (issue #2214)."""
1210+
grid = Grid(shape=(8, 8))
1211+
x, y = grid.dimensions
1212+
f = Function(name='f', grid=grid)
1213+
1214+
Operator(Eq(f, 10*x + y))()
1215+
1216+
check = 10*np.arange(8)[:, None] + np.arange(8)
1217+
assert np.all(f.data[:] == check[f.local_indices])
1218+
1219+
@pytest.mark.parallel(mode=[2, 4])
1220+
def test_dimension_as_expression_subdomain(self, mode):
1221+
"""Same as above, but restricted to a SubDomain."""
1222+
grid = Grid(shape=(8, 8))
1223+
x, y = grid.dimensions
1224+
f = Function(name='f', grid=grid)
1225+
1226+
Operator(Eq(f, 10*x + y, subdomain=grid.interior))()
1227+
1228+
check = 10*np.arange(8)[:, None] + np.arange(8)
1229+
check[[0, -1], :] = check[:, [0, -1]] = 0
1230+
assert np.all(f.data[:] == check[f.local_indices])
1231+
1232+
@pytest.mark.parallel(mode=[2])
1233+
def test_dimension_indexing_unaffected(self, mode):
1234+
"""Dimensions indexing a Function remain rank-local."""
1235+
grid = Grid(shape=(8,))
1236+
x = grid.dimensions[0]
1237+
f = Function(name='f', grid=grid, space_order=1)
1238+
g = Function(name='g', grid=grid)
1239+
1240+
f.data[:] = np.arange(8)
1241+
Operator(Eq(g, f.subs(x, x + 1) + x))()
1242+
1243+
# The last point reads a zero from the physical boundary halo
1244+
check = 2*np.arange(8) + 1
1245+
check[-1] = 7
1246+
assert np.all(g.data[:] == check[g.local_indices])
1247+
1248+
def test_serial_code_unchanged(self):
1249+
"""No index offset is generated without MPI."""
1250+
grid = Grid(shape=(4, 4))
1251+
x, y = grid.dimensions
1252+
f = Function(name='f', grid=grid)
1253+
1254+
op = Operator(Eq(f, x + y))
1255+
1256+
assert not any(p.name.startswith('io_') for p in op.parameters)
1257+
1258+
12051259
class TestHaloWrites:
12061260

12071261
@pytest.mark.parallel(mode=[1, 2])

0 commit comments

Comments
 (0)