Skip to content

Commit 7a426df

Browse files
Merge pull request #3081 from devitocodes/fix-tensor-name-underscore
types: Fix TensorFunction/VectorFunction name with underscores
2 parents a9e3f31 + 620f434 commit 7a426df

4 files changed

Lines changed: 46 additions & 5 deletions

File tree

‎devito/ir/support/guards.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ class BaseGuardOverflow(Guard):
214214
A guard for buffer overflow.
215215
"""
216216

217-
pass
217+
def __new__(cls, p0, p1, **kwargs):
218+
return super().__new__(cls, p0, p1, evaluate=False)
218219

219220

220221
class GuardOverflowGe(BaseGuardOverflow, Ge):

‎devito/types/basic.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,9 +1640,15 @@ def grid(self):
16401640

16411641
@property
16421642
def name(self):
1643+
# `_name` may be unset if `__init_finalize__` was skipped in `_fromrep`
1644+
name = getattr(self, '_name', None)
1645+
if name is not None:
1646+
return name
1647+
# Components are named `{name}_{suffix}`, with the suffix built from
1648+
# the Dimension names, so the base name is everything before the last `_`
16431649
for c in self.values():
16441650
try:
1645-
return c.name.split('_')[0]
1651+
return c.name.rsplit('_', 1)[0]
16461652
except AttributeError:
16471653
# `c` is not a devito object
16481654
pass
@@ -1660,7 +1666,7 @@ def _rebuild(self, *args, **kwargs):
16601666
# We need to rebuild the components with the new name then
16611667
# rebuild the matrix
16621668
newname = kwargs.pop('name', self.name)
1663-
comps = [f.func(*args, name=f.name.replace(self.name, newname), **kwargs)
1669+
comps = [f.func(*args, name=f"{newname}{f.name[len(self.name):]}", **kwargs)
16641670
for f in self.flat()]
16651671
# Rebuild the matrix with the new components
16661672
return self._new(*self.shape, comps)

‎tests/test_pickle.py‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@
1313
)
1414
from devito.data import LEFT, OWNED
1515
from devito.finite_differences.tools import centered, direct, left, right, transpose
16-
from devito.ir import Backward, Forward, GuardBound, GuardBoundNext, GuardFactor
16+
from devito.ir import (
17+
Backward, Forward, GuardBound, GuardBoundNext, GuardFactor, GuardOverflow
18+
)
1719
from devito.mpi.halo_scheme import Halo
1820
from devito.mpi.routines import (
1921
MPIMsgEnriched, MPIRegion, MPIRequestObject, MPIStatusObject
2022
)
2123
from devito.symbolics import (
2224
CallFromPointer, Cast, DefFunction, FieldFromPointer, IntDiv, ListInitializer, SizeOf,
23-
indexify, pow_to_mul
25+
as_long, indexify, pow_to_mul
2426
)
2527
from devito.symbolics.unevaluation import Mod
2628
from devito.tools import EnrichedTuple
@@ -566,6 +568,22 @@ def test_guard_bound_next(self, pickle, direction):
566568

567569
assert str(gbn) == str(new_gbn)
568570

571+
@pytest.mark.parametrize('negated', [False, True])
572+
def test_guard_overflow(self, pickle, negated):
573+
d = Dimension(name='d')
574+
freespace = Scalar(name='freespace')
575+
size = SizeOf(np.float32)*as_long((d + 4)*(d + 8))
576+
577+
guard = GuardOverflow(freespace, size)
578+
if negated:
579+
guard = guard.negated
580+
581+
new_guard = pickle.loads(pickle.dumps(guard))
582+
583+
assert type(new_guard) is type(guard)
584+
assert str(new_guard) == str(guard)
585+
assert new_guard.canonical is new_guard
586+
569587
def test_temp_function(self, pickle):
570588
grid = Grid(shape=(3, 3))
571589
d = Dimension(name='d')

‎tests/test_tensors.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,22 @@ def test_rebuild(func1):
487487
assert j.dimensions == tuple(new_dims)
488488

489489

490+
@pytest.mark.parametrize('func1', [TensorFunction, TensorTimeFunction,
491+
VectorFunction, VectorTimeFunction])
492+
@pytest.mark.parametrize('name', ['tau_s', 'my_tau_x', 'x'])
493+
def test_name_with_underscore(func1, name):
494+
grid = Grid(tuple([5]*3))
495+
f1 = func1(name=name, grid=grid)
496+
assert f1.name == name
497+
assert (2 * f1).name == name
498+
assert (f1 + f1).name == name
499+
500+
f2 = f1.func(name='f2')
501+
assert f2.name == 'f2'
502+
for (i, j) in zip(f1.flat(), f2.flat(), strict=True):
503+
assert j.name == f"f2{i.name[len(name):]}"
504+
505+
490506
@pytest.mark.parametrize('func1', [Function, TimeFunction,
491507
TensorFunction, TensorTimeFunction,
492508
VectorFunction, VectorTimeFunction])

0 commit comments

Comments
 (0)