Conjugate Gradients solves
cg
cg!
The method should work fine on the GPU. As a minimal working example, consider:
using LinearAlgebra, CuArrays, IterativeSolvers
n = 100
A = cu(rand(n, n))
A = A + A' + 2*n*I
b = cu(rand(n))
x = cg(A, b)
!!! note
Make sure that all state vectors are stored on the GPU. For instance when calling cg!(x, A, b)
, one might have an issue when x
is stored on the GPU, while b
is stored on the CPU -- IterativeSolvers.jl does not copy the vectors to the same device.
The current implementation follows a rather standard approach. Note that preconditioned CG (or PCG) is slightly different from ordinary CG, because the former must compute the residual explicitly, while it is available as byproduct in the latter. Our implementation of CG ensures the minimal number of vector operations.
!!! tip CG can be used as an [iterator](@ref Iterators).