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

make preconditioners part of the solver rather than a random extra #514

Merged
merged 14 commits into from
Aug 8, 2024
Prev Previous commit
Next Next commit
modify extension algs
oscardssmith committed Aug 5, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit dea2e35ce662e267de62c0ed3ff60e53b7728019
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -90,6 +90,7 @@ JET = "0.8.28"
KLU = "0.6"
KernelAbstractions = "0.9.16"
Krylov = "0.9"
KrylovPreconditioners = "0.2"
KrylovKit = "0.8"
LazyArrays = "1.8, 2"
Libdl = "1.10"
2 changes: 1 addition & 1 deletion docs/src/advanced/custom.md
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@ The inputs to the function are as follows:
- `p`, a set of parameters
- `newA`, a `Bool` which is `true` if `A` has been modified since last solve
- `Pl`, left-preconditioner
- `Pr`, right-preconditioner
- `Pr`, right-preconditionerz
- `solverdata`, solver cache set to `nothing` if solver hasn't been initialized
- `kwargs`, standard SciML keyword arguments such as `verbose`, `maxiters`, `abstol`, `reltol`

11 changes: 5 additions & 6 deletions docs/src/basics/FAQ.md
Original file line number Diff line number Diff line change
@@ -45,8 +45,8 @@ a few ways:

## How do I use IterativeSolvers solvers with a weighted tolerance vector?

IterativeSolvers.jl computes the norm after the application of the left preconditioner
`Pl`. Thus, in order to use a vector tolerance `weights`, one can mathematically
IterativeSolvers.jl computes the norm after the application of the left preconditioner.
Thus, in order to use a vector tolerance `weights`, one can mathematically
hack the system via the following formulation:

```@example FAQPrec
@@ -57,11 +57,10 @@ A = rand(n, n)
b = rand(n)

weights = [1e-1, 1]
Pl = LinearSolve.InvPreconditioner(Diagonal(weights))
Pr = Diagonal(weights)
precs = Returns(LinearSolve.InvPreconditioner(Diagonal(weights)), Diagonal(weights))

prob = LinearProblem(A, b)
sol = solve(prob, KrylovJL_GMRES(), Pl = Pl, Pr = Pr)
sol = solve(prob, KrylovJL_GMRES(precs))

sol.u
```
@@ -84,5 +83,5 @@ Pl = LinearSolve.ComposePreconditioner(LinearSolve.InvPreconditioner(Diagonal(we
Pr = Diagonal(weights)

prob = LinearProblem(A, b)
sol = solve(prob, KrylovJL_GMRES(), Pl = Pl, Pr = Pr)
sol = solve(prob, KrylovJL_GMRES(precs=Returns((Pl,Pr))))
```
4 changes: 2 additions & 2 deletions ext/LinearSolveIterativeSolversExt.jl
Original file line number Diff line number Diff line change
@@ -12,9 +12,9 @@ end

function LinearSolve.IterativeSolversJL(args...;
generate_iterator = IterativeSolvers.gmres_iterable!,
gmres_restart = 0, kwargs...)
gmres_restart = 0, precs = DEFAULT_PRECS, kwargs...)
return IterativeSolversJL(generate_iterator, gmres_restart,
args, kwargs)
precs, args, kwargs)
end

function LinearSolve.IterativeSolversJL_CG(args...; kwargs...)
5 changes: 3 additions & 2 deletions ext/LinearSolveKrylovKitExt.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
module LinearSolveKrylovKitExt

using LinearSolve, KrylovKit, LinearAlgebra
using LinearSolve: LinearCache
using LinearSolve: LinearCache, DEFAULT_PRECS

function LinearSolve.KrylovKitJL(args...;
KrylovAlg = KrylovKit.GMRES, gmres_restart = 0,
precs = DEFAULT_PRECS,
kwargs...)
return KrylovKitJL(KrylovAlg, gmres_restart, args, kwargs)
return KrylovKitJL(KrylovAlg, gmres_restart, precs, args, kwargs)
end

function LinearSolve.KrylovKitJL_CG(args...; kwargs...)
6 changes: 4 additions & 2 deletions src/extension_algs.jl
Original file line number Diff line number Diff line change
@@ -259,9 +259,10 @@ solvers.

Using this solver requires adding the package KrylovKit.jl, i.e. `using KrylovKit`
"""
struct KrylovKitJL{F, A, I, K} <: LinearSolve.AbstractKrylovSubspaceMethod
struct KrylovKitJL{F, I, P, A, K} <: LinearSolve.AbstractKrylovSubspaceMethod
KrylovAlg::F
gmres_restart::I
precs::P
args::A
kwargs::K
end
@@ -306,9 +307,10 @@ A generic wrapper over the IterativeSolvers.jl solvers.

Using this solver requires adding the package IterativeSolvers.jl, i.e. `using IterativeSolvers`
"""
struct IterativeSolversJL{F, I, A, K} <: LinearSolve.AbstractKrylovSubspaceMethod
struct IterativeSolversJL{F, I, P, A, K} <: LinearSolve.AbstractKrylovSubspaceMethod
generate_iterator::F
gmres_restart::I
precs::P
args::A
kwargs::K
end
2 changes: 1 addition & 1 deletion src/iterative_wrappers.jl
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ KrylovJL(args...; KrylovAlg = Krylov.gmres!,

A generic wrapper over the Krylov.jl krylov-subspace iterative solvers.
"""
struct KrylovJL{F, I, A, P, K} <: AbstractKrylovSubspaceMethod
struct KrylovJL{F, I, P, A, K} <: AbstractKrylovSubspaceMethod
KrylovAlg::F
gmres_restart::I
window::I