Skip to content

Commit b7fe5c0

Browse files
Fix check checking on previous Julia versions (#332)
* Fix check checking on previous Julia versions * flip check * fix precompile for posdef * fix test definition for symmetric * Fix symmetric test * only precompile sparse if v1.9 * Fix tests * only do check on sparse for v1.9 up * fix cholesky pivots * try different pivots... again.. * ugh * wrong version * require some version bumping We just need to drop the older versions soon, this is crazy. * Diagonal is v1.9 * another versioning fix * a few more versionings... * one more * only JET on v1.9 * format
1 parent c0bed7b commit b7fe5c0

File tree

6 files changed

+145
-73
lines changed

6 files changed

+145
-73
lines changed

.github/workflows/CI.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ jobs:
1717
version:
1818
- '1'
1919
- '1.6'
20+
- '1.7'
21+
- '1.8'
2022
include:
2123
- version: '1'
2224
group: 'LinearSolveHYPRE'

src/LinearSolve.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,14 @@ end
153153
end
154154
end
155155

156-
PrecompileTools.@compile_workload begin
157-
A = sprand(4, 4, 0.3) + I
158-
b = rand(4)
159-
prob = LinearProblem(A, b)
160-
sol = solve(prob) # in case sparspak is used as default
161-
sol = solve(prob, SparspakFactorization())
156+
@static if VERSION > v"1.9-"
157+
PrecompileTools.@compile_workload begin
158+
A = sprand(4, 4, 0.3) + I
159+
b = rand(4)
160+
prob = LinearProblem(A * A', b)
161+
sol = solve(prob) # in case sparspak is used as default
162+
sol = solve(prob, SparspakFactorization())
163+
end
162164
end
163165

164166
export LUFactorization, SVDFactorization, QRFactorization, GenericFactorization,

src/factorization.jl

Lines changed: 108 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function init_cacheval(alg::Union{LUFactorization, GenericLUFactorization},
120120
nothing
121121
end
122122

123-
@static if VERSION < v"1.7-"
123+
@static if VERSION < v"1.9-"
124124
function init_cacheval(alg::Union{LUFactorization, GenericLUFactorization},
125125
A::Union{Diagonal, SymTridiagonal}, b, u, Pl, Pr,
126126
maxiters::Int, abstol, reltol, verbose::Bool,
@@ -188,7 +188,7 @@ function init_cacheval(alg::QRFactorization, A::AbstractSciMLOperator, b, u, Pl,
188188
nothing
189189
end
190190

191-
@static if VERSION < v"1.7-"
191+
@static if VERSION < v"1.9-"
192192
function init_cacheval(alg::QRFactorization,
193193
A::Union{Diagonal, SymTridiagonal, Tridiagonal}, b, u, Pl, Pr,
194194
maxiters::Int, abstol, reltol, verbose::Bool,
@@ -220,7 +220,7 @@ end
220220

221221
function CholeskyFactorization(; pivot = nothing, tol = 0.0, shift = 0.0, perm = nothing)
222222
if pivot === nothing
223-
pivot = @static if VERSION < v"1.7beta"
223+
pivot = @static if VERSION < v"1.8beta"
224224
Val(false)
225225
else
226226
NoPivot()
@@ -229,16 +229,30 @@ function CholeskyFactorization(; pivot = nothing, tol = 0.0, shift = 0.0, perm =
229229
CholeskyFactorization(pivot, 16, shift, perm)
230230
end
231231

232-
function do_factorization(alg::CholeskyFactorization, A, b, u)
233-
A = convert(AbstractMatrix, A)
234-
if A isa SparseMatrixCSC
235-
fact = cholesky!(A; shift = alg.shift, check = false, perm = alg.perm)
236-
elseif alg.pivot === Val(false) || alg.pivot === NoPivot()
237-
fact = cholesky!(A, alg.pivot; check = false)
238-
else
239-
fact = cholesky!(A, alg.pivot; tol = alg.tol, check = false)
232+
@static if VERSION > v"1.8-"
233+
function do_factorization(alg::CholeskyFactorization, A, b, u)
234+
A = convert(AbstractMatrix, A)
235+
if A isa SparseMatrixCSC
236+
fact = cholesky!(A; shift = alg.shift, check = false, perm = alg.perm)
237+
elseif alg.pivot === Val(false) || alg.pivot === NoPivot()
238+
fact = cholesky!(A, alg.pivot; check = false)
239+
else
240+
fact = cholesky!(A, alg.pivot; tol = alg.tol, check = false)
241+
end
242+
return fact
243+
end
244+
else
245+
function do_factorization(alg::CholeskyFactorization, A, b, u)
246+
A = convert(AbstractMatrix, A)
247+
if A isa SparseMatrixCSC
248+
fact = cholesky!(A; shift = alg.shift, perm = alg.perm)
249+
elseif alg.pivot === Val(false) || alg.pivot === NoPivot()
250+
fact = cholesky!(A, alg.pivot)
251+
else
252+
fact = cholesky!(A, alg.pivot; tol = alg.tol)
253+
end
254+
return fact
240255
end
241-
return fact
242256
end
243257

244258
function init_cacheval(alg::CholeskyFactorization, A, b, u, Pl, Pr,
@@ -247,7 +261,7 @@ function init_cacheval(alg::CholeskyFactorization, A, b, u, Pl, Pr,
247261
ArrayInterface.cholesky_instance(convert(AbstractMatrix, A), alg.pivot)
248262
end
249263

250-
@static if VERSION < v"1.7beta"
264+
@static if VERSION < v"1.8beta"
251265
cholpivot = Val(false)
252266
else
253267
cholpivot = NoPivot()
@@ -268,7 +282,7 @@ function init_cacheval(alg::CholeskyFactorization,
268282
nothing
269283
end
270284

271-
@static if VERSION < v"1.7beta"
285+
@static if VERSION < v"1.9beta"
272286
function init_cacheval(alg::CholeskyFactorization,
273287
A::Union{SymTridiagonal, Tridiagonal}, b, u, Pl, Pr,
274288
maxiters::Int, abstol, reltol, verbose::Bool,
@@ -361,7 +375,7 @@ function init_cacheval(alg::SVDFactorization, A, b, u, Pl, Pr,
361375
nothing
362376
end
363377

364-
@static if VERSION < v"1.7-"
378+
@static if VERSION < v"1.9-"
365379
function init_cacheval(alg::SVDFactorization,
366380
A::Union{Diagonal, SymTridiagonal, Tridiagonal}, b, u, Pl, Pr,
367381
maxiters::Int, abstol, reltol, verbose::Bool,
@@ -852,22 +866,42 @@ function init_cacheval(alg::CHOLMODFactorization,
852866
PREALLOCATED_CHOLMOD
853867
end
854868

855-
function SciMLBase.solve!(cache::LinearCache, alg::CHOLMODFactorization; kwargs...)
856-
A = cache.A
857-
A = convert(AbstractMatrix, A)
869+
@static if VERSION > v"1.8-"
870+
function SciMLBase.solve!(cache::LinearCache, alg::CHOLMODFactorization; kwargs...)
871+
A = cache.A
872+
A = convert(AbstractMatrix, A)
858873

859-
if cache.isfresh
860-
cacheval = @get_cacheval(cache, :CHOLMODFactorization)
861-
fact = cholesky(A; check = false)
862-
if !LinearAlgebra.issuccess(fact)
863-
ldlt!(fact, A; check = false)
874+
if cache.isfresh
875+
cacheval = @get_cacheval(cache, :CHOLMODFactorization)
876+
fact = cholesky(A; check = false)
877+
if !LinearAlgebra.issuccess(fact)
878+
ldlt!(fact, A; check = false)
879+
end
880+
cache.cacheval = fact
881+
cache.isfresh = false
864882
end
865-
cache.cacheval = fact
866-
cache.isfresh = false
883+
884+
cache.u .= @get_cacheval(cache, :CHOLMODFactorization) \ cache.b
885+
SciMLBase.build_linear_solution(alg, cache.u, nothing, cache)
867886
end
887+
else
888+
function SciMLBase.solve!(cache::LinearCache, alg::CHOLMODFactorization; kwargs...)
889+
A = cache.A
890+
A = convert(AbstractMatrix, A)
891+
892+
if cache.isfresh
893+
cacheval = @get_cacheval(cache, :CHOLMODFactorization)
894+
fact = cholesky(A)
895+
if !LinearAlgebra.issuccess(fact)
896+
ldlt!(fact, A)
897+
end
898+
cache.cacheval = fact
899+
cache.isfresh = false
900+
end
868901

869-
cache.u .= @get_cacheval(cache, :CHOLMODFactorization) \ cache.b
870-
SciMLBase.build_linear_solution(alg, cache.u, nothing, cache)
902+
cache.u .= @get_cacheval(cache, :CHOLMODFactorization) \ cache.b
903+
SciMLBase.build_linear_solution(alg, cache.u, nothing, cache)
904+
end
871905
end
872906

873907
## RFLUFactorization
@@ -909,7 +943,7 @@ function init_cacheval(alg::RFLUFactorization,
909943
nothing, nothing
910944
end
911945

912-
@static if VERSION < v"1.7-"
946+
@static if VERSION < v"1.9-"
913947
function init_cacheval(alg::RFLUFactorization,
914948
A::Union{Diagonal, SymTridiagonal, Tridiagonal}, b, u, Pl, Pr,
915949
maxiters::Int,
@@ -954,7 +988,7 @@ end
954988

955989
function NormalCholeskyFactorization(; pivot = nothing)
956990
if pivot === nothing
957-
pivot = @static if VERSION < v"1.7beta"
991+
pivot = @static if VERSION < v"1.8beta"
958992
Val(false)
959993
else
960994
NoPivot()
@@ -966,7 +1000,7 @@ end
9661000
default_alias_A(::NormalCholeskyFactorization, ::Any, ::Any) = true
9671001
default_alias_b(::NormalCholeskyFactorization, ::Any, ::Any) = true
9681002

969-
@static if VERSION < v"1.7beta"
1003+
@static if VERSION < v"1.8beta"
9701004
normcholpivot = Val(false)
9711005
else
9721006
normcholpivot = NoPivot()
@@ -996,7 +1030,7 @@ function init_cacheval(alg::NormalCholeskyFactorization,
9961030
nothing
9971031
end
9981032

999-
@static if VERSION < v"1.7-"
1033+
@static if VERSION < v"1.9-"
10001034
function init_cacheval(alg::NormalCholeskyFactorization,
10011035
A::Union{Tridiagonal, SymTridiagonal, Adjoint}, b, u, Pl, Pr,
10021036
maxiters::Int, abstol, reltol, verbose::Bool,
@@ -1005,26 +1039,54 @@ end
10051039
end
10061040
end
10071041

1008-
function SciMLBase.solve!(cache::LinearCache, alg::NormalCholeskyFactorization;
1009-
kwargs...)
1010-
A = cache.A
1011-
A = convert(AbstractMatrix, A)
1012-
if cache.isfresh
1042+
@static if VERSION > v"1.8-"
1043+
function SciMLBase.solve!(cache::LinearCache, alg::NormalCholeskyFactorization;
1044+
kwargs...)
1045+
A = cache.A
1046+
A = convert(AbstractMatrix, A)
1047+
if cache.isfresh
1048+
if A isa SparseMatrixCSC
1049+
fact = cholesky(Symmetric((A)' * A, :L); check = false)
1050+
else
1051+
fact = cholesky(Symmetric((A)' * A, :L), alg.pivot; check = false)
1052+
end
1053+
cache.cacheval = fact
1054+
cache.isfresh = false
1055+
end
10131056
if A isa SparseMatrixCSC
1014-
fact = cholesky(Symmetric((A)' * A, :L))
1057+
cache.u .= @get_cacheval(cache, :NormalCholeskyFactorization) \ (A' * cache.b)
1058+
y = cache.u
10151059
else
1016-
fact = cholesky(Symmetric((A)' * A, :L), alg.pivot)
1060+
y = ldiv!(cache.u,
1061+
@get_cacheval(cache, :NormalCholeskyFactorization),
1062+
A' * cache.b)
10171063
end
1018-
cache.cacheval = fact
1019-
cache.isfresh = false
1064+
SciMLBase.build_linear_solution(alg, y, nothing, cache)
10201065
end
1021-
if A isa SparseMatrixCSC
1022-
cache.u .= @get_cacheval(cache, :NormalCholeskyFactorization) \ (A' * cache.b)
1023-
y = cache.u
1024-
else
1025-
y = ldiv!(cache.u, @get_cacheval(cache, :NormalCholeskyFactorization), A' * cache.b)
1066+
else
1067+
function SciMLBase.solve!(cache::LinearCache, alg::NormalCholeskyFactorization;
1068+
kwargs...)
1069+
A = cache.A
1070+
A = convert(AbstractMatrix, A)
1071+
if cache.isfresh
1072+
if A isa SparseMatrixCSC
1073+
fact = cholesky(Symmetric((A)' * A, :L))
1074+
else
1075+
fact = cholesky(Symmetric((A)' * A, :L), alg.pivot)
1076+
end
1077+
cache.cacheval = fact
1078+
cache.isfresh = false
1079+
end
1080+
if A isa SparseMatrixCSC
1081+
cache.u .= @get_cacheval(cache, :NormalCholeskyFactorization) \ (A' * cache.b)
1082+
y = cache.u
1083+
else
1084+
y = ldiv!(cache.u,
1085+
@get_cacheval(cache, :NormalCholeskyFactorization),
1086+
A' * cache.b)
1087+
end
1088+
SciMLBase.build_linear_solution(alg, y, nothing, cache)
10261089
end
1027-
SciMLBase.build_linear_solution(alg, y, nothing, cache)
10281090
end
10291091

10301092
## NormalBunchKaufmanFactorization

test/basictests.jl

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ end
5151
@testset "Default Linear Solver" begin
5252
test_interface(nothing, prob1, prob2)
5353

54-
A1 = prob1.A
54+
A1 = prob1.A * prob1.A'
5555
b1 = prob1.b
5656
x1 = prob1.u0
5757
y = solve(prob1)
@@ -77,9 +77,11 @@ end
7777
y = solve(_prob)
7878
@test A1 * y b1
7979

80-
_prob = LinearProblem(sparse(A1), b1; u0 = x1)
81-
y = solve(_prob)
82-
@test A1 * y b1
80+
if VERSION > v"1.9-"
81+
_prob = LinearProblem(sparse(A1), b1; u0 = x1)
82+
y = solve(_prob)
83+
@test A1 * y b1
84+
end
8385
end
8486

8587
@testset "UMFPACK Factorization" begin
@@ -258,19 +260,21 @@ end
258260
end
259261
end
260262

261-
@testset "KrylovKit" begin
262-
kwargs = (; gmres_restart = 5)
263-
for alg in (("Default", KrylovKitJL(kwargs...)),
264-
("CG", KrylovKitJL_CG(kwargs...)),
265-
("GMRES", KrylovKitJL_GMRES(kwargs...)))
266-
@testset "$(alg[1])" begin
267-
test_interface(alg[2], prob1, prob2)
263+
if VERSION > v"1.9-"
264+
@testset "KrylovKit" begin
265+
kwargs = (; gmres_restart = 5)
266+
for alg in (("Default", KrylovKitJL(kwargs...)),
267+
("CG", KrylovKitJL_CG(kwargs...)),
268+
("GMRES", KrylovKitJL_GMRES(kwargs...)))
269+
@testset "$(alg[1])" begin
270+
test_interface(alg[2], prob1, prob2)
271+
end
272+
@test alg[2] isa KrylovKitJL
268273
end
269-
@test alg[2] isa KrylovKitJL
270274
end
271275
end
272276

273-
if VERSION > v"1.7-"
277+
if VERSION > v"1.9-"
274278
@testset "CHOLMOD" begin
275279
# Create a posdef symmetric matrix
276280
A = sprand(100, 100, 0.01)

test/default_algs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ solve(prob)
3131
prob = LinearProblem(sprand(11000, 11000, 0.5), zeros(11000))
3232
solve(prob)
3333

34-
@static if VERSION >= v"v1.7-"
34+
@static if VERSION >= v"v1.9-"
3535
# Test inference
3636
A = rand(4, 4)
3737
b = rand(4)

test/resolve.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,16 @@ A = Symmetric([1.0 2.0
5353
linsolve.A = A
5454
@test solve!(linsolve).u [1.0, 0.0]
5555

56-
A = Symmetric([1.0 2.0
57-
2.0 1.0])
56+
A = [1.0 2.0
57+
2.0 1.0]
58+
A = Symmetric(A * A')
5859
b = [1.0, 2.0]
5960
prob = LinearProblem(A, b)
6061
linsolve = init(prob, CholeskyFactorization(), alias_A = false, alias_b = false)
61-
@test solve!(linsolve).u [1.0, 0.0]
62-
@test solve!(linsolve).u [1.0, 0.0]
63-
A = Symmetric([1.0 2.0
64-
2.0 1.0])
62+
@test solve!(linsolve).u [-1 / 3, 2 / 3]
63+
@test solve!(linsolve).u [-1 / 3, 2 / 3]
64+
A = [1.0 2.0
65+
2.0 1.0]
66+
A = Symmetric(A * A')
6567
b = [1.0, 2.0]
66-
@test solve!(linsolve).u [1.0, 0.0]
68+
@test solve!(linsolve).u [-1 / 3, 2 / 3]

0 commit comments

Comments
 (0)