-
-
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
Closed
Closed
Add support for simple deduplication of terms (for improved performance) when building functions #698
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0241a8d
Add capability to deduplicate specific terms in the build_function (m…
dpad 0f72ac4
Support converting a Function call to a Symbol as well.
dpad 6f1f4fe
Add deduplication of terms test.
dpad ae0179a
Update test/deduplication_terms_test.jl
ChrisRackauckas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using Revise # DELETE ME | ||
ChrisRackauckas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
using Test | ||
using ModelingToolkit | ||
using LinearAlgebra.BLAS | ||
ChrisRackauckas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@parameters t | ||
@variables vars[1:10] | ||
@derivatives D'~t | ||
|
||
const sleep_time = 0.1 | ||
function untrace_dupe_f(a::Float64, t::Float64) | ||
# This is a function that is both expensive and untraceable (symbolically), | ||
# and whose result is used several times in our ODE system of equations. | ||
sleep(sleep_time) # Expensive function! | ||
BLAS.dot(10, fill(a, 10), 1, fill(t, 20), 2) # Untraceable result! | ||
end | ||
@test untrace_dupe_f(5., 12.) == 600.0 | ||
|
||
# Confirm that our function cannot be traced symbolically... | ||
@test_throws MethodError untrace_dupe_f(vars[1], t) | ||
|
||
# Register the function so we can now use it without having to trace it. | ||
@register untrace_dupe_f(a, t) | ||
|
||
# Build an ODE system of equations that uses several duplicated, untraceable terms. | ||
dupe_terms = (untrace_dupe_f(x, t) for x in vars) | ||
eqs = (D.(vars) .~ prod(dupe_terms)) | ||
@test length(eqs) == length(vars) | ||
|
||
# Build the ODE functions. | ||
ode_system = ODESystem(eqs, t, vars, []) | ||
ode_function_naive = ODEFunction(ode_system; jac=false, tgrad=false, eval_expression=false) | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
t0 = 10.0 | ||
result_naive = ode_function_naive(u0, [], t0) | ||
result_deduplicated = ode_function_deduplicated(u0, [], t0) | ||
@test result_naive == result_deduplicated | ||
|
||
# The deduplicated version should run much faster, since it does not need to recompute the terms multiple times. | ||
fuzzy_factor = 0.9 | ||
expected_speedup = fuzzy_factor * length(eqs) | ||
@test @elapsed(ode_function_naive(u0, [], t0)) > (expected_speedup * @elapsed(ode_function_deduplicated(u0, [], t0))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
.