You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 2b0fa61
Browse filesBrowse the repository at this point in the historyBrowse files
Extract R before constructing Q in Householder QR/LQ (#267)
* Extract R before constructing Q in Householder QR/LQ
cuSOLVER's `ormqr` needs a workspace of ~2*m*n elements whose size can only be
queried through a 32-bit integer, so it becomes unusable past m ~ 35000. `orgqr`
needs no workspace and is faster, but generates Q in the array holding the
reflectors, which the old order (construct Q, then copy R out of the packed A)
could not combine with returning R.
Extracting R/L first lifts the restrictions on an inplace Q (`Q === A`): it now
combines with computing R/L and with `positive = true`. The blocked path keeps
constructing Q first, as R doubles as workspace for T there. The CUSOLVER driver
now always constructs Q with `ungqr!`.
Also fixes LQ gauge fixing running with `positive = false` when L is not
computed, and makes the Native and GLA drivers reject `Q === A` instead of
silently returning garbage.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* Drop the cuSOLVER workspace queries
They are no longer used now that the CUSOLVER driver always constructs Q with
`ungqr!`. Inline the ungqr!-based Q construction into `_qr_buildQ!`.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* code review
---------
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
throw(ArgumentError("inplace Q only supported if matrix is wide (`m <= n`), L is not required, and using the unblocked algorithm (`blocksize = 1`) with `positive = false`"))
154
+
if inplaceQ
155
+
# unglq! builds Q in the space of A, so L has to be extracted first and cannot alias A
156
+
(blocksize ==1&& n >= m) ||
157
+
throw(ArgumentError(lazy"in-place Q is only supported if matrix is wide (`$m ≤ $n`) and using the unblocked algorithm (`blocksize = $blocksize`)"))
158
+
(computeL && Base.mightalias(L, A)) &&
159
+
throw(ArgumentError("in-place Q is only supported if L does not share memory with A"))
160
+
end
161
+
149
162
if blocksize >1
163
+
# L doubles as workspace for T, so Q is constructed before L is extracted
150
164
mb =min(minmn, blocksize)
151
165
if computeL # first use L as space for T
152
166
A, T =gelqt!(driver, A, view(L, 1:mb, 1:minmn))
153
167
else
154
168
A, T =gelqt!(driver, A, similar(A, mb, minmn))
155
169
end
156
170
Q =gemlqt!(driver, 'R', 'N', A, T, one!(Q))
171
+
computeL &&_lq_copyL!(L, A)
172
+
positive &&gaugefix!(lq_householder!, computeL ? L :nothing, Q, diagview(A))
157
173
else
158
174
A, τ =gelqf!(driver, A)
175
+
computeL &&_lq_copyL!(L, A)
176
+
Lf = computeL ? L :nothing
159
177
if inplaceQ
160
-
Q =unglq!(driver, A, τ)
178
+
Ld = positive ?copy(diagview(A)) :nothing# unglq! destroys the diagonal of A
179
+
unglq!(driver, A, τ) # Q === A, so no need to rebind Q
180
+
positive &&gaugefix!(lq_householder!, Lf, Q, Ld)
161
181
else
162
182
Q =unmlq!(driver, 'R', 'N', A, τ, one!(Q))
183
+
positive &&gaugefix!(lq_householder!, Lf, Q, diagview(A))
163
184
end
164
185
end
165
186
166
-
if computeL
167
-
# we need to first copy then gaugefix - avoiding aliasing between L and Ld for broadcast
168
-
Ld =diagview(A)
169
-
copyto!(L, lowertriangular!(view(A, axes(L)...)))
170
-
positive &&gaugefix!(lq_householder!, L, Q, Ld)
171
-
else
172
-
gaugefix!(lq_householder!, nothing, Q, diagview(A))
173
-
end
174
-
175
187
return L, Q
176
188
end
177
189
functionlq_householder!(
@@ -183,6 +195,8 @@ function lq_householder!(
183
195
throw(ArgumentError(lazy"$driver does not provide a blocked LQ decomposition"))
184
196
pivoted &&
185
197
throw(ArgumentError(lazy"$driver does not provide a pivoted LQ decomposition"))
198
+
Q === A &&
199
+
throw(ArgumentError(lazy"$driver does not provide an inplace Q"))
throw(ArgumentError("inplace Q only supported if matrix is tall (`m >= n`), R is not required, and using the unblocked algorithm (`blocksize = 1`) with `positive = false`"))
176
+
if inplaceQ
177
+
# ungqr! builds Q in the space of A, so R has to be extracted first and cannot alias A
178
+
(blocksize ==1&& m >= n) ||
179
+
throw(ArgumentError(lazy"in-place Q is only supported if matrix is tall (`$m >= $n`) and using the unblocked algorithm (`blocksize = $blocksize`)"))
180
+
(computeR && Base.mightalias(R, A)) &&
181
+
throw(ArgumentError("in-place Q is only supported if R does not share memory with A"))
182
+
end
154
183
155
184
# Compute QR in packed form
156
185
if blocksize >1
186
+
# R doubles as workspace for T, so Q is constructed before R is extracted
157
187
nb =min(minmn, blocksize)
158
188
if computeR # first use R as space for T
159
189
A, T =geqrt!(driver, A, view(R, 1:nb, 1:minmn))
160
190
else
161
191
A, T =geqrt!(driver, A, similar(A, nb, minmn))
162
192
end
163
193
Q =gemqrt!(driver, 'L', 'N', A, T, one!(Q))
194
+
computeR &&_qr_copyR!(R, A)
195
+
positive &&gaugefix!(qr_householder!, Q, computeR ? R :nothing, diagview(A))
164
196
else
165
197
if pivoted
166
198
A, τ, jpvt =geqp3!(driver, A)
199
+
computeR &&_qr_copyR!(R, A, jpvt)
167
200
else
168
201
A, τ =geqrf!(driver, A)
202
+
computeR &&_qr_copyR!(R, A)
169
203
end
204
+
Rf = computeR ? R :nothing# gaugefix! rescales rows, which commutes with the pivoting
170
205
if inplaceQ
171
-
Q =ungqr!(driver, A, τ)
206
+
Rd = positive ?copy(diagview(A)) :nothing# ungqr! destroys the diagonal of A
207
+
ungqr!(driver, A, τ) # Q === A, so no need to rebind Q
208
+
positive &&gaugefix!(qr_householder!, Q, Rf, Rd)
172
209
else
173
-
Q =unmqr!(driver, 'L', 'N', A, τ, one!(Q))
210
+
Q =_qr_buildQ!(driver, Q, A, τ, minmn)
211
+
positive &&gaugefix!(qr_householder!, Q, Rf, diagview(A))
174
212
end
175
213
end
176
214
177
-
if computeR
178
-
# we need to first copy then gaugefix - avoiding aliasing between R and Rd for broadcast
0 commit comments