-
Notifications
You must be signed in to change notification settings - Fork 245
dsl: Introduce abstractions for multi-stage time integrators #2599
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 4 commits
1d830b8
7f087b3
214d882
d6c4d4a
78f8a0b
1c9d517
11db48b
83dfb04
d47a106
1f93a45
eea3a52
11d1429
4637ac2
ac1da7e
e9b3533
dc3dd77
1fd4a02
a0c45c1
93c6e3f
ef8d1ac
fa5acac
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 |
|---|---|---|
|
|
@@ -13,7 +13,9 @@ | |
| from devito.data.allocators import DataReference | ||
| from devito.logger import warning | ||
|
|
||
| __all__ = ['dimension_sort', 'lower_exprs', 'concretize_subdims'] | ||
| from devito.types.multistage import MultiStage | ||
|
|
||
| __all__ = ['dimension_sort', 'lower_multistage', 'lower_exprs', 'concretize_subdims'] | ||
|
|
||
|
|
||
| def dimension_sort(expr): | ||
|
|
@@ -95,6 +97,34 @@ def handle_indexed(indexed): | |
| return ordering | ||
|
|
||
|
|
||
| def lower_multistage(expressions): | ||
| """ | ||
| Separating the multi-stage time-integrator scheme in stages: | ||
| * If the object is MultiStage, it creates the stages of the method. | ||
| """ | ||
| lowered = [] | ||
| for i, eq in enumerate(as_tuple(expressions)): | ||
| lowered.extend(_lower_multistage(eq, i)) | ||
| return lowered | ||
|
|
||
|
|
||
| @singledispatch | ||
| def _lower_multistage(expr, index): | ||
| """ | ||
| Default handler for expressions that are not MultiStage. | ||
| Simply return them in a list. | ||
| """ | ||
| return [expr] | ||
|
|
||
|
|
||
| @_lower_multistage.register | ||
|
||
| def _(expr: MultiStage, index): | ||
| """ | ||
| Specialized handler for MultiStage expressions. | ||
| """ | ||
| return expr.method(expr.eq.rhs, expr.eq.lhs)._evaluate(eq_num=index) | ||
|
|
||
|
|
||
| def lower_exprs(expressions, subs=None, **kwargs): | ||
| """ | ||
| Lowering an expression consists of the following passes: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ | |
| from devito.finite_differences.derivative import Derivative | ||
| from devito.tools import as_tuple | ||
|
|
||
| from devito.types.multistage import MultiStage | ||
|
|
||
| __all__ = ['solve', 'linsolve'] | ||
|
|
||
|
|
||
|
|
@@ -15,7 +17,7 @@ class SolveError(Exception): | |
| pass | ||
|
|
||
|
|
||
| def solve(eq, target, **kwargs): | ||
| def solve(eq, target, method = None, eq_num = 0, **kwargs): | ||
|
||
| """ | ||
| Algebraically rearrange an Eq w.r.t. a given symbol. | ||
|
|
||
|
|
@@ -56,9 +58,15 @@ def solve(eq, target, **kwargs): | |
|
|
||
| # We need to rebuild the vector/tensor as sympy.solve outputs a tuple of solutions | ||
| if len(sols) > 1: | ||
| return target.new_from_mat(sols) | ||
| sols_temp=target.new_from_mat(sols) | ||
|
||
| else: | ||
| sols_temp=sols[0] | ||
|
|
||
| if method is not None: | ||
| method_cls = MultiStage._resolve_method(method) | ||
|
||
| return method_cls(sols_temp, target)._evaluate(eq_num=eq_num) | ||
| else: | ||
| return sols[0] | ||
| return sols_temp | ||
|
|
||
|
|
||
| def linsolve(expr, target, **kwargs): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,7 @@ | |
| InvalidOperator) | ||
| from devito.logger import (debug, info, perf, warning, is_log_enabled_for, | ||
| switch_log_level) | ||
| from devito.ir.equations import LoweredEq, lower_exprs, concretize_subdims | ||
| from devito.ir.equations import LoweredEq, lower_multistage, lower_exprs, concretize_subdims | ||
| from devito.ir.clusters import ClusterGroup, clusterize | ||
| from devito.ir.iet import (Callable, CInterface, EntryFunction, FindSymbols, | ||
| MetaCall, derive_parameters, iet_build) | ||
|
|
@@ -36,7 +36,6 @@ | |
| disk_layer) | ||
| from devito.types.dimension import Thickness | ||
|
|
||
|
|
||
|
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. Please run the linter (
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. done |
||
| __all__ = ['Operator'] | ||
|
|
||
|
|
||
|
|
@@ -327,6 +326,8 @@ def _lower_exprs(cls, expressions, **kwargs): | |
| * Apply substitution rules; | ||
| * Shift indices for domain alignment. | ||
| """ | ||
| expressions=lower_multistage(expressions) | ||
|
||
|
|
||
| expand = kwargs['options'].get('expand', True) | ||
|
|
||
| # Specialization is performed on unevaluated expressions | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,3 +22,5 @@ | |
| from .relational import * # noqa | ||
| from .sparse import * # noqa | ||
| from .tensor import * # noqa | ||
|
|
||
| from .multistage import * | ||
|
||
|
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. I think this file should be moved to somewhere like
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. I renamed the class to Regarding the file location, it’s currently in |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,186 @@ | ||||||
| # from devito import Function, Eq | ||||||
|
||||||
| from .equation import Eq | ||||||
|
||||||
| from .dense import Function | ||||||
| from devito.symbolics import uxreplace | ||||||
|
|
||||||
| from .array import Array # Trying Array | ||||||
|
|
||||||
|
|
||||||
| class MultiStage(Eq): | ||||||
| """ | ||||||
| Abstract base class for multi-stage time integration methods | ||||||
| (e.g., Runge-Kutta schemes) in Devito. | ||||||
|
|
||||||
| This class wraps a symbolic equation of the form `target = rhs` and | ||||||
| provides a mechanism to associate a time integration scheme via the | ||||||
| `method` argument. Subclasses must implement the `_evaluate` method to | ||||||
| generate stage-wise update expressions. | ||||||
|
|
||||||
| Parameters | ||||||
| ---------- | ||||||
| rhs : expr-like | ||||||
| The right-hand side of the equation to integrate. | ||||||
| target : Function | ||||||
| The time-updated symbol on the left-hand side, e.g., `u` or `u.forward`. | ||||||
| method : str or None | ||||||
|
||||||
| A string identifying the time integration method (e.g., 'RK44'), | ||||||
| which must correspond to a class defined in the global scope and | ||||||
| implementing `_evaluate`. If None, no method is applied. | ||||||
|
|
||||||
| Attributes | ||||||
| ---------- | ||||||
| eq : Eq | ||||||
| The symbolic equation `target = rhs`. | ||||||
| method : class | ||||||
| The integration method class resolved from the `method` string. | ||||||
| """ | ||||||
|
|
||||||
| def __new__(cls, rhs, target, method=None): | ||||||
|
||||||
| eq = Eq(target, rhs) | ||||||
| obj = Eq.__new__(cls, eq.lhs, eq.rhs) | ||||||
| obj._eq = eq | ||||||
| obj._method = cls._resolve_method(method) | ||||||
| return obj | ||||||
|
|
||||||
| @classmethod | ||||||
| def _resolve_method(cls, method): | ||||||
| try: | ||||||
| if cls is MultiStage: | ||||||
| return globals()[method] | ||||||
|
||||||
| else: | ||||||
| return cls | ||||||
| except KeyError: | ||||||
| raise ValueError(f"The time integrator '{method}' is not implemented.") | ||||||
|
|
||||||
| @property | ||||||
| def eq(self): | ||||||
|
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. Can be dropped with the restructured
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. done |
||||||
| return self._eq | ||||||
|
|
||||||
| @property | ||||||
| def method(self): | ||||||
| return self._method | ||||||
|
|
||||||
| def _evaluate(self, expand=False): | ||||||
|
||||||
| raise NotImplementedError( | ||||||
| f"_evaluate() must be implemented in the subclass {self.__class__.__name__}") | ||||||
|
|
||||||
|
|
||||||
| class RK(MultiStage): | ||||||
|
||||||
| class RK(MultiStage): | |
| class RungeKutta(MultiStage): |
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.
done
Outdated
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.
This seems strangely formed. I would have a, b, and c as either positional args or kwargs. If self.s is constructed from knowledge about these parameters, then it should be a cached_property
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.
I worked on this. Can you confirm if this is what you meant?
Outdated
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.
This error handling should happen at the point where these values are first supplied.
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.
I worked on this too. Can you confirm if this is what you meant?
Outdated
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.
Should have **kwargs for consistency with Eq._evaluate()
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.
done
Outdated
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.
You shouldn't need counters like this. Use the SymbolRegistry (called sregistry) for this operator build and purge this kwarg
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.
I tried to incorporate this suggestion, but I could use a bit of help. I think I should create a variable like
sym_registry=SymbolRegistry()
and then call
sym_registry.make_name(prefix='k')
However, I’m not sure where exactly I should declare the variable, or if this is the best approach overall.
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.
Nitpick: they will be Devito Eq objects
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.
done
Outdated
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.
With the __new__ specified above, these would just be u = self.lhs.function etc, which would clean things up
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.
done
Outdated
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.
Nitpick: these are Array now
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.
right!
Outdated
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.
These are internal to Devito, should not appear in operator arguments, and should not be touched by the user, and so should use Array, not Function
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.
The thing is, when I try using Array, the second test fails, but if I use Functions, it works without any errors. I'm not sure if I'm doing something wrong.
Outdated
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.
I would set these as tuples in the __init__. Definitely should not be mutable if set on a class level.
I would personally instead have a
def __init__(self):
a = (...
b = (...
c = (...
super.__init__(a=a, b=b, c=c)
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.
I did something like that... could you check?
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.
Rather than having the the
as_tuplehere, why not have a dispatch for_lower_multistagethat dispatches on iterable types as per_concretize_subdims?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.
done, I think..