How to pass a matrix free preconditioner to IterativeSolver of KrylovKit?

Say I have a matrix A and a linear function P that takes as argument a vector and returns a vector of the same size. How can I use a preconditioned conjugate gradient from IterativeSolver.jl of KrylovKit.jl for solving Ax=b preconditioned by P?

Hi Frédéric,

One way to achieve this would be create a type that wraps your function, say the identity function

foo(x) = x

then create a type to wrap your function

struct MyPreconditioner{F}
	fun::F
end
P = MyPreconditioner(foo)

and finally add whatever method IterativeSolver.jl or KrylovKit.jl will need :

using LinearAlgebra
LinearAlgebra.ldiv!(y, P::MyPreconditioner, x) = y .= P.fun(x)
1 Like

IterativeSolvers.jl requires ldiv! and \: Preconditioning · IterativeSolvers.jl

KrylovKit.jl doesn’t currently support preconditioning (Preconditionning · Issue #7 · Jutho/KrylovKit.jl · GitHub).

many thanks!

Thanks

You can easily use left preconditioning in KrylovKit, see here

Only thing I would add is that some methods of IterativeSolvers.jl require you to also define the following in-place ldiv!:

LinearAlgebra.ldiv!(P::MyPreconditioner, X) = X.= P.fun(X) #or whatever solver you choose