-
-
Notifications
You must be signed in to change notification settings - Fork 42
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 caching for solvers without init #382
Merged
Merged
Changes from all commits
Commits
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
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,37 @@ | ||
# Some algorithms don't support creating a cache and doing `solve!`, this unfortunately | ||
# makes it difficult to write generic code that supports caching. For the algorithms that | ||
# don't have a `__init` function defined, we create a "Fake Cache", which just calls | ||
# `__solve` from `solve!` | ||
@concrete mutable struct NonlinearSolveNoInitCache{iip, timeit} <: | ||
AbstractNonlinearSolveCache{iip, timeit} | ||
prob | ||
alg | ||
args | ||
kwargs::Any | ||
end | ||
|
||
function SciMLBase.reinit!( | ||
cache::NonlinearSolveNoInitCache, u0 = cache.prob.u0; p = cache.prob.p, kwargs...) | ||
prob = remake(cache.prob; u0, p) | ||
cache.prob = prob | ||
cache.kwargs = merge(cache.kwargs, kwargs) | ||
return cache | ||
end | ||
|
||
function Base.show(io::IO, cache::NonlinearSolveNoInitCache) | ||
print(io, "NonlinearSolveNoInitCache(alg = $(cache.alg))") | ||
end | ||
|
||
function SciMLBase.__init(prob::AbstractNonlinearProblem{uType, iip}, | ||
alg::Union{AbstractNonlinearSolveAlgorithm, | ||
SimpleNonlinearSolve.AbstractSimpleNonlinearSolveAlgorithm}, | ||
args...; | ||
maxtime = nothing, | ||
kwargs...) where {uType, iip} | ||
return NonlinearSolveNoInitCache{iip, maxtime !== nothing}( | ||
prob, alg, args, merge((; maxtime), kwargs)) | ||
end | ||
|
||
function SciMLBase.solve!(cache::NonlinearSolveNoInitCache) | ||
return solve(cache.prob, cache.alg, cache.args...; cache.kwargs...) | ||
end |
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,23 @@ | ||
@testitem "NoInit Caching" begin | ||
using LinearAlgebra | ||
import NLsolve, NLSolvers | ||
|
||
solvers = [SimpleNewtonRaphson(), SimpleTrustRegion(), SimpleDFSane(), NLsolveJL(), | ||
NLSolversJL(NLSolvers.LineSearch(NLSolvers.Newton(), NLSolvers.Backtracking()))] | ||
|
||
prob = NonlinearProblem((u, p) -> u .^ 2 .- p, [0.1, 0.3], 2.0) | ||
|
||
for alg in solvers | ||
cache = init(prob, alg) | ||
sol = solve!(cache) | ||
@test SciMLBase.successful_retcode(sol) | ||
@test norm(sol.resid, Inf) ≤ 1e-6 | ||
|
||
reinit!(cache; p = 5.0) | ||
@test cache.prob.p == 5.0 | ||
sol = solve!(cache) | ||
@test SciMLBase.successful_retcode(sol) | ||
@test norm(sol.resid, Inf) ≤ 1e-6 | ||
@test norm(sol.u .^ 2 .- 5.0, Inf) ≤ 1e-6 | ||
end | ||
end |
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
Oops, something went wrong.
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.
does this effect type inference? I don't see why this wouldn't be concrete as well.
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.
this is needed because
cache.kwargs = merge(cache.kwargs, kwargs)
can change the type inreinit