{"id":5669,"library":"nvidia-cusparse","title":"NVIDIA cuSPARSE","description":"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.","status":"active","version":"12.7.9.17","language":"en","source_language":"en","source_url":"https://docs.nvidia.com/cuda/cusparse/index.html","tags":["NVIDIA","CUDA","GPU","sparse matrix","numerical computing","cuSPARSE"],"install":[{"cmd":"pip install nvidia-cusparse-cu12","lang":"bash","label":"For CUDA 12.x"},{"cmd":"pip install cupy-cuda12x","lang":"bash","label":"Companion library for Python usage"}],"dependencies":[{"reason":"Provides Python bindings and a high-level API for using cuSPARSE functionalities. `nvidia-cusparse` itself does not expose direct Python imports.","package":"cupy","optional":false},{"reason":"Provides core CUDA runtime libraries, often a peer dependency.","package":"nvidia-cuda-runtime","optional":true},{"reason":"Provides BLAS operations, often a peer dependency for numerical libraries.","package":"nvidia-cublas","optional":true}],"imports":[{"note":"`nvidia-cusparse` itself does not provide direct Python symbols for import. It is a low-level C++ library. Python libraries like `cupy` wrap its functionality, with `cupy.sparse` offering high-level Pythonic access to sparse GPU operations. For lower-level access, `cupy.cuda.cusparse` can be used.","symbol":"cupy.sparse","correct":"import cupy.sparse as csp"}],"quickstart":{"code":"import cupy as cp\nimport cupy.sparse as csp\n\n# Create a sparse matrix on GPU using CuPy, which utilizes cuSPARSE internally.\n# Example: Coordinate format (COO)\nrow = cp.array([0, 1, 2, 0])\ncol = cp.array([0, 1, 2, 2])\ndata = cp.array([1.0, 2.0, 3.0, 4.0])\nshape = (3, 3)\n\ncoo_matrix = csp.coo_matrix((data, (row, col)), shape=shape)\n\nprint(\"Sparse COO Matrix on GPU:\")\nprint(coo_matrix)\nprint(f\"Number of non-zero elements: {coo_matrix.nnz}\")\n\n# Convert to Compressed Sparse Row (CSR) format\ncsr_matrix = coo_matrix.tocsr()\nprint(\"\\nSparse CSR Matrix on GPU:\")\nprint(csr_matrix)\n\n# Perform a simple operation, e.g., matrix-vector multiplication\nvec = cp.array([10., 20., 30.])\nresult = csr_matrix @ vec\nprint(\"\\nMatrix-vector multiplication result:\")\nprint(result)\n\n# Note: The `nvidia-cusparse` package provides the underlying CUSPARSE runtime libraries.\n# Higher-level libraries like CuPy wrap these for Python usage.","lang":"python","description":"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."},"warnings":[{"fix":"Use a wrapping library like `cupy` (e.g., `import cupy.sparse`) to interact with cuSPARSE functionality in Python.","message":"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.","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure the installed `nvidia-cusparse` package matches your system's CUDA toolkit version. Consult NVIDIA's documentation or the respective Python library's (e.g., CuPy) installation guide.","message":"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.","severity":"gotcha","affected_versions":"All"},{"fix":"Familiarize yourself with sparse matrix theory and best practices for GPU computing. Utilize performance profiling tools to identify bottlenecks in your sparse GPU workloads.","message":"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.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}