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

Refactor Parser #3265

Merged
merged 1 commit into from
May 17, 2023
Merged

Conversation

WeiqunZhang
Copy link
Member

Refactor Parser to allow for further optimization. One issue of the old version is its types have different sizes. That makes it hard to swap nodes that are different types. In the current version, even though the types still have different sizes, the malloc calls allocate enough memory so that we can cast from one type to another. Another issue is that there are a lot more AST node types in the old version than in the current versions. Many of them are introduced for optimization. In the current version, a lot of AST node types including subtraction have been removed. So (- a b) (i.e., a-b) now becomes (+ a (* -1 b)). The seems less efficient. However this makes it much easier to optimize the AST because of the associative property of add. Note that after the AST is generated there is a compilation step that flattens the AST into a sequence of instructions. The removed types (e.g., sub) and special instructions are now added in the compilation step. The new version could be significantly faster for many non-simplifed expressions. For example, sin(x) + y**2*y*sin(x) is about 2x faster, because the new version optimizes it to (1+y**2)*sin(x). However, there could be no performance improvement if the expression is already simplified.

In addition to the refactor and optimization, we have also added a few new math functions, atan2, asinh, acosh, and atanh, for completeness.

I have tested the new version with thousands of expressions.

The new version will have roundoff errors with the old version.

Src/Base/Parser/AMReX_Parser_Y.cpp Dismissed Show dismissed Hide dismissed
Src/Base/Parser/AMReX_Parser_Y.cpp Fixed Show fixed Hide fixed
Src/Base/Parser/AMReX_Parser_Y.cpp Dismissed Show dismissed Hide dismissed
@WeiqunZhang WeiqunZhang force-pushed the optimize_parser branch 6 times, most recently from cbeea1b to f59976b Compare May 2, 2023 22:36
@WeiqunZhang WeiqunZhang marked this pull request as ready for review May 2, 2023 22:37
Refactor Parser to allow for further optimization. One issue of the old
version is its types have different sizes. That makes it hard to swap nodes
that are different types. In the current version, even though the types
still have different sizes, the malloc calls allocate enough memory so that
we can cast from one type to another. Another issue is that there are a lot
more AST node types in the old version than in the current versions. Many of
them are introduced for optimization. In the current version, a lot of AST
node types including subtraction have been removed. So `(- a b)` (i.e.,
`a-b`) now becomes `(+ a (* -1 b))`. The seems less efficient. However this
makes it much easier to optimize the AST because of the associative property
of add. Note that after the AST is generated there is a compilation step
that flattens the AST into a sequence of instructions. The removed types
(e.g., sub) and special instructions are now added in the compilation
step. The new version could be significantly faster for many non-simplifed
expressions. For example, `sin(x) + y**2*y*sin(x)` is about 2x faster,
because the new version optimizes it to `(1+y**2)*sin(x)`.  However, there
could be no performance improvement if the expression is already simplified.

In addition to the refactor and optimization, we have also added a few new
math functions, atan2, asinh, acosh, and atanh, for completeness.

I have tested the new version with thousands of expressions.

The new version will have roundoff errors with the old version.
@WeiqunZhang WeiqunZhang merged commit 23b1579 into AMReX-Codes:development May 17, 2023
56 checks passed
@WeiqunZhang WeiqunZhang deleted the optimize_parser branch May 17, 2023 23:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants