JuliaLinearAlgebra / IterativeSolvers.jl

Iterative algorithms for solving linear systems, eigensystems, and singular value problems
MIT License
399 stars 105 forks source link

CG with Diagonal Preconditioner has dubiously high memory allocations #262

Closed GregVernon closed 4 years ago

GregVernon commented 4 years ago

Compared to other preconditioners, when I use DiagonalPreconditioner from Preconditioners.jl there is an inordinate amount of memory allocations (e.g. 5 orders of magnitude more than when using an ILU preconditioner!). Not sure if this issue belongs on IterativeSolvers or on Preconditioners issues page, but I thought I'd start here. See below for MWE of the issue.

import BenchmarkTools
import IterativeSolvers
import IncompleteLU
import Preconditioners
import MatrixDepot

# Create a Wathen matrix
NX = 100
NY = 100
A = MatrixDepot.wathen(NX,NY)
b = rand(size(A,1))

# Precompute Preconditioners
PL_ilu = IncompleteLU.ilu(A,τ=1e-2)
PL_jac = Preconditioners.DiagonalPreconditioner(A)  # <===== This one
PL_amg_1 = Preconditioners.AMGPreconditioner{Preconditioners.RugeStuben}(A)
PL_amg_2 = Preconditioners.AMGPreconditioner{Preconditioners.SmoothedAggregation}(A)

# Benchmark the solvers
println("Direct Solver (\\)")
BenchmarkTools.@btime $A\$b

println("Iterative Solver (cg)")
BenchmarkTools.@btime IterativeSolvers.cg($A,$b,tol=1e-5);

println("Iterative Solver (pcg-ilu)")
BenchmarkTools.@btime IterativeSolvers.cg($A,$b,tol=1e-5,Pl=$PL_ilu);

println("Iterative Solver (pcg-jac)")
BenchmarkTools.@btime IterativeSolvers.cg($A,$b,tol=1e-5,Pl=$PL_jac); # <===== This one

println("Iterative Solver (pcg-amg:rs)")
BenchmarkTools.@btime IterativeSolvers.cg($A,$b,tol=1e-5,Pl=$PL_amg_1);

println("Iterative Solver (pcg-amg:sa)")
BenchmarkTools.@btime IterativeSolvers.cg($A,$b,tol=1e-5,Pl=$PL_amg_2);

Results in

Direct Solver (\)
          149.337 ms (53 allocations: 51.89 MiB)
Iterative Solver (cg)
          212.769 ms (19 allocations: 1.16 MiB)
Iterative Solver (pcg-ilu)
          5.610 ms (20 allocations: 1.16 MiB)
Iterative Solver (pcg-jac)
          149.278 ms (4560170 allocations: 209.91 MiB) # <===== This one
Iterative Solver (pcg-amg:rs)
          65.514 ms (29 allocations: 1.16 MiB)
Iterative Solver (pcg-amg:sa)
          50.865 ms (29 allocations: 1.16 MiB)
GregVernon commented 4 years ago

https://github.com/mohamed82008/Preconditioners.jl/issues/12