Skip to content

Commit

Permalink
Add copyto! for KrylovStats
Browse files Browse the repository at this point in the history
  • Loading branch information
amontoison committed Dec 19, 2023
1 parent 80fa61c commit 0379bf8
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
107 changes: 107 additions & 0 deletions src/krylov_stats.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export KrylovStats, SimpleStats, LsmrStats, LanczosStats, LanczosShiftStats,
SymmlqStats, AdjointStats, LNLQStats, LSLQStats

import Base.copyto!

"Abstract type for statistics returned by a solver"
abstract type KrylovStats{T} end

Expand Down Expand Up @@ -32,6 +34,18 @@ function reset!(stats :: SimpleStats)
empty!(stats.Acond)
end

function copyto!(dest :: SimpleStats, src :: SimpleStats)
dest.niter = src.niter
dest.solved = src.solved
dest.inconsistent = src.inconsistent
dest.residuals = copy(src.residuals)
dest.Aresiduals = copy(src.Aresiduals)
dest.Acond = copy(src.Acond)
dest.timer = src.timer
dest.status = src.status
return dest
end

"""
Type for statistics returned by LSMR. The attributes are:
- niter
Expand Down Expand Up @@ -65,6 +79,22 @@ function reset!(stats :: LsmrStats)
empty!(stats.Aresiduals)
end

function copyto!(dest :: LsmrStats, src :: LsmrStats)
dest.niter = src.niter
dest.solved = src.solved
dest.inconsistent = src.inconsistent
dest.residuals = copy(src.residuals)
dest.Aresiduals = copy(src.Aresiduals)
dest.residual = src.residual
dest.Aresidual = src.Aresidual
dest.Acond = src.Acond
dest.Anorm = src.Anorm
dest.xNorm = src.xNorm
dest.timer = src.timer
dest.status = src.status
return dest
end

"""
Type for statistics returned by CG-LANCZOS, the attributes are:
- niter
Expand All @@ -91,6 +121,18 @@ function reset!(stats :: LanczosStats)
empty!(stats.residuals)
end

function copyto!(dest :: LanczosStats, src :: LanczosStats)
dest.niter = src.niter
dest.solved = src.solved
dest.residuals = copy(src.residuals)
dest.indefinite = src.indefinite
dest.Anorm = src.Anorm
dest.Acond = src.Acond
dest.timer = src.timer
dest.status = src.status
return dest
end

"""
Type for statistics returned by CG-LANCZOS with shifts, the attributes are:
- niter
Expand Down Expand Up @@ -119,6 +161,18 @@ function reset!(stats :: LanczosShiftStats)
end
end

function copyto!(dest :: LanczosShiftStats, src :: LanczosShiftStats)
dest.niter = src.niter
dest.solved = src.solved
dest.residuals = deepcopy(src.residuals)
dest.indefinite = src.indefinite
dest.Anorm = src.Anorm
dest.Acond = src.Acond
dest.timer = src.timer
dest.status = src.status
return dest
end

"""
Type for statistics returned by SYMMLQ, the attributes are:
- niter
Expand Down Expand Up @@ -152,6 +206,20 @@ function reset!(stats :: SymmlqStats)
empty!(stats.errorscg)
end

function copyto!(dest :: SymmlqStats, src :: SymmlqStats)
dest.niter = src.niter
dest.solved = src.solved
dest.residuals = copy(src.residuals)
dest.residualscg = copy(src.residualscg)
dest.errors = copy(src.errors)
dest.errorscg = copy(src.errorscg)
dest.Anorm = src.Anorm
dest.Acond = src.Acond
dest.timer = src.timer
dest.status = src.status
return dest
end

"""
Type for statistics returned by adjoint systems solvers BiLQR and TriLQR, the attributes are:
- niter
Expand All @@ -177,6 +245,18 @@ function reset!(stats :: AdjointStats)
empty!(stats.residuals_dual)
end

function copyto!(dest :: AdjointStats, src :: AdjointStats)
dest.niter = src.niter
dest.solved_primal = src.solved_primal
dest.solved_dual = src.solved_dual
dest.residuals_primal = copy(src.residuals_primal)
dest.residuals_dual = copy(src.residuals_dual)
dest.timer = src.timer
dest.status = src.status
return dest
end


"""
Type for statistics returned by the LNLQ method, the attributes are:
- niter
Expand Down Expand Up @@ -205,6 +285,18 @@ function reset!(stats :: LNLQStats)
empty!(stats.error_bnd_y)
end

function copyto!(dest :: LNLQStats, src :: LNLQStats)
dest.niter = src.niter
dest.solved = src.solved
dest.residuals = copy(src.residuals)
dest.error_with_bnd = src.error_with_bnd
dest.error_bnd_x = copy(src.error_bnd_x)
dest.error_bnd_y = copy(src.error_bnd_y)
dest.timer = src.timer
dest.status = src.status
return dest
end

"""
Type for statistics returned by the LSLQ method, the attributes are:
- niter
Expand Down Expand Up @@ -241,6 +333,21 @@ function reset!(stats :: LSLQStats)
empty!(stats.err_ubnds_cg)
end

function copyto!(dest :: LSLQStats, src :: LSLQStats)
dest.niter = src.niter
dest.solved = src.solved
dest.inconsistent = src.inconsistent
dest.residuals = copy(src.residuals)
dest.Aresiduals = copy(src.Aresiduals)
dest.err_lbnds = copy(src.err_lbnds)
dest.error_with_bnd = src.error_with_bnd
dest.err_ubnds_lq = copy(src.err_ubnds_lq)
dest.err_ubnds_cg = copy(src.err_ubnds_cg)
dest.timer = src.timer
dest.status = src.status
return dest
end

import Base.show

special_fields = Dict(
Expand Down
16 changes: 16 additions & 0 deletions test/test_stats.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
@testset "stats" begin
stats = Krylov.SimpleStats(0, true, true, Float64[1.0], Float64[2.0], Float64[], 1.234, "unknown")
stats2 = Krylov.SimpleStats(1, true, true, Float64[1.0], Float64[2.0], Float64[], 1.234, "unknown")
copyto!(stats2, stats)
io = IOBuffer()
show(io, stats)
showed = String(take!(io))
Expand All @@ -20,6 +22,8 @@
@test nbytes_allocated == 0

stats = Krylov.LsmrStats(0, true, true, Float64[1.0], Float64[2.0], Float64(3.0), Float64(4.0), Float64(5.0), Float64(6.0), Float64(7.0), 0.1234, "unknown")
stats2 = Krylov.LsmrStats(1, true, true, Float64[1.0], Float64[2.0], Float64(3.0), Float64(4.0), Float64(5.0), Float64(6.0), Float64(7.0), 0.1234, "unknown")
copyto!(stats2, stats)
io = IOBuffer()
show(io, stats)
showed = String(take!(io))
Expand All @@ -44,6 +48,8 @@
@test nbytes_allocated == 0

stats = Krylov.LanczosStats(0, true, Float64[3.0], true, NaN, NaN, 1.234, "unknown")
stats2 = Krylov.LanczosStats(1, true, Float64[3.0], true, NaN, NaN, 1.234, "unknown")
copyto!(stats2, stats)
io = IOBuffer()
show(io, stats)
showed = String(take!(io))
Expand All @@ -64,6 +70,8 @@
@test nbytes_allocated == 0

stats = Krylov.LanczosShiftStats(0, true, [Float64[0.9, 0.5], Float64[0.6, 0.4, 0.1]], BitVector([false, true]), NaN, NaN, 0.00056789, "unknown")
stats2 = Krylov.LanczosShiftStats(1, true, [Float64[0.9, 0.5], Float64[0.6, 0.4, 0.1]], BitVector([false, true]), NaN, NaN, 0.00056789, "unknown")
copyto!(stats2, stats)
io = IOBuffer()
show(io, stats)
showed = String(take!(io))
Expand All @@ -83,6 +91,8 @@
@test nbytes_allocated == 0

stats = Krylov.SymmlqStats(0, true, Float64[4.0], Union{Float64,Missing}[5.0, missing], Float64[6.0], Union{Float64,Missing}[7.0, missing], NaN, NaN, 1.234, "unknown")
stats2 = Krylov.SymmlqStats(1, true, Float64[4.0], Union{Float64,Missing}[5.0, missing], Float64[6.0], Union{Float64,Missing}[7.0, missing], NaN, NaN, 1.234, "unknown")
copyto!(stats2, stats)
io = IOBuffer()
show(io, stats)
showed = String(take!(io))
Expand All @@ -105,6 +115,8 @@
@test nbytes_allocated == 0

stats = Krylov.AdjointStats(0, true, true, Float64[8.0], Float64[9.0], 1.234, "unknown")
stats2 = Krylov.AdjointStats(1, true, true, Float64[8.0], Float64[9.0], 1.234, "unknown")
copyto!(stats2, stats)
io = IOBuffer()
show(io, stats)
showed = String(take!(io))
Expand All @@ -124,6 +136,8 @@
@test nbytes_allocated == 0

stats = Krylov.LNLQStats(0, true, Float64[10.0], false, Float64[11.0], Float64[12.0], 1.234, "unknown")
stats2 = Krylov.LNLQStats(1, true, Float64[10.0], false, Float64[11.0], Float64[12.0], 1.234, "unknown")
copyto!(stats2, stats)
io = IOBuffer()
show(io, stats)
showed = String(take!(io))
Expand All @@ -144,6 +158,8 @@
@test nbytes_allocated == 0

stats = Krylov.LSLQStats(0, true, false, Float64[13.0], Float64[14.0], Float64[15.0], false, Float64[16.0], Float64[17.0], 1.234, "unknown")
stats2 = Krylov.LSLQStats(1, true, false, Float64[13.0], Float64[14.0], Float64[15.0], false, Float64[16.0], Float64[17.0], 1.234, "unknown")
copyto!(stats2, stats)
io = IOBuffer()
show(io, stats)
showed = String(take!(io))
Expand Down

0 comments on commit 0379bf8

Please sign in to comment.