-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2327 from devitocodes/error-checking
compiler: Add optional pass for runtime stability check
- Loading branch information
Showing
9 changed files
with
165 additions
and
9 deletions.
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) |