|
| 1 | +using LinearAlgebra |
| 2 | +using Libdl |
| 3 | + |
| 4 | +# For now, only use BLAS from Accelerate (that is to say, vecLib) |
| 5 | +global const libacc = "/System/Library/Frameworks/Accelerate.framework/Accelerate" |
| 6 | + |
| 7 | +""" |
| 8 | +```julia |
| 9 | +AppleAccelerateLUFactorization() |
| 10 | +``` |
| 11 | +
|
| 12 | +A wrapper over Apple's Accelerate Library. Direct calls to Acceelrate in a way that pre-allocates workspace |
| 13 | +to avoid allocations and does not require libblastrampoline. |
| 14 | +""" |
| 15 | +struct AppleAccelerateLUFactorization <: AbstractFactorization end |
| 16 | + |
| 17 | +function appleaccelerate_isavailable() |
| 18 | + libacc_hdl = Libdl.dlopen_e(libacc) |
| 19 | + if libacc_hdl == C_NULL |
| 20 | + return false |
| 21 | + end |
| 22 | + |
| 23 | + if dlsym_e(libacc_hdl, "dgetrf_") == C_NULL |
| 24 | + return false |
| 25 | + end |
| 26 | + return true |
| 27 | +end |
| 28 | + |
| 29 | +function aa_getrf!(A::AbstractMatrix{<:Float64}; ipiv = similar(A, Cint, min(size(A,1),size(A,2))), info = Ref{Cint}(), check = false) |
| 30 | + require_one_based_indexing(A) |
| 31 | + check && chkfinite(A) |
| 32 | + chkstride1(A) |
| 33 | + m, n = size(A) |
| 34 | + lda = max(1,stride(A, 2)) |
| 35 | + if isempty(ipiv) |
| 36 | + ipiv = similar(A, Cint, min(size(A,1),size(A,2))) |
| 37 | + end |
| 38 | + |
| 39 | + ccall(("dgetrf_", libacc), Cvoid, |
| 40 | + (Ref{Cint}, Ref{Cint}, Ptr{Float64}, |
| 41 | + Ref{Cint}, Ptr{Cint}, Ptr{Cint}), |
| 42 | + m, n, A, lda, ipiv, info) |
| 43 | + info[] < 0 && throw(ArgumentError("Invalid arguments sent to LAPACK dgetrf_")) |
| 44 | + A, ipiv, BlasInt(info[]), info #Error code is stored in LU factorization type |
| 45 | +end |
| 46 | + |
| 47 | +function aa_getrs!(trans::AbstractChar, A::AbstractMatrix{<:Float64}, ipiv::AbstractVector{Cint}, B::AbstractVecOrMat{<:Float64}; info = Ref{Cint}()) |
| 48 | + require_one_based_indexing(A, ipiv, B) |
| 49 | + LinearAlgebra.LAPACK.chktrans(trans) |
| 50 | + chkstride1(A, B, ipiv) |
| 51 | + n = LinearAlgebra.checksquare(A) |
| 52 | + if n != size(B, 1) |
| 53 | + throw(DimensionMismatch("B has leading dimension $(size(B,1)), but needs $n")) |
| 54 | + end |
| 55 | + if n != length(ipiv) |
| 56 | + throw(DimensionMismatch("ipiv has length $(length(ipiv)), but needs to be $n")) |
| 57 | + end |
| 58 | + nrhs = size(B, 2) |
| 59 | + ccall(("dgetrs_", libacc), Cvoid, |
| 60 | + (Ref{UInt8}, Ref{Cint}, Ref{Cint}, Ptr{Float64}, Ref{Cint}, |
| 61 | + Ptr{Cint}, Ptr{Float64}, Ref{Cint}, Ptr{Cint}, Clong), |
| 62 | + trans, n, size(B,2), A, max(1,stride(A,2)), ipiv, B, max(1,stride(B,2)), info, 1) |
| 63 | + LinearAlgebra.LAPACK.chklapackerror(BlasInt(info[])) |
| 64 | + B |
| 65 | +end |
| 66 | + |
| 67 | +default_alias_A(::AppleAccelerateLUFactorization, ::Any, ::Any) = false |
| 68 | +default_alias_b(::AppleAccelerateLUFactorization, ::Any, ::Any) = false |
| 69 | + |
| 70 | +function LinearSolve.init_cacheval(alg::AppleAccelerateLUFactorization, A, b, u, Pl, Pr, |
| 71 | + maxiters::Int, abstol, reltol, verbose::Bool, |
| 72 | + assumptions::OperatorAssumptions) |
| 73 | + luinst = ArrayInterface.lu_instance(convert(AbstractMatrix, A)) |
| 74 | + LU(luinst.factors,similar(A, Cint, 0), luinst.info), Ref{Cint}() |
| 75 | +end |
| 76 | + |
| 77 | +function SciMLBase.solve!(cache::LinearCache, alg::AppleAccelerateLUFactorization; |
| 78 | + kwargs...) |
| 79 | + A = cache.A |
| 80 | + A = convert(AbstractMatrix, A) |
| 81 | + if cache.isfresh |
| 82 | + cacheval = @get_cacheval(cache, :AppleAccelerateLUFactorization) |
| 83 | + res = aa_getrf!(A; ipiv = cacheval[1].ipiv, info = cacheval[2]) |
| 84 | + fact = LU(res[1:3]...), res[4] |
| 85 | + cache.cacheval = fact |
| 86 | + cache.isfresh = false |
| 87 | + end |
| 88 | + |
| 89 | + A, info = @get_cacheval(cache, :AppleAccelerateLUFactorization) |
| 90 | + LinearAlgebra.require_one_based_indexing(cache.u, cache.b) |
| 91 | + m, n = size(A, 1), size(A, 2) |
| 92 | + if m > n |
| 93 | + Bc = copy(cache.b) |
| 94 | + aa_getrs!('N', A.factors, A.ipiv, Bc; info) |
| 95 | + return copyto!(cache.u, 1, Bc, 1, n) |
| 96 | + else |
| 97 | + copyto!(cache.u, cache.b) |
| 98 | + aa_getrs!('N', A.factors, A.ipiv, cache.u; info) |
| 99 | + end |
| 100 | + |
| 101 | + SciMLBase.build_linear_solution(alg, cache.u, nothing, cache) |
| 102 | +end |
0 commit comments