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,41 +167,85 @@ 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, 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)
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))
158
214
end
159
215
160
- function init_cacheval (alg:: Union{ LUFactorization, GenericLUFactorization} ,
216
+ function init_cacheval (alg:: LUFactorization ,
161
217
A:: Union{<:Adjoint, <:Transpose} , b, u, Pl, Pr, maxiters:: Int , abstol, reltol,
162
218
verbose:: Bool , assumptions:: OperatorAssumptions )
163
219
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
170
230
end
171
231
172
232
const PREALLOCATED_LU = ArrayInterface. lu_instance (rand (1 , 1 ))
173
233
174
- function init_cacheval (alg:: Union{ LUFactorization, GenericLUFactorization} ,
234
+ function init_cacheval (alg:: LUFactorization ,
175
235
A:: Matrix{Float64} , b, u, Pl, Pr,
176
236
maxiters:: Int , abstol, reltol, verbose:: Bool ,
177
237
assumptions:: OperatorAssumptions )
178
238
PREALLOCATED_LU
179
239
end
180
240
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 ,
182
249
A:: AbstractSciMLOperator , b, u, Pl, Pr,
183
250
maxiters:: Int , abstol, reltol, verbose:: Bool ,
184
251
assumptions:: OperatorAssumptions )
0 commit comments