Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 77 additions & 1 deletion src/eigen_workspace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ are overwritten by the next call. A workspace must not be used concurrently.
struct EigenWorkspace{T<:AbstractFloat}
scratch::Matrix{T}
lapack::Union{Nothing,FastLapackInterface.HermitianEigenWs{T,Matrix{T},T}}
# Work arrays of `syevd`, resized by its first call
syevd_work::Vector{T}
syevd_iwork::Vector{LinearAlgebra.BlasInt}
end

function EigenWorkspace{T}(scratch::Matrix{T}, lapack) where {T}
return EigenWorkspace{T}(scratch, lapack, T[], LinearAlgebra.BlasInt[])
end

function EigenWorkspace(scratch::Matrix{T}) where {T}
Expand All @@ -24,7 +31,8 @@ end
eigen!(workspace::EigenWorkspace, A; vectors=false, abstol=-1.0)

Compute symmetric eigenvalues, optionally with eigenvectors, overwriting `A`.
Returned arrays are borrowed until the next use of `workspace`.
Returned arrays are borrowed until the next use of `workspace`. With
`vectors = true`, the eigenvectors are stored in `A` and `abstol` is ignored.
"""
function eigen!(
ws::EigenWorkspace{T},
Expand All @@ -34,6 +42,12 @@ function eigen!(
) where {T<:Union{Float32,Float64}}
lapack = ws.lapack::FastLapackInterface.HermitianEigenWs{T,Matrix{T},T}
LinearAlgebra.LAPACK.chkuplofinite(A, 'U')
if vectors
# The divide-and-conquer algorithm of `syevd` is about twice as fast
# as `syevr` when the eigenvalues are clustered, as for the scaling
# of interior-point methods near the central path.
return _syevd!(ws, lapack.w, A)
end
return LinearAlgebra.LAPACK.syevr!(
lapack,
vectors ? 'V' : 'N',
Expand All @@ -48,6 +62,68 @@ function eigen!(
resize = false,
)
end
# `LinearAlgebra.LAPACK.syevd!` allocates its outputs and work arrays.
# TODO: Replace by `FastLapackInterface.syevd!` once
# https://github.com/DynareJulia/FastLapackInterface.jl/pull/55 is released,
# as done in the branch `bl/fli_syevd`.
for (syevd, T) in ((:dsyevd_, :Float64), (:ssyevd_, :Float32))
@eval begin
function _syevd!(ws::EigenWorkspace{$T}, w, A)
LinearAlgebra.BLAS.chkstride1(A)
n = LinearAlgebra.checksquare(A)
# Minimal sizes documented by LAPACK for the eigenvectors. They
# are the sizes returned by its workspace query except for tiny
# matrices.
lwork = n <= 1 ? 1 : 1 + 6n + 2n^2
liwork = n <= 1 ? 1 : 3 + 5n
if length(ws.syevd_work) < lwork
resize!(ws.syevd_work, lwork)
end
if length(ws.syevd_iwork) < liwork
resize!(ws.syevd_iwork, liwork)
end
info = Ref{LinearAlgebra.BlasInt}()
ccall(
(
LinearAlgebra.BLAS.@blasfunc($syevd),
LinearAlgebra.BLAS.libblastrampoline,
),
Cvoid,
(
Ref{UInt8},
Ref{UInt8},
Ref{LinearAlgebra.BlasInt},
Ptr{$T},
Ref{LinearAlgebra.BlasInt},
Ptr{$T},
Ptr{$T},
Ref{LinearAlgebra.BlasInt},
Ptr{LinearAlgebra.BlasInt},
Ref{LinearAlgebra.BlasInt},
Ptr{LinearAlgebra.BlasInt},
Clong,
Clong,
),
'V',
'U',
n,
A,
max(1, stride(A, 2)),
w,
ws.syevd_work,
length(ws.syevd_work),
ws.syevd_iwork,
length(ws.syevd_iwork),
info,
1,
1,
)
LinearAlgebra.LAPACK.chklapackerror(info[])
return w, A
end
end
end

function eigen!(::EigenWorkspace, A; vectors = false, abstol = -1.0)
if vectors
F = LinearAlgebra.eigen!(LinearAlgebra.Symmetric(A))
Expand Down
27 changes: 27 additions & 0 deletions test/eigen_workspace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,38 @@ function check_workspace(::Type{T}) where {T}
return
end

function eigenvectors_allocations(workspace, A)
return @allocated LRO.eigen!(workspace, A; vectors = true)
end

# Above 25, `syevd` uses divide and conquer, which checks the size of the work
# arrays.
function check_syevd(::Type{T}) where {T}
n = 40
B = randn(T, n, n)
input = B + B'
workspace = LRO.EigenWorkspace(zeros(T, n, n))
for _ in 1:2
A = copy(input)
values, vectors = LRO.eigen!(workspace, A; vectors = true)
@test vectors === A
@test values ≈ eigvals(Symmetric(input))
@test input * vectors ≈ vectors * Diagonal(values) rtol = 100eps(T)
@test length(workspace.syevd_work) == 1 + 6n + 2n^2
@test length(workspace.syevd_iwork) == 3 + 5n
end
@test eigenvectors_allocations(workspace, copy(input)) == 0
return
end

@testset "Symmetric eigenvalue workspace" begin
setprecision(BigFloat, 256) do
for T in (Float32, Float64, BigFloat)
@testset "$T" begin
check_workspace(T)
if T != BigFloat
check_syevd(T)
end
end
end
end
Expand Down
Loading