Skip to content

Commit

Permalink
Merge pull request #2432 from devitocodes/honour-conditional
Browse files Browse the repository at this point in the history
compiler: Patch indirect ConditionalDimension
  • Loading branch information
FabioLuporini authored Aug 1, 2024
2 parents 4e95e2f + e1473d1 commit 98453a6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 5 additions & 2 deletions devito/ir/equations/equation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)})

Expand Down
18 changes: 18 additions & 0 deletions tests/test_dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down

0 comments on commit 98453a6

Please sign in to comment.