Skip to content

Commit 6b6d2dd

Browse files
committed
commiting updates - still WIP
1 parent 3d82d71 commit 6b6d2dd

File tree

3 files changed

+57
-35
lines changed

3 files changed

+57
-35
lines changed

src/LinearSolve.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module LinearSolve
22

33
using ArrayInterface
44
using RecursiveFactorization
5-
using Base: cache_dependencies, Bool
5+
using Base: cache_dependencies, Bool, eltype
66
using LinearAlgebra
77
using SparseArrays
88
using SciMLBase: AbstractDiffEqOperator, AbstractLinearAlgorithm

src/common.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ function SciMLBase.init(prob::LinearProblem, alg::Union{SciMLLinearSolveAlgorith
7070
isfresh = cacheval === nothing
7171
Tc = isfresh ? Any : typeof(cacheval)
7272

73-
Pl = LinearAlgebra.I
74-
Pr = LinearAlgebra.I
73+
Pl = ScaleVector(one(eltype(A)), true)
74+
Pr = ScaleVector(one(eltype(A)), false)
7575

7676
A = alias_A ? A : deepcopy(A)
7777
b = alias_b ? b : deepcopy(b)

src/wrappers.jl

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,72 @@
11

22
## Preconditioners
33

4+
"""
5+
P * x = x .* (1/P.s)
6+
7+
Pi * x = x .* (P.s)
8+
"""
49
struct ScaleVector{T}
510
s::T
611
isleft::Bool
712
end
813

9-
function LinearAlgebra.ldiv!(P::ScaleVector, x)
10-
P.s == one(eltype(P.s)) && return x
14+
function LinearAlgebra.mul!(y, A::ScaleVector, x)
15+
end
16+
17+
function LinearAlgebra.mul!(C, A::ScaleVector, B, α, β)
18+
end
19+
20+
function LinearAlgebra.ldiv!(A::ScaleVector, x)
21+
A.s == one(eltype(A.s)) && return x
1122

12-
if P.isleft
13-
@. x = x * P.s # @.. doesnt speed up scalar computation
23+
if A.isleft
24+
@. x = x * A.s
1425
else
15-
@. x = x / P.s
26+
@. x = x / A.s
1627
end
1728
end
1829

19-
function LinearAlgebra.ldiv!(y, P::ScaleVector, x)
20-
P.s == one(eltype(P.s)) && return y = x
30+
function LinearAlgebra.ldiv!(y, A::ScaleVector, x)
31+
P.s == one(eltype(A.s)) && return y = x
2132

22-
if P.isleft
23-
@. y = x / P.s
33+
if A.isleft
34+
@. y = x * A.s
2435
else
25-
@. y = x * P.s
36+
@. y = x / A.s
2637
end
2738
end
2839

40+
Base.eltype(A::ScaleVector) = eltype(A.s)
41+
42+
"""
43+
C * x = P * Q * x
44+
45+
Ci * x = Qi * Pi * x
46+
"""
2947
struct ComposePreconditioner{Ti,To}
3048
inner::Ti
3149
outer::To
32-
isleft::Bool
3350
end
3451

35-
function LinearAlgebra.ldiv!(P::ComposePreconditioner, x)
36-
@unpack inner, outer, isleft = P
37-
if isleft
38-
ldiv!(outer, x)
39-
ldiv!(inner, x)
40-
else
41-
ldiv!(inner, x)
42-
ldiv!(outer, x)
43-
end
52+
function LinearAlgebra.ldiv!(A::ComposePreconditioner, x)
53+
@unpack inner, outer, isleft = A
54+
55+
ldiv!(inner, x)
56+
ldiv!(outer, x)
57+
end
58+
59+
function LinearAlgebra.ldiv!(y, A::ComposePreconditioner, x)
60+
@unpack inner, outer = A
61+
62+
ldiv!(y, inner, x)
63+
ldiv!(outer, y)
64+
end
65+
66+
function LinearAlgebra.mul!(y, A::ComposePreconditioner, x)
4467
end
4568

46-
function LinearAlgebra.ldiv!(y, P::ComposePreconditioner, x)
47-
@unpack inner, outer, isleft = P
69+
function LinearAlgebra.mul!(C, A::ComposePreconditioner, B, α, β)
4870
end
4971

5072
## Krylov.jl
@@ -150,8 +172,8 @@ function SciMLBase.solve(cache::LinearCache, alg::KrylovJL; kwargs...)
150172
cache = set_cacheval(cache, solver)
151173
end
152174

153-
# Pl = ComposePreconditioner(alg.Pl, cache.Pl, true)
154-
# Pr = ComposePreconditioner(alg.Pr, cache.Pr, false)
175+
M = alg.Pl #ComposePreconditioner(alg.Pl, cache.Pl) # left precond
176+
N = alg.Pr #ComposePreconditioner(alg.Pr, cache.Pr) # right
155177

156178
atol = cache.abstol
157179
rtol = cache.reltol
@@ -163,20 +185,20 @@ function SciMLBase.solve(cache::LinearCache, alg::KrylovJL; kwargs...)
163185
alg.kwargs...)
164186

165187
if cache.cacheval isa Krylov.CgSolver
166-
alg.Pr != LinearAlgebra.I &&
188+
N != LinearAlgebra.I &&
167189
@warn "$(alg.KrylovAlg) doesn't support right preconditioning."
168-
Krylov.solve!(args...; M=alg.Pl,
190+
Krylov.solve!(args...; M=M,
169191
kwargs...)
170192
elseif cache.cacheval isa Krylov.GmresSolver
171-
Krylov.solve!(args...; M=alg.Pl, N=alg.Pr,
193+
Krylov.solve!(args...; M=M, N=N,
172194
kwargs...)
173195
elseif cache.cacheval isa Krylov.BicgstabSolver
174-
Krylov.solve!(args...; M=alg.Pl, N=alg.Pr,
196+
Krylov.solve!(args...; M=M, N=N,
175197
kwargs...)
176198
elseif cache.cacheval isa Krylov.MinresSolver
177-
alg.Pr != LinearAlgebra.I &&
199+
N != LinearAlgebra.I &&
178200
@warn "$(alg.KrylovAlg) doesn't support right preconditioning."
179-
Krylov.solve!(args...; M=alg.Pl,
201+
Krylov.solve!(args...; M=M,
180202
kwargs...)
181203
else
182204
Krylov.solve!(args...; kwargs...)
@@ -228,8 +250,8 @@ function init_cacheval(alg::IterativeSolversJL, cache::LinearCache)
228250
Pl = alg.Pl
229251
Pr = alg.Pr
230252

231-
# Pl = ComposePreconditioner(alg.Pl, cache.Pl, true)
232-
# Pr = ComposePreconditioner(alg.Pr, cache.Pr, false)
253+
# Pl = ComposePreconditioner(alg.Pl, cache.Pl)
254+
# Pr = ComposePreconditioner(alg.Pr, cache.Pr)
233255

234256
abstol = cache.abstol
235257
reltol = cache.reltol

0 commit comments

Comments
 (0)