/
benchmark-linear-systems.jl
80 lines (59 loc) · 2.53 KB
/
benchmark-linear-systems.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
module LinearSystemsBench
import Base.A_ldiv_B!, Base.\
using BenchmarkTools
using IterativeSolvers
include("../test/advection_diffusion.jl")
# A DiagonalMatrix that doesn't check whether it is singular in the \ op.
immutable DiagonalPreconditioner{T}
diag::Vector{T}
end
function A_ldiv_B!{T}(y::AbstractVector{T}, A::DiagonalPreconditioner{T}, b::AbstractVector{T})
for i = 1 : length(b)
@inbounds y[i] = A.diag[i] \ b[i]
end
y
end
(\)(D::DiagonalPreconditioner, b::AbstractVector) = D.diag .\ b
function posdef(n)
A = SymTridiagonal(fill(2.01, n), fill(-1.0, n))
b = A * ones(n)
return A, b
end
function cg(; n = 1_000_000, tol = 1e-6, maxiter::Int = 200)
A, b = posdef(n)
P = DiagonalPreconditioner(collect(linspace(1.0, 2.0, n)))
println("Symmetric positive definite matrix of size ", n)
println("Eigenvalues in interval [0.01, 4.01]")
println("Tolerance = ", tol, "; max #iterations = ", maxiter)
# Dry run
initial = rand(n)
IterativeSolvers.cg!(copy(initial), A, b, Pl = P, maxiter = maxiter, tol = tol, log = false)
# Actual benchmark
@benchmark IterativeSolvers.cg!(x0, $A, $b, Pl = $P, maxiter = $maxiter, tol = $tol, log = false) setup=(x0 = copy($initial))
end
function indefinite(n)
# Generate an indefinite "hard" matrix
srand(1)
A = speye(n) + sprand(n, n, 5.0 / n)
A = (A + A') / 2
x = ones(n)
b = A * x
A, b
end
function gmres(; n = 100_000, tol = 1e-5, restart::Int = 15, maxiter::Int = 210)
A, b = indefinite(n)
println("Matrix of size ", n, " with ~", nnz(A) / n, " nonzeros per row")
println("Tolerance = ", tol, "; restart = ", restart, "; max #iterations = ", maxiter)
impr = @benchmark IterativeSolvers.improved_gmres($A, $b, tol = $tol, restart = $restart, maxiter = $maxiter, log = false)
old = @benchmark IterativeSolvers.gmres($A, $b, tol = $tol, restart = $restart, maxiter = $maxiter, log = false)
impr, old
end
function bicgstabl()
A, b = advection_dominated()
b1 = @benchmark IterativeSolvers.bicgstabl($A, $b, 2, max_mv_products = 1000, convex_combination = false) setup = (srand(1))
b2 = @benchmark IterativeSolvers.bicgstabl($A, $b, 2, max_mv_products = 1000, convex_combination = true) setup = (srand(1))
b3 = @benchmark IterativeSolvers.bicgstabl($A, $b, 4, max_mv_products = 1000, convex_combination = false) setup = (srand(1))
b4 = @benchmark IterativeSolvers.bicgstabl($A, $b, 4, max_mv_products = 1000, convex_combination = true) setup = (srand(1))
b1, b2, b3, b4
end
end