Skip to content

Commit 5bc7566

Browse files
committed
Implementing cg with axpy! and axpby!
1 parent 7b6ff20 commit 5bc7566

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

PartitionedSolvers/src/krylov_solvers.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ function pcg_step(x,ws,b,phase=:start;kwargs...)
206206
phase = :advance
207207
copyto!(r,b)
208208
mul!(s,A,x)
209-
r .-= s
209+
axpy!(-one(eltype(s)),s,r)
210+
#r .-= s
210211
current = norm(r)
211212
target = max(reltol*current,abstol)
212213
dx .= zero(eltype(dx))
@@ -218,12 +219,15 @@ function pcg_step(x,ws,b,phase=:start;kwargs...)
218219
ρ_prev = ρ
219220
ρ = dot(r,u)
220221
β = ρ / ρ_prev
221-
dx .= u .+ β .* dx
222+
axpby!(one(eltype(u)),u,β,dx)
223+
#dx .= u .+ β .* dx
222224
s = u
223225
mul!(s,A,dx)
224226
α = ρ / dot(s,dx)
225-
x .+= α .* dx
226-
r .-= α .* s
227+
axpy!(α,dx,x)
228+
#x .+= α .* dx
229+
axpy!(-α,s,r)
230+
#r .-= α .* s
227231
current = norm(r)
228232
iteration += 1
229233
current = norm(r)

PartitionedSolvers/test/krylov_solvers_tests.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ s = PS.solve(s)
2525

2626
z = IterativeSolvers.cg(A,b;verbose=true,Pl)
2727

28+
parts_per_dir = (2,2,2)
29+
ranks = PA.DebugArray(LinearIndices((prod(parts_per_dir),)))
2830

31+
args = PA.laplacian_fem(nodes_per_dir,parts_per_dir,ranks)
32+
A = PA.psparse(args...) |> fetch
33+
x = PA.pones(axes(A,2))
34+
b = A*x
35+
36+
y = similar(x)
37+
y .= 0
38+
p = PS.linear_problem(y,A,b)
39+
Pl = PS.preconditioner(PS.amg,p)
40+
s = PS.pcg(p;verbose=true,Pl)
41+
s = PS.solve(s)
42+
@test x PS.solution(p)
43+
s = PS.update(s,matrix=2*A)
44+
s = PS.solve(s)
45+
@test x 2*PS.solution(p)
2946

3047
end # module

src/p_vector.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,3 +1524,14 @@ function renumber(a::PVector,row_partition_2;renumber_local_indices=Val(true))
15241524
PVector(values,row_partition_2)
15251525
end
15261526

1527+
function LinearAlgebra.axpy!(a::Number,x::PVector,y::PVector)
1528+
y .+= a .* x
1529+
y
1530+
end
1531+
1532+
function LinearAlgebra.axpby!(a::Number,x::PVector,b::Number,y::PVector)
1533+
y .= a .* x .+ b .* y
1534+
y
1535+
end
1536+
1537+

0 commit comments

Comments
 (0)