Address #13: Use sympy.codegen.rewriting.optimize
It's really comfortable to write optimizations in terms of sympy.codegen.rewrite.RewriteOptim
:
# Evaluates all constant terms
evaluate_constant_terms = ReplaceOptim(
lambda e: hasattr(e, 'is_constant') and e.is_constant,
lambda p: p.evalf()
)
This PR adds a parameter sympy_optimizations
to the create_*_kernel
functions that applies the list of optimizations to the assignments before creating the AST.
sympy.codegen.rewrite
already has some optimizations. Some similar to the optimizations of pystencils.
For example create_expand_pow_optimization(limit)
is really similar to the logic in CustomSympyPrinter._print_Pow
.
See #13 (closed)
Problem: old versions of sympy (e.g. from ubuntu CI) don't have sympy.codegen.rewrite
. The optimizations are skipped in that case. test_and_coverage
applies all optimizations.
We could also try to implement a fma-optimization (fused-multipy add) with that and sympy.Wild
.
Merge request reports
Activity
changed title from Address #13 (closed): Use codegen.rewriting.optimize to Address #13 (closed): Use sympy.codegen.rewriting.optimize
added 1 commit
- 0cc6af3d - Add assumption is_integer == True to IntegerFunctionTwoArgsMixIn
Looks good! I didn't know about the sympy optimization rules. One small thing though: Is it necessary to call this in all the
create_kernel
functions? I'd prefer that the user calls the optimization explicitly, or create a simplification strategy. At least this is the way all the other optimization currently work. The user would then separately doassignments = optimize(assignments) ast = create_kernel(assignments)
added 45 commits
-
0cc6af3d...64734915 - 33 commits from branch
pycodegen:master
- 938e0de0 - Let iterator of AssignmentCollection iterate over all_assignments (not only main_assignments)
- 280f28f9 - Add pystencils.optimizations
- 7ab81857 - Apply default optimizations in `create_kernel` routines
- 0c671ea0 - Add expm1 test
- f6b324a5 - Add sympy optimization: evaluate_constant_terms
- 38bea5c9 - Regard case that pystencils.astnodes.Conditional is in assignments or...
- 47f9d708 - Rename: pystencils.optimizations -> pystencils.math_optimizations
- 873969f9 - Avoid optimizations of IntegerFunctionTwoArgsMixIn
- d8327971 - Rename in create_kernel: optimizations -> sympy_optimizations
- b0aa8134 - Do not evalf() if expression is_integer
- 58558da9 - Add assumption functions to cast_func
- 4ed5b6a6 - Add assumption is_integer == True to IntegerFunctionTwoArgsMixIn
Toggle commit list-
0cc6af3d...64734915 - 33 commits from branch
added 16 commits
-
4ed5b6a6...ddbe84da - 4 commits from branch
pycodegen:master
- 268f3e26 - Let iterator of AssignmentCollection iterate over all_assignments (not only main_assignments)
- 3dd6f491 - Add pystencils.optimizations
- b731bc49 - Apply default optimizations in `create_kernel` routines
- 1286f042 - Add expm1 test
- 03fe9ab0 - Add sympy optimization: evaluate_constant_terms
- 3531885b - Regard case that pystencils.astnodes.Conditional is in assignments or...
- 2e23b82e - Rename: pystencils.optimizations -> pystencils.math_optimizations
- dfc5428b - Avoid optimizations of IntegerFunctionTwoArgsMixIn
- b3af8449 - Rename in create_kernel: optimizations -> sympy_optimizations
- 5cdf85c4 - Do not evalf() if expression is_integer
- 06964419 - Add assumption functions to cast_func
- c9c9ca55 - Add assumption is_integer == True to IntegerFunctionTwoArgsMixIn
Toggle commit list-
4ed5b6a6...ddbe84da - 4 commits from branch
added 1 commit
- e8a92a9e - Suppress evaluation of constants in test_sum_prod
mentioned in merge request !47 (closed)
added 24 commits
-
e8a92a9e...71b8767b - 20 commits from branch
pycodegen:master
- 8479aa27 - Let iterator of AssignmentCollection iterate over all_assignments (not only main_assignments)
- da5cfa52 - Add dtype assumptions to cast_func
- c3a4fb73 - Add property is_integer -> True to IntegerFunctionTwoArgsMixIn
- 8d610054 - Add pystencils.math_optimizations
Toggle commit list-
e8a92a9e...71b8767b - 20 commits from branch
mentioned in merge request !54 (closed)
@bauer I made this PR less controversial by removing all changes to kernelcreation.
I also didn't want to add another parameter to
create_kernel
since it already has a lot. However, I want pystencils to always optimize and still give the user the chance to add own optimizations or remove some. I thought this concept is pretty abstract to include custom optimizations forsympy.Pow
(no inCustomSympyPrinter
and by that steer the behavior of pystencils.So this would be a parameter to
create_kernel
to avoid parameters tocreate_kernel
in future.Created a follow-up PR with these changes: !54 (closed)
mentioned in commit 97307848