-
Notifications
You must be signed in to change notification settings - Fork 256
api: fix handling of multiple conditions for buffering #2850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mloubout
wants to merge
7
commits into
main
Choose a base branch
from
multi-cond-again
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1ef6f4b
compiler: fix multi-cond async
mloubout d82b832
compiler: fix multi-cond for multi-layer
mloubout 51993c8
api: fix interpolate with complex dtype
mloubout f0a945a
api: fix buffering with multiple conditions
mloubout cd1f1e4
compiler: fix various corner case of multi buffering
mloubout a453d38
compiler: support mutli-buffering
mloubout 5c424fe
misc: cleanup and add comments
mloubout File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,14 +2,19 @@ | |
| from functools import singledispatch | ||
|
|
||
| from devito.data.allocators import DataReference | ||
| from devito.finite_differences.differentiable import diff2sympy | ||
| from devito.ir.support import GuardFactor | ||
| from devito.logger import warning | ||
| from devito.symbolics import ( | ||
| retrieve_dimensions, retrieve_functions, retrieve_indexed, uxreplace | ||
| IntDiv, retrieve_dimensions, retrieve_functions, retrieve_indexed, uxreplace | ||
| ) | ||
| from devito.tools import ( | ||
| Ordering, as_tuple, filter_ordered, filter_sorted, flatten, frozendict | ||
| ) | ||
| from devito.types import ConditionalDimension, Dimension, Eq, IgnoreDimSort, SubDimension | ||
| from devito.types import ( | ||
| ConditionalDimension, Dimension, Eq, IgnoreDimSort, SubDimension, relational_min, | ||
| relational_shift | ||
| ) | ||
| from devito.types.array import Array | ||
| from devito.types.basic import AbstractFunction | ||
| from devito.types.dimension import MultiSubDimension, Thickness | ||
|
|
@@ -339,3 +344,50 @@ def _(d, mapper, rebuilt, sregistry): | |
| kwargs['functions'] = functions | ||
|
|
||
| mapper[d] = d._rebuild(**kwargs) | ||
|
|
||
|
|
||
| def generate_conditionals(expr, input_expr, ordering): | ||
| """ | ||
| Generate the conditionals for the given expression, | ||
| based on the input expression and the ordering of dimensions. | ||
| """ | ||
| # Construct the conditionals | ||
| conditionals = {} | ||
| for d in ordering: | ||
| if not d.is_Conditional: | ||
| continue | ||
|
|
||
| if d.condition is None: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| conditionals[d] = GuardFactor(d) | ||
| else: | ||
| cond = diff2sympy(lower_exprs(d.condition)) | ||
| if d._factor is not None: | ||
| cond = d.relation(cond, GuardFactor(d)) | ||
| conditionals[d] = cond | ||
|
|
||
| # Merge conditionals when possible. E.g., if an implicit_dim shares | ||
| # its parent Dimension with another ConditionalDimension, the two | ||
| # conditions can be merged into a single guard. | ||
| for d in input_expr.implicit_dims: | ||
| if d not in conditionals: | ||
| continue | ||
| for cd in list(conditionals): | ||
| if cd.parent == d.parent and cd is not d: | ||
| cond = conditionals.pop(d) | ||
| if d.relation == ConditionalDimension._STRICT: | ||
| conditionals[cd] = conditionals[d] = cond | ||
| else: | ||
| mode = cd.relation and d.relation | ||
| conditionals[cd] = mode(cond, conditionals[cd]) | ||
| break | ||
|
|
||
| # Replace the ConditionalDimensions in `expr` | ||
| for d, cond in conditionals.items(): | ||
| # Replace dimension with index | ||
| index = d.index | ||
| if d.condition is not None and expr.has(d): | ||
| index = index - relational_min(cond, d.parent) | ||
| shift = relational_shift(cond, d.parent) | ||
| expr = uxreplace(expr, {d: IntDiv(index, d.symbolic_factor) + shift}) | ||
|
|
||
| return expr, conditionals | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,8 @@ | |
| from sympy.logic.boolalg import BooleanFunction | ||
|
|
||
| from devito.ir.support.space import Forward, IterationDirection | ||
| from devito.symbolics import CondEq, CondNe, search | ||
| from devito.symbolics import CondEq, CondNe, IntDiv, search | ||
| from devito.symbolics.manipulation import _uxreplace_handle, _uxreplace_registry | ||
| from devito.tools import Pickable, as_tuple, frozendict, split | ||
| from devito.types import Dimension, LocalObject | ||
|
|
||
|
|
@@ -138,37 +139,29 @@ class BaseGuardBoundNext(Guard, Pickable): | |
| given `direction`. | ||
| """ | ||
|
|
||
| __rargs__ = ('d', 'direction') | ||
| __rargs__ = ('d', 'index', 'direction') | ||
| __rkwargs__ = ('d_min', 'd_max') | ||
|
|
||
| def __new__(cls, d, direction, **kwargs): | ||
| def __new__(cls, d, index, direction, | ||
| d_min=None, d_max=None, **kwargs): | ||
| assert isinstance(d, Dimension) | ||
| assert isinstance(direction, IterationDirection) | ||
|
|
||
| if direction == Forward: | ||
| p0 = d.root | ||
| p1 = d.root.symbolic_max | ||
| # Always take the next index in the iteration direction | ||
| next_index = eval_next_index(index, d, direction) | ||
|
|
||
| if d.is_Conditional: | ||
| v = d.symbolic_factor | ||
| # Round `p0 + 1` up to the nearest multiple of `v` | ||
| p0 = Mul((((p0 + 1) + v - 1) / v), v, evaluate=False) | ||
| else: | ||
| p0 = p0 + 1 | ||
| # The direction might be forward but accessing c - d | ||
| # making the access backward w.r.t | ||
| # Update direction according to access direction for valid guard | ||
| if index.has(-d): | ||
| direction = -direction | ||
|
|
||
| if direction == Forward: | ||
| p0 = next_index | ||
| p1 = d_max or d.root.symbolic_max | ||
| else: | ||
| p0 = d.root.symbolic_min | ||
| p1 = d.root | ||
|
|
||
| if d.is_Conditional: | ||
| v = d.symbolic_factor | ||
| # Round `p1 - 1` down to the nearest sub-multiple of `v` | ||
| # NOTE: we use ABS to make sure we handle negative values properly. | ||
| # Once `p1 - 1` is negative (e.g. `iteration=time - 1` and `time=0`), | ||
| # as long as we get a negative number, rather than 0 and even if it's | ||
| # not `-v`, we're good | ||
| p1 = (p1 - 1) - abs(p1 - 1) % v | ||
| else: | ||
| p1 = p1 - 1 | ||
| p0 = d_min if d_min is not None else d.root.symbolic_min | ||
| p1 = next_index | ||
|
|
||
| try: | ||
| if cls.__base__._eval_relation(p0, p1) is true: | ||
|
|
@@ -180,12 +173,15 @@ def __new__(cls, d, direction, **kwargs): | |
|
|
||
| obj.d = d | ||
| obj.direction = direction | ||
| obj.index = index | ||
| obj.d_min = d_min | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these should probably be |
||
| obj.d_max = d_max | ||
|
|
||
| return obj | ||
|
|
||
| @property | ||
| def _args_rebuild(self): | ||
| return (self.d, self.direction) | ||
| return (self.d, self.index, self.direction) | ||
|
|
||
|
|
||
| class GuardBoundNextLe(BaseGuardBoundNext, Le): | ||
|
|
@@ -544,3 +540,47 @@ def pairwise_or(*guards): | |
| pass | ||
|
|
||
| return guard | ||
|
|
||
|
|
||
| _uxreplace_registry.register(BaseGuardBoundNext) | ||
|
|
||
|
|
||
| @_uxreplace_handle.register(BaseGuardBoundNext) | ||
| def _(expr, args, kwargs): | ||
| return expr.func(expr.d, expr.index, expr.direction, **kwargs) | ||
|
|
||
|
|
||
| @singledispatch | ||
| def eval_next_index(expr, dim, dir): | ||
| """ | ||
| Evaluate `expr` at the next iteration point along `dim` in the given | ||
| `dir`-ection. The "next" point is obtained by substituting `dim` with | ||
| `dim + 1` for `Forward` and `dim - 1` for `Backward`. | ||
|
|
||
| For `IntDiv` expressions encoding subsampling (`dim.root // factor`), | ||
| the result is rounded to the next valid coarse-grained slot. | ||
| """ | ||
| if dir == Forward: | ||
| return expr._subs(dim, dim + 1) | ||
| else: | ||
| return expr._subs(dim, dim - 1) | ||
|
|
||
|
|
||
| @eval_next_index.register(Expr) | ||
| def _(expr, dim, dir): | ||
| if not expr.args: | ||
| if dir == Forward: | ||
| return expr._subs(dim, dim + 1) | ||
| else: | ||
| return expr._subs(dim, dim - 1) | ||
| return expr.func(*[eval_next_index(a, dim, dir) for a in expr.args]) | ||
|
|
||
|
|
||
| @eval_next_index.register(IntDiv) | ||
| def _(expr, dim, dir): | ||
| v = dim.symbolic_factor | ||
| p0 = dim.root | ||
| if dir == Forward: | ||
| return Mul((((p0 + 1) + v - 1) / v), v, evaluate=False) | ||
| else: | ||
| return (p0 - 1) - abs(p0 - 1) % v | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is
v[0]safe here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There can only be one
strictcds and the dimension is the key so there is only one value possibleThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this bit suffers from a legacy choice
instead of
vbeing a list, why don't we incrementally build the guard, usingmode(imho to be renamed asclsorrelation) from the get go? this way you don't have to check for themodevalue again (which is not ideal)