Skip to content

Commit a487058

Browse files
authored
Merge pull request #2685 from devitocodes/data-singlempi
data: prevent mpi comm with single rank
2 parents 217a3af + 03ff042 commit a487058

3 files changed

Lines changed: 70 additions & 71 deletions

File tree

devito/data/data.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def _local(self):
154154

155155
def _global(self, glb_idx, decomposition):
156156
"""A "global" view of ``self`` over a given Decomposition."""
157-
if self._is_distributed:
157+
if self._is_decomposed:
158158
raise ValueError("Cannot derive a decomposed view from a decomposed Data")
159159
if len(decomposition) != self.ndim:
160160
raise ValueError("`decomposition` should have ndim=%d entries" % self.ndim)
@@ -176,13 +176,12 @@ def _check_idx(func):
176176
@wraps(func)
177177
def wrapper(data, *args, **kwargs):
178178
glb_idx = args[0]
179-
is_gather = isinstance(kwargs.get('gather_rank', None), int)
179+
is_gather = isinstance(kwargs.get('gather_rank'), int)
180180
if is_gather and all(i == slice(None, None, 1) for i in glb_idx):
181181
comm_type = gather
182-
elif len(args) > 1 and isinstance(args[1], Data) \
183-
and args[1]._is_mpi_distributed:
182+
elif len(args) > 1 and isinstance(args[1], Data) and args[1]._is_decomposed:
184183
comm_type = index_by_index
185-
elif data._is_mpi_distributed:
184+
elif data._is_decomposed:
186185
for i in as_tuple(glb_idx):
187186
if isinstance(i, slice) and i.step is not None and i.step < 0:
188187
comm_type = index_by_index
@@ -196,8 +195,9 @@ def wrapper(data, *args, **kwargs):
196195
return wrapper
197196

198197
@property
199-
def _is_mpi_distributed(self):
200-
return self._is_distributed and configuration['mpi']
198+
def _is_decomposed(self):
199+
return self._is_distributed and configuration['mpi'] and \
200+
self._distributor.comm.size > 1
201201

202202
def __repr__(self):
203203
return super(Data, self._local).__repr__()
@@ -341,7 +341,7 @@ def __setitem__(self, glb_idx, val, comm_type):
341341
super().__setitem__(loc_idx, val)
342342
else:
343343
super().__setitem__(glb_idx, val)
344-
elif isinstance(val, Data) and val._is_distributed:
344+
elif isinstance(val, Data) and val._is_decomposed:
345345
if comm_type is index_by_index:
346346
glb_idx, val = self._process_args(glb_idx, val)
347347
val_idx = as_tuple([slice(i.glb_min, i.glb_max+1, 1) for
@@ -361,14 +361,14 @@ def __setitem__(self, glb_idx, val, comm_type):
361361
or data_global[j].size == 0
362362
if not skip:
363363
self.__setitem__(idx_global[j], data_global[j])
364-
elif self._is_distributed:
364+
elif self._is_decomposed:
365365
# `val` is decomposed, `self` is decomposed -> local set
366366
super().__setitem__(glb_idx, val)
367367
else:
368368
# `val` is decomposed, `self` is replicated -> gatherall-like
369369
raise NotImplementedError
370370
elif isinstance(val, np.ndarray):
371-
if self._is_distributed:
371+
if self._is_decomposed:
372372
# `val` is replicated, `self` is decomposed -> `val` gets decomposed
373373
glb_idx = self._normalize_index(glb_idx)
374374
glb_idx, val = self._process_args(glb_idx, val)
@@ -401,7 +401,7 @@ def __setitem__(self, glb_idx, val, comm_type):
401401
pass
402402
super().__setitem__(glb_idx, val)
403403
elif isinstance(val, Iterable):
404-
if self._is_mpi_distributed:
404+
if self._is_decomposed:
405405
raise NotImplementedError("With MPI, data can only be set "
406406
"via scalars, numpy arrays or "
407407
"other data ")
@@ -478,7 +478,7 @@ def _index_glb_to_loc(self, glb_idx):
478478
if len(glb_idx) > self.ndim:
479479
# Maybe user code is trying to add a new axis (see np.newaxis),
480480
# so the resulting array will be higher dimensional than `self`
481-
if self._is_mpi_distributed:
481+
if self._is_decomposed:
482482
raise ValueError("Cannot increase dimensionality of MPI-distributed Data")
483483
else:
484484
# As by specification, we are forced to ignore modulo indexing
@@ -494,7 +494,7 @@ def _index_glb_to_loc(self, glb_idx):
494494
try:
495495
v = convert_index(i, dec, mode='glb_to_loc')
496496
except TypeError:
497-
if self._is_mpi_distributed:
497+
if self._is_decomposed:
498498
raise NotImplementedError("Unsupported advanced indexing with "
499499
"MPI-distributed Data")
500500
v = i

examples/seismic/self_adjoint/sa_03_iso_correctness.ipynb

Lines changed: 49 additions & 50 deletions
Large diffs are not rendered by default.

examples/seismic/self_adjoint/test_wavesolver_iso.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@ def test_linearization_F(self, shape, dtype, so):
128128
ns = 2 * size + 1
129129
if len(shape) == 2:
130130
nx2, nz2 = shape[0]//2, shape[1]//2
131-
dm.data[nx2-size:nx2+size, nz2-size:nz2+size] = \
131+
dm.data[nx2-size:nx2+size+1, nz2-size:nz2+size+1] = \
132132
-1 + 2 * np.random.rand(ns, ns)
133133
else:
134134
nx2, ny2, nz2 = shape[0]//2, shape[1]//2, shape[2]//2
135135
nx, ny, nz = shape
136-
dm.data[nx2-size:nx2+size, ny2-size:ny2+size, nz2-size:nz2+size] = \
136+
dm.data[nx2-size:nx2+size+1, ny2-size:ny2+size+1, nz2-size:nz2+size+1] = \
137137
-1 + 2 * np.random.rand(ns, ns, ns)
138138

139139
# Compute F(m + dm)
@@ -194,12 +194,12 @@ def test_linearity_forward_J(self, shape, dtype, so):
194194
ns = 2 * size + 1
195195
if len(shape) == 2:
196196
nx2, nz2 = shape[0]//2, shape[1]//2
197-
m1.data[nx2-size:nx2+size, nz2-size:nz2+size] = \
197+
m1.data[nx2-size:nx2+size+1, nz2-size:nz2+size+1] = \
198198
-1 + 2 * np.random.rand(ns, ns)
199199
else:
200200
nx2, ny2, nz2 = shape[0]//2, shape[1]//2, shape[2]//2
201201
nx, ny, nz = shape
202-
m1.data[nx2-size:nx2+size, ny2-size:ny2+size, nz2-size:nz2+size] = \
202+
m1.data[nx2-size:nx2+size+1, ny2-size:ny2+size+1, nz2-size:nz2+size+1] = \
203203
-1 + 2 * np.random.rand(ns, ns, ns)
204204

205205
a = np.random.rand()
@@ -242,12 +242,12 @@ def test_linearity_adjoint_J(self, shape, dtype, so):
242242
ns = 2 * size + 1
243243
if len(shape) == 2:
244244
nx2, nz2 = shape[0]//2, shape[1]//2
245-
m1.data[nx2-size:nx2+size, nz2-size:nz2+size] = \
245+
m1.data[nx2-size:nx2+size+1, nz2-size:nz2+size+1] = \
246246
-1 + 2 * np.random.rand(ns, ns)
247247
else:
248248
nx2, ny2, nz2 = shape[0]//2, shape[1]//2, shape[2]//2
249249
nx, ny, nz = shape
250-
m1.data[nx2-size:nx2+size, ny2-size:ny2+size, nz2-size:nz2+size] = \
250+
m1.data[nx2-size:nx2+size+1, ny2-size:ny2+size+1, nz2-size:nz2+size+1] = \
251251
-1 + 2 * np.random.rand(ns, ns, ns)
252252

253253
a = np.random.rand()
@@ -289,12 +289,12 @@ def test_adjoint_J(self, shape, dtype, so):
289289
ns = 2 * size + 1
290290
if len(shape) == 2:
291291
nx2, nz2 = shape[0]//2, shape[1]//2
292-
dm1.data[nx2-size:nx2+size, nz2-size:nz2+size] = \
292+
dm1.data[nx2-size:nx2+size+1, nz2-size:nz2+size+1] = \
293293
-1 + 2 * np.random.rand(ns, ns)
294294
else:
295295
nx2, ny2, nz2 = shape[0]//2, shape[1]//2, shape[2]//2
296296
nx, ny, nz = shape
297-
dm1.data[nx2-size:nx2+size, ny2-size:ny2+size, nz2-size:nz2+size] = \
297+
dm1.data[nx2-size:nx2+size+1, ny2-size:ny2+size+1, nz2-size:nz2+size+1] = \
298298
-1 + 2 * np.random.rand(ns, ns, ns)
299299

300300
# Data perturbation

0 commit comments

Comments
 (0)