-
-
Notifications
You must be signed in to change notification settings - Fork 211
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
Add support for simple deduplication of terms (for improved performance) when building functions #698
Conversation
…ostly useful for Terms that are the result of function calls).
@YingboMa what about using this for
|
test/deduplication_terms_test.jl
Outdated
@@ -0,0 +1,46 @@ | |||
using Revise # DELETE ME |
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.
.
Thanks @ChrisRackauckas for your comment, I agree the name is bad. I am not sure what you are referring to about |
Yes no worries. I'm going to ask Chris Foster for help finishing the other PR once the holidays are completed, and for here I think it's up to @YingboMa and @shashi as they are digging through |
ode_function_deduplicated = ODEFunction(ode_system; jac=false, tgrad=false, eval_expression=false, deduplicate_terms=dupe_terms) | ||
|
||
# Run both functions and compare... | ||
u0 = rand(Float64, length(x)) |
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.
x
is not defined anywhere. I suggest you to use @safetestset
to run the tests locally, so that you won't pollute your test environment.
I don't think pushing dup terms into a let block is extendable. The expression could be x = expensivefun1()
y = x + expensivefun2() and a let block cannot handle that. In general, some kind of dependency analysis is also needed here. If you don't need this feature urgently, I'd suggest to wait for a more general handling of |
@YingboMa @ChrisRackauckas Any news on the idea of |
We discussed all of this and for performance, it's best to do this automatically. What you're calling "de-duplicate" is common subexpression elimination (CSE) which the next generation of build_function has planned to have a switch for to do in full. See JuliaSymbolics/SymbolicUtils.jl#121 and JuliaSymbolics/SymbolicUtils.jl#200 . Given those pieces, I'll be closing this but let me know if you have a use case where directly doing CSE by hand for only a few chosen terms is necessary, as the compiler style will automatically apply it to all cases. |
This adds a
deduplicate_terms
argument tobuild_function()
that allows the user to provide a list ofTerm
s that they want to "deduplicate". Essentially, theseTerm
s will be computed upfront before the system equations, and their values are substituted in to the system equations, thereby potentially saving a lot of extra processing. These are computed inside theiip
/oop
'slet
block, so the user has access to their variable names (e.g. they can uset
).A test is provided demonstrating the use and showing the speed up afforded.
NOTE: I've only added support for non-parallel, non-scalar output Julia-target functions. It will throw an error if a user tries to provide both a
parallel
/multithread
anddeduplicate_terms
.