Skip to content

Linear Algebra ​

Standard LinearAlgebra functions work directly on ROCArray, dispatching to AMD's rocBLAS (dense BLAS) and rocSOLVER (LAPACK-style factorizations). You write ordinary Julia linear algebra; the GPU libraries are used automatically.

Supported out of the box on ROCArray:

  • Matrix–matrix and matrix–vector products (*, mul!), including mixed- and low-precision (Float16) matmul.

  • BLAS level-1 operations: dot, norm, axpy!, axpby!, rmul!.

  • Triangular solves and \ (linear systems, LU-based).

  • Factorizations: cholesky, lu, qr, svd.

julia
julia> A = ROCArray(Float32[4 1; 1 3]);

julia> b = ROCArray(Float32[1, 2]);

julia> x = A \ b;                       # solve, LU via rocSOLVER

julia> Array(A * x) ≈ Array(b)
true

julia> C = cholesky(A);                 # Cholesky factorization

julia> Array(C.U' * C.U) ≈ Array(A)
true

Element types follow rocBLAS/rocSOLVER: Float32, Float64, ComplexF32, and ComplexF64 (with Float16 also supported for matmul).

Singular value decomposition ​

svd, svd!, and svdvals run on ROCMatrix via rocSOLVER. The alg keyword selects the backend. QRAlgorithm(), the default, is bidiagonalization and QR iteration (gesvd) and is accurate for every singular value, like LAPACK on the CPU. JacobiAlgorithm() is rocSOLVER's Jacobi method (gesvdj): several times faster, but it works on A'A, so singular values below sqrt(eps)·σ₁ and their vectors are inaccurate without any indication; use it for well-conditioned matrices when speed matters. Known rocSOLVER issue (rocm-libraries#15788): for a rank-deficient ComplexF32 matrix, gesvd may return an unnormalized singular vector for a zero singular value.

julia
using AMDGPU.rocSOLVER: JacobiAlgorithm, QRAlgorithm

F = svd(A)                              # QR iteration by default; F.U, F.S, F.Vt
s = svdvals(A)                          # singular values only
Ff = svd(A; full = true, alg = JacobiAlgorithm())

Because svdvals is supported, so is cond. (opnorm(A, 2) is not yet available — it relies on scalar indexing internal to LinearAlgebra.)

Direct library access ​

Lower-level routines are available in the AMDGPU.rocBLAS and AMDGPU.rocSOLVER submodules for cases the generic interface does not cover — for example batched factorizations, or the raw gesvd! (QR) and gesvdj! (Jacobi) SVD kernels.

Checking solver results ​

rocSOLVER writes the status of a routine (LAPACK's info) to device memory. gesvd!, gesvdj!, syevd!/heevd!, sygvd!/hegvd! and geblttrf! read it after the call, which synchronizes the stream, and throw as LinearAlgebra's LAPACK wrappers do: an ArgumentError for an invalid argument, a LAPACKException when the routine did not converge, a PosDefException when the B of a generalized eigenproblem is not positive definite.

With check = false the status is not read: the wrapper does not synchronize, a failure goes unreported, and the results are then garbage (gesvdj! returns residual, n_sweeps and info as device arrays). rocSOLVER routines also synchronize internally (in ROCm 7.2.3 gesvd and syevd always, gesvdj beyond a few dozen rows), so only small Jacobi SVDs actually run ahead of the host. The wrappers that return info (potrf!, getrf!, sytrf!, the batched factorizations) always read it.

AMDGPU.rocSOLVER.check_info Function
julia
check_info(devinfo::ROCVector{Cint}, chk = chklapackerror; check::Bool = true)

Consume the status (LAPACK's info) that a rocSOLVER routine wrote to the device array devinfo. With check = true, copy it to the host (which synchronizes the stream), free devinfo and pass the value to chk: chklapackerror throws an ArgumentError for info < 0 and a LAPACKException for info > 0 (no convergence), chkargsok only the former. With check = false, only free devinfo: no synchronization, and a failure of the routine goes unreported.

source
AMDGPU.rocSOLVER.gesvd! Function
julia
gesvd!(jobu, jobvt, A; check = true) -> (U, S, Vt)

Singular value decomposition of A by bidiagonalization and QR iteration (rocsolver_Xgesvd), destroying A. jobu and jobvt are LAPACK's: all ('A'), the first min(m, n) ('S'), overwriting A ('O') or no ('N') singular vectors; a factor that is not returned separately is C_NULL.

Throws a LAPACKException when the iteration did not converge; check = false skips reading the status (no synchronization, failures unreported), see check_info.

source
AMDGPU.rocSOLVER.gesvdj! Function
julia
gesvdj!(A; jobu = 'S', jobvt = 'S', abstol = eps(real(eltype(A))), max_sweeps = 100,
        check = true) -> (U, S, Vt, residual, n_sweeps, info)

Singular value decomposition of A by rocSOLVER's Jacobi method (rocsolver_Xgesvdj), destroying A. jobu and jobvt select all ('A'), the first min(m, n) ('S') or no ('N', returned as C_NULL) singular vectors; Vt holds the adjoint of the right singular vectors, as for gesvd!. The iteration stops when the off-diagonal part of the implicit A'A is below abstol relative to its norm (abstol ≤ 0: machine precision) or after max_sweeps sweeps; residual and n_sweeps report where it stopped.

Throws a LAPACKException when max_sweeps was exhausted (info = 1); check = false skips reading the status (no synchronization, failures unreported) and returns residual, n_sweeps and info as one-element device arrays, see check_info.

source

Device-side scalars ​

The scalar arguments of the rocBLAS wrappers (e.g. alpha and beta) can reside on the device, so that they don't need to be known on the host when an operation is enqueued — for example because a previous operation computed them. Pass an AMDGPU.ROCRef, or a one-element ROCArray; scalars on the host can still be passed as numbers, and the two can be mixed (host-side scalars are then written to the device, which is ordered on the stream without synchronizing). Routines that return a scalar (dot, dotc, dotu, nrm2, asum) accept an extra argument to write the result into, which avoids synchronizing with the host when it is a device-side scalar:

julia
x, y = AMDGPU.rand(Float32, 1024), AMDGPU.rand(Float32, 1024)

d = ROCRef{Float32}()
rocBLAS.dot(length(x), x, 1, y, 1, d)  # d = x ⋅ y, without synchronizing
rocBLAS.scal!(length(x), d, x, 1)      # x .*= d
d[]                                    # copy to the host

ROCRef(A, i) refers to element i of the array A, without copying it.

The Givens rotation routines rotg! and rotmg! overwrite their scalar arguments, so these can't be copied between the host and the device: either pass them all as host Refs, or all on the device. That makes it possible to construct a rotation on the device and apply it with rot! or rotm! without synchronizing. rotg(a, b) and rotmg(d1, d2, x1, y1) compute a rotation from host values instead.

AMDGPU.ROCRef Type
julia
ROCRef{T}()
ROCRef{T}(x)
ROCRef(x)
ROCRef(A::ROCArray, i::Integer=1)

A reference to a single value of type T that resides in GPU memory, e.g. to pass a device-side scalar to a library routine. The first three forms allocate a new value (optionally initialized to x), while the last one refers to element i of the (strided) device array A without copying it.

Reading a reference with r[] copies the value to the host, after the operations on the current stream have completed. Writing it with r[] = x (or constructing it from a value) is ordered on the current stream, but doesn't wait for it: the host can continue right away.

julia
alpha = ROCRef(2f0)
rocBLAS.scal!(length(x), alpha, x, 1)   # scale by a scalar that resides on the device

result = ROCRef{Float32}()
rocBLAS.dot(length(x), x, 1, y, 1, result)  # keep the result on the device
result[]
source