Skip to main content

spmv

Function spmv 

Source
pub fn spmv<Compute: DataTypeLike>(
    ctx: &Context,
    operation: Operation,
    alpha: Scalar<'_, Compute>,
    matrix: &SparseMatrixDescriptor<'_>,
    x: &DenseVectorDescriptor<'_>,
    beta: Scalar<'_, Compute>,
    y: &mut DenseVectorDescriptor<'_>,
    algorithm: SpMvAlgorithm,
    external_buffer: Option<DevicePtr>,
) -> Result<()>
Expand description

Multiplies sparse matrix matrix by dense vector x. Formally, it computes y = alpha * op(A) * x + beta * y, 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.

op(A) is selected by operation and may be A, A^T, or A^H.

spmv_buffer_size returns the workspace size needed by spmv_preprocess and spmv.

The sparse matrix formats currently supported are listed below:

spmv supports the following index type for representing the sparse matrix matrix:

spmv supports the following data types:

Uniform-precision computation:

Mixed-precision computation:

AX/Y/compute_type
DataType::F32DataType::F64

Mixed Regular/Complex computation:

DataType::F16, DataType::Bf16, DataType::ComplexF16, and DataType::ComplexBf16 data types always imply mixed-precision computation.

spmv supports the following algorithms:

AlgorithmNotes
SpMvAlgorithm::DefaultDefault algorithm for any sparse matrix format.
SpMvAlgorithm::Coo1Default algorithm for COO sparse matrix format. May produce slightly different results during different runs with the same input parameters.
SpMvAlgorithm::Coo2Provides deterministic (bitwise) results for each run. If operation is not Operation::NonTranspose, it is identical to SpMvAlgorithm::Coo1.
SpMvAlgorithm::Csr1Default algorithm for CSR/CSC sparse matrix format. May produce slightly different results during different runs with the same input parameters.
SpMvAlgorithm::Csr2Provides deterministic (bitwise) results for each run. If operation is not Operation::NonTranspose, it is identical to SpMvAlgorithm::Csr1.
SpMvAlgorithm::Sell1Default algorithm for Sliced Ellpack sparse matrix format. Provides deterministic (bitwise) results for each run.
SpMvAlgorithm::Bsr1Default algorithm for BSR sparse matrix format. Provides deterministic (bitwise) results for each run. Supports only Operation::NonTranspose. Supports both row-major and column-major block layouts in A.

Calling spmv_preprocess is optional. It may accelerate subsequent calls to spmv. Useful when spmv is called multiple times with the same sparsity pattern (matrix).

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

Calling spmv_preprocess again with matrix and a new buffer makes the new buffer active and makes the previously active buffer inactive. For spmv, 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 spmv_preprocess once per handle with different workspace buffers.

Calling spmv 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, spmv_preprocess is writing to matrix internal state.

Performance notes:

spmv has the following properties:

  • Requires extra storage for CSR/CSC format (all algorithms) and for COO format with SpMvAlgorithm::Coo2 algorithm.
  • Provides deterministic (bitwise) results for each run only for SpMvAlgorithm::Coo2, SpMvAlgorithm::Csr2 and SpMvAlgorithm::Bsr1 algorithms, and only with Operation::NonTranspose.
  • Supports asynchronous execution.
  • compute-sanitizer could report false race conditions for this operation when beta is 0. These reports result from optimization and do not affect computation correctness.
  • Allows the indices of matrix to be unsorted.

spmv supports the following optimizations:

  • CUDA graph capture.
  • Hardware Memory Compression.