Skip to content
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: Implement graceful lowering of derivatives (aka "unexpansion") #2060

Merged
merged 61 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
f799a72
compiler: Prototype unexpansion
FabioLuporini Nov 24, 2022
ffc8c21
compiler: Revamp code generation from _C_ctype
FabioLuporini Nov 25, 2022
91eee45
compiler: Support trivial unexpanded-derivatives examples
FabioLuporini Nov 26, 2022
0eddd5d
dsl: Patch cross_derivative evaluation
FabioLuporini Nov 26, 2022
63f41e8
dsl: Introduce Spacing subclass
FabioLuporini Nov 28, 2022
a94ab05
compiler: Patch StencilDimension reconstruction
FabioLuporini Nov 28, 2022
ce7c96c
compiler: Extend unexpansion machinery
FabioLuporini Nov 26, 2022
4d63f17
compiler: Support StencilDimension in estimate_cost
FabioLuporini Dec 3, 2022
380d262
compiler: Enhance fusion upon lower_index_derivative
FabioLuporini Dec 3, 2022
45b5fa4
compiler: Rework is_cross rule for Cluster fusion
FabioLuporini Dec 10, 2022
8805cd9
compiler: Implement maximal fusion for lowered IndexDerivatives
FabioLuporini Dec 12, 2022
573ba59
compiler: Patch profiling in presence of StencilDimensions
FabioLuporini Dec 12, 2022
fda18ef
compiler: Improve IndexDerivative lowering to catch duplicates
FabioLuporini Dec 12, 2022
be02291
compiler: Enhance pow_to_mul to work around SymPy misbehavior
FabioLuporini Dec 13, 2022
c069dfe
compiler: Rework globals generation for device backends
FabioLuporini Dec 13, 2022
573b4cb
compiler: Rework weights generation for device backends
FabioLuporini Dec 13, 2022
eb8616c
compiler: Patch index mode detection with StencilDimensions
FabioLuporini Dec 15, 2022
0044b3b
compiler: Add IndexDerivative.mapper
FabioLuporini Dec 22, 2022
fb6c226
compiler: Patch lower_index_derivative
FabioLuporini Dec 23, 2022
9a6679a
tests: Patch draft flaky unexpansion test
FabioLuporini Dec 23, 2022
958c9a0
compiler: Patch lower_index_derivatives
FabioLuporini Dec 23, 2022
3f73098
compiler: Patch globs codegen for deterministic output
FabioLuporini Dec 27, 2022
8d28be1
compiler: Relax Properties manipulation methods
FabioLuporini Dec 27, 2022
3eecf4d
compiler: Change IndexDerivative.mapper
FabioLuporini Dec 30, 2022
9a843ba
compiler: Add IterationSpace.translate
FabioLuporini Jan 3, 2023
52e076a
compiler: Move IndexSum.mapper to IndexDerivative.mapper
FabioLuporini Jan 3, 2023
a993d0d
compiler: Patch IndexDerivative.mapper
FabioLuporini Jan 5, 2023
417dac4
compiler: Relax WAR dependencies involving shared Array
FabioLuporini Jan 9, 2023
9c6ac52
compiler: Maximize likelihood of fusing clusters over shm
FabioLuporini Jan 9, 2023
42627b1
compiler: Improve data dependence analysis
FabioLuporini Jan 10, 2023
fead97c
compiler: Add Jump mixin class
FabioLuporini Jan 11, 2023
19f6af3
compiler: Patch collect_derivative pass
FabioLuporini Jan 13, 2023
17ab99b
compiler: Add shm-related heuristics to Cluster fusion
FabioLuporini Jan 20, 2023
c2044d5
compiler: Add Properties methods
FabioLuporini Jan 21, 2023
60e2e03
compiler: Make IndexDerivatives comparable; fix their CSE
FabioLuporini Jan 25, 2023
e5ea4d6
compiler: Draft Guards, akin to Properties
FabioLuporini Jan 31, 2023
9d579b1
compiler: Rework customization of clusters visitors
FabioLuporini Jan 31, 2023
fcc2e86
compiler: Fix Cluster properties normalization at init
FabioLuporini Jan 31, 2023
50025f5
compiler: Extend uxreplace to substitute types as well
FabioLuporini Feb 3, 2023
3496221
compiler: Fixup linearization with isolated routines
FabioLuporini Feb 3, 2023
fcfa448
misc: Fixup pep8 violations
FabioLuporini Feb 4, 2023
c562c30
compiler: Introduce AffineIndexAccessFunction
FabioLuporini Feb 6, 2023
43e700f
compiler: Improve IndexDerivative
FabioLuporini Feb 6, 2023
a588b6d
compiler: Enhance dtype retrieval
FabioLuporini Feb 6, 2023
18bc963
compiler: Tidy up Interval.expand()
FabioLuporini Feb 6, 2023
2640df3
compiler: Drop has_free for compatibility with older SymPy versions
FabioLuporini Feb 7, 2023
5113825
examples: Update expected notebook output
FabioLuporini Feb 7, 2023
0297597
compiler: Patch codegen upon pow_to_mul
FabioLuporini Feb 7, 2023
2c5eb4d
misc: Postpone codegen speed improvement
FabioLuporini Feb 7, 2023
403a194
examples: Update expected output
FabioLuporini Feb 8, 2023
62e0fc8
examples: Disable openmp where necessary due to issue 2061
FabioLuporini Feb 8, 2023
0c801bd
compiler: Exploit SubDim.local to support nasty deps in examples
FabioLuporini Feb 8, 2023
a97f6e6
ci: Drop support for gcc5, sympy1.7, sympy1.8
FabioLuporini Feb 8, 2023
77eae11
compiler: Add IndexDerivative.total_order
FabioLuporini Feb 8, 2023
9af12f4
arch: Enable openmp with nvc on CPU
FabioLuporini Feb 8, 2023
f33fcfc
ci: Add back forgotten gcc-11
FabioLuporini Feb 9, 2023
88fafce
compiler: Tweak lower_index_derivatives
FabioLuporini Feb 9, 2023
dde53cc
compiler: IndexDerivative.total_order -> depth
FabioLuporini Feb 10, 2023
2ce2797
misc: Tweak docstring
FabioLuporini Feb 10, 2023
95b482c
compiler: Lift overrides from AffineIndexAccessFunc into IndexAccessFunc
FabioLuporini Feb 10, 2023
0453503
arch: Add amdclang mapping
FabioLuporini Feb 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
compiler: Support StencilDimension in estimate_cost
  • Loading branch information
FabioLuporini committed Feb 7, 2023
commit 4d63f17034f283d586d0daec5781aedbd5a88c42
1 change: 0 additions & 1 deletion devito/operator/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,6 @@ def _lower_clusters(cls, expressions, profiler=None, **kwargs):
clusters = cls._specialize_clusters(clusters, **kwargs)

# Operation count after specialization
#TODO: extend estimate_cost to parse StencilDimension correctly
final_ops = sum(estimate_cost(c.exprs) for c in clusters if c.is_dense)
try:
profiler.record_ops_variation(init_ops, final_ops)
Expand Down
23 changes: 22 additions & 1 deletion devito/symbolics/inspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import numpy as np
from sympy import Function, Indexed, Integer, Mul, Number, Pow, S, Symbol, Tuple

from devito.finite_differences import Derivative
from devito.finite_differences.differentiable import IndexDerivative
from devito.logger import warning
from devito.symbolics.extended_sympy import (INT, CallFromPointer, Cast,
DefFunction, ReservedWord)
from devito.symbolics.queries import q_routine
from devito.symbolics.search import search
from devito.tools import as_tuple
from devito.tools import as_tuple, prod

__all__ = ['compare_ops', 'count', 'estimate_cost']

Expand Down Expand Up @@ -220,3 +222,22 @@ def _(expr, estimate):
else:
flops += 1
return flops, False


@_estimate_cost.register(Derivative)
def _(expr, estimate):
return _estimate_cost(expr._evaluate(expand=False), estimate)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to have the Derivative know its own cost without evaluation that can be expensive for large expression (and that will be re-eavluated). In theory it should always be expr.fd_order * 2 * _estimate_cost(expr.expr) and would be correct with and without expand for free

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not entirely sure about that? doesn't it also depend on deriv_order, whether it's left/right/center , or perhaps even shifted, etc etc

Anyway, in practice you never ever run estimate_cost on unlowered expressions, hence, you never actually hit this handle...



@_estimate_cost.register(IndexDerivative)
def _(expr, estimate):
flops, _ = _estimate_cost(expr.expr, estimate)

# It's an increment
flops += 1

# To be multiplied by the number of points this index sum implicitly
# iterates over
flops *= prod(i._size for i in expr.dimensions)
mloubout marked this conversation as resolved.
Show resolved Hide resolved

return flops, False
10 changes: 8 additions & 2 deletions tests/test_dse.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from devito.symbolics import (INT, FLOAT, DefFunction, FieldFromPointer, # noqa
Keyword, SizeOf, estimate_cost, pow_to_mul, indexify)
from devito.tools import as_tuple, generator
from devito.types import Scalar, Array, Symbol
from devito.types import Array, Scalar, Symbol

from examples.seismic.acoustic import AcousticWaveSolver
from examples.seismic import demo_model, AcquisitionGeometry
Expand Down Expand Up @@ -225,6 +225,11 @@ def test_pow_to_mul(expr, expected):
('Eq(t0, ffp)', 0, True),
('Eq(t0, ffp + 1.)', 1, True),
('Eq(t0, ffp + ffp)', 1, True),
# W/ StencilDimensions
('Eq(fb, fd.dx)', 10, False),
('Eq(fb, fd.dx)', 10, True),
('Eq(fb, fd.dx._evaluate(expand=False))', 10, False),
('Eq(fb, fd.dx.dy + fa.dx)', 66, False),
])
def test_estimate_cost(expr, expected, estimate):
# Note: integer arithmetic isn't counted
Expand All @@ -241,6 +246,7 @@ def test_estimate_cost(expr, expected, estimate):
fa = Function(name='fa', grid=grid, shape=(4,), dimensions=(x,)) # noqa
fb = Function(name='fb', grid=grid, shape=(4,), dimensions=(x,)) # noqa
fc = Function(name='fc', grid=grid) # noqa
fd = Function(name='fd', grid=grid, space_order=4) # noqa
foo = lambda *args: DefFunction('foo', tuple(args)) # noqa
k = Keyword('k') # noqa
ffp = FieldFromPointer('size', fa._C_symbol) # noqa
Expand Down Expand Up @@ -2730,7 +2736,7 @@ def test_fullopt(self):
bns, _ = assert_blocking(op1, {'x0_blk0'}) # due to loop blocking

assert summary0[('section0', None)].ops == 50
assert summary0[('section1', None)].ops == 140
assert summary0[('section1', None)].ops == 148
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

! Εxpected ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it was miscounted before IIRC

assert np.isclose(summary0[('section0', None)].oi, 2.851, atol=0.001)

assert summary1[('section0', None)].ops == 31
Expand Down