Skip to content

api: fix handling of multiple conditions for buffering - #2850

Open
mloubout wants to merge 8 commits into
mainfrom
multi-cond-again
Open

api: fix handling of multiple conditions for buffering#2850
mloubout wants to merge 8 commits into
mainfrom
multi-cond-again

Conversation

@mloubout

Copy link
Copy Markdown
Contributor

No description provided.

@mloubout mloubout added the API api (symbolics, types, ...) label Feb 16, 2026
@codecov

codecov Bot commented Feb 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 68.98396% with 116 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.25%. Comparing base (110463b) to head (b9eaa6e).

Files with missing lines Patch % Lines
devito/passes/clusters/asynchrony.py 20.33% 47 Missing ⚠️
devito/passes/clusters/buffering.py 69.29% 28 Missing and 11 partials ⚠️
devito/ir/support/guards.py 67.34% 14 Missing and 2 partials ⚠️
tests/test_gpu_common.py 0.00% 5 Missing ⚠️
devito/ir/stree/algorithms.py 20.00% 3 Missing and 1 partial ⚠️
devito/parameters.py 25.00% 2 Missing and 1 partial ⚠️
devito/ir/clusters/algorithms.py 81.81% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2850      +/-   ##
==========================================
- Coverage   83.54%   79.25%   -4.30%     
==========================================
  Files         257      257              
  Lines       53922    54159     +237     
  Branches     4613     4661      +48     
==========================================
- Hits        45050    42923    -2127     
- Misses       8074    10381    +2307     
- Partials      798      855      +57     
Flag Coverage Δ
pytest-gpu-aomp-amdgpuX ?
pytest-gpu-gcc- 78.13% <68.98%> (-0.05%) ⬇️
pytest-gpu-icx- 78.05% <68.98%> (-0.05%) ⬇️
pytest-gpu-nvc-nvidiaX ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread devito/symbolics/extended_sympy.py Outdated
return CondNe(*self.args, evaluate=False)

@property
def _as_min(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would drop this and rather have a singledispatch handler for CondEq where necessary


def relational_shift(expr, s):
"""
Infer shift incurred by the expression. Generally only

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could use an example here to quickly visualise what's it trying to do

Comment thread devito/ir/equations/equation.py Outdated
expr = uxreplace(expr, {d: IntDiv(index, d.symbolic_factor)})

# Merge conditionals when possible. E.g if we have an implicit_dim
# and there is a dimension with the same parent, we ca merged

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dimension

"ca merged"

"their conditions"

you could also make the example a bit more practical

Comment thread devito/ir/equations/equation.py Outdated
for d in input_expr.implicit_dims:
if d not in conditionals:
continue
for cd in dict(conditionals):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

list(...) is fine

Comment thread devito/ir/equations/equation.py Outdated
# Replace the ConditionalDimensions in `expr`
for d, cond in conditionals.items():
# Replace dimension with index
index = d.index

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can spare this line

Comment thread devito/ir/equations/equation.py Outdated
ispace = IterationSpace(intervals, iterators)

# Construct the conditionals and replace the ConditionalDimensions in `expr`
# Construct the conditionals

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should place this whole block of code, which constructs/lowers the conditionals, into its own separate functions, and a docstring with some examples

@mloubout
mloubout force-pushed the multi-cond-again branch 4 times, most recently from ef708e5 to b997156 Compare May 22, 2026 15:13
@mloubout
mloubout force-pushed the multi-cond-again branch 5 times, most recently from 7a1a6aa to c7786ea Compare May 28, 2026 17:17
@review-notebook-app

Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@mloubout
mloubout force-pushed the multi-cond-again branch 11 times, most recently from f904760 to 0500469 Compare May 29, 2026 16:45
Comment thread devito/ir/equations/equation.py Outdated
shift = relational_shift(cond, d.parent)
expr = uxreplace(expr, {d: IntDiv(index, d.symbolic_factor) + shift})

# Merge conditionals when possible. E.g if we have an implicit_dim

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw this block imho deserves its own function

Comment thread devito/passes/clusters/asynchrony.py Outdated
if d is not dim:
continue

if d in c0.guards and not c0.guards[d].has(Mod):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

searching for Mod is a bit meh, I'd rather add a special guard to ir/support/guards.py and look for that instead (there's quite a few already in there!)

Comment thread devito/passes/clusters/asynchrony.py Outdated
_actions_from_update_memcpy(c, d, clusters, actions, sregistry)
elif d.is_Custom and is_integer(c.ispace[d].size):
_actions_from_init(c, d, actions)
_actions_from_init(c, d, clusters, actions)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leftover, I guess

Comment thread devito/passes/clusters/asynchrony.py Outdated


def _actions_from_init(c, d, actions):
def _actions_from_init(c, d, clusters, actions):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leftover, I guess

Comment thread examples/userapi/05_conditional_dimension.ipynb
@mloubout
mloubout force-pushed the multi-cond-again branch from 0500469 to 30790f0 Compare May 30, 2026 14:35
@mloubout mloubout added the no-pro-trigger Skip the devitopro submodule update on merge label Jul 1, 2026
@mloubout
mloubout force-pushed the multi-cond-again branch 6 times, most recently from 89aea51 to 99f0a9e Compare July 3, 2026 07:41
Comment thread devito/ir/equations/algorithms.py Outdated
if d not in conditionals:
continue
for cd in list(conditionals):
if cd.parent == d.parent and cd is not d:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpicking: cd.parent is d.parent ...

Comment thread devito/ir/equations/algorithms.py Outdated
continue
for cd in list(conditionals):
if cd.parent == d.parent and cd is not d:
cond = conditionals.pop(d)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpicking: you don't need this line here, you may just .pop in the else branch, and avoid the = cond in the if branch

Comment thread devito/ir/equations/algorithms.py Outdated

# Replace the ConditionalDimensions in `expr`
for d, cond in conditionals.items():
# Replace dimension with index

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant comment?

def generate_conditionals(expr, input_expr, ordering):
"""
Generate the conditionals for the given expression,
based on the input expression and the ordering of dimensions.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpicking, we could take the opportunity to add some Examples to the docstring, or at least refer to ConditionalDimension.__doc__ ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is additional docstring to ConditionalDimension.doc

g = c0.guards.get(d)
# Explicit compute guards need no pipeline; memcpy clusters
# still need WithLock for the copy-back sync
if g is not None and not wraps_memcpy(c0):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a lot of the complexity inherent in this guard and the nested one could be mitigated by simply introducing somehting along the lines of c0.guards.has(d, GuardFactor)

Comment thread devito/passes/clusters/buffering.py Outdated

# Alias deferred entries to an existing f-buffer; create one if none
for f, deferred in extras.items():
reusable = [mapper[(ff, kk)] for (ff, kk) in mapper if ff == f]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ff is f

Comment thread devito/passes/clusters/buffering.py Outdated
# Add all missing buffering dimensions and reorder to
# avoid duplicates with different ordering
ispaces.add(c.ispace.insert(self.dim, edims).reorder())
elif ((self.f in c.scope.reads or self.f in c.scope.writes) and

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.f in c.scope.functions or in fact self.f in c.functions should suffice

Comment thread devito/passes/clusters/buffering.py Outdated

lhs = b.indexify()._subs(v.xd, v.first_idx.b)
rhs = f.indexify()._subs(v.dim, v.first_idx.f)
for b, vb in descriptors.items():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another potential occurrence of descriptors.flatten

Comment thread devito/passes/clusters/buffering.py Outdated
)
else:
size = async_degree
for k, ck in groupby(clusters, key=lambda c: c.guards):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ultra-uber-nitpicking, typically we use letter g for groups

return p, offset


def _buffer_dim(f, key, ck):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is potentially a legacy over-complication

is it basically what in PRO refer to as .hdim ?

because imho, if so, we could/should somehow abstract the concept here, and just use f.hdim (or whatever name you wanna give it)

@mloubout
mloubout force-pushed the multi-cond-again branch 6 times, most recently from ab8cd52 to b1147aa Compare July 30, 2026 22:25

if not conditionals and not input_expr.implicit_dims:
return expr, conditionals
# Merge conditionals when possible. E.g., if an implicit_dim shares

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line

if c.is_halo_touch:
hs = HaloScheme.union(e.rhs.halo_scheme for e in c.exprs)
queue.append(c.rebuild(exprs=[], halo_scheme=hs))
# Peel syncs (e.g. a WaitLock) off, they must survive an unbound halo.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could go one line above, then hs = shifts down here before the queue append...

also some blank lines

@mloubout
mloubout force-pushed the multi-cond-again branch 3 times, most recently from f0fbc35 to 3ca9d26 Compare August 11, 2026 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API api (symbolics, types, ...) no-pro-trigger Skip the devitopro submodule update on merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants