Skip to content

Commit 8db80ec

Browse files
committed
Wrap QR library calls for CUSOLVER
1 parent 5120b5d commit 8db80ec

1 file changed

Lines changed: 155 additions & 1 deletion

File tree

‎ext/MatrixAlgebraKitCUDAExt/yacusolver.jl‎

Lines changed: 155 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,164 @@ using CUDA.cuSOLVER
1010
using ..MatrixAlgebraKit: CHECK_LIBRARY_CALLS
1111

1212
# QR methods are implemented with full access to allocated arrays, so we do not need to redo this:
13-
using CUDA.cuSOLVER: geqrf!, ormqr!, orgqr!
13+
using CUDA.cuSOLVER: ormqr_blocked!, orgqr_blocked!
1414
const unmqr! = ormqr!
1515
const ungqr! = orgqr!
1616

17+
# wrap geqrf, ormqr, orgqr ourselves to avoid having to do the info check
18+
for (bname, fname, elty) in ((:cusolverDnSorgqr_bufferSize, :cusolverDnSorgqr, :Float32),
19+
(:cusolverDnDorgqr_bufferSize, :cusolverDnDorgqr, :Float64),
20+
(:cusolverDnCungqr_bufferSize, :cusolverDnCungqr, :ComplexF32),
21+
(:cusolverDnZungqr_bufferSize, :cusolverDnZungqr, :ComplexF64))
22+
@eval begin
23+
function orgqr!(A::StridedCuMatrix{$elty}, tau::CuVector{$elty};
24+
blocksize::Union{Nothing,Integer}=nothing),
25+
check::Bool = CHECK_LIBRARY_CALLS[]
26+
m = size(A , 1)
27+
n = min(m, size(A, 2))
28+
lda = max(1, stride(A, 2))
29+
k = length(tau)
30+
dh = cuSOLVER.dense_handle()
31+
32+
# this workspace query does not fail on overflow, but wraps around: to a
33+
# negative size at first, and past 2^32 elements to a small positive one that
34+
# makes cuSOLVER write out of bounds. The workspace is no larger than Q, up to
35+
# lower-order terms, so bounding `lda * n` rules out the latter.
36+
lwork = Ref{Cint}(0)
37+
if blocksize === nothing
38+
if lda * n > typemax(Cint) || prefer_blocked(m, k)
39+
blocksize = cuSOLVER.HOUSEHOLDER_BLOCKSIZE
40+
else
41+
status = $(Symbol(:(cuSOLVER.), :unchecked_, bname))(dh, m, n, k, A, lda, tau, lwork)
42+
if status == cuSOLVER.CUSOLVER_STATUS_INVALID_VALUE || lwork[] < 0
43+
blocksize = cuSOLVER.HOUSEHOLDER_BLOCKSIZE
44+
elseif status != cuSOLVER.CUSOLVER_STATUS_SUCCESS
45+
CUDA.throw_api_error(status)
46+
end
47+
end
48+
end
49+
50+
if blocksize !== nothing
51+
orgqr_blocked!(A, tau, n, blocksize)
52+
else
53+
CUDA.with_workspace(dh.workspace_gpu, lwork[] * sizeof($elty)) do buffer
54+
$fname(dh, m, n, k, A, lda, tau,
55+
buffer, sizeof(buffer) ÷ sizeof($elty), dh.info)
56+
end
57+
58+
if check
59+
info = @allowscalar dh.info[1]
60+
cuSOLVER.chkargsok(BlasInt(info))
61+
end
62+
end
63+
64+
if n < size(A, 2)
65+
A[:, 1:n]
66+
else
67+
A
68+
end
69+
end
70+
end
71+
end
72+
73+
for (bname, fname, elty) in ((:cusolverDnSormqr_bufferSize, :cusolverDnSormqr, :Float32),
74+
(:cusolverDnDormqr_bufferSize, :cusolverDnDormqr, :Float64),
75+
(:cusolverDnCunmqr_bufferSize, :cusolverDnCunmqr, :ComplexF32),
76+
(:cusolverDnZunmqr_bufferSize, :cusolverDnZunmqr, :ComplexF64))
77+
@eval begin
78+
function ormqr!(side::Char,
79+
trans::Char,
80+
A::StridedCuMatrix{$elty},
81+
tau::CuVector{$elty},
82+
C::StridedCuVecOrMat{$elty};
83+
blocksize::Union{Nothing,Integer}=nothing,
84+
check::Bool = CHECK_LIBRARY_CALLS[])
85+
# Support transa = 'C' for real matrices
86+
trans = $elty <: Real && trans == 'C' ? 'T' : trans
87+
($elty <: Complex) && (trans == 'T') && throw(ArgumentError("trans = 'T' is not supported with complex matrices."))
88+
89+
chkside(side)
90+
chktrans(trans)
91+
m, n = ndims(C) == 2 ? size(C) : (length(C), 1)
92+
mA = size(A, 1)
93+
k = length(tau)
94+
if side == 'L' && m != mA
95+
throw(DimensionMismatch("for a left-sided multiplication, the first dimension of C, $m, must equal the second dimension of A, $mA"))
96+
end
97+
if side == 'R' && n != mA
98+
throw(DimensionMismatch("for a right-sided multiplication, the second dimension of C, $m, must equal the second dimension of A, $mA"))
99+
end
100+
if side == 'L' && k > m
101+
throw(DimensionMismatch("invalid number of reflectors: k = $k should be <= m = $m"))
102+
end
103+
if side == 'R' && k > n
104+
throw(DimensionMismatch("invalid number of reflectors: k = $k should be <= n = $n"))
105+
end
106+
107+
lda = max(1, stride(A, 2))
108+
ldc = max(1, stride(C, 2))
109+
dh = cuSOLVER.dense_handle()
110+
111+
lwork = Ref{Cint}(0)
112+
if blocksize === nothing
113+
if max(m, n, lda, ldc) > typemax(Cint) || prefer_blocked(mA, k)
114+
blocksize = cuSOLVER.HOUSEHOLDER_BLOCKSIZE
115+
else
116+
status = $(Symbol(:(cuSOLVER.), :unchecked_, bname))(dh, side, trans, m, n, k, A, lda,
117+
tau, C, ldc, lwork)
118+
if status == cuSOLVER.CUSOLVER_STATUS_INVALID_VALUE || lwork[] < 0
119+
blocksize = cuSOLVER.HOUSEHOLDER_BLOCKSIZE
120+
elseif status != cuSOLVER.CUSOLVER_STATUS_SUCCESS
121+
CUDA.throw_api_error(status)
122+
end
123+
end
124+
end
125+
if blocksize !== nothing
126+
return ormqr_blocked!(side, trans, A, tau, C, blocksize)
127+
end
128+
129+
CUDA.with_workspace(dh.workspace_gpu, lwork[] * sizeof($elty)) do buffer
130+
$fname(dh, side, trans, m, n, k, A, lda, tau, C, ldc,
131+
buffer, sizeof(buffer) ÷ sizeof($elty), dh.info)
132+
end
133+
134+
if check
135+
info = @allowscalar dh.info[1]
136+
cuSOLVER.chkargsok(BlasInt(info))
137+
end
138+
139+
C
140+
end
141+
end
142+
end
143+
144+
for (bname, fname,elty) in ((:cusolverDnSgeqrf_bufferSize, :cusolverDnSgeqrf, :Float32),
145+
(:cusolverDnDgeqrf_bufferSize, :cusolverDnDgeqrf, :Float64),
146+
(:cusolverDnCgeqrf_bufferSize, :cusolverDnCgeqrf, :ComplexF32),
147+
(:cusolverDnZgeqrf_bufferSize, :cusolverDnZgeqrf, :ComplexF64))
148+
@eval begin
149+
function geqrf!(A::StridedCuMatrix{$elty}, tau::CuVector{$elty} = similar(A, $elty, min(size(A)...)); check::Bool = CHECK_LIBRARY_CALLS[])
150+
m, n = size(A)
151+
lda = max(1, stride(A, 2))
152+
dh = cuSOLVER.dense_handle()
153+
function bufferSize()
154+
out = Ref{Cint}(0)
155+
$bname(dh, m, n, A, lda, out)
156+
return out[] * sizeof($elty)
157+
end
158+
CUDA.with_workspace(dh.workspace_gpu, bufferSize) do buffer
159+
$fname(dh, m, n, A, lda, tau,
160+
buffer, sizeof(buffer) ÷ sizeof($elty), dh.info)
161+
end
162+
if check
163+
info = @allowscalar dh.info[1]
164+
cuSOLVER.chkargsok(BlasInt(info))
165+
end
166+
return A, tau
167+
end
168+
end
169+
end
170+
17171
# Wrapper for SVD via QR Iteration
18172
for (bname, fname, elty, relty) in
19173
(

0 commit comments

Comments
 (0)