Skip to content

Commit

Permalink
Fix eigen for Hermitian and Symmetric matrices (JuliaGPU#1677)
Browse files Browse the repository at this point in the history
  • Loading branch information
GVigne authored Nov 22, 2022
1 parent 2d25ef9 commit 774ac80
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/cusolver/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,17 @@ end
# eigenvalues

function LinearAlgebra.eigen(A::Symmetric{T,<:CuMatrix}) where {T<:BlasReal}
A2 = copy(A)
A2 = copy(A.data)
Eigen(syevd!('V', 'U', A2)...)
end
function LinearAlgebra.eigen(A::Hermitian{T,<:CuMatrix}) where {T<:BlasFloat}
A2 = copy(A)
function LinearAlgebra.eigen(A::Hermitian{T,<:CuMatrix}) where {T<:BlasComplex}
A2 = copy(A.data)
Eigen(heevd!('V', 'U', A2)...)
end
function LinearAlgebra.eigen(A::Hermitian{T,<:CuMatrix}) where {T<:BlasReal}
eigen(Symmetric(A))
end

function LinearAlgebra.eigen(A::CuMatrix{T}) where {T<:BlasReal}
A2 = copy(A)
issymmetric(A) ? Eigen(syevd!('V', 'U', A2)...) : error("GPU eigensolver supports only Hermitian or Symmetric matrices.")
Expand Down
17 changes: 17 additions & 0 deletions test/cusolver/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,23 @@ k = 1
end
h_W = collect(d_W)
@test Eig.values h_W

A = rand(elty,m,m)
A += A'
d_A = CuArray(A)
Eig = eigen(LinearAlgebra.Hermitian(A))
d_eig = eigen(LinearAlgebra.Hermitian(d_A))
@test Eig.values collect(d_eig.values)
h_V = collect(d_eig.vectors)
@test abs.(Eig.vectors'*h_V) I
if elty <: Real
Eig = eigen(LinearAlgebra.Symmetric(A))
d_eig = eigen(LinearAlgebra.Symmetric(d_A))
@test Eig.values collect(d_eig.values)
h_V = collect(d_eig.vectors)
@test abs.(Eig.vectors'*h_V) I
end

end

@testset "sygvd!" begin
Expand Down

0 comments on commit 774ac80

Please sign in to comment.