Skip to content

Commit cd37c8a

Browse files
committed
mpi: Patch bug where sparse interpolations with footprints spanning multiple ranks would result in incomplete sums being calculated with MPI
1 parent 8f24c29 commit cd37c8a

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

‎devito/types/sparse.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,9 +706,22 @@ def _dist_data_gather(self, data):
706706
self._comm.Alltoallv([data, rcount, rdisp, self._mpitype],
707707
[gathered, scount, sdisp, self._mpitype])
708708

709+
# A point is sent to every rank its support touches, but a rank only
710+
# grazed by the support reads up to `r` points into its halo, and so it
711+
# may compute a partial value. Keep the value computed by the rank owning
712+
# the point, whose reads all fall within that range
713+
shape = np.array(self.grid.shape)
714+
index = np.clip(self._coords_indices, 0, shape - 1)
715+
owners = self.grid.distributor.glb_to_rank(index) or {}
716+
keep = np.array(flatten(np.isin(dmap[r], owners.get(r, ())).tolist()
717+
for r in sorted(dmap)), dtype=bool)
718+
mask = list(mask)
719+
mask[self._sparse_position] = mask[self._sparse_position][keep]
720+
709721
# Unpack data values so that they follow the expected storage layout
710722
gathered = np.ascontiguousarray(np.transpose(gathered, self._dist_reorder_mask))
711-
self._data[mask] = gathered[:]
723+
self._data[tuple(mask)] = np.take(gathered, np.flatnonzero(keep),
724+
axis=self._sparse_position)
712725

713726
def _dist_subfunc_gather(self, sfuncd, subfunc):
714727
with suppress(AttributeError):

‎tests/test_mpi.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2945,6 +2945,33 @@ def test_interpolation_sinc(self, mode):
29452945

29462946
assert np.allclose(sf.data, [2., 2., 3., 3.][grid.distributor.myrank])
29472947

2948+
@pytest.mark.parametrize('r', [2, 4])
2949+
@pytest.mark.parallel(mode=[2, 4])
2950+
def test_interpolation_sinc_boundary(self, r, mode):
2951+
"""
2952+
Points whose support only grazes a neighbouring rank are sent there too,
2953+
where they are only partially evaluated; the gathered value must be the
2954+
complete one, computed by the rank owning the point.
2955+
"""
2956+
shape = (16, 16)
2957+
coords = np.array([(4.5, 6.5), (5.3, 9.6), (6.5, 4.5), (6.2, 6.2),
2958+
(7.5, 12.7), (9.6, 5.3), (12.7, 7.5), (2.2, 2.2)])
2959+
data = np.random.default_rng(0).random(shape, dtype=np.float32)
2960+
2961+
def interpolate(grid):
2962+
f = Function(name='f', grid=grid, space_order=2*r)
2963+
f.data[:] = data
2964+
sf = SparseFunction(name='sf', grid=grid, npoint=len(coords),
2965+
coordinates=coords, interpolation='sinc', r=r)
2966+
Operator(sf.interpolate(expr=f)).apply()
2967+
return sf
2968+
2969+
sf = interpolate(Grid(shape=shape, extent=(15., 15.)))
2970+
ref = interpolate(Grid(shape=shape, extent=(15., 15.), comm=MPI.COMM_SELF))
2971+
2972+
assert np.allclose(np.array(sf.data), np.array(ref.data)[sf.local_indices],
2973+
rtol=0, atol=1e-5)
2974+
29482975
@pytest.mark.parallel(mode=2)
29492976
def test_subsampling(self, mode):
29502977
grid = Grid(shape=(40,))

0 commit comments

Comments
 (0)