Skip to content

Commit

Permalink
misc: Refactoring to improve robustness and brevity
Browse files Browse the repository at this point in the history
  • Loading branch information
EdCaunt committed Jan 30, 2025
1 parent 31f2ec3 commit 6e2a61c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 33 deletions.
8 changes: 4 additions & 4 deletions devito/ir/equations/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ def make_dimension_map(expr):
"""
try:
dimension_map = {**expr.subdomain.dimension_map}

except AttributeError:
# Some Relationals may be pure SymPy objects, thus lacking the SubDomain
dimension_map = {}
else:
functions = [f for f in retrieve_functions(expr) if f._is_on_subdomain]
for f in functions:
dimension_map.update({d: expr.subdomain.dimension_map[d.root]
for d in f.space_dimensions if d.is_Sub})
except AttributeError:
# Some Relationals may be pure SymPy objects, thus lacking the SubDomain
dimension_map = {}

return frozendict(dimension_map)

Expand Down
5 changes: 4 additions & 1 deletion devito/mpi/distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,10 @@ def parent(self):
"""The parent distributor of this SubDistributor."""
return self._parent

p = parent # Shortcut for convenience
@property
def p(self):
"""Shortcut for `SubDistributor.parent`"""
return self.parent

@property
def par_slices(self):
Expand Down
45 changes: 17 additions & 28 deletions devito/types/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,29 +605,23 @@ def __subdomain_finalize_legacy__(self, grid):
# Case ('middle', int, int)
side, ltkn, rtkn = v
if side != 'middle':
raise ValueError("Expected side 'middle', not `%s`" % side)
sub_dimensions.append(SubDimension.middle('i%s' % k.name,
raise ValueError(f"Expected side 'middle', not `{side}`")
sub_dimensions.append(SubDimension.middle(f'i{k.name}',
k, ltkn, rtkn))
thickness = s-ltkn-rtkn
sdshape.append(thickness)
except ValueError:
side, thickness = v
if side == 'left':
if s-thickness < 0:
raise ValueError("Maximum thickness of dimension %s "
"is %d, not %d" % (k.name, s, thickness))
sub_dimensions.append(SubDimension.left('i%s' % k.name,
k, thickness))
sdshape.append(thickness)
elif side == 'right':
if s-thickness < 0:
raise ValueError("Maximum thickness of dimension %s "
"is %d, not %d" % (k.name, s, thickness))
sub_dimensions.append(SubDimension.right('i%s' % k.name,
k, thickness))
sdshape.append(thickness)
else:
raise ValueError("Expected sides 'left|right', not `%s`" % side)
constructor = {'left': SubDimension.left,
'right': SubDimension.right}.get(side, None)
if constructor is None:
raise ValueError(f"Expected sides 'left|right', not `{side}`")

if s - thickness < 0:
raise ValueError(f"Maximum thickness of dimension {k.name} "
f"is {s}, not {thickness}")
sub_dimensions.append(constructor(f'i{k.name}', k, thickness))
sdshape.append(thickness)

self._shape = tuple(sdshape)
self._dimensions = tuple(sub_dimensions)
Expand Down Expand Up @@ -659,14 +653,12 @@ def _arg_names(self):
try:
ret = self.grid._arg_names
except AttributeError:
raise AttributeError("%s is not attached to a Grid and has no _arg_names"
% self)
msg = f"{self} is not attached to a Grid and has no _arg_names"
raise AttributeError(msg)

# Names for SubDomain thicknesses
thickness_names = []
for d in self.dimensions:
if d.is_Sub:
thickness_names += [k.name for k in d._thickness_map]
thickness_names = tuple([k.name for k in d._thickness_map]
for d in self.dimensions if d.is_Sub)

ret += tuple(thickness_names)

Expand All @@ -675,10 +667,7 @@ def _arg_names(self):
def __getstate__(self):
state = self.__dict__.copy()
# A SubDistributor wraps an MPI communicator, which can't and shouldn't be pickled
try:
state.pop('_distributor')
except KeyError:
pass
state.pop('_distributor', None)
return state

def __setstate__(self, state):
Expand Down

0 comments on commit 6e2a61c

Please sign in to comment.