-
Notifications
You must be signed in to change notification settings - Fork 248
compiler: add rudimentary support for multi-cond buffering #2838
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -860,6 +860,8 @@ class ConditionalDimension(DerivedDimension): | |
| If True, use `self`, rather than the parent Dimension, to | ||
| index into arrays. A typical use case is when arrays are accessed | ||
| indirectly via the ``condition`` expression. | ||
| relation: Or/And, default=And | ||
| How this ConditionalDimension will be combined with other ones. | ||
|
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. Seems slightly unintuitive to specify this here? Surely it would make more sense to specify how Perhaps with an API like
Contributor
Author
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.
You cannot so that's not relvant. A Function a defined dimension. It will be combined with any indices and implicit_dims at lowering. Combinining it by hand won't do anything as it will be combined yet again at lowering with
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. Ah ok, so this is to do with how it's handled during lowering and the combination is not exposed at the API level? It might be worth adding a note to the notebook explaining when you would want to change |
||
|
|
||
| Examples | ||
| -------- | ||
|
|
@@ -913,10 +915,10 @@ class ConditionalDimension(DerivedDimension): | |
| is_Conditional = True | ||
|
|
||
| __rkwargs__ = DerivedDimension.__rkwargs__ + \ | ||
| ('factor', 'condition', 'indirect') | ||
| ('factor', 'condition', 'indirect', 'relation') | ||
|
|
||
| def __init_finalize__(self, name, parent=None, factor=None, condition=None, | ||
| indirect=False, **kwargs): | ||
| indirect=False, relation=sympy.And, **kwargs): | ||
| # `parent=None` degenerates to a ConditionalDimension outside of | ||
| # any iteration space | ||
| if parent is None: | ||
|
|
@@ -937,6 +939,7 @@ def __init_finalize__(self, name, parent=None, factor=None, condition=None, | |
|
|
||
| self._condition = condition | ||
| self._indirect = indirect | ||
| self._relation = relation | ||
|
|
||
| @property | ||
| def uses_symbolic_factor(self): | ||
|
|
@@ -978,6 +981,10 @@ def condition(self): | |
| def indirect(self): | ||
| return self._indirect | ||
|
|
||
| @property | ||
| def relation(self): | ||
| return self._relation | ||
|
|
||
| @cached_property | ||
| def free_symbols(self): | ||
| retval = set(super().free_symbols) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,10 @@ | ||
| import numpy as np | ||
| import pytest | ||
| from sympy import Or | ||
|
|
||
| from conftest import skipif | ||
| from devito import ( | ||
| ConditionalDimension, Constant, Eq, Grid, Operator, SubDimension, SubDomain, | ||
| CondEq, ConditionalDimension, Constant, Eq, Grid, Operator, SubDimension, SubDomain, | ||
| TimeFunction, configuration, switchconfig | ||
| ) | ||
| from devito.arch.archinfo import AppleArm | ||
|
|
@@ -751,3 +752,36 @@ def test_buffer_reuse(): | |
|
|
||
| assert all(np.all(usave.data[i-1] == i) for i in range(1, nt + 1)) | ||
| assert all(np.all(vsave.data[i-1] == i + 1) for i in range(1, nt + 1)) | ||
|
|
||
|
|
||
| def test_multi_cond(): | ||
| grid = Grid((3, 3)) | ||
| nt = 5 | ||
|
|
||
| x, y = grid.dimensions | ||
|
|
||
| factor = 2 | ||
| ntmod = (nt - 1) * factor + 1 | ||
|
|
||
| ct1 = ConditionalDimension(name="ct1", parent=grid.time_dim, | ||
|
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. This API is pretty unintuitive imo - it isn't really clear to me what the |
||
| factor=factor, relation=Or) | ||
| ctend = ConditionalDimension(name="ctend", parent=grid.time_dim, | ||
| condition=CondEq(grid.time_dim, ntmod - 2), | ||
| relation=Or) | ||
|
|
||
| f = TimeFunction(grid=grid, name='f', time_order=0, | ||
| space_order=0, save=nt, time_dim=ct1) | ||
| T = TimeFunction(grid=grid, name='T', time_order=0, space_order=0) | ||
|
|
||
| eqs = [Eq(T, grid.time_dim)] | ||
| # this to save times from 0 to nt - 2 | ||
| eqs.append(Eq(f, T)) | ||
| # this to save the last time sample nt - 1 | ||
| eqs.append(Eq(f.forward, T+1, implicit_dims=ctend)) | ||
|
|
||
| # run operator with buffering | ||
| op = Operator(eqs, opt=('streaming', 'buffering')) | ||
|
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. you don't need or want "streaming" here |
||
| op.apply(time_m=0, time_M=ntmod-2) | ||
|
|
||
| for i in range(nt): | ||
| assert np.allclose(f.data[i], i*2) | ||
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.
"Combination mode is
Andby default. If all conditions areOrthenOrcombination mode is used." may be less ambiguousThere 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.
(nitpicking, with backtips ` around mode)