NVIDIA cuSPARSE
nvidia-cusparse provides the native NVIDIA cuSPARSE runtime libraries, offering highly optimized routines for sparse matrix computations on NVIDIA GPUs. It is a foundational component for other Python libraries like CuPy to enable sparse GPU operations. The current version is 12.7.9.17, and it typically releases alongside new CUDA toolkit versions.
Warnings
- gotcha The `nvidia-cusparse` package does not expose direct Python importable modules. It provides the underlying C++ shared libraries that other Python packages (e.g., `cupy`) link against. Users should access cuSPARSE functionality via such higher-level libraries.
- gotcha Installation typically requires specifying the CUDA toolkit version (e.g., `nvidia-cusparse-cu12` for CUDA 12.x). Installing the generic `nvidia-cusparse` or a version mismatched with your system's CUDA toolkit can lead to runtime errors or degraded GPU acceleration.
- gotcha Effective use of cuSPARSE requires understanding sparse matrix storage formats (e.g., CSR, CSC, COO) and their implications for GPU memory access patterns and computational efficiency. Naive usage without considering these aspects can lead to sub-optimal performance.
Install
-
pip install nvidia-cusparse-cu12 -
pip install cupy-cuda12x
Imports
- cupy.sparse
import cupy.sparse as csp
Quickstart
import cupy as cp
import cupy.sparse as csp
# Create a sparse matrix on GPU using CuPy, which utilizes cuSPARSE internally.
# Example: Coordinate format (COO)
row = cp.array([0, 1, 2, 0])
col = cp.array([0, 1, 2, 2])
data = cp.array([1.0, 2.0, 3.0, 4.0])
shape = (3, 3)
coo_matrix = csp.coo_matrix((data, (row, col)), shape=shape)
print("Sparse COO Matrix on GPU:")
print(coo_matrix)
print(f"Number of non-zero elements: {coo_matrix.nnz}")
# Convert to Compressed Sparse Row (CSR) format
csr_matrix = coo_matrix.tocsr()
print("\nSparse CSR Matrix on GPU:")
print(csr_matrix)
# Perform a simple operation, e.g., matrix-vector multiplication
vec = cp.array([10., 20., 30.])
result = csr_matrix @ vec
print("\nMatrix-vector multiplication result:")
print(result)
# Note: The `nvidia-cusparse` package provides the underlying CUSPARSE runtime libraries.
# Higher-level libraries like CuPy wrap these for Python usage.