Expand description
Safe Rust wrappers for NVIDIA cuSPARSE.
Covers the modern generic-API surface: Handle, SpMat (CSR/CSC/COO/BSR),
DnMat, DnVec, and the family of op algorithms — SpMV, SpMM, SpGEMM,
SpSV, SpSM, SDDMM — plus CSR↔CSC and sparse↔dense conversions, and the
sparse BLAS-1 helpers (axpby, gather, scatter, rot).
All matrix/vector descriptors borrow the underlying DeviceBuffers and
tie their lifetime to them so the buffers can’t be freed while cuSPARSE
is still holding references.
§Examples
SpMV (sparse-times-dense vector): y = A · x for a 4×4 CSR matrix
with 6 non-zeros.
use baracuda_cusparse::{spmv, spmv_buffer_size, DnVec, Handle, Op, SpMVAlg, SpMat};
use baracuda_driver::{Context, Device, DeviceBuffer};
baracuda_driver::init()?;
let device = Device::get(0)?;
let ctx = Context::new(&device)?;
let handle = Handle::new()?;
// A = [[1, 0, 0, 2], [0, 3, 0, 0], [0, 0, 0, 4], [5, 0, 6, 0]], nnz = 6
let mut row_offsets = DeviceBuffer::from_slice(&ctx, &[0i32, 2, 3, 4, 6])?;
let mut col_indices = DeviceBuffer::from_slice(&ctx, &[0i32, 3, 1, 3, 0, 2])?;
let mut values = DeviceBuffer::from_slice(&ctx, &[1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0])?;
let mut x = DeviceBuffer::from_slice(&ctx, &[1.0f32, 2.0, 3.0, 4.0])?;
let mut y: DeviceBuffer<f32> = DeviceBuffer::zeros(&ctx, 4)?;
let a = SpMat::csr(4, 4, 6, &mut row_offsets, &mut col_indices, &mut values)?;
let x_d = DnVec::new(&mut x)?;
let mut y_d = DnVec::new(&mut y)?;
let bytes = spmv_buffer_size::<f32>(
&handle, Op::N, &1.0, &a, &x_d, &0.0, &y_d, SpMVAlg::Default)?;
let mut workspace: DeviceBuffer<u8> = DeviceBuffer::new(&ctx, bytes.max(1))?;
spmv::<f32>(&handle, Op::N, &1.0, &a, &x_d, &0.0, &mut y_d, SpMVAlg::Default, &mut workspace)?;COO descriptor build — feed an unsorted (row, col, value) triple
stream into cuSPARSE without pre-converting to CSR:
use baracuda_cusparse::SpMat;
use baracuda_driver::{Context, Device, DeviceBuffer};
baracuda_driver::init()?;
let device = Device::get(0)?;
let ctx = Context::new(&device)?;
// Same 4×4 matrix as above, expressed as (row, col, value) triples.
let mut row_idx = DeviceBuffer::from_slice(&ctx, &[0i32, 0, 1, 2, 3, 3])?;
let mut col_idx = DeviceBuffer::from_slice(&ctx, &[0i32, 3, 1, 3, 0, 2])?;
let mut values = DeviceBuffer::from_slice(&ctx, &[1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0])?;
let _coo = SpMat::coo(4, 4, 6, &mut row_idx, &mut col_idx, &mut values)?;
// Pass `_coo` to `spmv`, `spmm`, or `sparse_to_dense` just like a CSR descriptor.SpMM (sparse-times-dense matrix): C = A · B where A is sparse,
B and C are dense column-major.
use baracuda_cusparse::{spmm, spmm_buffer_size, DnMat, Handle, Op, Order, SpMMAlg, SpMat};
use baracuda_driver::{Context, Device, DeviceBuffer};
baracuda_driver::init()?;
let device = Device::get(0)?;
let ctx = Context::new(&device)?;
let handle = Handle::new()?;
// 4×4 CSR A (same as the SpMV example) times a 4×2 dense B → 4×2 dense C.
let mut row_offsets = DeviceBuffer::from_slice(&ctx, &[0i32, 2, 3, 4, 6])?;
let mut col_indices = DeviceBuffer::from_slice(&ctx, &[0i32, 3, 1, 3, 0, 2])?;
let mut values = DeviceBuffer::from_slice(&ctx, &[1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0])?;
let mut b_data: DeviceBuffer<f32> = DeviceBuffer::zeros(&ctx, 4 * 2)?;
let mut c_data: DeviceBuffer<f32> = DeviceBuffer::zeros(&ctx, 4 * 2)?;
let a = SpMat::csr(4, 4, 6, &mut row_offsets, &mut col_indices, &mut values)?;
let b = DnMat::new(4, 2, /* ld */ 4, Order::Col, &mut b_data)?;
let mut c = DnMat::new(4, 2, /* ld */ 4, Order::Col, &mut c_data)?;
let bytes = spmm_buffer_size::<f32>(
&handle, Op::N, Op::N, &1.0, &a, &b, &0.0, &c, SpMMAlg::Default)?;
let mut workspace: DeviceBuffer<u8> = DeviceBuffer::new(&ctx, bytes.max(1))?;
spmm::<f32>(&handle, Op::N, Op::N, &1.0, &a, &b, &0.0, &mut c, SpMMAlg::Default,
&mut workspace)?;Structs§
- DnMat
- Dense matrix descriptor for cuSPARSE’s generic API.
- DnVec
- Dense vector descriptor for cuSPARSE’s generic API.
- Handle
- Owned cuSPARSE handle.
- SpGEMM
Plan - Per-plan handle for a 3-phase SpGEMM computation.
- SpMat
- A sparse-matrix descriptor (CSR / CSC / COO / BSR). The descriptor keeps pointers to externally-owned device buffers; the lifetime parameter ties those buffers to the descriptor.
- SpSM
Plan - Per-plan handle for an SpSM (sparse triangular solve, multiple RHS).
- SpSV
Plan - Per-plan handle for an SpSV (sparse triangular solve, single RHS).
Enums§
- Csr2
CscAlg - Algorithm selector for CSR-to-CSC conversion.
- Diag
- Unit-diagonal flag for triangular sparse matrices.
- Fill
- Triangular storage mode for sparse matrices.
- Index
Base - Zero- vs one-based indexing for sparse-matrix index arrays.
- Op
- Transpose / conjugate-transpose selector.
- Order
- Dense-matrix storage order.
- SDDMM
Alg - Algorithm selector for
cusparseSDDMM. - SpGEMM
Alg - Algorithm selector for
cusparseSpGEMM. - SpMMAlg
- Algorithm selector for
cusparseSpMM. - SpMVAlg
- Algorithm selector for
cusparseSpMV. - SpSMAlg
- Algorithm selector for
cusparseSpSM. - SpSVAlg
- Algorithm selector for
cusparseSpSV.
Traits§
- Sparse
Scalar - Types that cuSPARSE’s generic API accepts as element / compute type.
Functions§
- axpby
- Sparse AXPBY:
y = alpha * x + beta * yover dense vectors (cusparseAxpby). - csr2csc_
ex2 - Convert a CSR matrix to CSC format using the modern Ex2 entry point —
supports algorithm selection (
alg) and arbitrary value types. - csr2csc_
ex2_ buffer_ size - Workspace size in bytes for
csr2csc_ex2. - dense_
to_ sparse_ analysis - Run the dense → sparse analysis phase
(
cusparseDenseToSparse_analysis); fills in the sparsity pattern. - dense_
to_ sparse_ buffer_ size - Query buffer-size for a dense → sparse conversion
(
cusparseDenseToSparse_bufferSize). - dense_
to_ sparse_ convert - Execute the dense → sparse conversion
(
cusparseDenseToSparse_convert); fills in the sparse values. - gather
- Sparse gather:
x = y[indices](cusparseGather). - rot
- Givens rotation applied to a pair of sparse/dense vectors
(
cusparseRot). - scatter
- Sparse scatter:
y[indices] = x(cusparseScatter). - sddmm
- Sampled dense-dense matrix multiply (
cusparseSDDMM): computesC = alpha * op(A) * op(B) + beta * Cat the sparsity pattern ofC. - sddmm_
buffer_ size - Query buffer-size for the sampled dense-dense matmul
(
cusparseSDDMM_bufferSize). - sddmm_
preprocess - One-time preprocessing before
sddmm. Seespmm_preprocessfor the rationale. - sparse_
to_ dense - Convert a sparse matrix into a dense one
(
cusparseSparseToDense). - sparse_
to_ dense_ buffer_ size - Query buffer-size for a sparse → dense conversion
(
cusparseSparseToDense_bufferSize). - spgemm_
compute ⚠ - Phase 2: compute. Same two-step pattern for
buffer2. - spgemm_
copy - Phase 3: write output arrays into the pre-populated output
SpMat. - spgemm_
work_ ⚠estimation - Phase 1: work-estimation. The caller provides
buffer1whose size is returned insize1; passnullthe first time, then allocate & re-call. - spmm
C = alpha * op(A) * op(B) + beta * CwithAsparse,B/Cdense (cusparseSpMM).- spmm_
buffer_ size - Query buffer-size for
C = alpha * op(A) * op(B) + beta * C,Asparse. - spmm_
preprocess - One-time preprocessing before
spmm. Pre-computes algorithm-specific state intoworkspaceso subsequentspmmcalls (with the same A sparsity pattern + dimensions) are faster. Use this when the same matrix is multiplied many times. - spmv
y = alpha * op(A) * x + beta * y.- spmv_
buffer_ size - Query buffer-size for
y = alpha * op(A) * x + beta * y. - spsm_
analysis - Run the SpSM analysis phase
(
cusparseSpSM_analysis) — must be called once beforespsm_solve. - spsm_
buffer_ size - Query buffer-size for a multi-RHS sparse triangular solve
(
cusparseSpSM_bufferSize). - spsm_
solve - Execute the multi-RHS sparse triangular solve
op(A) * C = alpha * op(B)(cusparseSpSM_solve). - spsv_
analysis - Run the SpSV analysis phase
(
cusparseSpSV_analysis) — must be called once beforespsv_solve. - spsv_
buffer_ size - Query buffer-size for a sparse triangular solve
(
cusparseSpSV_bufferSize). - spsv_
solve - Execute the sparse triangular solve
op(A) * y = alpha * x(cusparseSpSV_solve). Requires a priorspsv_analysiscall.
Type Aliases§
- CsrMatrix
- Legacy alias kept for callers from v0.1 — prefer
SpMat::csr. - Dense
Vector - Legacy alias — prefer
DnVec. - Error
- Error type for cuSPARSE operations.
- Result
- Result alias.