Skip to content

Commit 20ce135

Browse files
shivin9lopezm94
authored andcommitted
reduced memory allocations to optimize cg.jl (#125)
1 parent 995345c commit 20ce135

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# mkdocs site
22
/site
33
/docs/build
4+
*.mem

src/cg.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function cg_method!(log::ConvergenceHistory, x, K, b;
5151
push!(log,:resnorm,resnorm)
5252
verbose && @printf("%3d\t%1.2e\n",iter,resnorm)
5353
resnorm < tol && break
54-
z = solve(Pl,r)
54+
solve!(z,Pl,r)
5555
oldγ = γ
5656
γ = dot(r, z)
5757
β = γ/oldγ

src/common.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,14 @@ end
4444
Solve `A\b` with a direct solver. When `A` is a function `A(b)` is dispatched instead.
4545
"""
4646
solve(A::Function,b) = A(b)
47+
4748
solve(A,b) = A\b
4849

50+
solve!{T}(out::AbstractArray{T},A::Int,b::AbstractArray{T}) = scale!(out,b, 1/A)
51+
52+
solve!{T}(out::AbstractArray{T},A,b::AbstractArray{T}) = A_ldiv_B!(out,A,b)
53+
solve!{T}(out::AbstractArray{T},A::Function,b::AbstractArray{T}) = copy!(out,A(b))
54+
4955
"""
5056
initrand!(v)
5157
Overwrite `v` with a random unitary vector of the same length.

src/gmres.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export gmres, gmres!
77
gmres(A, b; kwargs...) = gmres!(zerox(A,b), A, b; kwargs...)
88

99
function gmres!(x, A, b;
10-
tol=sqrt(eps(typeof(real(b[1])))), restart::Int=min(20,length(b)),
11-
maxiter::Int=restart, plot::Bool=false, log::Bool=false, kwargs...
10+
tol = sqrt(eps(typeof(real(b[1])))), restart::Int=min(20,length(b)),
11+
maxiter::Int = restart, plot::Bool=false, log::Bool=false, kwargs...
1212
)
1313
(plot & !log) && error("Can't plot when log keyword is false")
1414
history = ConvergenceHistory(partial=!log, restart=restart)
@@ -26,7 +26,7 @@ end
2626

2727
#One Arnoldi iteration
2828
#Optionally takes a truncation parameter l
29-
function arnoldi!(K::KrylovSubspace, w; l=K.order)
29+
function arnoldi(K::KrylovSubspace, w; l=K.order)
3030
v = nextvec(K)
3131
w = copy(v)
3232
n = min(length(K.v), l)
@@ -86,7 +86,7 @@ function gmres_method!(log::ConvergenceHistory, x, A, b;
8686
for j = 1:restart
8787
nextiter!(log, mvps=1)
8888
#Calculate next orthonormal basis vector in the Krylov subspace
89-
H[1:j+1, j] = arnoldi!(K, w)
89+
H[1:j+1, j] = arnoldi(K, w)
9090

9191
#Update QR factorization of H
9292
#The Q is stored as a series of Givens rotations in J

0 commit comments

Comments
 (0)