1+ @generated function SciMLBase. solve! (cache:: LinearCache , alg:: AbstractFactorization ;
2+ kwargs... )
3+ quote
4+ if cache. isfresh
5+ fact = do_factorization (alg, cache. A, cache. b, cache. u)
6+ cache. cacheval = fact
7+
8+ # If factorization was not successful, return failure. Don't reset `isfresh`
9+ if _notsuccessful (fact)
10+ return SciMLBase. build_linear_solution (
11+ alg, cache. u, nothing , cache; retcode = ReturnCode. Failure)
12+ end
13+
14+ cache. isfresh = false
15+ end
16+
17+ y = _ldiv! (cache. u, @get_cacheval (cache, $ (Meta. quot (defaultalg_symbol (alg)))),
18+ cache. b)
19+ return SciMLBase. build_linear_solution (alg, y, nothing , cache; retcode = ReturnCode. Success)
20+ end
21+ end
22+
123macro get_cacheval (cache, algsym)
224 quote
325 if $ (esc (cache)). alg isa DefaultLinearSolver
@@ -8,6 +30,8 @@ macro get_cacheval(cache, algsym)
830 end
931end
1032
33+ const PREALLOCATED_IPIV = Vector {LinearAlgebra.BlasInt} (undef, 0 )
34+
1135_ldiv! (x, A, b) = ldiv! (x, A, b)
1236
1337_ldiv! (x, A, b:: SVector ) = (x .= A \ b)
@@ -41,8 +65,7 @@ function LinearSolve.init_cacheval(
4165 alg:: RFLUFactorization , A:: Matrix{Float64} , b, u, Pl, Pr,
4266 maxiters:: Int ,
4367 abstol, reltol, verbose:: Bool , assumptions:: OperatorAssumptions )
44- ipiv = Vector {LinearAlgebra.BlasInt} (undef, 0 )
45- PREALLOCATED_LU, ipiv
68+ PREALLOCATED_LU, PREALLOCATED_IPIV
4669end
4770
4871function LinearSolve. init_cacheval (alg:: RFLUFactorization ,
@@ -144,41 +167,85 @@ function do_factorization(alg::LUFactorization, A, b, u)
144167 return fact
145168end
146169
147- function do_factorization (alg:: GenericLUFactorization , A, b, u)
170+ function init_cacheval (
171+ alg:: GenericLUFactorization , A, b, u, Pl, Pr,
172+ maxiters:: Int , abstol, reltol, verbose:: Bool ,
173+ assumptions:: OperatorAssumptions )
174+ ipiv = Vector {LinearAlgebra.BlasInt} (undef, min (size (A)... ))
175+ ArrayInterface. lu_instance (convert (AbstractMatrix, A)), ipiv
176+ end
177+
178+ function init_cacheval (
179+ alg:: GenericLUFactorization , A:: Matrix{Float64} , b, u, Pl, Pr,
180+ maxiters:: Int , abstol, reltol, verbose:: Bool ,
181+ assumptions:: OperatorAssumptions )
182+ PREALLOCATED_LU, PREALLOCATED_IPIV
183+ end
184+
185+ function SciMLBase. solve! (cache:: LinearSolve.LinearCache , alg:: GenericLUFactorization ;
186+ kwargs... )
187+ A = cache. A
148188 A = convert (AbstractMatrix, A)
149- fact = LinearAlgebra. generic_lufact! (A, alg. pivot, check = false )
150- return fact
189+ fact, ipiv = LinearSolve. @get_cacheval (cache, :GenericLUFactorization )
190+
191+ if cache. isfresh
192+ if length (ipiv) != min (size (A)... )
193+ ipiv = Vector {LinearAlgebra.BlasInt} (undef, min (size (A)... ))
194+ end
195+ fact = generic_lufact! (A, alg. pivot, ipiv; check = false )
196+ cache. cacheval = (fact, ipiv)
197+
198+ if ! LinearAlgebra. issuccess (fact)
199+ return SciMLBase. build_linear_solution (
200+ alg, cache. u, nothing , cache; retcode = ReturnCode. Failure)
201+ end
202+
203+ cache. isfresh = false
204+ end
205+ y = ldiv! (cache. u, LinearSolve. @get_cacheval (cache, :GenericLUFactorization )[1 ], cache. b)
206+ SciMLBase. build_linear_solution (alg, y, nothing , cache)
151207end
152208
153209function init_cacheval (
154- alg:: Union{ LUFactorization, GenericLUFactorization} , A, b, u, Pl, Pr,
210+ alg:: LUFactorization , A, b, u, Pl, Pr,
155211 maxiters:: Int , abstol, reltol, verbose:: Bool ,
156212 assumptions:: OperatorAssumptions )
157213 ArrayInterface. lu_instance (convert (AbstractMatrix, A))
158214end
159215
160- function init_cacheval (alg:: Union{ LUFactorization, GenericLUFactorization} ,
216+ function init_cacheval (alg:: LUFactorization ,
161217 A:: Union{<:Adjoint, <:Transpose} , b, u, Pl, Pr, maxiters:: Int , abstol, reltol,
162218 verbose:: Bool , assumptions:: OperatorAssumptions )
163219 error_no_cudss_lu (A)
164- if alg isa LUFactorization
165- return lu (A; check = false )
166- else
167- A isa GPUArraysCore. AnyGPUArray && return nothing
168- return LinearAlgebra. generic_lufact! (copy (A), alg. pivot; check = false )
169- end
220+ return lu (A; check = false )
221+ end
222+
223+ function init_cacheval (alg:: GenericLUFactorization ,
224+ A:: Union{<:Adjoint, <:Transpose} , b, u, Pl, Pr, maxiters:: Int , abstol, reltol,
225+ verbose:: Bool , assumptions:: OperatorAssumptions )
226+ error_no_cudss_lu (A)
227+ A isa GPUArraysCore. AnyGPUArray && return nothing
228+ ipiv = Vector {LinearAlgebra.BlasInt} (undef, 0 )
229+ return LinearAlgebra. generic_lufact! (copy (A), alg. pivot; check = false ), ipiv
170230end
171231
172232const PREALLOCATED_LU = ArrayInterface. lu_instance (rand (1 , 1 ))
173233
174- function init_cacheval (alg:: Union{ LUFactorization, GenericLUFactorization} ,
234+ function init_cacheval (alg:: LUFactorization ,
175235 A:: Matrix{Float64} , b, u, Pl, Pr,
176236 maxiters:: Int , abstol, reltol, verbose:: Bool ,
177237 assumptions:: OperatorAssumptions )
178238 PREALLOCATED_LU
179239end
180240
181- function init_cacheval (alg:: Union{LUFactorization, GenericLUFactorization} ,
241+ function init_cacheval (alg:: LUFactorization ,
242+ A:: AbstractSciMLOperator , b, u, Pl, Pr,
243+ maxiters:: Int , abstol, reltol, verbose:: Bool ,
244+ assumptions:: OperatorAssumptions )
245+ nothing
246+ end
247+
248+ function init_cacheval (alg:: GenericLUFactorization ,
182249 A:: AbstractSciMLOperator , b, u, Pl, Pr,
183250 maxiters:: Int , abstol, reltol, verbose:: Bool ,
184251 assumptions:: OperatorAssumptions )
0 commit comments