|
12 | 12 | from devito.data import CENTER, LEFT, RIGHT, Decomposition |
13 | 13 | from devito.parameters import configuration |
14 | 14 | from devito.tools import ( |
15 | | - EnrichedTuple, as_tuple, ctypes_to_cstr, filter_ordered, frozendict |
| 15 | + EnrichedTuple, as_tuple, ctypes_to_cstr, filter_ordered, frozendict, is_integer, |
| 16 | + memoized_meth |
16 | 17 | ) |
17 | 18 | from devito.types import CompositeObject, Constant, Object |
18 | | -from devito.types.utils import DimensionTuple |
| 19 | +from devito.types.utils import DimensionTuple, GlobalLocal |
19 | 20 |
|
20 | 21 | # Do not prematurely initialize MPI |
21 | 22 | # This allows launching a Devito program from within another Python program |
@@ -213,6 +214,25 @@ def loc_empty(self): |
213 | 214 | """This rank is empty""" |
214 | 215 | return any(d.loc_empty for d in self.decomposition) |
215 | 216 |
|
| 217 | + @memoized_meth |
| 218 | + def subsample(self, dimensions): |
| 219 | + """ |
| 220 | + The Distributor of the ConditionalDimensions in `dimensions` subsampling |
| 221 | + those of `self`, or `self` if there are none. |
| 222 | + """ |
| 223 | + subsampled = {d.parent: d for d in dimensions |
| 224 | + if d.is_Conditional and d.parent in self.dimensions |
| 225 | + and is_integer(d.factor)} |
| 226 | + if not subsampled or not self.is_parallel: |
| 227 | + return self |
| 228 | + |
| 229 | + return SubGridDistributor( |
| 230 | + self, |
| 231 | + tuple(subsampled.get(d, d) for d in self.dimensions), |
| 232 | + tuple(subsampled[d].factor if d in subsampled else 1 |
| 233 | + for d in self.dimensions) |
| 234 | + ) |
| 235 | + |
216 | 236 |
|
217 | 237 | class DenseDistributor(AbstractDistributor): |
218 | 238 |
|
@@ -300,6 +320,13 @@ def all_ranges(self): |
300 | 320 | getters=self.dimensions)) |
301 | 321 | return tuple(ret) |
302 | 322 |
|
| 323 | + @cached_property |
| 324 | + def size_map(self): |
| 325 | + """Map between Dimensions and their global/local size.""" |
| 326 | + return {d: GlobalLocal(g, l) |
| 327 | + for d, g, l in zip(self.dimensions, self.glb_shape, self.shape, |
| 328 | + strict=True)} |
| 329 | + |
303 | 330 | @cached_property |
304 | 331 | def _obj_comm(self): |
305 | 332 | """An Object representing the MPI communicator.""" |
@@ -494,21 +521,33 @@ def _rebuild(self, shape=None, dimensions=None, comm=None): |
494 | 521 | class SubGridDistributor(Distributor): |
495 | 522 |
|
496 | 523 | """ |
497 | | - Decompose a SubGrid over a set of MPI processes in a manner consistent with |
498 | | - the parent distributor: a rank owns the SubGrid points on its parent points. |
| 524 | + Decompose a subsampling of a parent Distributor's domain, e.g. a SubGrid, over |
| 525 | + the parent's MPI processes: a rank owns the subsampled points on its parent |
| 526 | + points. |
499 | 527 |
|
500 | 528 | Parameters |
501 | 529 | ---------- |
502 | | - subgrid : SubGrid |
503 | | - The SubGrid to be decomposed. |
| 530 | + parent : Distributor |
| 531 | + The Distributor of the subsampled domain. |
| 532 | + dimensions : tuple of Dimensions |
| 533 | + The decomposed Dimensions, one per parent Dimension. |
| 534 | + factors : tuple of ints |
| 535 | + The subsampling factor along each Dimension. |
504 | 536 | """ |
505 | 537 |
|
506 | | - def __init__(self, subgrid): |
507 | | - self._parent = subgrid.parent.distributor |
508 | | - self._factors = subgrid.factor |
| 538 | + def __init__(self, parent, dimensions, factors): |
| 539 | + shape = tuple((n + f - 1)//f |
| 540 | + for n, f in zip(parent.glb_shape, factors, strict=True)) |
| 541 | + AbstractDistributor.__init__(self, shape, dimensions) |
| 542 | + |
| 543 | + self._parent = parent |
| 544 | + self._factors = tuple(factors) |
509 | 545 |
|
510 | | - super().__init__(subgrid.shape, subgrid.dimensions, self.parent.comm, |
511 | | - tuple(self.parent.topology)) |
| 546 | + # Same processes and topology as the parent, hence the same communicator |
| 547 | + self._comm = parent.comm |
| 548 | + self._topology = parent.topology |
| 549 | + |
| 550 | + self.__decomposition_setup__() |
512 | 551 |
|
513 | 552 | def __decomposition_setup__(self): |
514 | 553 | """ |
|
0 commit comments