From e1473d125c5bc4271cb501128c56f06b57f8fa4d Mon Sep 17 00:00:00 2001 From: Fabio Luporini Date: Wed, 31 Jul 2024 09:47:46 +0000 Subject: [PATCH] compiler: Patch indirect ConditionalDimension --- devito/ir/equations/equation.py | 7 +++++-- tests/test_dimension.py | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/devito/ir/equations/equation.py b/devito/ir/equations/equation.py index 487fcc7109..b5f8b6979f 100644 --- a/devito/ir/equations/equation.py +++ b/devito/ir/equations/equation.py @@ -71,7 +71,10 @@ def apply(self, func): """ args = [func(self.lhs), func(self.rhs)] kwargs = dict(self.state) - kwargs['conditionals'] = {k: func(v) for k, v in self.conditionals.items()} + + conditionals = {k: func(v) for k, v in self.conditionals.items()} + kwargs['conditionals'] = frozendict(conditionals) + return self.func(*args, **kwargs) def __repr__(self): @@ -197,7 +200,7 @@ def __new__(cls, *args, **kwargs): conditionals[d] = cond # Replace dimension with index index = d.index - if d.condition is not None: + if d.condition is not None and d in expr.free_symbols: index = index - relational_min(d.condition, d.parent) expr = uxreplace(expr, {d: IntDiv(index, d.factor)}) diff --git a/tests/test_dimension.py b/tests/test_dimension.py index 1cf98d2e9a..76068fda8e 100644 --- a/tests/test_dimension.py +++ b/tests/test_dimension.py @@ -1255,6 +1255,24 @@ def test_no_index_sparse(self): assert np.all(f.data[0, :, 0] == 0.) assert np.all(f.data[0, :, -1] == 0.) + def test_no_index_symbolic(self): + grid = Grid(shape=(10, 10, 10)) + x, y, z = grid.dimensions + + u = TimeFunction(name='u', grid=grid) + + v0 = Constant(name='v0', dtype=np.float32) + v1 = Constant(name='v1', dtype=np.float32) + condition = And(Ge(x, v0), Le(x, v1)) + cd = ConditionalDimension(name='cd', parent=x, condition=condition, + indirect=True) + + eq = Eq(u.forward, u + 1, implicit_dims=cd) + + # Ensure both code generation and jitting work + op = Operator(eq) + op.cfunction + def test_symbolic_factor(self): """ Test ConditionalDimension with symbolic factor (provided as a Constant).