forked from SciML/LinearSolve.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearSolveFastLapackInterfaceExt.jl
More file actions
86 lines (78 loc) · 3.24 KB
/
Copy pathLinearSolveFastLapackInterfaceExt.jl
File metadata and controls
86 lines (78 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
module LinearSolveFastLapackInterfaceExt
using LinearSolve, LinearAlgebra
using FastLapackInterface
struct WorkspaceAndFactors{W, F}
workspace::W
factors::F
end
function LinearSolve.init_cacheval(::FastLUFactorization, A, b, u, Pl, Pr,
maxiters::Int, abstol, reltol, verbose::LinearVerbosity,
assumptions::OperatorAssumptions)
ws = LUWs(A)
return WorkspaceAndFactors(
ws, LinearSolve.ArrayInterface.lu_instance(convert(AbstractMatrix, A)))
end
function SciMLBase.solve!(
cache::LinearSolve.LinearCache, alg::FastLUFactorization; kwargs...)
A = cache.A
A = convert(AbstractMatrix, A)
ws_and_fact = LinearSolve.@get_cacheval(cache, :FastLUFactorization)
if cache.isfresh
# we will fail here if A is a different *size* than in a previous version of the same cache.
# it may instead be desirable to resize the workspace.
LinearSolve.@set! ws_and_fact.factors = LinearAlgebra.LU(LAPACK.getrf!(
ws_and_fact.workspace,
A)...)
cache.cacheval = ws_and_fact
cache.isfresh = false
end
y = ldiv!(cache.u, cache.cacheval.factors, cache.b)
SciMLBase.build_linear_solution(alg, y, nothing, cache)
end
function LinearSolve.init_cacheval(
alg::FastQRFactorization{NoPivot}, A::AbstractMatrix, b, u, Pl, Pr,
maxiters::Int, abstol, reltol, verbose::LinearVerbosity,
assumptions::OperatorAssumptions)
ws = QRWYWs(A; blocksize = alg.blocksize)
return WorkspaceAndFactors(ws,
LinearSolve.ArrayInterface.qr_instance(convert(AbstractMatrix, A)))
end
function LinearSolve.init_cacheval(
::FastQRFactorization{ColumnNorm}, A::AbstractMatrix, b, u, Pl, Pr,
maxiters::Int, abstol, reltol, verbose::LinearVerbosity,
assumptions::OperatorAssumptions)
ws = QRpWs(A)
return WorkspaceAndFactors(ws,
LinearSolve.ArrayInterface.qr_instance(convert(AbstractMatrix, A)))
end
function LinearSolve.init_cacheval(alg::FastQRFactorization, A, b, u, Pl, Pr,
maxiters::Int, abstol, reltol, verbose::LinearVerbosity,
assumptions::OperatorAssumptions)
return init_cacheval(alg, convert(AbstractMatrix, A), b, u, Pl, Pr,
maxiters::Int, abstol, reltol, verbose::LinearVerbosity,
assumptions::OperatorAssumptions)
end
function SciMLBase.solve!(cache::LinearSolve.LinearCache, alg::FastQRFactorization{P};
kwargs...) where {P}
A = cache.A
A = convert(AbstractMatrix, A)
ws_and_fact = LinearSolve.@get_cacheval(cache, :FastQRFactorization)
if cache.isfresh
# we will fail here if A is a different *size* than in a previous version of the same cache.
# it may instead be desirable to resize the workspace.
if P === NoPivot
LinearSolve.@set! ws_and_fact.factors = LinearAlgebra.QRCompactWY(LAPACK.geqrt!(
ws_and_fact.workspace,
A)...)
else
LinearSolve.@set! ws_and_fact.factors = LinearAlgebra.QRPivoted(LAPACK.geqp3!(
ws_and_fact.workspace,
A)...)
end
cache.cacheval = ws_and_fact
cache.isfresh = false
end
y = ldiv!(cache.u, cache.cacheval.factors, cache.b)
SciMLBase.build_linear_solution(alg, y, nothing, cache)
end
end