@@ -319,19 +319,33 @@ end
319319
320320# GPU logic (randomized SVD - CUSOLVER_Randomized has no CPU analog, kept as-is)
321321# ---------------------------------------------------------------------------------
322+ # A randomized SVD only computes the `k` largest singular triplets, so the outputs only need
323+ # room for those: `U` is `m × r`, `S` is `r × r` and `Vᴴ` is `r × n` for some `k ≤ r ≤ minmn`
324+ # (`initialize_output` uses `r = k`). Only the first `k` columns/values/rows are written.
325+
326+ function _randomized_rank (alg:: CUSOLVER_Randomized )
327+ haskey (alg. kwargs, :k ) ||
328+ throw (ArgumentError (" $alg requires the number of singular values to compute, `k`" ))
329+ return alg. kwargs. k:: Int
330+ end
322331
323332function check_input (
324333 :: Union{typeof(svd_trunc!), typeof(svd_trunc_no_error!)} , A:: AbstractMatrix , USVᴴ, alg:: CUSOLVER_Randomized
325334 )
326335 m, n = size (A)
327336 minmn = min (m, n)
337+ k = _randomized_rank (alg)
328338 U, S, Vᴴ = USVᴴ
329339 @assert U isa AbstractMatrix && S isa Diagonal && Vᴴ isa AbstractMatrix
330- @check_size (U, (m, m))
340+ r = size (S, 1 )
341+ k ≤ r ≤ minmn ||
342+ throw (DimensionMismatch (" size of S ($r ) must be between k ($k ) and min(size(A)...) ($minmn )" ))
343+ (size (U, 1 ) == m && k ≤ size (U, 2 ) ≤ m) ||
344+ throw (DimensionMismatch (" size of U $(size (U)) must be ($m , r) with r between k ($k ) and $m " ))
345+ (size (Vᴴ, 2 ) == n && k ≤ size (Vᴴ, 1 ) ≤ n) ||
346+ throw (DimensionMismatch (" size of Vᴴ $(size (Vᴴ)) must be (r, $n ) with r between k ($k ) and $n " ))
331347 @check_scalar (U, A)
332- @check_size (S, (minmn, minmn))
333348 @check_scalar (S, A, real)
334- @check_size (Vᴴ, (n, n))
335349 @check_scalar (Vᴴ, A)
336350 return nothing
337351end
@@ -340,10 +354,10 @@ function initialize_output(
340354 :: Union{typeof(svd_trunc!), typeof(svd_trunc_no_error!)} , A:: AbstractMatrix , alg:: TruncatedAlgorithm{<:CUSOLVER_Randomized}
341355 )
342356 m, n = size (A)
343- minmn = min (m, n )
344- U = similar (A, (m, m ))
345- S = Diagonal (similar (A, real (eltype (A)), (minmn ,)))
346- Vᴴ = similar (A, (n , n))
357+ k = _randomized_rank (alg . alg )
358+ U = similar (A, (m, k ))
359+ S = Diagonal (similar (A, real (eltype (A)), (k ,)))
360+ Vᴴ = similar (A, (k , n))
347361 return (U, S, Vᴴ)
348362end
349363
@@ -356,13 +370,18 @@ end
356370function svd_trunc_no_error! (A:: AbstractMatrix , USVᴴ, alg:: TruncatedAlgorithm{<:GPU_Randomized} )
357371 U, S, Vᴴ = USVᴴ
358372 check_input (svd_trunc_no_error!, A, (U, S, Vᴴ), alg. alg)
359- _gpu_Xgesvdr! (A, diagview (S), U, Vᴴ; alg. alg. kwargs... )
373+ k = _randomized_rank (alg. alg)
374+ # only the first `k` values are computed: restrict to those, so that the truncation never
375+ # sees the (uninitialized) remainder of larger outputs
376+ Uk = size (U, 2 ) == k ? U : view (U, :, 1 : k)
377+ Sk = size (S, 1 ) == k ? S : Diagonal (view (diagview (S), 1 : k))
378+ Vᴴk = size (Vᴴ, 1 ) == k ? Vᴴ : view (Vᴴ, 1 : k, :)
379+ kwargs = Base. structdiff (alg. alg. kwargs, NamedTuple{(:fixgauge ,)})
380+ _gpu_Xgesvdr! (A, diagview (Sk), Uk, Vᴴk; kwargs... )
360381
361- # TODO : make sure that truncation is based on maxrank, otherwise this might be wrong
362- (Utr, Str, Vᴴtr), _ = truncate (svd_trunc!, (U, S, Vᴴ), alg. trunc)
382+ (Utr, Str, Vᴴtr), _ = truncate (svd_trunc!, (Uk, Sk, Vᴴk), alg. trunc)
363383
364384 do_gauge_fix = get (alg. alg. kwargs, :fixgauge , default_fixgauge ()):: Bool
365- # the output matrices here are the same size as for svd_full!
366385 do_gauge_fix && gaugefix! (svd_trunc!, Utr, Vᴴtr)
367386
368387 return Utr, Str, Vᴴtr
0 commit comments