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$.Xis a dense vector of size $k$.Yis 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:
- 32-bit indices (
IndexType::I32) - 64-bit indices (
IndexType::I64)
spmv supports the following data types:
Uniform-precision computation:
A/X/Y/compute_type |
|---|
DataType::F32 |
DataType::F64 |
DataType::ComplexF32 |
DataType::ComplexF64 |
Mixed-precision computation:
A | X/Y/compute_type |
|---|---|
DataType::F32 | DataType::F64 |
Mixed Regular/Complex computation:
A | X/Y/compute_type |
|---|---|
DataType::F32 | DataType::ComplexF32 |
DataType::F64 | DataType::ComplexF64 |
DataType::F16, DataType::Bf16, DataType::ComplexF16, and DataType::ComplexBf16 data types always imply mixed-precision computation.
spmv supports the following algorithms:
| Algorithm | Notes |
|---|---|
SpMvAlgorithm::Default | Default algorithm for any sparse matrix format. |
SpMvAlgorithm::Coo1 | Default algorithm for COO sparse matrix format. May produce slightly different results during different runs with the same input parameters. |
SpMvAlgorithm::Coo2 | Provides deterministic (bitwise) results for each run. If operation is not Operation::NonTranspose, it is identical to SpMvAlgorithm::Coo1. |
SpMvAlgorithm::Csr1 | Default algorithm for CSR/CSC sparse matrix format. May produce slightly different results during different runs with the same input parameters. |
SpMvAlgorithm::Csr2 | Provides deterministic (bitwise) results for each run. If operation is not Operation::NonTranspose, it is identical to SpMvAlgorithm::Csr1. |
SpMvAlgorithm::Sell1 | Default algorithm for Sliced Ellpack sparse matrix format. Provides deterministic (bitwise) results for each run. |
SpMvAlgorithm::Bsr1 | Default 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:
SpMvAlgorithm::Coo1andSpMvAlgorithm::Csr1provide higher performance thanSpMvAlgorithm::Coo2andSpMvAlgorithm::Csr2.- In general,
Operation::NonTransposeis 3x faster than transpose or conjugate-transpose operations. - Using
spmv_preprocesshelps improve performance ofspmvin CSR. It is beneficial whenspmvruns multiple times with the same matrix (spmv_preprocessis executed only once).
spmv has the following properties:
- Requires extra storage for CSR/CSC format (all algorithms) and for COO format with
SpMvAlgorithm::Coo2algorithm. - Provides deterministic (bitwise) results for each run only for
SpMvAlgorithm::Coo2,SpMvAlgorithm::Csr2andSpMvAlgorithm::Bsr1algorithms, and only withOperation::NonTranspose. - Supports asynchronous execution.
compute-sanitizercould report false race conditions for this operation whenbetais0. These reports result from optimization and do not affect computation correctness.- Allows the indices of
matrixto be unsorted.
spmv supports the following optimizations:
- CUDA graph capture.
- Hardware Memory Compression.