Skip to content

Commit

Permalink
Make Krylov solver verbose and public (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdalle authored Sep 26, 2024
1 parent 7c0aa99 commit a2ad001
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/ImplicitDifferentiation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ using LinearAlgebra: axpby!, factorize, lu
include("implicit_function.jl")
include("operators.jl")

export ImplicitFunction
export ImplicitFunction, KrylovLinearSolver

end
55 changes: 31 additions & 24 deletions src/implicit_function.jl
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
"""
KrylovLinearSolver
Callable object that can solve linear systems `As = b` and `AS = b` in the same way as the built-in `\\`.
Callable object that can solve linear systems `Ax = b` and `AX = B` in the same way as the built-in `\\`.
Uses an iterative solver from [Krylov.jl](https://github.com/JuliaSmoothOptimizers/Krylov.jl) under the hood.
# Note
# Constructor
This name is not exported, and thus not part of the public API, but it is used in the [`ImplicitFunction`](@ref) constructors.
"""
struct KrylovLinearSolver end
KrylovLinearSolver(; verbose=true)
If `verbose` is `true`, the solver logs a warning in case of failure.
Otherwise it will fail silently, and may return solutions that do not exactly satisfy the linear system.
# Callable behavior
"""
(::KylovLinearSolver)(A, b::AbstractVector)
Solve a linear system with a single right-hand side.
"""
function (::KrylovLinearSolver)(A, b::AbstractVector)
x, stats = gmres(A, b)
return x
end
"""
(::KrylovLinearSolver)(A, B::AbstractMatrix)
Solve a linear system with multiple right-hand sides.
"""
function (::KrylovLinearSolver)(A, B::AbstractMatrix)
Base.@kwdef struct KrylovLinearSolver
verbose::Bool = true
end

function (solver::KrylovLinearSolver)(A, b::AbstractVector)
x, stats = gmres(A, b)
if !stats.solved || stats.inconsistent
solver.verbose &&
@warn "Failed to solve the linear system in the implicit function theorem with `Krylov.gmres`" stats
end
return x
end

function (solver::KrylovLinearSolver)(A, B::AbstractMatrix)
# X, stats = block_gmres(A, B) # https://github.com/JuliaSmoothOptimizers/Krylov.jl/issues/854
X = mapreduce(hcat, eachcol(B)) do b
first(gmres(A, b))
solver(A, b)
end
return X
end
Expand Down Expand Up @@ -80,6 +89,14 @@ Picks the `lazy` parameter automatically based on the `linear_solver`, using the
Picks the `linear_solver` automatically based on the `lazy` parameter.
# Callable behavior
(implicit::ImplicitFunction)(x::AbstractVector, args...; kwargs...)
Return `implicit.forward(x, args...; kwargs...)`, which can be either an `AbstractVector` `y` or a tuple `(y, z)`.
This call makes `y` differentiable with respect to `x`.
# Function signatures
There are two possible signatures for `forward` and `conditions`, which must be consistent with one another:
Expand Down Expand Up @@ -122,9 +139,6 @@ struct ImplicitFunction{
conditions_y_backend::B2
end

"""
"""
function ImplicitFunction{lazy}(
forward::F,
conditions::C;
Expand Down Expand Up @@ -163,13 +177,6 @@ function Base.show(io::IO, implicit::ImplicitFunction{lazy}) where {lazy}
)
end

"""
(implicit::ImplicitFunction)(x::AbstractVector, args...; kwargs...)
Return `implicit.forward(x, args...; kwargs...)`, which can be either an `AbstractVector` `y` or a tuple `(y, z)`.
This call makes `y` differentiable with respect to `x`.
"""
function (implicit::ImplicitFunction)(x::AbstractVector, args...; kwargs...)
y_or_yz = implicit.forward(x, args...; kwargs...)
return y_or_yz
Expand Down

2 comments on commit a2ad001

@gdalle
Copy link
Member Author

@gdalle gdalle commented on a2ad001 Sep 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/116194

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.2 -m "<description of version>" a2ad001b369c4790f8f09cb6bf05ffe37ce183ce
git push origin v0.6.2

Please sign in to comment.