Skip to content

Commit 448e7da

Browse files
authored
mpi: Fix halos of SubDomain Functions next to empty ranks (#3099)
* mpi: Fix halos of SubDomain Functions next to empty ranks * misc: Revert to explicit copy * compiler: Remove input search for MPINeighborhood * compiler: Refactor MPINeighborhood._arg_values * misc: Improve comments * misc: Fixup arg_values oversight for distributor
1 parent 00e78a5 commit 448e7da

7 files changed

Lines changed: 67 additions & 15 deletions

File tree

‎devito/core/autotuning.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def autotune(operator, args, level, mode):
6060
for p in operator.parameters:
6161
if isinstance(p, MPINeighborhood):
6262
at_args.update(
63-
MPINeighborhood(p.neighborhood)._arg_values()
63+
MPINeighborhood(p.neighborhood, name=p.name)._arg_values()
6464
)
6565
for i in p.fields:
6666
setattr(at_args[p.name]._obj, i, MPI.PROC_NULL)

‎devito/data/decomposition.py‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,11 @@ def reshape(self, *args):
511511
512512
>>> d.reshape(slice(4))
513513
Decomposition([0,2], [3,3], <<[]>>, [])
514+
515+
Extensions are attached to the outermost non-empty subdomains
516+
517+
>>> Decomposition([[], [0, 1], [2, 3], []], 1).reshape(1, 1)
518+
Decomposition([], <<[0,2]>>, [3,5], [])
514519
"""
515520

516521
if len(args) == 1:
@@ -547,7 +552,9 @@ def reshape(self, *args):
547552

548553
# Handle left extension/reduction
549554
if nleft > 0:
550-
items = [np.concatenate([np.arange(-nleft, 0), items[0]])] + items[1:]
555+
# Extend the first non-empty subdomain, as empty ones own no halo
556+
i = next((i for i, sd in enumerate(items) if sd.size), 0)
557+
items[i] = np.concatenate([np.arange(-nleft, 0), items[i]])
551558
elif nleft < 0:
552559
n = 0
553560
for i, sd in enumerate(list(items)):
@@ -558,8 +565,10 @@ def reshape(self, *args):
558565

559566
# Handle right extension/reduction
560567
if nright > 0:
568+
# Extend the last non-empty subdomain, as empty ones own no halo
569+
i = max((i for i, sd in enumerate(items) if sd.size), default=-1)
561570
extension = np.arange(self.glb_max + 1, self.glb_max + 1 + nright)
562-
items = items[:-1] + [np.concatenate([items[-1], extension])]
571+
items[i] = np.concatenate([items[i], extension])
563572
elif nright < 0:
564573
n = 0
565574
for i, sd in enumerate(reversed(list(items))):

‎devito/mpi/distributed.py‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,14 @@ def neighborhood(self):
705705

706706
return ret
707707

708+
@cached_property
709+
def _obj_neighborhood(self):
710+
# The neighborhood is determined by the SubDomain thicknesses, hence the
711+
# name. E.g., `nb_x0_7` for `{x: ('middle', 0, 7), y: y}`
712+
name = ''.join(f'_{d.root.name}{d.ltkn.value}_{d.rtkn.value}'
713+
for d in self.dimensions if d.is_Sub)
714+
return MPINeighborhood(self.neighborhood, name=f'nb{name}')
715+
708716
@cached_property
709717
def rank_populated(self):
710718
"""Constant symbol for a switch indicating that data is allocated on this rank"""
@@ -852,14 +860,15 @@ def _arg_values(self, *args, **kwargs):
852860
class MPINeighborhood(CompositeObject):
853861

854862
__rargs__ = ('neighborhood',)
863+
__rkwargs__ = ('name',)
855864

856-
def __init__(self, neighborhood):
865+
def __init__(self, neighborhood, name='nb'):
857866
self._neighborhood = neighborhood
858867

859868
self._entries = [i for i in neighborhood if isinstance(i, tuple)]
860869

861870
fields = [(''.join(j.name[0] for j in i), c_int) for i in self.entries]
862-
super().__init__('nb', 'neighborhood', fields)
871+
super().__init__(name, 'neighborhood', fields)
863872

864873
@property
865874
def entries(self):
@@ -894,12 +903,11 @@ def _arg_defaults(self):
894903
return values
895904

896905
def _arg_values(self, *args, **kwargs):
897-
grid = kwargs.get('grid')
898-
# Update `nb` based on object attached to `grid`
899-
if grid is not None:
900-
return grid.distributor._obj_neighborhood._arg_defaults()
901-
else:
902-
return self._arg_defaults()
906+
# Use `nb` as provided by the (possibly overriding) Grid or SubDomain.
907+
# E.g., an override may be defined on a new Grid
908+
if self.name in kwargs.get('args', {}):
909+
return {}
910+
return self._arg_defaults()
903911

904912

905913
class CustomTopology(DimensionTuple):

‎devito/mpi/routines.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1286,7 +1286,7 @@ def _arg_defaults(self, alias=None, args=None):
12861286
super()._arg_defaults(alias, args=args)
12871287

12881288
f = alias or self.target.c0
1289-
neighborhood = args.grid.distributor.neighborhood
1289+
neighborhood = f.grid.distributor.neighborhood
12901290

12911291
for i, halo in enumerate(self.halos):
12921292
entry = self.value[i]

‎devito/types/grid.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,10 +412,10 @@ def _arg_defaults(self):
412412
args.update({k.name: v for k, v in self.origin_map.items()})
413413

414414
# MPI-related objects
415-
if self.distributor.is_parallel:
415+
if self.distributor.comm:
416416
distributor = self.distributor
417417
args[distributor._obj_comm.name] = distributor._obj_comm.value
418-
args[distributor._obj_neighborhood.name] = distributor._obj_neighborhood.value
418+
args.update(distributor._obj_neighborhood._arg_defaults())
419419

420420
return args
421421

@@ -731,12 +731,18 @@ def comm(self):
731731

732732
def _arg_values(self, **kwargs):
733733
try:
734-
return self.grid._arg_values(**kwargs)
734+
values = self.grid._arg_values(**kwargs)
735735
except AttributeError as e:
736736
raise AttributeError(
737737
f'{self} is not attached to a Grid and has no _arg_values'
738738
) from e
739739

740+
# The SubDomain neighborhood, used by the Functions defined on it
741+
if self.distributor.comm:
742+
values.update(self.distributor._obj_neighborhood._arg_values(**kwargs))
743+
744+
return values
745+
740746

741747
class SubDomain(AbstractSubDomain):
742748

‎tests/test_pickle.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,10 @@ def test_mpi_objects(self, pickle, mode):
935935
assert obj.pname == new_obj.pname
936936
assert obj.pfields == new_obj.pfields
937937

938+
obj = obj._rebuild(name='nbsd')
939+
new_obj = pickle.loads(pickle.dumps(obj))
940+
assert new_obj.name == 'nbsd'
941+
938942
# Communicator
939943
obj = grid.distributor._obj_comm
940944
pkl_obj = pickle.dumps(obj)

‎tests/test_subdomains.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,6 +1792,31 @@ def define(self, dimensions):
17921792
assert np.all(h.data == h_check[slices])
17931793
assert np.all(i.data == i_check)
17941794

1795+
@pytest.mark.parallel(mode=[(2, 'basic'), (2, 'diag'), (2, 'diag2')])
1796+
def test_halo_empty_neighbor_mpi(self, mode):
1797+
"""
1798+
Check that halo exchanges of Functions on SubDomains skip neighboring
1799+
ranks holding none of the SubDomain, also alongside Grid Functions.
1800+
"""
1801+
grid = Grid(shape=(11, 11), extent=(10., 10.), topology=(2, 1))
1802+
# Only held by rank 0
1803+
reduced_domain = ReducedDomain(('middle', 0, 7), None, grid=grid)
1804+
1805+
f = Function(name='f', grid=reduced_domain, space_order=2)
1806+
g = Function(name='g', grid=grid, space_order=2)
1807+
fx = Function(name='fx', grid=grid)
1808+
gx = Function(name='gx', grid=grid)
1809+
1810+
# Sentinel, as nothing should be exchanged into the halo of `f`
1811+
f.data_with_halo[:] = -1
1812+
1813+
Operator([Eq(f, 1), Eq(g, 1),
1814+
Eq(fx, f.dx, subdomain=reduced_domain), Eq(gx, g.dx)]).apply()
1815+
1816+
assert (np.count_nonzero(f.data_with_halo == -1) ==
1817+
f.data_with_halo.size - f.data.size)
1818+
assert np.all(gx.data[1:-1] == 0)
1819+
17951820
def set_indices(data):
17961821
"""
17971822
Set up individual indices for indexing/slicing check.

0 commit comments

Comments
 (0)