From ad688914d8ef7311448e5ecc0794f51c8490226c Mon Sep 17 00:00:00 2001 From: Dilum Aluthge Date: Mon, 28 Nov 2022 02:58:46 -0500 Subject: [PATCH 1/4] Use `Preferences` to toggle precompilation of code that requires GPL dependencies --- Project.toml | 2 ++ src/LinearSolve.jl | 21 ++++++++++++++++----- src/factorization.jl | 8 -------- src/factorization_sparse.jl | 7 +++++++ 4 files changed, 25 insertions(+), 13 deletions(-) create mode 100644 src/factorization_sparse.jl diff --git a/Project.toml b/Project.toml index 46cc57b61..cd35b3aed 100644 --- a/Project.toml +++ b/Project.toml @@ -13,6 +13,7 @@ KLU = "ef3ab10e-7fda-4108-b977-705223b18434" Krylov = "ba0b0d4f-ebba-5204-a429-3ac8c609bfb7" KrylovKit = "0b1a1467-8014-51b9-945f-bf0ae24f4b77" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +Preferences = "21216c6a-2e73-6563-6e65-726566657250" RecursiveFactorization = "f2c3362d-daeb-58d1-803e-2bc74f2840b4" Reexport = "189a3867-3050-52da-a836-e630ba90ab69" SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462" @@ -31,6 +32,7 @@ IterativeSolvers = "0.9.2" KLU = "0.3.0, 0.4" Krylov = "0.9" KrylovKit = "0.5, 0.6" +Preferences = "1" RecursiveFactorization = "0.2.8" Reexport = "1" SciMLBase = "1.68" diff --git a/src/LinearSolve.jl b/src/LinearSolve.jl index 1945bde48..fd7cdd16a 100644 --- a/src/LinearSolve.jl +++ b/src/LinearSolve.jl @@ -15,6 +15,7 @@ using KLU using FastLapackInterface using DocStringExtensions import GPUArraysCore +import Preferences # wrap import Krylov @@ -47,6 +48,11 @@ include("solve_function.jl") include("default.jl") include("init.jl") +const INCLUDE_SPARSE = Preferences.@load_preference("include_sparse", Base.USE_GPL_LIBS) +@static if INCLUDE_SPARSE + include("factorization_sparse.jl") +end + const IS_OPENBLAS = Ref(true) isopenblas() = IS_OPENBLAS[] @@ -60,12 +66,17 @@ SnoopPrecompile.@precompile_all_calls begin sol = solve(prob, LUFactorization()) sol = solve(prob, RFLUFactorization()) sol = solve(prob, KrylovJL_GMRES()) +end - A = sprand(4, 4, 0.3) + I - prob = LinearProblem(A, b) - sol = solve(prob) - sol = solve(prob, KLUFactorization()) - sol = solve(prob, UMFPACKFactorization()) +@static if INCLUDE_SPARSE + SnoopPrecompile.@precompile_all_calls begin + A = sprand(4, 4, 0.3) + I + b = rand(4) + prob = LinearProblem(A, b) + sol = solve(prob) + sol = solve(prob, KLUFactorization()) + sol = solve(prob, UMFPACKFactorization()) + end end export LUFactorization, SVDFactorization, QRFactorization, GenericFactorization, diff --git a/src/factorization.jl b/src/factorization.jl index 91de7800d..47f89ae6e 100644 --- a/src/factorization.jl +++ b/src/factorization.jl @@ -5,14 +5,6 @@ function _ldiv!(x::Vector, A::Factorization, b::Vector) ldiv!(A, x) end -# Specialize QR for the non-square case -# Missing ldiv! definitions: https://github.com/JuliaSparse/SparseArrays.jl/issues/242 -function _ldiv!(x::Vector, - A::Union{SparseArrays.QR, LinearAlgebra.QRCompactWY, - SuiteSparse.SPQR.QRSparse}, b::Vector) - x .= A \ b -end - function SciMLBase.solve(cache::LinearCache, alg::AbstractFactorization; kwargs...) if cache.isfresh fact = do_factorization(alg, cache.A, cache.b, cache.u) diff --git a/src/factorization_sparse.jl b/src/factorization_sparse.jl new file mode 100644 index 000000000..f549f24d3 --- /dev/null +++ b/src/factorization_sparse.jl @@ -0,0 +1,7 @@ +# Specialize QR for the non-square case +# Missing ldiv! definitions: https://github.com/JuliaSparse/SparseArrays.jl/issues/242 +function _ldiv!(x::Vector, + A::Union{SparseArrays.QR, LinearAlgebra.QRCompactWY, + SuiteSparse.SPQR.QRSparse}, b::Vector) + x .= A \ b +end From f418c7c91ef2bd1f6d07660e1f2a89cf47f7dc45 Mon Sep 17 00:00:00 2001 From: Dilum Aluthge Date: Mon, 28 Nov 2022 03:35:17 -0500 Subject: [PATCH 2/4] Move up the definition of `INCLUDE_SPARSE` --- src/LinearSolve.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/LinearSolve.jl b/src/LinearSolve.jl index fd7cdd16a..2f6f667a5 100644 --- a/src/LinearSolve.jl +++ b/src/LinearSolve.jl @@ -39,6 +39,8 @@ needs_concrete_A(alg::AbstractSolveFunction) = false # Code +const INCLUDE_SPARSE = Preferences.@load_preference("include_sparse", Base.USE_GPL_LIBS) + include("common.jl") include("factorization.jl") include("simplelu.jl") @@ -48,7 +50,6 @@ include("solve_function.jl") include("default.jl") include("init.jl") -const INCLUDE_SPARSE = Preferences.@load_preference("include_sparse", Base.USE_GPL_LIBS) @static if INCLUDE_SPARSE include("factorization_sparse.jl") end From b576e4032ef147db454a21123c875e8dfc8537e6 Mon Sep 17 00:00:00 2001 From: Dilum Aluthge Date: Mon, 28 Nov 2022 03:38:35 -0500 Subject: [PATCH 3/4] If `INCLUDE_SPARSE` is false, then default to `KrylovJL_GMRES()` for sparse matrices --- src/default.jl | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/default.jl b/src/default.jl index f316e7f2a..f191dc744 100644 --- a/src/default.jl +++ b/src/default.jl @@ -34,11 +34,17 @@ function defaultalg(A::SymTridiagonal, b, ::OperatorAssumptions{true}) GenericFactorization(; fact_alg = ldlt!) end -function defaultalg(A::SparseMatrixCSC, b, ::OperatorAssumptions{true}) - if length(b) <= 10_000 - KLUFactorization() - else - UMFPACKFactorization() +@static if INCLUDE_SPARSE + function defaultalg(A::SparseMatrixCSC, b, ::OperatorAssumptions{true}) + if length(b) <= 10_000 + KLUFactorization() + else + UMFPACKFactorization() + end + end +else + function defaultalg(A::SparseMatrixCSC, b, ::OperatorAssumptions{true}) + KrylovJL_GMRES() end end @@ -51,11 +57,7 @@ function defaultalg(A::GPUArraysCore.AbstractGPUArray, b, ::OperatorAssumptions{ end function defaultalg(A, b::GPUArraysCore.AbstractGPUArray, ::OperatorAssumptions{true}) - if VERSION >= v"1.8-" - LUFactorization() - else - QRFactorization() - end + KrylovJL_GMRES() end function defaultalg(A::SciMLBase.AbstractDiffEqOperator, b, From fb6fd69254c34be6043c2f46ce6862f4c82198e7 Mon Sep 17 00:00:00 2001 From: Dilum Aluthge Date: Mon, 28 Nov 2022 03:43:00 -0500 Subject: [PATCH 4/4] Undo an unintentional change --- src/default.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/default.jl b/src/default.jl index f191dc744..a97748d2c 100644 --- a/src/default.jl +++ b/src/default.jl @@ -57,7 +57,11 @@ function defaultalg(A::GPUArraysCore.AbstractGPUArray, b, ::OperatorAssumptions{ end function defaultalg(A, b::GPUArraysCore.AbstractGPUArray, ::OperatorAssumptions{true}) - KrylovJL_GMRES() + if VERSION >= v"1.8-" + LUFactorization() + else + QRFactorization() + end end function defaultalg(A::SciMLBase.AbstractDiffEqOperator, b,