From a07a096b8d2dc9844ff1b66d0110913165bac462 Mon Sep 17 00:00:00 2001 From: oscarddssmith Date: Mon, 1 Jul 2024 11:47:00 -0400 Subject: [PATCH] modify extension algs --- Project.toml | 1 + docs/src/advanced/custom.md | 2 +- docs/src/basics/FAQ.md | 11 +++++------ ext/LinearSolveIterativeSolversExt.jl | 4 ++-- ext/LinearSolveKrylovKitExt.jl | 5 +++-- src/extension_algs.jl | 6 ++++-- src/iterative_wrappers.jl | 2 +- test/static_arrays.jl | 2 +- 8 files changed, 18 insertions(+), 15 deletions(-) diff --git a/Project.toml b/Project.toml index 605811821..a81d2c9ca 100644 --- a/Project.toml +++ b/Project.toml @@ -88,6 +88,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" diff --git a/docs/src/advanced/custom.md b/docs/src/advanced/custom.md index 1b6dda391..2ac9acbda 100644 --- a/docs/src/advanced/custom.md +++ b/docs/src/advanced/custom.md @@ -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` diff --git a/docs/src/basics/FAQ.md b/docs/src/basics/FAQ.md index 108f748b6..10bec1e2d 100644 --- a/docs/src/basics/FAQ.md +++ b/docs/src/basics/FAQ.md @@ -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)))) ``` diff --git a/ext/LinearSolveIterativeSolversExt.jl b/ext/LinearSolveIterativeSolversExt.jl index 507e75ad2..23ce663c8 100644 --- a/ext/LinearSolveIterativeSolversExt.jl +++ b/ext/LinearSolveIterativeSolversExt.jl @@ -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...) diff --git a/ext/LinearSolveKrylovKitExt.jl b/ext/LinearSolveKrylovKitExt.jl index a26e26688..1aa1e5d52 100644 --- a/ext/LinearSolveKrylovKitExt.jl +++ b/ext/LinearSolveKrylovKitExt.jl @@ -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...) diff --git a/src/extension_algs.jl b/src/extension_algs.jl index c5e2db955..7534d2fa1 100644 --- a/src/extension_algs.jl +++ b/src/extension_algs.jl @@ -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 diff --git a/src/iterative_wrappers.jl b/src/iterative_wrappers.jl index ba2016975..1ba9067fd 100644 --- a/src/iterative_wrappers.jl +++ b/src/iterative_wrappers.jl @@ -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 diff --git a/test/static_arrays.jl b/test/static_arrays.jl index 300c86c25..ea2e3e297 100644 --- a/test/static_arrays.jl +++ b/test/static_arrays.jl @@ -19,7 +19,7 @@ for alg in (nothing, LUFactorization(), SVDFactorization(), CholeskyFactorizatio @test norm(A * sol .- b) < 1e-10 if __non_native_static_array_alg(alg) - @test_broken __solve_no_alloc(A, b, alg) + @test_nowarn __solve_no_alloc(A, b, alg) else @test_nowarn __solve_no_alloc(A, b, alg) end