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

Redesign default ODE solver to be fully type-grounded #2103

Closed
wants to merge 5 commits into from

Conversation

ChrisRackauckas
Copy link
Member

This accomplishes a few things:

  • Faster precompile times by precompiling less
  • Full inference of results when using the automatic algorithm
  • Hopefully faster load times by also precompiling less

This is done the same way as

and is thus the more modern SciML way of doing it. It avoids dispatch by having a single algorithm that always generates the full cache and instead of dispatching between algorithms always branches for the choice.

It turns out, the mechanism already existed for this in OrdinaryDiffEq... it's CompositeAlgorithm, the same bones as AutoSwitch! As such, this reuses quite a bit of code from the auto-switch algorithms but instead of just having two choices it (currently) has 6 that it chooses between. This means that it has stiffness detection and switching behavior, but also in a size-dependent way.

There are still some optimizations to do though. Like LinearSolve.jl, it would be more efficient to have a way to initialize the caches to size zero and then have a way to re-initialize them to the correct size. Right now, it'll generate the same Jacobian N times and it shouldn't need to do that.

This accomplishes a few things:

* Faster precompile times by precompiling less
* Full inference of results when using the automatic algorithm
* Hopefully faster load times by also precompiling less

This is done the same way as

* linearsolve SciML/LinearSolve.jl#307
* nonlinearsolve SciML/NonlinearSolve.jl#238

and is thus the more modern SciML way of doing it. It avoids dispatch by having a single algorithm that always generates the full cache and instead of dispatching between algorithms always branches for the choice.

It turns out, the mechanism already existed for this in OrdinaryDiffEq... it's CompositeAlgorithm, the same bones as AutoSwitch! As such, this reuses quite a bit of code from the auto-switch algorithms but instead of just having two choices it (currently) has 6 that it chooses between. This means that it has stiffness detection and switching behavior, but also in a size-dependent way.

There are still some optimizations to do though. Like LinearSolve.jl, it would be more efficient to have a way to initialize the caches to size zero and then have a way to re-initialize them to the correct size. Right now, it'll generate the same Jacobian N times and it shouldn't need to do that.
@ChrisRackauckas
Copy link
Member Author

1+1
@time using OrdinaryDiffEq
function lorenz!(du,u,p,t)
 du[1] = 10.0(u[2]-u[1])
 du[2] = u[1]*(28.0-u[3]) - u[2]
 du[3] = u[1]*u[2] - (8/3)*u[3]
end
u0 = [1.0;0.0;0.0]
tspan = (0.0,100.0)
prob = ODEProblem(lorenz!,u0,tspan)
@time sol = solve(prob, OrdinaryDiffEq.DefaultODEAlgorithm())

using Test
@inferred solve(prob, OrdinaryDiffEq.DefaultODEAlgorithm())

using OrdinaryDiffEq, SnoopCompile

function lorenz(du,u,p,t)
 du[1] = 10.0(u[2]-u[1])
 du[2] = u[1]*(28.0-u[3]) - u[2]
 du[3] = u[1]*u[2] - (8/3)*u[3]
end

u0 = [1.0;0.0;0.0]
tspan = (0.0,100.0)
prob = ODEProblem(lorenz,u0,tspan)
alg = OrdinaryDiffEq.DefaultODEAlgorithm()
tinf = @snoopi_deep solve(prob,alg)

using ProfileView
ProfileView.view(flamegraph(tinf))

@ChrisRackauckas
Copy link
Member Author

# PR

# Precompile and load
# 47.139365 seconds (10.32 M allocations: 690.241 MiB, 0.47% gc time, 10.08% compilation time: 2% of which was recompilation)
# Just load
# 3.222100 seconds (4.06 M allocations: 249.297 MiB, 2.98% gc time, 1.94% compilation time)
# First solve
# 0.129151 seconds (115.04 k allocations: 8.043 MiB, 98.91% compilation time)

# Master
# 48.996228 seconds (10.33 M allocations: 691.287 MiB, 0.41% gc time, 9.80% compilation time: 2% of which was recompilation
# Just Load
# 3.320052 seconds (4.06 M allocations: 250.294 MiB, 3.17% gc time, 1.94% compilation time)

@ChrisRackauckas
Copy link
Member Author

Precompilation doesn't drop much more on #2104

Co-authored-by: Nathanael Bosch <[email protected]>
src/composite_algs.jl Outdated Show resolved Hide resolved
@ChrisRackauckas
Copy link
Member Author

Closed as it was rebased in #2184

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.

2 participants