-
Notifications
You must be signed in to change notification settings - Fork 230
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
compiler: Add optional pass for runtime stability check #2327
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 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 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 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 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,87 @@ | ||
import cgen as c | ||
import numpy as np | ||
from sympy import Not | ||
|
||
from devito.ir.iet import (Call, Conditional, EntryFunction, Iteration, List, | ||
Return, FindNodes, FindSymbols, Transformer, | ||
make_callable) | ||
from devito.passes.iet.engine import iet_pass | ||
from devito.symbolics import CondEq, DefFunction | ||
from devito.types import Eq, Inc, Symbol | ||
|
||
__all__ = ['check_stability', 'error_mapper'] | ||
|
||
|
||
def check_stability(graph, options=None, rcompile=None, sregistry=None, **kwargs): | ||
""" | ||
Check if the simulation is stable. If not, return to Python as quickly as | ||
possible with an error code. | ||
""" | ||
if options['errctl'] != 'max': | ||
return | ||
|
||
_, wmovs = graph.data_movs | ||
|
||
_check_stability(graph, wmovs=wmovs, rcompile=rcompile, sregistry=sregistry) | ||
|
||
|
||
@iet_pass | ||
def _check_stability(iet, wmovs=(), rcompile=None, sregistry=None): | ||
if not isinstance(iet, EntryFunction): | ||
return iet, {} | ||
|
||
# NOTE: Stability is a domain-specific concept, hence looking for time | ||
# Iterations and TimeFunctions is acceptable | ||
efuncs = [] | ||
includes = [] | ||
mapper = {} | ||
for n in FindNodes(Iteration).visit(iet): | ||
if not n.dim.is_Time: | ||
continue | ||
|
||
functions = [f for f in FindSymbols().visit(n) | ||
if f.is_TimeFunction and f.time_dim.is_Stepping] | ||
|
||
# We compute the norm of just one TimeFunction, hence we sort for | ||
# determinism and reproducibility | ||
candidates = sorted(set(functions) & set(wmovs), key=lambda f: f.name) | ||
for f in candidates: | ||
if f in wmovs: | ||
break | ||
else: | ||
continue | ||
|
||
accumulator = Symbol(name='accumulator', dtype=f.dtype) | ||
eqns = [Eq(accumulator, 0.0), | ||
Inc(accumulator, f.subs(f.time_dim, 0))] | ||
irs, byproduct = rcompile(eqns) | ||
|
||
name = sregistry.make_name(prefix='is_finite') | ||
retval = Return(DefFunction('isfinite', accumulator)) | ||
body = irs.iet.body.body + (retval,) | ||
efunc = make_callable(name, body, retval='int') | ||
|
||
efuncs.extend([i.root for i in byproduct.funcs]) | ||
efuncs.append(efunc) | ||
|
||
includes.extend(byproduct.includes) | ||
|
||
name = sregistry.make_name(prefix='check') | ||
check = Symbol(name=name, dtype=np.int32) | ||
|
||
errctl = Conditional(CondEq(n.dim % 100, 0), List(body=[ | ||
Call(efunc.name, efunc.parameters, retobj=check), | ||
Conditional(Not(check), Return(error_mapper['Stability'])) | ||
])) | ||
errctl = List(header=c.Comment("Stability check"), body=[errctl]) | ||
mapper[n] = n._rebuild(nodes=n.nodes + (errctl,)) | ||
|
||
iet = Transformer(mapper).visit(iet) | ||
|
||
return iet, {'efuncs': efuncs, 'includes': includes} | ||
|
||
|
||
error_mapper = { | ||
'Stability': 100, | ||
'KernelLaunch': 200, | ||
} |
This file contains 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,27 @@ | ||
import pytest | ||
|
||
from devito import Grid, Function, TimeFunction, Eq, Operator, switchconfig | ||
from devito.exceptions import ExecutionError | ||
|
||
|
||
@switchconfig(safe_math=True) | ||
@pytest.mark.parametrize("expr", [ | ||
'u/f', | ||
'(u + v)/f', | ||
]) | ||
def test_stability(expr): | ||
grid = Grid(shape=(10, 10)) | ||
|
||
f = Function(name='f', grid=grid, space_order=2) # noqa | ||
u = TimeFunction(name='u', grid=grid, space_order=2) | ||
v = TimeFunction(name='v', grid=grid, space_order=2) | ||
|
||
eq = Eq(u.forward, eval(expr)) | ||
|
||
op = Operator(eq, opt=('advanced', {'errctl': 'max'})) | ||
|
||
u.data[:] = 1. | ||
v.data[:] = 2. | ||
|
||
with pytest.raises(ExecutionError): | ||
op.apply(time_M=200, dt=.1) |
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.
Nitpicking: Might be cases where there is no stepping dim and only time dime, but can be for later if someone runs into it (unlikely).