-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add direct calls to BLAS to compute SVDs (#1259)
* Add direct calls to BLAS to compute SVD * Move funcs with direct BLAS interface to new file * Use @CCall * Restrict BLAS interface to Julia v1.7 or higher * Bump version to 1.9.5
- Loading branch information
Showing
5 changed files
with
168 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
# This file contains funtions that uses a direct interface to BLAS library. We use this | ||
# approach to reduce allocations. | ||
|
||
import LinearAlgebra: BLAS, LAPACK, libblastrampoline | ||
|
||
# == Singular Value Decomposition ========================================================== | ||
|
||
# Implement direct call to BLAS functions that computes the SVD values for `SMatrix` and | ||
# `MMatrix` reducing allocations. In this case, we use `MMatrix` to call the library and | ||
# convert the result back to the input type. Since the former does not exit this scope, we | ||
# can reduce allocations. | ||
# | ||
# We are implementing here the following functions: | ||
# | ||
# svdvals(A::SMatrix{M, N, Float64}) where {M, N} | ||
# svdvals(A::SMatrix{M, N, Float32}) where {M, N} | ||
# svdvals(A::MMatrix{M, N, Float64}) where {M, N} | ||
# svdvals(A::MMatrix{M, N, Float32}) where {M, N} | ||
# | ||
for (gesdd, elty) in ((:dgesdd_, :Float64), (:sgesdd_, :Float32)), | ||
(mtype, vtype) in ((SMatrix, SVector), (MMatrix, MVector)) | ||
|
||
blas_func = @eval BLAS.@blasfunc($gesdd) | ||
|
||
@eval begin | ||
function svdvals(A::$mtype{M, N, $elty}) where {M, N} | ||
K = min(M, N) | ||
|
||
# Convert the input to a `MMatrix` and allocate the required arrays. | ||
Am = MMatrix{M, N, $elty}(A) | ||
Sm = MVector{K, $elty}(undef) | ||
|
||
# We compute the `lwork` (size of the work array) by obtaining the maximum value | ||
# from the possibilities shown in: | ||
# https://docs.oracle.com/cd/E19422-01/819-3691/dgesdd.html | ||
lwork = max(8N, 3N + max(M, 7N), 8M, 3M + max(N, 7M)) | ||
work = MVector{lwork, $elty}(undef) | ||
iwork = MVector{8min(M, N), BLAS.BlasInt}(undef) | ||
info = Ref(1) | ||
|
||
@ccall libblastrampoline.$blas_func( | ||
'N'::Ref{UInt8}, | ||
M::Ref{BLAS.BlasInt}, | ||
N::Ref{BLAS.BlasInt}, | ||
Am::Ptr{$elty}, | ||
M::Ref{BLAS.BlasInt}, | ||
Sm::Ptr{$elty}, | ||
C_NULL::Ptr{C_NULL}, | ||
M::Ref{BLAS.BlasInt}, | ||
C_NULL::Ptr{C_NULL}, | ||
K::Ref{BLAS.BlasInt}, | ||
work::Ptr{$elty}, | ||
lwork::Ref{BLAS.BlasInt}, | ||
iwork::Ptr{BLAS.BlasInt}, | ||
info::Ptr{BLAS.BlasInt}, | ||
1::Clong | ||
)::Cvoid | ||
|
||
# Check if the return result of the function. | ||
LAPACK.chklapackerror(info.x) | ||
|
||
# Convert the vector to static arrays and return. | ||
S = $vtype{K, $elty}(Sm) | ||
|
||
return S | ||
end | ||
end | ||
end | ||
|
||
# For matrices with interger numbers, we should promote them to float and call `svdvals`. | ||
@inline svdvals(A::StaticMatrix{<: Any, <: Any, <: Integer}) = svdvals(float(A)) | ||
|
||
# Implement direct call to BLAS functions that computes the SVD for `SMatrix` and `MMatrix` | ||
# reducing allocations. In this case, we use `MMatrix` to call the library and convert the | ||
# result back to the input type. Since the former does not exit this scope, we can reduce | ||
# allocations. | ||
# | ||
# We are implementing here the following functions: | ||
# | ||
# _svd(A::SMatrix{M, N, Float64}, full::Val{false}) where {M, N} | ||
# _svd(A::SMatrix{M, N, Float64}, full::Val{true}) where {M, N} | ||
# _svd(A::SMatrix{M, N, Float32}, full::Val{false}) where {M, N} | ||
# _svd(A::SMatrix{M, N, Float32}, full::Val{true}) where {M, N} | ||
# _svd(A::MMatrix{M, N, Float64}, full::Val{false}) where {M, N} | ||
# _svd(A::MMatrix{M, N, Float64}, full::Val{true}) where {M, N} | ||
# _svd(A::MMatrix{M, N, Float32}, full::Val{false}) where {M, N} | ||
# _svd(A::MMatrix{M, N, Float32}, full::Val{true}) where {M, N} | ||
# | ||
for (gesvd, elty) in ((:dgesvd_, :Float64), (:sgesvd_, :Float32)), | ||
full in (false, true), | ||
(mtype, vtype) in ((SMatrix, SVector), (MMatrix, MVector)) | ||
|
||
blas_func = @eval BLAS.@blasfunc($gesvd) | ||
|
||
@eval begin | ||
function _svd(A::$mtype{M, N, $elty}, full::Val{$full}) where {M, N} | ||
K = min(M, N) | ||
|
||
# Convert the input to a `MMatrix` and allocate the required arrays. | ||
Am = MMatrix{M, N, $elty}(A) | ||
Um = MMatrix{M, $(full ? :M : :K), $elty}(undef) | ||
Sm = MVector{K, $elty}(undef) | ||
Vtm = MMatrix{$(full ? :N : :K), N, $elty}(undef) | ||
lwork = max(3min(M, N) + max(M, N), 5min(M, N)) | ||
work = MVector{lwork, $elty}(undef) | ||
info = Ref(1) | ||
|
||
@ccall libblastrampoline.$blas_func( | ||
$(full ? 'A' : 'S')::Ref{UInt8}, | ||
$(full ? 'A' : 'S')::Ref{UInt8}, | ||
M::Ref{BLAS.BlasInt}, | ||
N::Ref{BLAS.BlasInt}, | ||
Am::Ptr{$elty}, | ||
M::Ref{BLAS.BlasInt}, | ||
Sm::Ptr{$elty}, | ||
Um::Ptr{$elty}, | ||
M::Ref{BLAS.BlasInt}, | ||
Vtm::Ptr{$elty}, | ||
$(full ? :N : :K)::Ref{BLAS.BlasInt}, | ||
work::Ptr{$elty}, | ||
lwork::Ref{BLAS.BlasInt}, | ||
info::Ptr{BLAS.BlasInt}, | ||
1::Clong, | ||
1::Clong | ||
)::Cvoid | ||
|
||
# Check if the return result of the function. | ||
LAPACK.chklapackerror(info.x) | ||
|
||
# Convert the matrices to the correct type and return. | ||
U = $mtype{M, $(full ? :M : :K), $elty}(Um) | ||
S = $vtype{K, $elty}(Sm) | ||
Vt = $mtype{$(full ? :N : :K), N, $elty}(Vtm) | ||
|
||
return SVD(U, S, Vt) | ||
end | ||
end | ||
end | ||
|
||
# For matrices with interger numbers, we should promote them to float and call `svd`. | ||
@inline svd(A::StaticMatrix{<: Any, <: Any, <: Integer}) = svd(float(A)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
609aa34
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
609aa34
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/108399
Tip: Release Notes
Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.
To add them here just re-invoke and the PR will be updated.
Tagging
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: