From bd963b350b49612c725f2d0796cf85022212ace5 Mon Sep 17 00:00:00 2001 From: Jorge Date: Tue, 24 Nov 2026 15:21:31 -0600 Subject: [PATCH] oneMKL: eigen and eigvals of Symmetric and Hermitian matrices The stdlib's default driver for these is LAPACK.syevr!, which has no oneMKL method, so eigen(Symmetric(A)) of a oneMatrix reached the host method and stopped on scalar indexing. eigen! and eigvals! now call the syevd/heevd wrappers for Float32, Float64 and their complex types, and sort by `sortby` on the device. --- lib/mkl/wrappers_lapack.jl | 25 +++++++++++++++++++++++++ test/onemkl.jl | 25 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/lib/mkl/wrappers_lapack.jl b/lib/mkl/wrappers_lapack.jl index 00804294..4a381467 100644 --- a/lib/mkl/wrappers_lapack.jl +++ b/lib/mkl/wrappers_lapack.jl @@ -673,3 +673,28 @@ for elty in (:ComplexF32, :ComplexF64) LinearAlgebra.LAPACK.syevd!(jobz::Char, uplo::Char, A::oneStridedMatrix{$elty}) = oneMKL.heevd!(jobz, uplo, A) end end + +# `eigen` and `eigvals` of a symmetric or Hermitian matrix. The stdlib's default driver is +# `LAPACK.syevr!`, which oneMKL does not have, and its host method reads the matrix element +# by element. These use the divide-and-conquer driver (`syevd`, `heevd`) whatever `alg` +# (Julia 1.12 and later) names, as `LAPACK.syev!` above does. That driver returns the +# eigenvalues in ascending order, the stdlib's order. +const oneRealHermSymComplexHerm = LinearAlgebra.RealHermSymComplexHerm{<:Union{Float32, Float64}, <:oneStridedMatrix} + +function LinearAlgebra.eigen!(A::oneRealHermSymComplexHerm; alg = nothing, + sortby::Union{Function, Nothing} = nothing) + W, V = LinearAlgebra.LAPACK.syevd!('V', A.uplo, A.data) + # not `LinearAlgebra.sorteig!`, which permutes the columns of `V` element by element + if sortby !== nothing + p = sortperm(W; by = sortby) + W, V = W[p], V[:, p] + end + return LinearAlgebra.Eigen(W, V) +end + +function LinearAlgebra.eigvals!(A::oneRealHermSymComplexHerm; alg = nothing, + sortby::Union{Function, Nothing} = nothing) + W = LinearAlgebra.LAPACK.syevd!('N', A.uplo, A.data) + sortby === nothing || sort!(W, by = sortby) + return W +end diff --git a/test/onemkl.jl b/test/onemkl.jl index bf4110a6..f7edc419 100644 --- a/test/onemkl.jl +++ b/test/onemkl.jl @@ -2112,6 +2112,31 @@ end end end + @testset "eigen -- eigvals" begin + @testset "$wrapper, uplo = $uplo" for wrapper in (elty <: Real ? (Symmetric, Hermitian) : (Hermitian,)), + uplo in (:L, :U) + A = rand(elty,n,n) + B = A + A' + # only the triangle named by `uplo` may be read + C = copy(B) + uplo == :U ? (C[n,1] = 42) : (C[1,n] = 42) + d_C = oneMatrix(C) + F = eigen(wrapper(d_C, uplo)) + @test F.values isa oneVector{real(elty)} + @test F.vectors isa oneMatrix{elty} + @test collect(F.values) ≈ eigvals(wrapper(B, uplo)) + @test B ≈ collect(F.vectors * Diagonal(F.values) * F.vectors') + # the input is left as it was + @test collect(d_C) == C + @test collect(eigvals(wrapper(d_C, uplo))) ≈ eigvals(wrapper(B, uplo)) + + F = eigen(wrapper(d_C, uplo); sortby = abs) + @test collect(F.values) ≈ eigvals(wrapper(B, uplo); sortby = abs) + @test B ≈ collect(F.vectors * Diagonal(F.values) * F.vectors') + @test collect(eigvals(wrapper(d_C, uplo); sortby = abs)) ≈ eigvals(wrapper(B, uplo); sortby = abs) + end + end + @testset "sygvd! -- hegvd!" begin A = rand(elty,m,m) B = rand(elty,m,m)