Generic interface
Krylov.jl provides a generic interface for solving linear systems using (block) Krylov methods. The interface is designed to be common for all methods and contains three routines krylov_workspace, krylov_solve and krylov_solve!. They allow to build Krylov workspaces and call both in-place and out-of-place variants of the solvers with a unified API.
Krylov.krylov_workspace — Function
workspace = krylov_workspace(method, args...; kwargs...)
workspace = krylov_workspace(Val(method), args...; kwargs...)Generic function that dispatches to the appropriate workspace constructor for each subtype of KrylovWorkspace and BlockKrylovWorkspace. In both calls, method is a symbol (such as :cg, :gmres or :block_minres) that specifies the (block) Krylov method for which a workspace is desired. The returned workspace can later be used by krylov_solve! to execute the (block) Krylov method in-place.
Pass the plain symbol method for convenience, or wrap it in Val(method) to enable full compile-time specialization, improve type inference, and achieve maximum performance.
Note that for each (block) Krylov method, you can use a workspace constructor that accepts exactly the same arguments as the corresponding solver and krylov_solve, for convenience and consistency.
Krylov.krylov_solve — Function
krylov_solve(method, args...; kwargs...)
krylov_solve(Val(method), args...; kwargs...)Generic function that dispatches to the appropriate out-of-place (block) Krylov method specified by method. In both calls, method is a symbol (such as :cg, :gmres or :block_minres).
Pass the plain symbol method for convenience, or wrap it in Val(method) to enable full compile-time specialization, improve type inference, and achieve maximum performance.
Krylov.krylov_solve! — Function
krylov_solve!(workspace, args...; kwargs...)Generic function that dispatches to the appropriate in-place (block) Krylov method based on the type of workspace. The argument workspace must be a subtype of KrylovWorkspace or BlockKrylovWorkspace (such as CgWorkspace, GmresWorkspace or BlockMinresWorkspace).
The section on workspace accessors describes how to retrieve the solution, statistics, and other results from a workspace after calling krylov_solve!.
Examples
using Krylov, SparseArrays, LinearAlgebra
# Define a symmetric positive definite matrix A and a right-hand side vector b
n = 1000
A = sprandn(n, n, 0.005)
A = A * A' + I
b = randn(n)
# Out-of-place interface
for method in (:cg, :cr, :car)
x, stats = krylov_solve(Val(method), A, b)
r = b - A * x
println("Residual norm for $(method): ", norm(r))
endResidual norm for cg: 3.539686824410548e-7
Residual norm for cr: 3.679400775138882e-7
Residual norm for car: 4.4540604814308697e-7using Krylov, SparseArrays, LinearAlgebra
# Define a square nonsymmetric matrix A and a right-hand side vector b
n = 100
A = sprand(n, n, 0.05) + I
b = rand(n)
# In-place interface
for method in (:bicgstab, :gmres, :qmr)
# Create a workspace for the Krylov method
workspace = krylov_workspace(Val(method), A, b)
# Solve the system in-place
krylov_solve!(workspace, A, b)
# Get the statistics
stats = Krylov.statistics(workspace)
# Retrieve the solution
x = Krylov.solution(workspace)
# Check if the solver converged
solved = Krylov.issolved(workspace)
println("Convergence of $method: ", solved)
# Display the number of iterations
niter = Krylov.iteration_count(workspace)
println("Number of iterations for $method: ", niter)
# Display the allocation timer
allocation_timer = Krylov.elapsed_allocation_time(workspace)
println("Elapsed allocation time for $method: ", allocation_timer, " seconds")
# Display the computation timer
computation_timer = Krylov.elapsed_time(workspace)
println("Elapsed time for $method: ", computation_timer, " seconds")
# Display the number of operator-vector products with A and A'
nAprod = Krylov.Aprod_count(workspace)
nAtprod = Krylov.Atprod_count(workspace)
println("Number of operator-vector products with A and A' for $method: ", (nAprod, nAtprod))
println()
endConvergence of bicgstab: false
Number of iterations for bicgstab: 200
Elapsed allocation time for bicgstab: 1.923e-6 seconds
Elapsed time for bicgstab: 0.000287179 seconds
Number of operator-vector products with A and A' for bicgstab: (400, 0)
Convergence of gmres: true
Number of iterations for gmres: 79
Elapsed allocation time for gmres: 2.6215999999999998e-5 seconds
Elapsed time for gmres: 0.000231366 seconds
Number of operator-vector products with A and A' for gmres: (79, 0)
Convergence of qmr: true
Number of iterations for qmr: 107
Elapsed allocation time for qmr: 1.9140000000000002e-6 seconds
Elapsed time for qmr: 0.000155493 seconds
Number of operator-vector products with A and A' for qmr: (107, 107)