Repository navigation
API: Subgrid (integer) support #3080
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
11
commits into
main
Choose a base branch
from
subgrid-strided-subsampling
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
11 commits
Select commit
Hold shift + click to select a range
13bce35
compiler: Lower integer subsampling to a strided loop
mloubout 3cf66eb
tests: Add coverage for strided subsampling
mloubout dcddc17
docs: Add a subgrids tutorial
mloubout a9ac791
api: Add SubGrid for integer subsampling
mloubout f10c537
compiler: Iterate subsampled Dimensions with unit stride
mloubout 35fb9ea
compiler: Carry properties onto the subsampled Dimension
mloubout 87248bc
docs: Restructure the subgrids tutorial around the SubGrid API
mloubout d07af91
api: Make SubGrid's rejection messages actionable
mloubout aab92cb
tests: Fix the subsampling checks under linearization
mloubout 1ffb50a
tests: Add MPI coverage for subsampling
mloubout c5e56cd
compiler: Don't treat a subsampled Inc as a reduction
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
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 |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| from devito.ir.clusters import Queue | ||
| from devito.ir.support import GuardFactor | ||
| from devito.symbolics import IntDiv, uxreplace | ||
| from devito.tools import timed_pass | ||
|
|
||
| __all__ = ['subsampling'] | ||
|
|
||
|
|
||
| @timed_pass() | ||
| def subsampling(clusters): | ||
| """ | ||
| Strengthen factor-based ConditionalDimension guards into strided iteration. | ||
|
|
||
| A Function subsampled by `factor` along `d` is written under a | ||
| `d % factor == 0` guard, so the enclosing loop visits every point of `d` | ||
| and discards all but one in `factor` of them. Where legal, drop the guard | ||
| and step the loop `factor` points at a time instead | ||
|
|
||
| for (x = x_m; x <= x_M; x += 1) for (x = ...; x <= x_M; x += xf) | ||
| if (x%xf == 0) -> g[x/xf] = ... | ||
| g[x/xf] = ... | ||
|
|
||
| The loop is taken over the subsampled Dimension `cd` itself, with a *unit* | ||
| stride, and every access to the parent `d` is rescaled to `cd*factor`: | ||
|
|
||
| for (x = x_m; x <= x_M; x += 1) for (xs = xs_m; xs <= xs_M; xs++) | ||
| if (x%xf == 0) -> g[xs] = u[xs*xf] | ||
| g[x/xf] = u[x] | ||
|
|
||
| A non-unitary increment is deliberately avoided: it would escape the | ||
| Lamport-style dependence analysis the compiler relies on. The loop bounds | ||
| are `cd`'s own runtime values, which `ConditionalDimension._arg_values` | ||
| derives as `ceil(x_m/factor) .. floor(x_M/factor)`, so no anchoring of the | ||
| loop minimum is needed either. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| clusters : tuple of Clusters | ||
| Input Clusters, subject of the optimization pass. | ||
| """ | ||
| return Subsampling().process(clusters) | ||
|
|
||
|
|
||
| class Subsampling(Queue): | ||
|
|
||
| """ | ||
| Replace uniform factor-based guards with strided iteration. | ||
|
|
||
| Clusters are grouped by iteration space prefix alone, deliberately *not* | ||
| by guard: a guard may only become a stride if every Cluster sharing the | ||
| prefix carries the very same one. Striding a loop nest that also hosts an | ||
| unguarded expression would run that expression on a subset of its | ||
| iterations -- and dropping the guard is what makes the nest shared in the | ||
| first place, since `stree_build` reuses a subtree across Clusters with | ||
| matching guards (see `reuse_whole_subtree`). | ||
| """ | ||
|
|
||
| def callback(self, clusters, prefix): | ||
| if not prefix: | ||
| return clusters | ||
|
|
||
| d = prefix[-1].dim | ||
|
|
||
| guards = {c.guards.get(d) for c in clusters} | ||
| if len(guards) > 1: | ||
| # Non-uniformly guarded, hence `d`'s loop nest is shared with | ||
| # expressions that must keep running at every point | ||
| return clusters | ||
|
|
||
| guard = guards.pop() | ||
| if not all(_is_strideable(c, d, guard) for c in clusters): | ||
| return clusters | ||
|
|
||
| cd = guard.d | ||
| factor = cd.symbolic_factor | ||
|
|
||
| processed = [] | ||
| for c in clusters: | ||
| # Iterate the subsampled Dimension itself, with unit stride, and | ||
| # rescale every parent access `d` to `cd*factor` | ||
| ispace = c.ispace.switch(d, cd) | ||
|
|
||
| # `IntDiv(d, factor)` is how the subsampled access was lowered; | ||
| # with `d -> cd*factor` it collapses to `cd`, so rewrite it | ||
| # directly rather than leaving `(cd*factor)/factor` behind | ||
| subs = {IntDiv(d, factor): cd, d: cd*factor} | ||
| exprs = [uxreplace(e, subs) for e in c.exprs] | ||
|
|
||
| # `cd` inherits `d`'s computational properties: it covers the very | ||
| # same points, just addressed in subsampled coordinates. Without | ||
| # this the loop would lose PARALLEL and so all of its OpenMP | ||
| properties = c.properties.drop(d) | ||
| properties = properties.add(cd, c.properties[d]) | ||
|
|
||
| processed.append(c.rebuild(exprs=exprs, ispace=ispace, | ||
| guards=c.guards.popany(d), | ||
| properties=properties)) | ||
|
|
||
| return processed | ||
|
|
||
|
|
||
| def _is_strideable(cluster, d, guard): | ||
| """ | ||
| True if iteration over `d` may step `guard`'s subsampling factor points at | ||
| a time in place of evaluating `guard` at every point. | ||
| """ | ||
| # The guard must be exactly a subsampling guard. Anything `And`/`Or`-ed in | ||
| # (e.g. a user-supplied `condition`) must still be evaluated pointwise | ||
| if not isinstance(guard, GuardFactor): | ||
| return False | ||
|
|
||
| if d not in cluster.ispace.itdims: | ||
| return False | ||
|
|
||
| # Skipping points is only sound if no dependence is carried along `d`; | ||
| # otherwise the discarded iterations may be doing work that `d`'s | ||
| # remaining iterations rely upon | ||
| if not cluster.properties.is_parallel_relaxed(d): | ||
| return False | ||
|
|
||
| # Non-affine accesses may read the points that would be skipped | ||
| return cluster.properties.is_affine(d) | ||
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 |
|---|---|---|
|
|
@@ -116,6 +116,7 @@ class Dimension(ArgProvider): | |
| is_Custom = False | ||
| is_Derived = False | ||
| is_NonlinearDerived = False | ||
| is_Injective = False | ||
| is_AbstractSub = False | ||
| is_Sub = False | ||
| is_MultiSub = False | ||
|
|
@@ -1090,6 +1091,19 @@ def __init_finalize__(self, name, parent=None, factor=None, condition=None, | |
| self._indirect = indirect | ||
| self._relation = relation | ||
|
|
||
| @property | ||
| def is_Injective(self): | ||
| """ | ||
| True if distinct iterations of the parent that pass the guard map to | ||
| distinct indices of `self`, so that no two iterations can access the | ||
| same memory location through it. | ||
|
|
||
| A pure factor subsampling is injective: the guard admits only | ||
| `parent % factor == 0`, and `i -> i//factor` is one-to-one on those. | ||
| An arbitrary `condition` carries no such promise. | ||
| """ | ||
| return self._factor is not None and self._condition is None | ||
|
|
||
| @property | ||
| def uses_symbolic_factor(self): | ||
| return self._factor is not None | ||
|
|
@@ -1153,13 +1167,18 @@ def _arg_values(self, interval, grid=None, args=None, **kwargs): | |
| if isinstance(fact, Constant): | ||
| fact = fact.data | ||
|
|
||
| toint = lambda x: math.ceil(x / fact) | ||
| vals = {} | ||
|
|
||
| # The first subsampled point at or after the parent's min... | ||
| with suppress(KeyError, TypeError): | ||
| vals[self.min_name] = toint(kwargs.get(self.parent.min_name)) | ||
| vals[self.min_name] = math.ceil(kwargs.get(self.parent.min_name) / fact) | ||
|
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. to be sure, '/' or '//' ?
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.
|
||
|
|
||
| # ...and the last one at or before its max. Note `floor` rather than | ||
| # `ceil`: with e.g. `factor=3` and `x_M=11` the last subsampled point | ||
| # is `x=9`, hence index 3, while `ceil(11/3) == 4` would overshoot the | ||
| # Function by one | ||
| with suppress(KeyError, TypeError): | ||
| vals[self.max_name] = toint(kwargs.get(self.parent.max_name)) | ||
| vals[self.max_name] = kwargs.get(self.parent.max_name) // fact | ||
|
|
||
| vals[self.symbolic_factor.name] = fact | ||
|
|
||
|
|
||
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.
indentation