Skip to content

Commit 1793e51

Browse files
committed
scalevector --> default_preconditioner, start with lazy invcomposepreconditioner
1 parent 3575cb2 commit 1793e51

File tree

3 files changed

+38
-58
lines changed

3 files changed

+38
-58
lines changed

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 = ScaleVector(one(eltype(A)), true)
74-
Pr = ScaleVector(one(eltype(A)), false)
73+
Pl = default_preconditioner(one(eltype(A)), true)
74+
Pr = default_preconditioner(one(eltype(A)), false)
7575

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

src/wrappers.jl

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,7 @@
11

22
## Preconditioners
33

4-
"""
5-
LEFT
6-
P * x = x .* (1/P.s)
7-
Pi * x = x .* (P.s)
8-
9-
RIGHT
10-
P * x = x .* (P.s)
11-
Pi * x = x .* (1/P.s)
12-
"""
13-
struct ScaleVector{T}
14-
s::T
15-
isleft::Bool
16-
end
17-
18-
Base.eltype(A::ScaleVector) = eltype(A.s)
19-
Base.adjoint(A::ScaleVector) = copy(A)
20-
Base.inv(A::ScaleVector) = ScaleVector(1/A.s, A.isleft)
21-
22-
# y = A x
23-
function LinearAlgebra.mul!(y, A::ScaleVector, x)
24-
A.s == one(eltype(A.s)) && return y = x
25-
26-
s = A.isleft ? 1/A.s : A.s
27-
mul!(y, s, x)
28-
end
29-
30-
# A B α + C β
31-
function LinearAlgebra.mul!(C, A::ScaleVector, B, α, β)
32-
A.s == one(eltype(A.s)) && return @. C = α * B + β * C
33-
34-
s = A.isleft ? 1/A.s : A.s
35-
mul!(C, s, B, α, β)
36-
end
37-
38-
function LinearAlgebra.ldiv!(A::ScaleVector, x)
39-
A.s == one(eltype(A.s)) && return x
40-
41-
s = A.isleft ? A.s : 1/A.s
42-
@. x = x * s
43-
end
44-
45-
function LinearAlgebra.ldiv!(y, A::ScaleVector, x)
46-
A.s == one(eltype(A.s)) && return y = x
47-
48-
s = A.isleft ? A.s : 1/A.s
49-
mul!(y, s, x)
50-
end
4+
default_preconditioner(s, isleft) = isleft ? I * s : I * (1/s)
515

526
"""
537
C * x = P * Q * x
@@ -60,17 +14,15 @@ end
6014

6115
Base.eltype(A::ComposePreconditioner) = Float64 #eltype(A.inner)
6216
Base.adjoint(A::ComposePreconditioner) = ComposePreconditioner(A.outer', A.inner')
63-
Base.inv(A::ComposePreconditioner) = ComposePreconditioner(inv(A.outer), inv(A.inner))
17+
Base.inv(A::ComposePreconditioner) = InvComposePreconditioner(A)
6418

65-
# y = A x
6619
function LinearAlgebra.mul!(y, A::ComposePreconditioner, x)
6720
@unpack inner, outer = A
6821
tmp = similar(y)
6922
mul!(tmp, outer, x)
7023
mul!(y, inner, tmp)
7124
end
7225

73-
# A B α + C β
7426
function LinearAlgebra.mul!(C, A::ComposePreconditioner, B, α, β)
7527
@unpack inner, outer = A
7628
tmp = similar(B)
@@ -92,6 +44,34 @@ function LinearAlgebra.ldiv!(y, A::ComposePreconditioner, x)
9244
ldiv!(outer, y)
9345
end
9446

47+
struct InvComposePreconditioner{Tp <: ComposePreconditioner}
48+
P::Tp
49+
end
50+
51+
InvComposePreconditioner(inner, outer) = InvComposePreconditioner(ComposePreconditioner(inner, outer))
52+
53+
Base.eltype(A::InvComposePreconditioner) = Float64 #eltype(A.inner)
54+
#Base.adjoint(A::InvComposePreconditioner) = ComposePreconditioner(A.outer', A.inner')
55+
Base.inv(A::InvComposePreconditioner) = ComposePreconditioner(A.P)
56+
57+
function LinearAlgebra.mul!(y, A::InvComposePreconditioner, x)
58+
@unpack P = A
59+
ldiv!(y, P, x)
60+
end
61+
62+
function LinearAlgebra.mul!(C, A::InvComposePreconditioner, B, α, β)
63+
@unpack P = A
64+
end
65+
66+
function LinearAlgebra.ldiv!(A::InvComposePreconditioner, x)
67+
@unpack P = A
68+
end
69+
70+
function LinearAlgebra.ldiv!(y, A::InvComposePreconditioner, x)
71+
@unpack P = A
72+
mul!(y, P, x)
73+
end
74+
9575
## Krylov.jl
9676

9777
struct KrylovJL{F,Tl,Tr,I,A,K} <: AbstractKrylovSubspaceMethod
@@ -202,8 +182,8 @@ function SciMLBase.solve(cache::LinearCache, alg::KrylovJL; kwargs...)
202182
TODO - pass in inv(Pl), inv(Pr) to Krylov.jl
203183
"""
204184

205-
# M = ComposePreconditioner(alg.Pl, cache.Pl) # left precond
206-
# N = ComposePreconditioner(alg.Pr, cache.Pr) # right
185+
# M = InvComposePreconditioner(alg.Pl, cache.Pl) # left precond
186+
# N = InvComposePreconditioner(alg.Pr, cache.Pr) # right
207187

208188
atol = cache.abstol
209189
rtol = cache.reltol

test/runtests.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ end
135135
x = rand(n,n)
136136
y = rand(n,n)
137137

138-
Pl = LinearSolve.ScaleVector(s, true)
139-
Pr = LinearSolve.ScaleVector(s, false)
138+
Pl = LinearSolve.default_preconditioner(s, true)
139+
Pr = LinearSolve.default_preconditioner(s, false)
140140

141141
mul!(y, Pl, x)
142142
mul!(y, Pr, x)
@@ -160,8 +160,8 @@ end
160160
x = rand(n,n)
161161
y = rand(n,n)
162162

163-
Pi = LinearSolve.ScaleVector(s, true)
164-
Po = LinearSolve.ScaleVector(s, false)
163+
Pi = LinearSolve.default_preconditioner(s, true)
164+
Po = LinearSolve.default_preconditioner(s, false)
165165

166166
P = LinearSolve.ComposePreconditioner(Pi,Po)
167167

0 commit comments

Comments
 (0)