cuSPARSE

cuSPARSE.jl offers sparse arrays on the GPU, and NVIDIA's sparse linear algebra routines at three levels:

  • The sparse array types, CuSparseVector, CuSparseMatrixCSR, CuSparseMatrixCSC, CuSparseMatrixCOO and CuSparseMatrixBSR, work with SparseArrays and LinearAlgebra: convert from and to SparseMatrixCSC and CuMatrix, broadcast, multiply with * and mul!, add, and solve triangular systems with ldiv!. This should be the first choice.
  • The functions documented below wrap individual cuSPARSE routines, for when more control is needed. They are named after the operation they compute, as in BLAS, or after the cuSPARSE routine.
  • The generated bindings in libcusparse.jl follow NVIDIA's C API, and change with the CUDA toolkit. Use them together with handle and the descriptors below.

The wrappers follow LinearAlgebra.BLAS's conventions. Character arguments select the variant to compute: transa, transb and transx are 'N', 'T' or 'C', uplo is 'U' or 'L', and diag is 'U' for a unit diagonal or 'N'. Functions whose name ends in ! overwrite one of their arguments, the result, and return it. The index base of the sparse operands is the keyword index: 'O' (the default) for one-based indices as in Julia, or 'Z' for zero-based ones. Routines that offer several algorithms take a cuSPARSE algorithm enum, such as CUSPARSE_SPMV_CSR_ALG2, as an optional last positional argument.

Most wrappers use cuSPARSE's generic API, and check that the installed cuSPARSE supports them. Some only exist in NVIDIA's legacy API: the BSR triangular solves sv2! and sm2!, the incomplete factorizations ic02! and ilu02!, gemvi!, color and prune. NVIDIA has deprecated these, as well as the generic axpby!, rot! and vv!, so they may disappear from a future CUDA toolkit.

Several wrappers share their name with a function in LinearAlgebra, e.g. cuSPARSE.axpby! and LinearAlgebra.axpby!, but are different functions with different arguments. Qualify them with the module name.

Public

cuSPARSE.CUSPARSEError — Type
CUSPARSEError

Exception thrown when a cuSPARSE call returns an error status. The code field holds the cusparseStatus_t.

source
cuSPARSE.CuDenseMatrixDescriptor — Type
CuDenseMatrixDescriptor(A::DenseCuMatrix; transposed=false)

A descriptor of the dense matrix A, for calling cuSPARSE's generic API directly. With transposed = true, it describes transpose(A) in row-major order. Like CuDenseVectorDescriptor, it refers to the memory of A without keeping it alive.

source
cuSPARSE.CuDenseVectorDescriptor — Type
CuDenseVectorDescriptor(x::DenseCuVector)

A descriptor of the dense vector x, for calling cuSPARSE's generic API directly. It converts to a cusparseDnVecDescr_t, and is destroyed when it is garbage collected. The descriptor refers to the memory of x without keeping it alive, so x must be kept alive (e.g. with GC.@preserve) while the descriptor is in use. Its fields are not part of the API.

source
cuSPARSE.CuSparseArrayCSR — Type
CuSparseArrayCSR{Tv, Ti, N}

A batch of CSR matrices of the same size and number of nonzeros on the GPU, with the matrices along the third and later dimensions. Their row pointers, column indices and values are the columns of N-1-dimensional arrays. Construct it by concatenating CSR matrices with cat(As...; dims=3). Used by bmm!.

source
cuSPARSE.CuSparseMatrixBSR — Type
CuSparseMatrixBSR{Tv, Ti}

Container to hold sparse matrices in block compressed sparse row (BSR) format on the GPU. BSR format is also used in Intel MKL, and is suited to matrices that are "block" sparse - rare blocks of non-sparse regions.

Construct it from another sparse format or a dense CuMatrix and a block size, as CuSparseMatrixBSR(A, blockDim). The blocks are stored row-major, unless converted from a CSR matrix with CuSparseMatrixBSR{Tv}(A, blockDim; dir='C').

source
cuSPARSE.CuSparseMatrixCOO — Type
CuSparseMatrixCOO{Tv, Ti}

Container to hold sparse matrices in coordinate (COO) format on the GPU. COO format is mainly useful to initially construct sparse matrices, afterwards switch to CuSparseMatrixCSR for more functionality.

Construct it from its parts as CuSparseMatrixCOO(rowInd, colInd, nzVal, dims), or with sparse(I::CuVector, J::CuVector, V::CuVector, m, n; fmt=:coo), which also sorts the entries and combines duplicates.

source
cuSPARSE.CuSparseMatrixCSC — Type
CuSparseMatrixCSC{Tv, Ti}

Container to hold sparse matrices in compressed sparse column (CSC) format on the GPU, like SparseMatrixCSC. Construct it from a SparseMatrixCSC, a dense CuMatrix (with the index keyword), another sparse format, or from its parts as CuSparseMatrixCSC(colPtr, rowVal, nzVal, dims).

Note

Most CUSPARSE operations work with CSR formatted matrices, rather than CSC.

source
cuSPARSE.CuSparseMatrixCSR — Type
CuSparseMatrixCSR{Tv, Ti}

Container to hold sparse matrices in compressed sparse row (CSR) format on the GPU. Construct it from a SparseMatrixCSC, a dense CuMatrix (with the index keyword), another sparse format, or from its parts as CuSparseMatrixCSR(rowPtr, colVal, nzVal, dims).

Note

Most CUSPARSE operations work with CSR formatted matrices, rather than CSC.

CUDA 11

Support of indices type rather than Cint (Int32) requires at least CUDA 11.

source
cuSPARSE.CuSparseMatrixDescriptor — Type
CuSparseMatrixDescriptor(A, index::Char; transposed=false)

A descriptor of the CSR, CSC, COO or BSR matrix A with index base index ('O' for one-based, 'Z' for zero-based), for calling cuSPARSE's generic API directly. With transposed = true, it describes transpose(A), e.g. a CSC matrix as a CSR one; BSR matrices don't support it. Like CuDenseVectorDescriptor, it refers to the memory of A without keeping it alive.

source
cuSPARSE.CuSparseVector — Type
CuSparseVector{Tv, Ti}

Sparse vector on the GPU, holding the indices of its nonzeros (Ti, Cint by default) and their values (Tv), like SparseVector. Construct it from a SparseVector, as CuSparseVector(x), or from its parts as CuSparseVector(nzind::CuVector, nzval::CuVector, len).

source
cuSPARSE.axpby! — Method
axpby!(alpha, X::CuSparseVector, beta, Y::CuVector; index='O') -> Y

Compute Y = alpha * X + beta * Y for a sparse vector X and a dense vector Y. To add two sparse vectors into a new one, use axpby.

This function is not LinearAlgebra.axpby!, which works on CuArrays through cuBLAS.

Wraps cusparseAxpby, which NVIDIA deprecated in CUDA 12.8. Requires cuSPARSE 11.

source
cuSPARSE.bmm! — Method
bmm!(transa, transb, alpha, A::CuSparseArrayCSR, B::DenseCuArray, beta, C::DenseCuArray, [algo]; index='O') -> C

Compute C[:, :, i] = alpha * op(A[:, :, i]) * op(B[:, :, i]) + beta * C[:, :, i] for a batch of CSR matrices A and batches of dense matrices B and C. transa and transb select op ('N', 'T' or 'C').

Dimensions beyond the third are folded into the batch dimension. A batch of size one in A or B is broadcast over the batch of C. C must have more than one column when the batch has more than one matrix. algo is a cusparseSpMMAlg_t, by default CUSPARSE_SPMM_ALG_DEFAULT.

Wraps cusparseSpMM with strided batches. Requires cuSPARSE 11.7.2.

source
cuSPARSE.color — Function
color(A; index='O', percentage=1.0) -> ncolors, coloring, reordering

Color the adjacency graph of the square sparse CSR or CSC matrix A, which must have a symmetric sparsity pattern: pass A + transpose(A) otherwise. Neighbouring nodes get different colors.

The coloring is approximate, and stops when percentage of the nodes are colored; the other nodes get distinct colors after the last one used. coloring[i] is the color of node i, and reordering is a permutation that groups nodes of the same color together.

Wraps cusparse<t>csrcolor, which NVIDIA has deprecated, for the Float32, Float64, ComplexF32 and ComplexF64 element types.

source
cuSPARSE.enable_logging — Function
cuSPARSE.enable_logging(enable::Bool=true)

Forward log messages from cuSPARSE to Julia's logging system. API and kernel traces are reported at Debug level, performance hints at Info level, and problems at Error level. Starting Julia with JULIA_DEBUG=cuSPARSE enables this automatically, and also shows the Debug-level messages.

The logging API requires CUDA 11 or later; enabling on an older toolkit emits a warning.

source
cuSPARSE.gather! — Method
gather!(X::CuSparseVector, Y::CuVector; index='O') -> X

Gather the elements of the dense vector Y at the indices of the nonzeros of the sparse vector X into the nonzero values of X.

Wraps cusparseGather. Requires cuSPARSE 11.

source
cuSPARSE.geam — Method
geam(alpha, A, beta, B; index='O') -> C

Compute C = alpha * A + beta * B into a new sparse matrix, for sparse matrices A and B that are both in CSR or both in CSC format. This is what A + B and A - B use.

Wraps cusparseSpGEAM from cuSPARSE 12.8.1, and cusparse<t>csrgeam2 before, for the Float32, Float64, ComplexF32 and ComplexF64 element types.

source
cuSPARSE.gemm! — Method
gemm!(transa, transb, alpha, A, B, beta, C, [algo]; index='O') -> C

Compute C = alpha * A * B + beta * C for sparse matrices A, B and C, all in CSR or all in CSC format. Only transa = transb = 'N' is supported.

With beta ≠ 0, A * B must have the same sparsity pattern as C; an error is thrown if it has a different number of nonzeros, but a different pattern with the same number of nonzeros is not detected. With beta = 0, the storage of C is replaced when the number of nonzeros differs. algo is a cusparseSpGEMMAlg_t, by default CUSPARSE_SPGEMM_DEFAULT.

Wraps cusparseSpGEMM. Requires cuSPARSE 11.

source
cuSPARSE.gemm — Method
gemm(transa, transb, alpha, A, B, [algo]; index='O') -> C
gemm(transa, transb, alpha, A, B, beta, C, [algo]; index='O', same_pattern=false) -> D

Compute alpha * A * B, or alpha * A * B + beta * C, into a new sparse matrix, for sparse matrices A, B and C, all in CSR or all in CSC format. Only transa = transb = 'N' is supported.

With same_pattern = true, A * B must have the same sparsity pattern as C, and the result is computed in a copy of C with gemm!. Otherwise alpha * A * B and beta * C are added with geam. algo is a cusparseSpGEMMAlg_t, by default CUSPARSE_SPGEMM_DEFAULT.

Wraps cusparseSpGEMM. Requires cuSPARSE 11.

source
cuSPARSE.gemv — Function
gemv(transa, alpha, A, x::CuSparseVector, [algo]; index='O') -> CuSparseVector

Compute alpha * A * x for a sparse CSR or CSC matrix A and a sparse vector x, returning a new sparse vector. Only transa = 'N' is supported.

Like gemm, this is a sparse-sparse product, computed by treating x as a sparse matrix with one column. It only pays off when the result is very sparse too: A * x and mul! convert x to a dense vector and use mv! instead. algo is a cusparseSpGEMMAlg_t, by default CUSPARSE_SPGEMM_DEFAULT.

Wraps cusparseSpGEMM. Requires cuSPARSE 11.

source
cuSPARSE.gemvi! — Function
gemvi!(transa, alpha, A::CuMatrix, X::CuSparseVector, beta, Y::CuVector; index='O') -> Y

Compute Y = alpha * op(A) * X + beta * Y for a dense matrix A, a sparse vector X and a dense vector Y, where transa selects op ('N', 'T' or, for real matrices, 'C').

Wraps cusparse<t>gemvi, which NVIDIA deprecated in CUDA 12.8, for the Float32, Float64, ComplexF32 and ComplexF64 element types.

source
cuSPARSE.gtsv2 — Function
gtsv2(dl::CuVector, d::CuVector, du::CuVector, B::CuVecOrMat; pivoting=true) -> X

Solve A * X = B for the tridiagonal matrix A, like gtsv2! but without overwriting B.

source
cuSPARSE.gtsv2! — Function
gtsv2!(dl::CuVector, d::CuVector, du::CuVector, B::CuVecOrMat; pivoting=true) -> B

Solve A * X = B in place in B, for the tridiagonal matrix A with subdiagonal dl, diagonal d and superdiagonal du. The three vectors have the same length as the columns of B, at least 3, and dl[1] and du[end] must be zero. With pivoting, partial pivoting makes the solution more accurate but slower.

Wraps cusparse<t>gtsv2 and cusparse<t>gtsv2_nopivot.

source
cuSPARSE.handle — Method
cuSPARSE.handle()

The cuSPARSE handle of the current task, for its active context and stream, for calling the C API of cuSPARSE (cuSPARSE.cusparse*) directly. It converts to a cusparseHandle_t. The handle is borrowed: don't destroy it, or change its stream or other settings. Its fields are not part of the API.

source
cuSPARSE.ic02 — Function
ic02(A; index='O')

Compute the incomplete Cholesky factorization IC(0) of the sparse matrix A, like ic02! but in a copy of A. A can also be a CSR or CSC matrix wrapped in Hermitian, or for real element types in Symmetric, which is first expanded to the full matrix it represents.

source
cuSPARSE.ic02! — Function
ic02!(A; index='O') -> A

Compute the incomplete Cholesky factorization with zero fill-in, IC(0), of a sparse Hermitian positive definite matrix in place, keeping the sparsity pattern of A. An error is thrown when the factorization hits a zero pivot.

cuSPARSE reads the lower triangle of the matrix as stored row by row. For a CSR matrix, that is the lower triangle of A, which is overwritten with the factor L of A ≈ L * L'. A CSC matrix is processed as the CSR storage of its transpose: its upper triangle is read, and overwritten with the factor R = L' of A ≈ R' * R. BSR matrices follow the conventions of bsric02.

The result is meant as a preconditioner for iterative solvers, applied with ldiv! on triangular views of it.

Wraps cusparse<t>csric02 and cusparse<t>bsric02, which NVIDIA has deprecated, for the Float32, Float64, ComplexF32 and ComplexF64 element types.

source
cuSPARSE.ilu02 — Function
ilu02(A; index='O')

Compute the incomplete LU factorization ILU(0) of the sparse matrix A, like ilu02! but in a copy of A. A can also be a CSR or CSC matrix wrapped in Symmetric or Hermitian, which is first expanded to the full matrix it represents.

source
cuSPARSE.ilu02! — Function
ilu02!(A; index='O') -> A

Compute the incomplete LU factorization with zero fill-in, ILU(0), of the sparse square matrix A in place, keeping its sparsity pattern. A is overwritten with the factors L, with an implicit unit diagonal, and U of A ≈ L * U. An error is thrown when the factorization hits a zero pivot.

The result is meant as a preconditioner for iterative solvers, applied with ldiv! on triangular views of it.

Wraps cusparse<t>csrilu02 and cusparse<t>bsrilu02, which NVIDIA has deprecated, for the Float32, Float64, ComplexF32 and ComplexF64 element types.

source
cuSPARSE.mm! — Method
mm!(transa, transb, alpha, A::CuSparseMatrix, B, beta, C, [algo]; index='O') -> C
mm!(transa, transb, alpha, A::DenseCuMatrix, B::CuSparseMatrix, beta, C, [algo]; index='O') -> C

Compute C = alpha * op(A) * op(B) + beta * C for a sparse and a dense matrix, where transa and transb select op ('N', 'T' or 'C') and C is a dense matrix.

In the first form, A can be a CSR, CSC, COO or BSR matrix (BSR from cuSPARSE 12.5.1), and B and C can also be the transpose of a dense matrix, to use row-major storage. In the second form, B can be a CSR, CSC or COO matrix. algo is a cusparseSpMMAlg_t, by default CUSPARSE_SPMM_ALG_DEFAULT. Float16 and ComplexF16 are computed in single precision.

Wraps cusparseSpMM. Before cuSPARSE 11, B and C must be column-major and, unless A is a COO matrix, transb must be 'N'. Before cuSPARSE 12, transa = 'C' is not supported for complex CSC matrices.

source
cuSPARSE.mv! — Method
mv!(transa, alpha, A::CuSparseMatrix, X::DenseCuVector, beta, Y::DenseCuVector, [algo]; index='O') -> Y

Compute Y = alpha * op(A) * X + beta * Y for a sparse matrix A and dense vectors X and Y, where transa selects op ('N', 'T' or 'C').

A can be a CSR, CSC, COO or BSR matrix. algo is a cusparseSpMVAlg_t, by default CUSPARSE_SPMV_ALG_DEFAULT. Float16 and ComplexF16 are computed in single precision, from cuSPARSE 11.4 and 11.7.2 respectively.

Wraps cusparseSpMV. Before cuSPARSE 12.6.3, BSR matrices use cusparse<t>bsrmv instead, which only supports transa = 'N' and the Float32, Float64, ComplexF32 and ComplexF64 element types. Before cuSPARSE 12, transa = 'C' is not supported for complex CSC matrices.

source
cuSPARSE.prune — Function
prune(A, threshold; index='O')

Return a copy of the sparse CSR or CSC matrix A without the entries whose absolute value is not larger than threshold.

Wraps cusparse<t>pruneCsr2csr, which NVIDIA has deprecated, for the Float32 and Float64 element types. It uses 32-bit indices: an error is thrown when the size or number of nonzeros of A doesn't fit.

source
cuSPARSE.rot! — Method
rot!(X::CuSparseVector, Y::CuVector, c, s; index='O') -> X, Y

Apply the Givens rotation with cosine c and sine s to the sparse vector X and the dense vector Y at the indices of the nonzeros of X: X = c * X + s * Y and Y = c * Y - s * X.

Wraps cusparseRot, which NVIDIA has deprecated. Requires cuSPARSE 11.

source
cuSPARSE.scatter! — Method
scatter!(Y::CuVector, X::CuSparseVector; index='O') -> Y

Scatter the nonzeros of the sparse vector X into the dense vector Y, leaving the other elements of Y unchanged.

Wraps cusparseScatter. Requires cuSPARSE 11.

source
cuSPARSE.sddmm! — Method
sddmm!(transa, transb, alpha, A::DenseCuMatrix, B::DenseCuMatrix, beta, C, [algo]; index='O') -> C

Compute C = alpha * op(A) * op(B) + beta * C at the nonzeros of the sparse matrix C only, for dense matrices A and B. transa and transb select op ('N', 'T' or 'C').

C can be a CSR matrix, or a BSR matrix from cuSPARSE 12.1. algo is a cusparseSDDMMAlg_t, by default CUSPARSE_SDDMM_ALG_DEFAULT.

Wraps cusparseSDDMM. Requires cuSPARSE 11.4.1.

source
cuSPARSE.sm! — Method
sm!(transa, transb, uplo, diag, alpha, A, B::DenseCuMatrix, C::DenseCuMatrix, [algo]; index='O') -> C

Solve op(A) * C = alpha * op(B) for C, where A is a sparse triangular matrix in CSR, CSC or COO format, and transa and transb select op ('N', 'T' or 'C').

uplo selects the triangle of A to use ('U' or 'L'), and diag is 'U' for a unit diagonal, which is not read, or 'N'. algo is a cusparseSpSMAlg_t, by default CUSPARSE_SPSM_ALG_DEFAULT.

B and C can be the same matrix. With transb ≠ 'N', the solution is then written transposed into it, in the shape of B.

Wraps cusparseSpSM. Requires cuSPARSE 11.6. Before cuSPARSE 12.8.1, transa = 'C' is not supported for complex CSC matrices.

source
cuSPARSE.sm2! — Function
sm2!(transa, transxy, uplo, diag, alpha, A::CuSparseMatrixBSR, X::StridedCuMatrix; index='O') -> X

Solve op(A) * op(Y) = alpha * op(X) in place in X, for a sparse triangular matrix A in BSR format. transa selects op for A ('N', 'T' or 'C'), and transxy for X and Y ('N' or 'T', and 'C' for real matrices only).

uplo selects the triangle of A to use ('U' or 'L'), and diag is 'U' for a unit diagonal, which is not read, or 'N'. An error is thrown when the diagonal has a structural or numerical zero. For CSR, CSC and COO matrices, use sm!.

Wraps cusparse<t>bsrsm2_solve, which NVIDIA has deprecated, for the Float32, Float64, ComplexF32 and ComplexF64 element types.

source
cuSPARSE.sm2 — Method
sm2(transa, transxy, uplo, [diag='N'], [alpha=1], A::CuSparseMatrixBSR, X::StridedCuMatrix; index='O') -> Y

Solve op(A) * op(Y) = alpha * op(X) for Y, like sm2! but without overwriting X. alpha can only be given together with diag.

source
cuSPARSE.sort_coo — Method
sort_coo(A::CuSparseMatrixCOO; by=:row) -> CuSparseMatrixCOO

Return a copy of A with its entries sorted by row (by = :row), or by column (by = :col).

Wraps cusparseXcoosortByRow and cusparseXcoosortByColumn, which use 32-bit indices: an error is thrown when the size or number of nonzeros of A doesn't fit.

source
cuSPARSE.sort_csc — Method
sort_csc(A::CuSparseMatrixCSC; index='O') -> CuSparseMatrixCSC

Return a copy of A with the row indices sorted within each column.

Wraps cusparseXcscsort, which uses 32-bit indices: an error is thrown when the size or number of nonzeros of A doesn't fit.

source
cuSPARSE.sort_csr — Method
sort_csr(A::CuSparseMatrixCSR; index='O') -> CuSparseMatrixCSR

Return a copy of A with the column indices sorted within each row.

Wraps cusparseXcsrsort, which uses 32-bit indices: an error is thrown when the size or number of nonzeros of A doesn't fit.

source
cuSPARSE.sv! — Method
sv!(transa, uplo, diag, alpha, A, X::DenseCuVector, Y::DenseCuVector, [algo]; index='O') -> Y

Solve op(A) * Y = alpha * X for Y, where A is a sparse triangular matrix in CSR, CSC or COO format and transa selects op ('N', 'T' or 'C').

uplo selects the triangle of A to use ('U' or 'L'), and diag is 'U' for a unit diagonal, which is not read, or 'N'. algo is a cusparseSpSVAlg_t, by default CUSPARSE_SPSV_ALG_DEFAULT.

Wraps cusparseSpSV. Requires cuSPARSE 11.5. Before cuSPARSE 12.8.1, transa = 'C' is not supported for complex CSC matrices.

source
cuSPARSE.sv2! — Function
sv2!(transa, uplo, diag, alpha, A::CuSparseMatrixBSR, X::CuVector; index='O') -> X

Solve op(A) * Y = alpha * X in place in X, where A is a sparse triangular matrix in BSR format and transa selects op ('N', 'T' or 'C').

uplo selects the triangle of A to use ('U' or 'L'), and diag is 'U' for a unit diagonal, which is not read, or 'N'. An error is thrown when the diagonal has a structural or numerical zero. For CSR, CSC and COO matrices, use sv!.

Wraps cusparse<t>bsrsv2_solve, which NVIDIA has deprecated, for the Float32, Float64, ComplexF32 and ComplexF64 element types.

source
cuSPARSE.sv2 — Method
sv2(transa, uplo, [diag='N'], [alpha=1], A::CuSparseMatrixBSR, X::CuVector; index='O') -> Y

Solve op(A) * Y = alpha * X for Y, like sv2! but without overwriting X. alpha can only be given together with diag.

source
cuSPARSE.version — Method
cuSPARSE.version() -> VersionNumber

The version of the cuSPARSE library in use, which differs from the version of the CUDA toolkit.

source
cuSPARSE.vv! — Method
vv!(transx, X::CuSparseVector, Y::DenseCuVector; index='O') -> Number

Compute the dot product of the sparse vector X and the dense vector Y. With transx = 'C', the elements of X are conjugated, as in LinearAlgebra.dot(X, Y); with 'N' they are not. Despite the !, neither vector is modified.

Wraps cusparseSpVV, which NVIDIA deprecated in CUDA 12.8.

source

Private

SparseArrays.sparse — Method
sparse(x::DenseCuMatrix; fmt=:csc)
sparse(I::CuVector, J::CuVector, V::CuVector, [m, n]; fmt=:csc)

Return a sparse cuda matrix, with type determined by fmt. Possible formats are :csc, :csr, :bsr, and :coo.

source
cuSPARSE.chkbmmdims — Method

check that the dimensions of arrays B and C make sense for a batched matrix-matrix multiplication

source