JuliaLinearAlgebra / Preconditioners.jl

A few preconditioners for iterative solvers.
https://julialinearalgebra.github.io/Preconditioners.jl/
Other
50 stars 11 forks source link

Fix allocations #19

Closed t-bltg closed 3 years ago

t-bltg commented 3 years ago

While I'm on this, fix https://github.com/mohamed82008/Preconditioners.jl/issues/12.

import IterativeSolvers
import Preconditioners
import BenchmarkTools
import IncompleteLU
import MatrixDepot

main() = begin
  # Create a Wathen matrix
  NX, NY = 100, 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)
  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, reltol=1e-5)

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

  println("Iterative Solver (pcg-jac)")
  BenchmarkTools.@btime IterativeSolvers.cg($A, $b, reltol=1e-5, Pl=$PL_jac)

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

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

main()

Output

Direct Solver (\)
  109.960 ms (52 allocations: 51.89 MiB)
Iterative Solver (cg)
  224.699 ms (16 allocations: 951.36 KiB)
Iterative Solver (pcg-ilu)
  11.859 ms (16 allocations: 951.44 KiB)
Iterative Solver (pcg-jac)
  64.358 ms (16 allocations: 951.36 KiB)  # <===== All good
Iterative Solver (pcg-amg:rs)
  70.661 ms (16 allocations: 951.36 KiB)
Iterative Solver (pcg-amg:sa)
  57.407 ms (16 allocations: 951.36 KiB)
mohamed82008 commented 3 years ago

Views stopped being allocating in Julia for a while. Did you compare to master?

t-bltg commented 3 years ago

Views stopped being allocating in Julia for a while. Did you compare to master?

Still allocates without this PR:

Direct Solver (\)
  107.877 ms (52 allocations: 51.89 MiB)
Iterative Solver (cg)
  291.656 ms (16 allocations: 951.36 KiB)
Iterative Solver (pcg-ilu)
  18.325 ms (16 allocations: 951.44 KiB)
Iterative Solver (pcg-jac)
  334.762 ms (3800141 allocations: 209.68 MiB)  # <===== NOK
Iterative Solver (pcg-amg:rs)
  73.954 ms (16 allocations: 951.36 KiB)
Iterative Solver (pcg-amg:sa)
  58.413 ms (16 allocations: 951.36 KiB)
mohamed82008 commented 3 years ago

Wow ok. Thanks for the PR then :)