Skip to content

Commit

Permalink
Reuse memory for LSQR point in LSLQ
Browse files Browse the repository at this point in the history
  • Loading branch information
amontoison authored and dpo committed Mar 7, 2019
1 parent 270d90d commit 72d26ed
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/lslq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ function lslq(A :: AbstractLinearOperator, b :: AbstractVector{T};
ctol = conlim > 0.0 ? 1/conlim : 0.0

x_lq = zeros(T, n) # LSLQ point
x_cg = zeros(T, n) # LSQR point
err_lbnds = T[]
err_ubnds_lq = T[]
err_ubnds_cg = T[]
Expand All @@ -124,7 +123,7 @@ function lslq(A :: AbstractLinearOperator, b :: AbstractVector{T};
Mu = copy(b)
u = M * Mu
β₁ = sqrt(@kdot(m, u, Mu))
β₁ == 0.0 && return (x_lq, x_cg, err_lbnds, err_ubnds_lq, err_ubnds_cg,
β₁ == 0.0 && return (x_lq, zeros(T, n), err_lbnds, err_ubnds_lq, err_ubnds_cg,
SimpleStats(true, false, [0.0], [0.0], "x = 0 is a zero-residual solution"))
β = β₁

Expand All @@ -136,7 +135,7 @@ function lslq(A :: AbstractLinearOperator, b :: AbstractVector{T};
α = sqrt(@kdot(n, v, Nv)) # = α₁

# Aᵀb = 0 so x = 0 is a minimum least-squares solution
α == 0.0 && return (x_lq, x_cg, err_lbnds, err_ubnds_lq, err_ubnds_cg,
α == 0.0 && return (x_lq, zeros(T, n), err_lbnds, err_ubnds_lq, err_ubnds_cg,
SimpleStats(true, false, [β₁], [0.0], "x = 0 is a minimum least-squares solution"))
@kscal!(n, 1.0/α, v)
NisI || @kscal!(n, 1.0/α, Nv)
Expand Down Expand Up @@ -341,7 +340,8 @@ function lslq(A :: AbstractLinearOperator, b :: AbstractVector{T};
end

# compute LSQR point
@. x_cg = x_lq + ζ̄ *
@kaxpby!(n, 1.0, x_lq, ζ̄ , w̄)
x_cg =

tired && (status = "maximum number of iterations exceeded")
ill_cond_mach && (status = "condition number seems too large for this machine")
Expand Down
4 changes: 2 additions & 2 deletions test/test_alloc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ actual_craig_bytes = @allocated craig(Au, c)
@test actual_craig_bytes 1.1 * expected_craig_bytes

# without preconditioner and with (Ap, Aᵀq) preallocated, LSLQ needs:
# - 5 m-vectors: x_lq, x_cg, v, w, w̄
# - 4 m-vectors: x_lq, v, w, w̄
# - 1 n-vector: u
storage_lslq(n, m) = 5 * m + n
storage_lslq(n, m) = 4 * m + n
storage_lslq_bytes(n, m) = 8 * storage_lslq(n, m)
expected_lslq_bytes = storage_lslq_bytes(n, m)
(x, stats) = lslq(Ao, b) # warmup
Expand Down

0 comments on commit 72d26ed

Please sign in to comment.