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
+
1
23
macro get_cacheval (cache, algsym)
2
24
quote
3
25
if $ (esc (cache)). alg isa DefaultLinearSolver
@@ -8,6 +30,8 @@ macro get_cacheval(cache, algsym)
8
30
end
9
31
end
10
32
33
+ const PREALLOCATED_IPIV = Vector {LinearAlgebra.BlasInt} (undef, 0 )
34
+
11
35
_ldiv! (x, A, b) = ldiv! (x, A, b)
12
36
13
37
_ldiv! (x, A, b:: SVector ) = (x .= A \ b)
@@ -41,8 +65,7 @@ function LinearSolve.init_cacheval(
41
65
alg:: RFLUFactorization , A:: Matrix{Float64} , b, u, Pl, Pr,
42
66
maxiters:: Int ,
43
67
abstol, reltol, verbose:: Bool , assumptions:: OperatorAssumptions )
44
- ipiv = Vector {LinearAlgebra.BlasInt} (undef, 0 )
45
- PREALLOCATED_LU, ipiv
68
+ PREALLOCATED_LU, PREALLOCATED_IPIV
46
69
end
47
70
48
71
function LinearSolve. init_cacheval (alg:: RFLUFactorization ,
@@ -144,14 +167,47 @@ function do_factorization(alg::LUFactorization, A, b, u)
144
167
return fact
145
168
end
146
169
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
148
188
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; check = false , ipiv)
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)
151
207
end
152
208
153
209
function init_cacheval (
154
- alg:: Union{ LUFactorization, GenericLUFactorization} , A, b, u, Pl, Pr,
210
+ alg:: LUFactorization , A, b, u, Pl, Pr,
155
211
maxiters:: Int , abstol, reltol, verbose:: Bool ,
156
212
assumptions:: OperatorAssumptions )
157
213
ArrayInterface. lu_instance (convert (AbstractMatrix, A))
171
227
172
228
const PREALLOCATED_LU = ArrayInterface. lu_instance (rand (1 , 1 ))
173
229
174
- function init_cacheval (alg:: Union{ LUFactorization, GenericLUFactorization} ,
230
+ function init_cacheval (alg:: LUFactorization ,
175
231
A:: Matrix{Float64} , b, u, Pl, Pr,
176
232
maxiters:: Int , abstol, reltol, verbose:: Bool ,
177
233
assumptions:: OperatorAssumptions )
0 commit comments