Skip to content

Commit 49cc093

Browse files
lostellamohamed82008
authored andcommitted
updated code to support julia 0.7
1 parent 42a435f commit 49cc093

Some content is hidden

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

43 files changed

+562
-530
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
julia 0.6
1+
julia 0.7
22

33
RecipesBase

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: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export bicgstabl, bicgstabl!, bicgstabl_iterator, bicgstabl_iterator!, BiCGStabIterable
2-
2+
using Printf
33
import Base: start, next, done
44

55
mutable struct BiCGStabIterable{precT, matT, solT, vecT <: AbstractVector, smallMatT <: AbstractMatrix, realT <: Real, scalarT <: Number}
@@ -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)
@@ -81,30 +81,30 @@ function next(it::BiCGStabIterable, iteration::Int)
8181
L = 2 : it.l + 1
8282

8383
it.σ = -it.ω * it.σ
84-
84+
8585
## BiCG part
8686
for j = 1 : it.l
8787
ρ = dot(it.r_shadow, view(it.rs, :, j))
8888
β = ρ / it.σ
89-
89+
9090
# us[:, 1 : j] .= rs[:, 1 : j] - β * us[:, 1 : j]
9191
it.us[:, 1 : j] .= it.rs[:, 1 : j] .- β .* it.us[:, 1 : j]
9292

9393
# us[:, j + 1] = Pl \ (A * us[:, j])
9494
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)
95+
mul!(next_u, it.A, view(it.us, :, j))
96+
ldiv!(it.Pl, next_u)
9797

9898
it.σ = dot(it.r_shadow, next_u)
9999
α = ρ / it.σ
100100

101101
it.rs[:, 1 : j] .-= α .* it.us[:, 2 : j + 1]
102-
102+
103103
# rs[:, j + 1] = Pl \ (A * rs[:, j])
104104
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-
105+
mul!(next_r, it.A , view(it.rs, :, j))
106+
ldiv!(it.Pl, next_r)
107+
108108
# x = x + α * us[:, 1]
109109
it.x .+= α .* view(it.us, :, 1)
110110
end
@@ -113,13 +113,13 @@ function next(it::BiCGStabIterable, iteration::Int)
113113
it.mv_products += 2 * it.l
114114

115115
## MR part
116-
116+
117117
# M = rs' * rs
118-
Ac_mul_B!(it.M, it.rs, it.rs)
118+
mul!(it.M, adjoint(it.rs), it.rs)
119119

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))
120+
# γ = M[L, L] \ M[L, 1]
121+
F = lu!(view(it.M, L, L))
122+
ldiv!(it.γ, F, view(it.M, L, 1))
123123

124124
# This could even be BLAS 3 when combined.
125125
BLAS.gemv!('N', -one(T), view(it.us, :, L), it.γ, one(T), view(it.us, :, 1))
@@ -155,9 +155,9 @@ bicgstabl(A, b, l = 2; kwargs...) = bicgstabl!(zerox(A, b), A, b, l; initial_zer
155155
- `max_mv_products::Int = size(A, 2)`: maximum number of matrix vector products.
156156
For BiCGStab(l) this is a less dubious term than "number of iterations";
157157
- `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
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
161161
*preconditioned residual*.
162162
163163
# Return values
@@ -184,10 +184,10 @@ function bicgstabl!(x, A, b, l = 2;
184184

185185
# This doesn't yet make sense: the number of iters is smaller.
186186
log && reserve!(history, :resnorm, max_mv_products)
187-
187+
188188
# Actually perform CG
189189
iterable = bicgstabl_iterator!(x, A, b, l; Pl = Pl, tol = tol, max_mv_products = max_mv_products, kwargs...)
190-
190+
191191
if log
192192
history.mvps = iterable.mv_products
193193
end
@@ -200,10 +200,10 @@ function bicgstabl!(x, A, b, l = 2;
200200
end
201201
verbose && @printf("%3d\t%1.2e\n", iteration, iterable.residual)
202202
end
203-
203+
204204
verbose && println()
205205
log && setconv(history, converged(iterable))
206206
log && shrink!(history)
207-
207+
208208
log ? (iterable.x, history) : iterable.x
209209
end

src/cg.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Base: start, next, done
2-
2+
using Printf
33
export cg, cg!, CGIterable, PCGIterable, cg_iterator!, CGStateVariables
44

55
mutable struct CGIterable{matT, solT, vecT, numT <: Real}
@@ -46,7 +46,7 @@ function next(it::CGIterable, iteration::Int)
4646
it.u .= it.r .+ β .* it.u
4747

4848
# c = A * u
49-
A_mul_B!(it.c, it.A, it.u)
49+
mul!(it.c, it.A, it.u)
5050
α = it.residual^2 / dot(it.u, it.c)
5151

5252
# Improve solution and residual
@@ -65,17 +65,17 @@ end
6565
#####################
6666

6767
function next(it::PCGIterable, iteration::Int)
68-
A_ldiv_B!(it.c, it.Pl, it.r)
68+
ldiv!(it.c, it.Pl, it.r)
6969

7070
ρ_prev = it.ρ
7171
it.ρ = dot(it.c, it.r)
72-
72+
7373
# u := c + βu (almost an axpy)
7474
β = it.ρ / ρ_prev
7575
it.u .= it.c .+ β .* it.u
7676

7777
# c = A * u
78-
A_mul_B!(it.c, it.A, it.u)
78+
mul!(it.c, it.A, it.u)
7979
α = it.ρ / dot(it.u, it.c)
8080

8181
# Improve solution and residual
@@ -109,14 +109,14 @@ end
109109
function cg_iterator!(x, A, b, Pl = Identity();
110110
tol = sqrt(eps(real(eltype(b)))),
111111
maxiter::Int = size(A, 2),
112-
statevars::CGStateVariables = CGStateVariables{eltype(x),typeof(x)}(zeros(x), similar(x), similar(x)),
112+
statevars::CGStateVariables = CGStateVariables{eltype(x),typeof(x)}(zero(x), similar(x), similar(x)),
113113
initially_zero::Bool = false
114114
)
115115
u = statevars.u
116116
r = statevars.r
117117
c = statevars.c
118118
u .= zero(eltype(x))
119-
copy!(r, b)
119+
copyto!(r, b)
120120

121121
# Compute r with an MV-product or not.
122122
if initially_zero
@@ -126,7 +126,7 @@ function cg_iterator!(x, A, b, Pl = Identity();
126126
reltol = residual * tol # Save one dot product
127127
else
128128
mv_products = 1
129-
A_mul_B!(c, A, x)
129+
mul!(c, A, x)
130130
r .-= c
131131
residual = norm(r)
132132
reltol = norm(b) * tol
@@ -165,10 +165,10 @@ cg(A, b; kwargs...) = cg!(zerox(A, b), A, b; initially_zero = true, kwargs...)
165165
## Keywords
166166
167167
- `statevars::CGStateVariables`: Has 3 arrays similar to `x` to hold intermediate results;
168-
- `initially_zero::Bool`: If `true` assumes that `iszero(x)` so that one
169-
matrix-vector product can be saved when computing the initial
168+
- `initially_zero::Bool`: If `true` assumes that `iszero(x)` so that one
169+
matrix-vector product can be saved when computing the initial
170170
residual vector;
171-
- `Pl = Identity()`: left preconditioner of the method. Should be symmetric,
171+
- `Pl = Identity()`: left preconditioner of the method. Should be symmetric,
172172
positive-definite like `A`;
173173
- `tol::Real = sqrt(eps(real(eltype(b))))`: tolerance for stopping condition `|r_k| / |r_0| ≤ tol`;
174174
- `maxiter::Int = size(A,2)`: maximum number of iterations;
@@ -195,7 +195,7 @@ function cg!(x, A, b;
195195
tol = sqrt(eps(real(eltype(b)))),
196196
maxiter::Int = size(A, 2),
197197
log::Bool = false,
198-
statevars::CGStateVariables = CGStateVariables{eltype(x), typeof(x)}(zeros(x), similar(x), similar(x)),
198+
statevars::CGStateVariables = CGStateVariables{eltype(x), typeof(x)}(zero(x), similar(x), similar(x)),
199199
verbose::Bool = false,
200200
Pl = Identity(),
201201
kwargs...

0 commit comments

Comments
 (0)