NVIDIA cuSPARSE

12.7.9.17 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

Demonstrates how to create and manipulate sparse matrices on an NVIDIA GPU using CuPy, which internally leverages the `nvidia-cusparse` runtime libraries. This includes creating a COO matrix, converting it to CSR format, and performing a matrix-vector multiplication.

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.

view raw JSON →