Sparse matrices (cupyx.scipy.sparse)#
CuPy supports sparse matrices using cuSPARSE. These matrices have the same interfaces of SciPy’s sparse matrices.
Index dtype (int32 / int64)#
Like SciPy, CuPy sparse matrices automatically choose the index
dtype (indices, indptr, row, col) based on the
matrix dimensions and index values:
int32 when all index values and dimensions fit in a 32-bit integer (the common case).
int64 when any dimension or index value exceeds
2**31 - 1.
The dtype is chosen by get_index_dtype (mirroring SciPy’s logic)
and is preserved through format conversions, arithmetic, and indexing.
Operations that delegate to cuSPARSE use the native Generic API
(SpMatDescr) for int64 where available, with pure-CuPy
fallbacks for legacy int32-only APIs (e.g., csr2cscEx2,
xcoo2csr, csrgeam2).
Known limitations#
The following operations are int32-only and raise ValueError
when called on a matrix with int64 indices:
cupyx.scipy.sparse.linalg.spsolve()– the underlyingcusolverSp<t>csrlsvqrroutine has no int64 overload.cupyx.scipy.sparse.linalg.spilu()– the underlyingcusparse<t>csrilu02routine has no int64 overload.cupyx.scipy.sparse.linalg.spsolve_triangular()on CUDA builds older than 12.0, where the dispatch falls back tocusparse<t>csrsm2. On CUDA 12.0+ it usescusparseSpSM(Generic API), which supports int64.
Conversion to/from SciPy sparse matrices#
cupyx.scipy.sparse.*_matrix and scipy.sparse.*_matrix are not implicitly convertible to each other.
That means, SciPy functions cannot take cupyx.scipy.sparse.*_matrix objects as inputs, and vice versa.
To convert SciPy sparse matrices to CuPy, pass it to the constructor of each CuPy sparse matrix class.
To convert CuPy sparse matrices to SciPy, use
getmethod of each CuPy sparse matrix class.
Note that converting between CuPy and SciPy incurs data transfer between the host (CPU) device and the GPU device, which is costly in terms of performance.
Conversion to/from CuPy ndarrays#
To convert CuPy ndarray to CuPy sparse matrices, pass it to the constructor of each CuPy sparse matrix class.
To convert CuPy sparse matrices to CuPy ndarray, use
toarrayof each CuPy sparse matrix instance (e.g.,cupyx.scipy.sparse.csr_matrix.toarray()).
Converting between CuPy ndarray and CuPy sparse matrices does not incur data transfer; it is copied inside the GPU device.
Contents#
Sparse matrix classes#
|
COOrdinate format sparse matrix. |
|
Compressed Sparse Column matrix. |
|
Compressed Sparse Row matrix. |
|
Sparse matrix with DIAgonal storage. |
|
Base class of all sparse matrixes. |
Functions#
Building sparse matrices:
|
Creates a sparse matrix with ones on diagonal. |
|
Creates an identity matrix in sparse format. |
|
Kronecker product of sparse matrices A and B. |
|
Kronecker sum of sparse matrices A and B. |
|
Construct a sparse matrix from diagonals. |
|
Creates a sparse matrix from diagonals. |
|
Returns the lower triangular portion of a matrix in sparse format |
|
Returns the upper triangular portion of a matrix in sparse format |
|
Builds a sparse matrix from sparse sub-blocks |
|
Stacks sparse matrices horizontally (column wise) |
|
Stacks sparse matrices vertically (row wise) |
|
Generates a random sparse matrix. |
|
Generates a random sparse matrix. |
Sparse matrix tools:
|
Returns the indices and values of the nonzero elements of a matrix |
Identifying sparse matrices:
|
Checks if a given matrix is a sparse matrix. |
|
Checks if a given matrix is a sparse matrix. |
Checks if a given matrix is of CSC format. |
|
Checks if a given matrix is of CSR format. |
|
Checks if a given matrix is of COO format. |
|
Checks if a given matrix is of DIA format. |