Skip to main content

cusparseSpMV

Function cusparseSpMV 

Source
pub unsafe extern "C" fn cusparseSpMV(
    handle: cusparseHandle_t,
    opA: cusparseOperation_t,
    alpha: *const c_void,
    matA: cusparseConstSpMatDescr_t,
    vecX: cusparseConstDnVecDescr_t,
    beta: *const c_void,
    vecY: cusparseDnVecDescr_t,
    computeType: cudaDataType,
    alg: cusparseSpMVAlg_t,
    externalBuffer: *mut c_void,
) -> cusparseStatus_t
Expand description

This function performs the multiplication of a sparse matrix matA and a dense vector vecX

where

  • op(A) is a sparse matrix of size $m \times k$
  • X is a dense vector of size $k$
  • Y is a dense vector of size $m$
  • $\alpha$ and $\beta$ are scalars

Also, for matrix A: $$ \operatorname{op}(A) = \begin{cases} A & \text{if } op(A) = \text{CUSPARSE_OPERATION_NON_TRANSPOSE} \ A^T & \text{if } op(A) = \text{CUSPARSE_OPERATION_TRANSPOSE} \ A^H & \text{if } op(A) = \text{CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE} \end{cases} $$

The function cusparseSpMV_bufferSize returns the size of the workspace needed by cusparseSpMV_preprocess and cusparseSpMV

The sparse matrix formats currently supported are listed below:

cusparseSpMV supports the following index type for representing the sparse matrix matA:

cusparseSpMV supports the following data types:

Uniform-precision computation:

A/X/ Y/computeType
cudaDataType_t::CUDA_R_32F
cudaDataType_t::CUDA_R_64F
cudaDataType_t::CUDA_C_32F
cudaDataType_t::CUDA_C_64F

Mixed-precision computation:

A/XYcomputeTypeNotes
cudaDataType_t::CUDA_R_8IcudaDataType_t::CUDA_R_32IcudaDataType_t::CUDA_R_32I
cudaDataType_t::CUDA_R_8IcudaDataType_t::CUDA_R_32FcudaDataType_t::CUDA_R_32F
cudaDataType_t::CUDA_R_16F
cudaDataType_t::CUDA_R_16BF
cudaDataType_t::CUDA_R_16FcudaDataType_t::CUDA_R_16F
cudaDataType_t::CUDA_R_16BFcudaDataType_t::CUDA_R_16BF
cudaDataType_t::CUDA_C_32FcudaDataType_t::CUDA_C_32FcudaDataType_t::CUDA_C_32F
cudaDataType_t::CUDA_C_16FcudaDataType_t::CUDA_C_16F[DEPRECATED]
cudaDataType_t::CUDA_C_16BFcudaDataType_t::CUDA_C_16BF[DEPRECATED]
AX/Y/computeType
cudaDataType_t::CUDA_R_32FcudaDataType_t::CUDA_R_64F

Mixed Regular/Complex computation:

AX/Y/computeType
cudaDataType_t::CUDA_R_32FcudaDataType_t::CUDA_C_32F
cudaDataType_t::CUDA_R_64FcudaDataType_t::CUDA_C_64F

NOTE: cudaDataType_t::CUDA_R_16F, cudaDataType_t::CUDA_R_16BF, cudaDataType_t::CUDA_C_16F, and cudaDataType_t::CUDA_C_16BF data types always imply mixed-precision computation.

cusparseSpMV supports the following algorithms:

AlgorithmNotes
cusparseSpMVAlg_t::CUSPARSE_SPMV_ALG_DEFAULTDefault algorithm for any sparse matrix format.
cusparseSpMVAlg_t::CUSPARSE_SPMV_COO_ALG1Default algorithm for COO sparse matrix format. May produce slightly different results during different runs with the same input parameters.
cusparseSpMVAlg_t::CUSPARSE_SPMV_COO_ALG2Provides deterministic (bit-wise) results for each run. If opA != CUSPARSE_OPERATION_NON_TRANSPOSE, it is identical to cusparseSpMVAlg_t::CUSPARSE_SPMV_COO_ALG1.
cusparseSpMVAlg_t::CUSPARSE_SPMV_CSR_ALG1Default algorithm for CSR/CSC sparse matrix format. May produce slightly different results during different runs with the same input parameters.
cusparseSpMVAlg_t::CUSPARSE_SPMV_CSR_ALG2Provides deterministic (bit-wise) results for each run. If opA != CUSPARSE_OPERATION_NON_TRANSPOSE, it is identical to cusparseSpMVAlg_t::CUSPARSE_SPMV_CSR_ALG1.
cusparseSpMVAlg_t::CUSPARSE_SPMV_SELL_ALG1Default algorithm for Sliced Ellpack sparse matrix format. Provides deterministic (bit-wise) results for each run.
cusparseSpMVAlg_t::CUSPARSE_SPMV_BSR_ALG1Default algorithm for BSR sparse matrix format. Provides deterministic (bit-wise) results for each run. Supports only opA == CUSPARSE_OPERATION_NON_TRANSPOSE. Supports both row-major and column-major block layouts in A.

Calling cusparseSpMV_preprocess is optional. It may accelerate subsequent calls to cusparseSpMV. It is useful when cusparseSpMV is called multiple times with the same sparsity pattern (matA).

Calling cusparseSpMV_preprocess with buffer makes that buffer “active” for matA SpMV calls. Subsequent calls to cusparseSpMV with matA and the active buffer must use the same values for all parameters as the call to cusparseSpMV_preprocess. The exceptions are: alpha, beta, vecX, vecY, and the values (but not indices) of matA may be different. Importantly, the buffer contents must be unmodified since the call to cusparseSpMV_preprocess. When cusparseSpMV is called with matA and its active buffer, it may read acceleration data from the buffer.

Calling cusparseSpMV_preprocess again with matA and a new buffer will make the new buffer active, forgetting about the previously-active buffer and making it inactive. For cusparseSpMV, there can only be one active buffer per sparse matrix at a time. To get the effect of multiple active buffers for a single sparse matrix, create multiple matrix handles that all point to the same index and value buffers, and call cusparseSpMV_preprocess once per handle with different workspace buffers.

Calling cusparseSpMV with an inactive buffer is always permitted. However, there may be no acceleration from the preprocessing in that case.

For the purposes of thread safety, cusparseSpMV_preprocess is writing to matA internal state.

Performance notes:

cusparseSpMV has the following properties:

cusparseSpMV supports the following optimizations:

  • CUDA graph capture
  • Hardware Memory Compression

Please visit cuSPARSE Library Samples - cusparseSpMV CSR and cusparseSpMV COO for a code example.

§Parameters

  • handle: Handle to the cuSPARSE library context.
  • opA: Operation op(A).
  • alpha: $\alpha$ scalar used for multiplication of type computeType.
  • matA: Sparse matrix A.
  • vecX: Dense vector X.
  • beta: $\beta$ scalar used for multiplication of type computeType.
  • vecY: Dense vector Y.
  • computeType: Datatype in which the computation is executed.
  • alg: Algorithm for the computation.
  • externalBuffer: Pointer to a workspace buffer of at least bufferSize bytes.