Skip to content

Commit b02f6b6

Browse files
committed
Batched eigh
1 parent 7264b76 commit b02f6b6

8 files changed

Lines changed: 518 additions & 73 deletions

File tree

‎ext/MatrixAlgebraKitCUDAExt/MatrixAlgebraKitCUDAExt.jl‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ using MatrixAlgebraKit: diagview, sign_safe
77
using MatrixAlgebraKit: CUSOLVER, LQViaTransposedQR, TruncationByValue, AbstractAlgorithm
88
using MatrixAlgebraKit: default_qr_algorithm, default_lq_algorithm, default_svd_algorithm, default_eig_algorithm, default_eigh_algorithm
99
import MatrixAlgebraKit: geqrf!, ungqr!, unmqr!, gesvd!, gesvdp!, gesvdr!, gesvdj!
10-
import MatrixAlgebraKit: gesvdj_batched!
10+
import MatrixAlgebraKit: gesvdj_batched!, heev_batched!, heevj_batched!
1111
import MatrixAlgebraKit: heevj!, heevd!, geev!
1212
import MatrixAlgebraKit: _gpu_Xgesvdr!, _sylvester, svd_rank, svd_pullback!, eigh_pullback!, eig_pullback!, svd_pushforward!
1313
using CUDA, CUDA.cuBLAS
@@ -31,6 +31,9 @@ end
3131
function MatrixAlgebraKit.default_eigh_algorithm(::Type{T}; kwargs...) where {T <: StridedCuVecOrMat{<:BlasFloat}}
3232
return DivideAndConquer(; kwargs...)
3333
end
34+
function MatrixAlgebraKit.default_eigh_algorithm(::Type{T}; kwargs...) where {T <: StridedCuArray{<:BlasFloat, 3}}
35+
return Jacobi(; kwargs...)
36+
end
3437

3538
for f in (:geqrf!, :ungqr!, :unmqr!)
3639
@eval $f(::CUSOLVER, args...) = YACUSOLVER.$f(args...)
@@ -59,6 +62,12 @@ function gesvdj!(::CUSOLVER, A::StridedCuMatrix, S::StridedCuVector, U::StridedC
5962
return MatrixAlgebraKit.svd_via_adjoint!(gesvdj!, CUSOLVER(), A, S, U, Vᴴ; kwargs...)
6063
end
6164

65+
heevj_batched!(::CUSOLVER, As::StridedCuArray{T, 3}, Ds::StridedCuMatrix, Vs::StridedCuArray{T, 3}; kwargs...) where {T <: BlasFloat} =
66+
YACUSOLVER.heevj_batched!(As, Ds, Vs; kwargs...)
67+
68+
heev_batched!(::CUSOLVER, As::StridedCuArray{T, 3}, Ds::StridedCuMatrix, Vs::StridedCuArray{T, 3}; kwargs...) where {T <: BlasFloat} =
69+
YACUSOLVER.heev_batched!(As, Ds, Vs; kwargs...)
70+
6271
gesvdj_batched!(::CUSOLVER, As::StridedCuArray{T, 3}, Ss::StridedCuMatrix, Us::StridedCuArray{T, 3}, Vᴴs::StridedCuArray{T, 3}; kwargs...) where {T <: BlasFloat} =
6372
YACUSOLVER.gesvdj_batched!(As, Ss, Us, Vᴴs; kwargs...)
6473

‎ext/MatrixAlgebraKitCUDAExt/yacusolver.jl‎

Lines changed: 143 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,143 @@ for (celty, elty) in ((:ComplexF32, :Float32), (:ComplexF64, :Float64), (:Comple
509509
end
510510
end
511511

512+
for (jname, bname, fname, elty, relty) in
513+
((:heevj_batched!, :cusolverDnSsyevjBatched_bufferSize, :cusolverDnSsyevjBatched,
514+
:Float32, :Float32),
515+
(:heevj_batched!, :cusolverDnDsyevjBatched_bufferSize, :cusolverDnDsyevjBatched,
516+
:Float64, :Float64),
517+
(:heevj_batched!, :cusolverDnCheevjBatched_bufferSize, :cusolverDnCheevjBatched,
518+
:ComplexF32, :Float32),
519+
(:heevj_batched!, :cusolverDnZheevjBatched_bufferSize, :cusolverDnZheevjBatched,
520+
:ComplexF64, :Float64))
521+
@eval begin
522+
function $jname(A::StridedCuArray{$elty, 3},
523+
W::StridedCuMatrix{$relty} = CuMatrix{$relty}(undef, size(A, 2), size(A, 3)),
524+
V::StridedCuArray{$elty, 3} = CuArray{$elty, 3}(undef, size(A)...);
525+
check::Bool = CHECK_LIBRARY_CALLS[],
526+
uplo::Char = 'U',
527+
tol::$relty=eps($relty),
528+
max_sweeps::Int=100)
529+
# Set up information for the solver arguments
530+
chkuplo(uplo)
531+
m, n, batch_size = size(A)
532+
m == n || throw(DimensionMismatch("A must be square in its leading two dimensions"))
533+
size(V) == size(A) || size(V) == (0, 0, batch_size) || throw(DimensionMismatch("size mismatch between A and V"))
534+
lda = max(1, stride(A, 2))
535+
n == size(W, 1) || throw(DimensionMismatch("size mismatch between A and W"))
536+
batch_size == size(W, 2) || throw(DimensionMismatch("batch size mismatch between A and W"))
537+
if length(V) == 0
538+
jobz = 'N'
539+
else
540+
jobz = 'V'
541+
end
542+
params = Ref{cuSOLVER.syevjInfo_t}(C_NULL)
543+
544+
dh = cuSOLVER.dense_handle()
545+
resize!(dh.info, batch_size)
546+
547+
# Initialize the solver parameters
548+
cuSOLVER.cusolverDnCreateSyevjInfo(params)
549+
cuSOLVER.cusolverDnXsyevjSetTolerance(params[], tol)
550+
cuSOLVER.cusolverDnXsyevjSetMaxSweeps(params[], max_sweeps)
551+
552+
# Calculate the workspace size
553+
function bufferSize()
554+
out = Ref{Cint}(0)
555+
cuSOLVER.$bname(dh, jobz, uplo, n, A, lda, W, out, params[], batch_size)
556+
return out[] * sizeof($elty)
557+
end
558+
559+
# Run the solver
560+
with_workspace(dh.workspace_gpu, bufferSize) do buffer
561+
return cuSOLVER.$fname(dh, jobz, uplo, n, A, lda, W, buffer,
562+
sizeof(buffer) ÷ sizeof($elty), dh.info, params[], batch_size)
563+
end
564+
565+
if check
566+
# Copy the solver info and delete the device memory
567+
info = collect(dh.info)
568+
569+
# Double check the solver's exit status
570+
foreach(chkargsok ∘ BlasInt, info)
571+
end
572+
cuSOLVER.cusolverDnDestroySyevjInfo(params[])
573+
574+
# Return eigenvalues (in W) and possibly eigenvectors (in V)
575+
if jobz == 'N'
576+
return W
577+
elseif jobz == 'V'
578+
if V !== A
579+
copy!(V, A)
580+
end
581+
return W, V
582+
end
583+
end
584+
end
585+
end
586+
587+
function heev_batched!(A::StridedCuArray{T, 3},
588+
W::StridedCuMatrix{Tr} = CuMatrix{Tr}(undef, size(A, 2), size(A, 3)),
589+
V::StridedCuArray{T, 3} = CuArray{T, 3}(undef, size(A)...);
590+
check::Bool = CHECK_LIBRARY_CALLS[],
591+
uplo::Char = 'U',
592+
) where {T <: BlasFloat, Tr <: BlasReal}
593+
# Set up information for the solver arguments
594+
chkuplo(uplo)
595+
m, n, batch_size = size(A)
596+
m == n || throw(DimensionMismatch("A must be square in its leading two dimensions"))
597+
size(V) == size(A) || size(V) == (0, 0, batch_size) || throw(DimensionMismatch("size mismatch between A and V"))
598+
lda = max(1, stride(A, 2))
599+
n == size(W, 1) || throw(DimensionMismatch("size mismatch between A and W"))
600+
batch_size == size(W, 2) || throw(DimensionMismatch("batch size mismatch between A and W"))
601+
if length(V) == 0
602+
jobz = 'N'
603+
else
604+
jobz = 'V'
605+
end
606+
607+
dh = cuSOLVER.dense_handle()
608+
resize!(dh.info, batch_size)
609+
params = cuSOLVER.CuSolverParameters()
610+
611+
# Calculate the workspace size
612+
function bufferSize()
613+
out_cpu = Ref{Csize_t}(0)
614+
out_gpu = Ref{Csize_t}(0)
615+
cuSOLVER.cusolverDnXsyevBatched_bufferSize(dh, params, jobz, uplo, n, T, A, lda,
616+
Tr, W, T, out_gpu, out_cpu, batch_size)
617+
return out_gpu[], out_cpu[]
618+
end
619+
620+
# Run the solver
621+
cuSOLVER.with_workspaces(
622+
dh.workspace_gpu, dh.workspace_cpu,
623+
bufferSize()...
624+
) do buffer_gpu, buffer_cpu
625+
return cuSOLVER.cusolverDnXsyevBatched(dh, jobz, uplo, n, T, A, lda, Tr, W, T,
626+
buffer_gpu, sizeof(buffer_gpu), buffer_cpu,
627+
sizeof(buffer_cpu), dh.info, batch_size)
628+
end
629+
630+
if check
631+
# Copy the solver info and delete the device memory
632+
info = collect(dh.info)
633+
634+
# Double check the solver's exit status
635+
foreach(chkargsok ∘ BlasInt, info)
636+
end
637+
638+
if jobz == 'V' && V !== A
639+
copy!(V, A)
640+
end
641+
# Return eigenvalues (in W) and possibly eigenvectors (in V)
642+
if jobz == 'N'
643+
return W
644+
elseif jobz == 'V'
645+
return W, V
646+
end
647+
end
648+
512649
# for (jname, bname, fname, elty, relty) in
513650
# ((:sygvd!, :cusolverDnSsygvd_bufferSize, :cusolverDnSsygvd, :Float32, :Float32),
514651
# (:sygvd!, :cusolverDnDsygvd_bufferSize, :cusolverDnDsygvd, :Float64, :Float64),
@@ -612,72 +749,6 @@ end
612749
# end
613750
# end
614751

615-
# for (jname, bname, fname, elty, relty) in
616-
# ((:syevjBatched!, :cusolverDnSsyevjBatched_bufferSize, :cusolverDnSsyevjBatched,
617-
# :Float32, :Float32),
618-
# (:syevjBatched!, :cusolverDnDsyevjBatched_bufferSize, :cusolverDnDsyevjBatched,
619-
# :Float64, :Float64),
620-
# (:heevjBatched!, :cusolverDnCheevjBatched_bufferSize, :cusolverDnCheevjBatched,
621-
# :ComplexF32, :Float32),
622-
# (:heevjBatched!, :cusolverDnZheevjBatched_bufferSize, :cusolverDnZheevjBatched,
623-
# :ComplexF64, :Float64))
624-
# @eval begin
625-
# function $jname(jobz::Char,
626-
# uplo::Char,
627-
# A::StridedCuArray{$elty};
628-
# check::Bool = CHECK_LIBRARY_CALLS[],
629-
# tol::$relty=eps($relty),
630-
# max_sweeps::Int=100)
631-
632-
# # Set up information for the solver arguments
633-
# chkuplo(uplo)
634-
# n = checksquare(A)
635-
# lda = max(1, stride(A, 2))
636-
# batchSize = size(A, 3)
637-
# W = CuArray{$relty}(undef, n, batchSize)
638-
# params = Ref{syevjInfo_t}(C_NULL)
639-
640-
# dh = dense_handle()
641-
# resize!(dh.info, batchSize)
642-
643-
# # Initialize the solver parameters
644-
# cusolverDnCreateSyevjInfo(params)
645-
# cusolverDnXsyevjSetTolerance(params[], tol)
646-
# cusolverDnXsyevjSetMaxSweeps(params[], max_sweeps)
647-
648-
# # Calculate the workspace size
649-
# function bufferSize()
650-
# out = Ref{Cint}(0)
651-
# $bname(dh, jobz, uplo, n, A, lda, W, out, params[], batchSize)
652-
# return out[] * sizeof($elty)
653-
# end
654-
655-
# # Run the solver
656-
# with_workspace(dh.workspace_gpu, bufferSize) do buffer
657-
# return $fname(dh, jobz, uplo, n, A, lda, W, buffer,
658-
# sizeof(buffer) ÷ sizeof($elty), dh.info, params[], batchSize)
659-
# end
660-
661-
# if check
662-
# # Copy the solver info and delete the device memory
663-
# info = collect(dh.info)
664-
665-
# # Double check the solver's exit status
666-
# for i in 1:batchSize
667-
# chkargsok(BlasInt(info[i]))
668-
# end
669-
# end
670-
# cusolverDnDestroySyevjInfo(params[])
671-
672-
# # Return eigenvalues (in W) and possibly eigenvectors (in A)
673-
# if jobz == 'N'
674-
# return W
675-
# elseif jobz == 'V'
676-
# return W, A
677-
# end
678-
# end
679-
# end
680-
# end
681752

682753
# for (fname, elty) in ((:cusolverDnSpotrsBatched, :Float32),
683754
# (:cusolverDnDpotrsBatched, :Float64),
@@ -704,15 +775,15 @@ end
704775
# end
705776
# lda = max(1, stride(A[1], 2))
706777
# ldb = max(1, stride(B[1], 2))
707-
# batchSize = length(A)
778+
# batch_size = length(A)
708779

709780
# Aptrs = unsafe_batch(A)
710781
# Bptrs = unsafe_batch(B)
711782

712783
# dh = dense_handle()
713784

714785
# # Run the solver
715-
# $fname(dh, uplo, n, nrhs, Aptrs, lda, Bptrs, ldb, dh.info, batchSize)
786+
# $fname(dh, uplo, n, nrhs, Aptrs, lda, Bptrs, ldb, dh.info, batch_size)
716787

717788
# if check
718789
# # Copy the solver info and delete the device memory
@@ -735,22 +806,22 @@ end
735806
# chkuplo(uplo)
736807
# n = checksquare(A[1])
737808
# lda = max(1, stride(A[1], 2))
738-
# batchSize = length(A)
809+
# batch_size = length(A)
739810

740811
# Aptrs = unsafe_batch(A)
741812

742813
# dh = dense_handle()
743-
# resize!(dh.info, batchSize)
814+
# resize!(dh.info, batch_size)
744815

745816
# # Run the solver
746-
# $fname(dh, uplo, n, Aptrs, lda, dh.info, batchSize)
817+
# $fname(dh, uplo, n, Aptrs, lda, dh.info, batch_size)
747818

748819
# if check
749820
# # Copy the solver info and delete the device memory
750821
# info = collect(dh.info)
751822

752823
# # Double check the solver's exit status
753-
# for i in 1:batchSize
824+
# for i in 1:batch_size
754825
# chkargsok(BlasInt(info[i]))
755826
# end
756827
# end

‎src/MatrixAlgebraKit.jl‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export svd_compact, svd_full, svd_vals, svd_trunc, svd_trunc_no_error
2020
export svd_compact!, svd_full!, svd_vals!, svd_trunc!, svd_trunc_no_error!
2121
export batched_svd_compact, batched_svd_full, batched_svd_vals
2222
export batched_svd_compact!, batched_svd_full!, batched_svd_vals!
23+
export batched_eigh_full, batched_eigh_vals
24+
export batched_eigh_full!, batched_eigh_vals!
2325
export eigh_full, eigh_vals, eigh_trunc, eigh_trunc_no_error
2426
export eigh_full!, eigh_vals!, eigh_trunc!, eigh_trunc_no_error!
2527
export eig_full, eig_vals, eig_trunc, eig_trunc_no_error
@@ -114,6 +116,7 @@ include("interface/svd.jl")
114116
include("interface/batched_svd.jl")
115117
include("interface/eig.jl")
116118
include("interface/eigh.jl")
119+
include("interface/batched_eigh.jl")
117120
include("interface/gen_eig.jl")
118121
include("interface/schur.jl")
119122
include("interface/polar.jl")
@@ -128,6 +131,7 @@ include("implementations/svd.jl")
128131
include("implementations/batched_svd.jl")
129132
include("implementations/eig.jl")
130133
include("implementations/eigh.jl")
134+
include("implementations/batched_eigh.jl")
131135
include("implementations/gen_eig.jl")
132136
include("implementations/schur.jl")
133137
include("implementations/polar.jl")

‎src/common/gauge.jl‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ function gaugefix!(::Union{typeof(eig_full!), typeof(eigh_full!), typeof(gen_eig
4444
return V
4545
end
4646

47+
function gaugefix!(::typeof(batched_eigh_full!), V)
48+
for v in eachslice(V, dims = 3)
49+
gaugefix!(eigh_full!, v)
50+
end
51+
return V
52+
end
53+
4754
function gaugefix!(::typeof(svd_full!), U, Vᴴ)
4855
m, n = size(U, 2), size(Vᴴ, 1)
4956
for j in 1:max(m, n)

0 commit comments

Comments
 (0)