Skip to content

Commit 024aa37

Browse files
committed
Allocate a *similar* vector in zerox s.t. GPUArrays work out of the box
1 parent c6d92c3 commit 024aa37

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

docs/src/linear_systems/cg.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ cg
99
cg!
1010
```
1111

12+
## On the GPU
13+
14+
The method should work fine on the GPU. As a minimal working example, consider:
15+
16+
```julia
17+
using LinearAlgebra, CuArrays, IterativeSolvers
18+
19+
n = 100
20+
A = cu(rand(n, n))
21+
A = A + A' + 2*n*I
22+
b = cu(rand(n))
23+
x = cg(A, b)
24+
```
25+
26+
!!! note
27+
Make sure that all state vectors are stored on the GPU. CG will *not* copy the right-hand side to the GPU if the provided initial guess `x` is stored on the GPU.
28+
29+
1230
## Implementation details
1331

1432
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.

src/common.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@ Adivtype(A, b) = typeof(one(eltype(b))/one(eltype(A)))
1212

1313
"""
1414
zerox(A, b)
15-
Build a zeros vector `Vector{T}`, where `T` is `Adivtype(A,b)`.
15+
16+
Build a zeros vector similar to `b` of eltype `Adivtype(A,b)`.
1617
"""
17-
zerox(A, b) = zeros(Adivtype(A, b), size(A, 2))
18+
function zerox(A, b)
19+
T = Adivtype(A, b)
20+
x = similar(b, T, size(A, 2))
21+
fill!(x, zero(T))
22+
return x
23+
end
1824

1925
"""
2026
No-op preconditioner

0 commit comments

Comments
 (0)