Skip to content

Commit 3d1aa93

Browse files
kshyattKatharine Hyatt
authored andcommitted
Wrap QR library calls for CUSOLVER
1 parent 5120b5d commit 3d1aa93

1 file changed

Lines changed: 183 additions & 1 deletion

File tree

‎ext/MatrixAlgebraKitCUDAExt/yacusolver.jl‎

Lines changed: 183 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,189 @@ 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: unchecked_cusolverDnSorgqr_bufferSize, unchecked_cusolverDnDorgqr_bufferSize
14+
using CUDA.cuSOLVER: unchecked_cusolverDnZungqr_bufferSize, unchecked_cusolverDnCungqr_bufferSize
15+
using CUDA.cuSOLVER: unchecked_cusolverDnSormqr_bufferSize, unchecked_cusolverDnDormqr_bufferSize
16+
using CUDA.cuSOLVER: unchecked_cusolverDnZunmqr_bufferSize, unchecked_cusolverDnCunmqr_bufferSize
17+
18+
# whether to prefer the blocked implementation even when cuSOLVER accepts the problem. For
19+
# tall matrices the legacy routines' workspace grows with the number of rows rather than
20+
# with the number of reflectors (to about 5x the size of A for k = 64), and they are slower:
21+
# on an RTX 5080 the blocked ormqr is 1.5-4x faster from 2^15 rows and m >= 8k, while orgqr
22+
# is up to 3.7x faster for small k and on par otherwise. Smaller problems are dominated by
23+
# the blocked implementation's launch overhead.
24+
prefer_blocked(m, k) = m >= max(2^15, 8k)
25+
26+
# wrap geqrf, ormqr, orgqr ourselves to avoid having to do the info check
27+
for (bname, fname, elty) in (
28+
(:cusolverDnSorgqr_bufferSize, :cusolverDnSorgqr, :Float32),
29+
(:cusolverDnDorgqr_bufferSize, :cusolverDnDorgqr, :Float64),
30+
(:cusolverDnCungqr_bufferSize, :cusolverDnCungqr, :ComplexF32),
31+
(:cusolverDnZungqr_bufferSize, :cusolverDnZungqr, :ComplexF64),
32+
)
33+
@eval begin
34+
function orgqr!(
35+
A::StridedCuMatrix{$elty}, tau::CuVector{$elty};
36+
blocksize::Union{Nothing, Integer} = nothing,
37+
check::Bool = CHECK_LIBRARY_CALLS[]
38+
)
39+
m = size(A, 1)
40+
n = min(m, size(A, 2))
41+
lda = max(1, stride(A, 2))
42+
k = length(tau)
43+
dh = cuSOLVER.dense_handle()
44+
45+
# this workspace query does not fail on overflow, but wraps around: to a
46+
# negative size at first, and past 2^32 elements to a small positive one that
47+
# makes cuSOLVER write out of bounds. The workspace is no larger than Q, up to
48+
# lower-order terms, so bounding `lda * n` rules out the latter.
49+
lwork = Ref{Cint}(0)
50+
if blocksize === nothing
51+
if lda * n > typemax(Cint) || prefer_blocked(m, k)
52+
blocksize = cuSOLVER.HOUSEHOLDER_BLOCKSIZE
53+
else
54+
status = $(Symbol(:unchecked_, bname))(dh, m, n, k, A, lda, tau, lwork)
55+
if status == cuSOLVER.CUSOLVER_STATUS_INVALID_VALUE || lwork[] < 0
56+
blocksize = cuSOLVER.HOUSEHOLDER_BLOCKSIZE
57+
elseif status != cuSOLVER.CUSOLVER_STATUS_SUCCESS
58+
CUDA.throw_api_error(status)
59+
end
60+
end
61+
end
62+
63+
if blocksize !== nothing
64+
cuSOLVER.orgqr_blocked!(A, tau, n, blocksize)
65+
else
66+
CUDA.with_workspace(dh.workspace_gpu, lwork[] * sizeof($elty)) do buffer
67+
cuSOLVER.$fname(
68+
dh, m, n, k, A, lda, tau,
69+
buffer, sizeof(buffer) ÷ sizeof($elty), dh.info
70+
)
71+
end
72+
73+
if check
74+
info = @allowscalar dh.info[1]
75+
cuSOLVER.chkargsok(BlasInt(info))
76+
end
77+
end
78+
79+
return if n < size(A, 2)
80+
A[:, 1:n]
81+
else
82+
A
83+
end
84+
end
85+
end
86+
end
87+
88+
for (bname, fname, elty) in (
89+
(:cusolverDnSormqr_bufferSize, :cusolverDnSormqr, :Float32),
90+
(:cusolverDnDormqr_bufferSize, :cusolverDnDormqr, :Float64),
91+
(:cusolverDnCunmqr_bufferSize, :cusolverDnCunmqr, :ComplexF32),
92+
(:cusolverDnZunmqr_bufferSize, :cusolverDnZunmqr, :ComplexF64),
93+
)
94+
@eval begin
95+
function ormqr!(
96+
side::Char,
97+
trans::Char,
98+
A::StridedCuMatrix{$elty},
99+
tau::CuVector{$elty},
100+
C::StridedCuVecOrMat{$elty};
101+
blocksize::Union{Nothing, Integer} = nothing,
102+
check::Bool = CHECK_LIBRARY_CALLS[]
103+
)
104+
# Support transa = 'C' for real matrices
105+
trans = $elty <: Real && trans == 'C' ? 'T' : trans
106+
($elty <: Complex) && (trans == 'T') && throw(ArgumentError("trans = 'T' is not supported with complex matrices."))
107+
108+
chkside(side)
109+
chktrans(trans)
110+
m, n = ndims(C) == 2 ? size(C) : (length(C), 1)
111+
mA = size(A, 1)
112+
k = length(tau)
113+
if side == 'L' && m != mA
114+
throw(DimensionMismatch("for a left-sided multiplication, the first dimension of C, $m, must equal the second dimension of A, $mA"))
115+
end
116+
if side == 'R' && n != mA
117+
throw(DimensionMismatch("for a right-sided multiplication, the second dimension of C, $m, must equal the second dimension of A, $mA"))
118+
end
119+
if side == 'L' && k > m
120+
throw(DimensionMismatch("invalid number of reflectors: k = $k should be <= m = $m"))
121+
end
122+
if side == 'R' && k > n
123+
throw(DimensionMismatch("invalid number of reflectors: k = $k should be <= n = $n"))
124+
end
125+
126+
lda = max(1, stride(A, 2))
127+
ldc = max(1, stride(C, 2))
128+
dh = cuSOLVER.dense_handle()
129+
130+
lwork = Ref{Cint}(0)
131+
if blocksize === nothing
132+
if max(m, n, lda, ldc) > typemax(Cint) || prefer_blocked(mA, k)
133+
blocksize = cuSOLVER.HOUSEHOLDER_BLOCKSIZE
134+
else
135+
status = $(Symbol(:unchecked_, bname))(
136+
dh, side, trans, m, n, k, A, lda,
137+
tau, C, ldc, lwork
138+
)
139+
if status == cuSOLVER.CUSOLVER_STATUS_INVALID_VALUE || lwork[] < 0
140+
blocksize = cuSOLVER.HOUSEHOLDER_BLOCKSIZE
141+
elseif status != cuSOLVER.CUSOLVER_STATUS_SUCCESS
142+
CUDA.throw_api_error(status)
143+
end
144+
end
145+
CUDA.with_workspace(dh.workspace_gpu, lwork[] * sizeof($elty)) do buffer
146+
cuSOLVER.$fname(
147+
dh, side, trans, m, n, k, A, lda, tau, C, ldc,
148+
buffer, sizeof(buffer) ÷ sizeof($elty), dh.info
149+
)
150+
end
151+
152+
if check
153+
info = @allowscalar dh.info[1]
154+
cuSOLVER.chkargsok(BlasInt(info))
155+
end
156+
end
157+
if blocksize !== nothing
158+
cuSOLVER.ormqr_blocked!(side, trans, A, tau, C, blocksize)
159+
end
160+
return C
161+
end
162+
end
163+
end
164+
165+
for (bname, fname, elty) in (
166+
(:cusolverDnSgeqrf_bufferSize, :cusolverDnSgeqrf, :Float32),
167+
(:cusolverDnDgeqrf_bufferSize, :cusolverDnDgeqrf, :Float64),
168+
(:cusolverDnCgeqrf_bufferSize, :cusolverDnCgeqrf, :ComplexF32),
169+
(:cusolverDnZgeqrf_bufferSize, :cusolverDnZgeqrf, :ComplexF64),
170+
)
171+
@eval begin
172+
function geqrf!(A::StridedCuMatrix{$elty}, tau::CuVector{$elty} = similar(A, $elty, min(size(A)...)); check::Bool = CHECK_LIBRARY_CALLS[])
173+
m, n = size(A)
174+
lda = max(1, stride(A, 2))
175+
dh = cuSOLVER.dense_handle()
176+
function bufferSize()
177+
out = Ref{Cint}(0)
178+
cuSOLVER.$bname(dh, m, n, A, lda, out)
179+
return out[] * sizeof($elty)
180+
end
181+
CUDA.with_workspace(dh.workspace_gpu, bufferSize) do buffer
182+
cuSOLVER.$fname(
183+
dh, m, n, A, lda, tau,
184+
buffer, sizeof(buffer) ÷ sizeof($elty), dh.info
185+
)
186+
end
187+
if check
188+
info = @allowscalar dh.info[1]
189+
cuSOLVER.chkargsok(BlasInt(info))
190+
end
191+
return A, tau
192+
end
193+
end
194+
end
195+
14196
const unmqr! = ormqr!
15197
const ungqr! = orgqr!
16198

0 commit comments

Comments
 (0)