Skip to content

Commit 781bcef

Browse files
authored
Merge pull request #205 from lostella/julia-0.7-update
Julia 0.7 update
2 parents 3006251 + 8d9ac8d commit 781bcef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+665
-638
lines changed

.travis.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
language: julia
22
os:
3-
- linux
3+
- linux
44
julia:
5-
- 0.6
6-
- nightly
5+
- 0.7
6+
- nightly
77
matrix:
88
allow_failures:
99
- julia: nightly
1010
notifications:
11-
email: false
11+
email: false
1212
after_success:
13-
- julia -e 'Pkg.add("Coverage"); cd(Pkg.dir("IterativeSolvers")); using Coverage; Coveralls.submit(process_folder());'
14-
- julia -e 'Pkg.add("Documenter"); cd(Pkg.dir("IterativeSolvers")); include(joinpath("docs", "make.jl"))'
13+
- julia -e 'Pkg.add("Coverage"); cd(Pkg.dir("IterativeSolvers")); using Coverage; Coveralls.submit(process_folder());'
14+
- julia -e 'Pkg.add("Documenter"); cd(Pkg.dir("IterativeSolvers")); include(joinpath("docs", "make.jl"))'

REQUIRE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
julia 0.6
1+
julia 0.7
22

3-
RecipesBase
3+
RecipesBase 0.3.1

benchmark/advection_diffusion.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function advection_dominated(;N = 50, β = 1000.0)
1919
Δ = laplace_matrix(Float64, N, 3) ./ -h^2
2020

2121
# And the dx bit.
22-
∂x_1d = spdiagm((fill(-β / 2h, N - 1), fill/ 2h, N - 1)), (-1, 1))
22+
∂x_1d = spdiagm(-1 => fill(-β / 2h, N - 1), 1 => fill/ 2h, N - 1))
2323
∂x = kron(speye(N^2), ∂x_1d)
2424

2525
# Final matrix and rhs.
@@ -41,6 +41,6 @@ function laplace_matrix(::Type{T}, n, dims) where T
4141
end
4242

4343
second_order_central_diff(::Type{T}, dim) where {T} = convert(
44-
SparseMatrixCSC{T, Int},
44+
SparseMatrixCSC{T, Int},
4545
SymTridiagonal(fill(2 * one(T), dim), fill(-one(T), dim - 1))
46-
)
46+
)

benchmark/benchmark-hessenberg.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,24 @@ function backslash_versus_givens(; n = 1_000, ms = 10 : 10 : 100)
1616
println(m)
1717

1818
H = zeros(m + 1, m)
19-
19+
2020
# Create an orthonormal basis for the Krylov subspace
2121
V = rand(n, m + 1)
2222
V[:, 1] /= norm(V[:, 1])
23-
23+
2424
for i = 1 : m
2525
# Expand
2626
V[:, i + 1] = A * V[:, i]
27-
27+
2828
# Orthogonalize
2929
H[1 : i, i] = view(V, :, 1 : i)' * V[:, i + 1]
3030
V[:, i + 1] -= view(V, :, 1 : i) * H[1 : i, i]
31-
31+
3232
# Re-orthogonalize
3333
update = view(V, :, 1 : i)' * V[:, i + 1]
3434
H[1 : i, i] += update
3535
V[:, i + 1] -= view(V, :, 1 : i) * update
36-
36+
3737
# Normalize
3838
H[i + 1, i] = norm(V[:, i + 1])
3939
V[:, i + 1] /= H[i + 1, i]
@@ -43,11 +43,11 @@ function backslash_versus_givens(; n = 1_000, ms = 10 : 10 : 100)
4343
rhs = [i == 1 ? 1.0 : 0.0 for i = 1 : size(H, 1)]
4444

4545
# Run the benchmark
46-
results["givens_qr"][m] = @benchmark A_ldiv_B!(IterativeSolvers.Hessenberg(myH), myRhs) setup = (myH = copy($H); myRhs = copy($rhs))
46+
results["givens_qr"][m] = @benchmark ldiv!(IterativeSolvers.Hessenberg(myH), myRhs) setup = (myH = copy($H); myRhs = copy($rhs))
4747
results["backslash"][m] = @benchmark $H \ $rhs
4848
end
4949

5050
return results
5151
end
5252

53-
end
53+
end

benchmark/benchmark-linear-systems.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module LinearSystemsBench
22

3-
import Base.A_ldiv_B!, Base.\
3+
import Base.ldiv!, Base.\
44

55
using BenchmarkTools
66
using IterativeSolvers
@@ -12,7 +12,7 @@ struct DiagonalPreconditioner{T}
1212
diag::Vector{T}
1313
end
1414

15-
function A_ldiv_B!(y::AbstractVector{T}, A::DiagonalPreconditioner{T}, b::AbstractVector{T}) where T
15+
function ldiv!(y::AbstractVector{T}, A::DiagonalPreconditioner{T}, b::AbstractVector{T}) where T
1616
for i = 1 : length(b)
1717
@inbounds y[i] = A.diag[i] \ b[i]
1818
end
@@ -34,7 +34,7 @@ function cg(; n = 1_000_000, tol = 1e-6, maxiter::Int = 200)
3434
println("Symmetric positive definite matrix of size ", n)
3535
println("Eigenvalues in interval [0.01, 4.01]")
3636
println("Tolerance = ", tol, "; max #iterations = ", maxiter)
37-
37+
3838
# Dry run
3939
initial = rand(n)
4040
IterativeSolvers.cg!(copy(initial), A, b, Pl = P, maxiter = maxiter, tol = tol, log = false)

benchmark/benchmark-svd-florida.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ function runbenchmark(filename, benchmarkfilename)
7878
#Choose the same normalized unit vector to start with
7979
q = randn(n)
8080
eltype(A) <: Complex && (q += im*randn(n))
81-
scale!(q, inv(norm(q)))
81+
rmul!(q, inv(norm(q)))
8282
qm = randn(m)
8383
eltype(A) <: Complex && (q += im*randn(m))
84-
scale!(qm, inv(norm(qm)))
84+
rmul!(qm, inv(norm(qm)))
8585

8686
#Number of singular values to request
8787
nv = min(m, n, 10)

docs/src/getting_started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Rather than constructing an explicit matrix `A` of the type `Matrix` or `SparseM
2525
For matrix-free types of `A` the following interface is expected to be defined:
2626

2727
- `A*v` computes the matrix-vector product on a `v::AbstractVector`;
28-
- `A_mul_B!(y, A, v)` computes the matrix-vector product on a `v::AbstractVector` in-place;
28+
- `mul!(y, A, v)` computes the matrix-vector product on a `v::AbstractVector` in-place;
2929
- `eltype(A)` returns the element type implicit in the equivalent matrix representation of `A`;
3030
- `size(A, d)` returns the nominal dimensions along the `d`th axis in the equivalent matrix representation of `A`.
3131

docs/src/iterators.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ end
4343
@show norm(b1 - A * x) / norm(b1)
4444

4545
# Copy the next right-hand side into the iterable
46-
copy!(my_iterable.b, b2)
46+
copyto!(my_iterable.b, b2)
4747

4848
for item in my_iterable
4949
println("Iteration for rhs 2")

docs/src/preconditioning.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
Many iterative solvers have the option to provide left and right preconditioners (`Pl` and `Pr` resp.) in order to speed up convergence or prevent stagnation. They transform a problem $Ax = b$ into a better conditioned system $(P_l^{-1}AP_r^{-1})y = P_l^{-1}b$, where $x = P_r^{-1}y$.
44

5-
These preconditioners should support the operations
5+
These preconditioners should support the operations
66

7-
- `A_ldiv_B!(y, P, x)` computes `P \ x` in-place of `y`;
8-
- `A_ldiv_B!(P, x)` computes `P \ x` in-place of `x`;
7+
- `ldiv!(y, P, x)` computes `P \ x` in-place of `y`;
8+
- `ldiv!(P, x)` computes `P \ x` in-place of `x`;
99
- and `P \ x`.
1010

11-
If no preconditioners are passed to the solver, the method will default to
11+
If no preconditioners are passed to the solver, the method will default to
1212

1313
```julia
1414
Pl = Pr = IterativeSolvers.Identity()
@@ -18,4 +18,4 @@ Pl = Pr = IterativeSolvers.Identity()
1818
IterativeSolvers.jl itself does not provide any other preconditioners besides `Identity()`, but recommends the following external packages:
1919

2020
- [ILU.jl](https://github.com/haampie/ILU.jl) for incomplete LU decompositions (using drop tolerance);
21-
- [IncompleteSelectedInversion.jl](https://github.com/ettersi/IncompleteSelectedInversion.jl) for incomplete LDLt decompositions.
21+
- [IncompleteSelectedInversion.jl](https://github.com/ettersi/IncompleteSelectedInversion.jl) for incomplete LDLt decompositions.

src/bicgstabl.jl

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export bicgstabl, bicgstabl!, bicgstabl_iterator, bicgstabl_iterator!, BiCGStabIterable
2-
3-
import Base: start, next, done
2+
using Printf
3+
import Base: iterate
44

55
mutable struct BiCGStabIterable{precT, matT, solT, vecT <: AbstractVector, smallMatT <: AbstractMatrix, realT <: Real, scalarT <: Number}
66
A::matT
@@ -36,23 +36,23 @@ function bicgstabl_iterator!(x, A, b, l::Int = 2;
3636

3737
# Large vectors.
3838
r_shadow = rand(T, n)
39-
rs = Matrix{T}(n, l + 1)
39+
rs = Matrix{T}(undef, n, l + 1)
4040
us = zeros(T, n, l + 1)
4141

4242
residual = view(rs, :, 1)
43-
43+
4444
# Compute the initial residual rs[:, 1] = b - A * x
4545
# Avoid computing A * 0.
4646
if initial_zero
47-
copy!(residual, b)
47+
copyto!(residual, b)
4848
else
49-
A_mul_B!(residual, A, x)
49+
mul!(residual, A, x)
5050
residual .= b .- residual
5151
mv_products += 1
5252
end
5353

5454
# Apply the left preconditioner
55-
A_ldiv_B!(Pl, residual)
55+
ldiv!(Pl, residual)
5656

5757
γ = zeros(T, l)
5858
ω = σ = one(T)
@@ -76,35 +76,37 @@ end
7676
@inline start(::BiCGStabIterable) = 0
7777
@inline done(it::BiCGStabIterable, iteration::Int) = it.mv_products it.max_mv_products || converged(it)
7878

79-
function next(it::BiCGStabIterable, iteration::Int)
79+
function iterate(it::BiCGStabIterable, iteration::Int=start(it))
80+
if done(it, iteration) return nothing end
81+
8082
T = eltype(it.x)
8183
L = 2 : it.l + 1
8284

8385
it.σ = -it.ω * it.σ
84-
86+
8587
## BiCG part
8688
for j = 1 : it.l
8789
ρ = dot(it.r_shadow, view(it.rs, :, j))
8890
β = ρ / it.σ
89-
91+
9092
# us[:, 1 : j] .= rs[:, 1 : j] - β * us[:, 1 : j]
9193
it.us[:, 1 : j] .= it.rs[:, 1 : j] .- β .* it.us[:, 1 : j]
9294

9395
# us[:, j + 1] = Pl \ (A * us[:, j])
9496
next_u = view(it.us, :, j + 1)
95-
A_mul_B!(next_u, it.A, view(it.us, :, j))
96-
A_ldiv_B!(it.Pl, next_u)
97+
mul!(next_u, it.A, view(it.us, :, j))
98+
ldiv!(it.Pl, next_u)
9799

98100
it.σ = dot(it.r_shadow, next_u)
99101
α = ρ / it.σ
100102

101103
it.rs[:, 1 : j] .-= α .* it.us[:, 2 : j + 1]
102-
104+
103105
# rs[:, j + 1] = Pl \ (A * rs[:, j])
104106
next_r = view(it.rs, :, j + 1)
105-
A_mul_B!(next_r, it.A , view(it.rs, :, j))
106-
A_ldiv_B!(it.Pl, next_r)
107-
107+
mul!(next_r, it.A , view(it.rs, :, j))
108+
ldiv!(it.Pl, next_r)
109+
108110
# x = x + α * us[:, 1]
109111
it.x .+= α .* view(it.us, :, 1)
110112
end
@@ -113,13 +115,13 @@ function next(it::BiCGStabIterable, iteration::Int)
113115
it.mv_products += 2 * it.l
114116

115117
## MR part
116-
118+
117119
# M = rs' * rs
118-
Ac_mul_B!(it.M, it.rs, it.rs)
120+
mul!(it.M, adjoint(it.rs), it.rs)
119121

120-
# γ = M[L, L] \ M[L, 1]
121-
F = lufact!(view(it.M, L, L))
122-
A_ldiv_B!(it.γ, F, view(it.M, L, 1))
122+
# γ = M[L, L] \ M[L, 1]
123+
F = lu!(view(it.M, L, L))
124+
ldiv!(it.γ, F, view(it.M, L, 1))
123125

124126
# This could even be BLAS 3 when combined.
125127
BLAS.gemv!('N', -one(T), view(it.us, :, L), it.γ, one(T), view(it.us, :, 1))
@@ -155,9 +157,9 @@ bicgstabl(A, b, l = 2; kwargs...) = bicgstabl!(zerox(A, b), A, b, l; initial_zer
155157
- `max_mv_products::Int = size(A, 2)`: maximum number of matrix vector products.
156158
For BiCGStab(l) this is a less dubious term than "number of iterations";
157159
- `Pl = Identity()`: left preconditioner of the method;
158-
- `tol::Real = sqrt(eps(real(eltype(b))))`: tolerance for stopping condition `|r_k| / |r_0| ≤ tol`.
159-
Note that (1) the true residual norm is never computed during the iterations,
160-
only an approximation; and (2) if a preconditioner is given, the stopping condition is based on the
160+
- `tol::Real = sqrt(eps(real(eltype(b))))`: tolerance for stopping condition `|r_k| / |r_0| ≤ tol`.
161+
Note that (1) the true residual norm is never computed during the iterations,
162+
only an approximation; and (2) if a preconditioner is given, the stopping condition is based on the
161163
*preconditioned residual*.
162164
163165
# Return values
@@ -184,10 +186,10 @@ function bicgstabl!(x, A, b, l = 2;
184186

185187
# This doesn't yet make sense: the number of iters is smaller.
186188
log && reserve!(history, :resnorm, max_mv_products)
187-
189+
188190
# Actually perform CG
189191
iterable = bicgstabl_iterator!(x, A, b, l; Pl = Pl, tol = tol, max_mv_products = max_mv_products, kwargs...)
190-
192+
191193
if log
192194
history.mvps = iterable.mv_products
193195
end
@@ -200,10 +202,10 @@ function bicgstabl!(x, A, b, l = 2;
200202
end
201203
verbose && @printf("%3d\t%1.2e\n", iteration, iterable.residual)
202204
end
203-
205+
204206
verbose && println()
205207
log && setconv(history, converged(iterable))
206208
log && shrink!(history)
207-
209+
208210
log ? (iterable.x, history) : iterable.x
209211
end

0 commit comments

Comments
 (0)