1. Introduction

The cuSPARSE library contains a set of basic linear algebra subroutines used for handling sparse matrices. It is implemented on top of the NVIDIA® CUDA™ runtime (which is part of the CUDA Toolkit) and is designed to be called from C and C++. The library routines can be classified into four categories:

  • Level 1: operations between a vector in sparse format and a vector in dense format
  • Level 2: operations between a matrix in sparse format and a vector in dense format
  • Level 3: operations between a matrix in sparse format and a set of vectors in dense format (which can also usually be viewed as a dense tall matrix)
  • Conversion: operations that allow conversion between different matrix formats

The cuSPARSE library allows developers to access the computational resources of the NVIDIA graphics processing unit (GPU), although it does not auto-parallelize across multiple GPUs. The cuSPARSE API assumes that input and output data reside in GPU (device) memory, unless it is explicitly indicated otherwise by the string DevHostPtr in a function parameter's name (for example, the parameter *resultDevHostPtr in the function cusparse<t>doti()).

It is the responsibility of the developer to allocate memory and to copy data between GPU memory and CPU memory using standard CUDA runtime API routines, such as cudaMalloc(), cudaFree(), cudaMemcpy(), and cudaMemcpyAsync().

Note: The cuSPARSE library requires hardware with compute capability (CC) of at least 1.1 or higher. Please see the NVIDIA CUDA C Programming Guide, Appendix A for a list of the compute capabilities corresponding to all NVIDIA GPUs.

1.1. Naming Conventions

The cuSPARSE library functions are available for data types float, double, cuComplex, and cuDoubleComplex. The sparse Level 1, Level 2, and Level 3 functions follow this naming convention:

cusparse<t>[<matrix data format>]<operation>[<output matrix data format>]

where <t> can be S, D, C, Z, or X, corresponding to the data types float, double, cuComplex, cuDoubleComplex, and the generic type, respectively.

The <matrix data format> can be dense, coo, csr, csc, or hyb, corresponding to the dense, coordinate, compressed sparse row, compressed sparse column, and hybrid storage formats, respectively.

Finally, the <operation> can be axpyi, doti, dotci, gthr, gthrz, roti, or sctr, corresponding to the Level 1 functions; it also can be mv or sv, corresponding to the Level 2 functions, as well as mm or sm, corresponding to the Level 3 functions.

All of the functions have the return type cusparseStatus_t and are explained in more detail in the chapters that follow.

1.2. Asynchronous Execution

The cuSPARSE library functions are executed asynchronously with respect to the host and may return control to the application on the host before the result is ready. Developers can use the cudaDeviceSynchronize() function to ensure that the execution of a particular cuSPARSE library routine has completed.

A developer can also use the cudaMemcpy() routine to copy data from the device to the host and vice versa, using the cudaMemcpyDeviceToHost and cudaMemcpyHostToDevice parameters, respectively. In this case there is no need to add a call to cudaDeviceSynchronize() because the call to cudaMemcpy() with the above parameters is blocking and completes only when the results are ready on the host.

Static Library support

Starting with release 6.5, the cuSPARSE Library is also delivered in a static form as libcusparse_static.a on Linux and Mac OSes. The static cuSPARSE library and all others static maths libraries depend on a common thread abstraction layer library called libculibos.a on Linux and Mac and culibos.lib on Windows.

For example, on linux, to compile a small application using cuSPARSE against the dynamic library, the following command can be used:

    gcc myCublasApp.c    -lcusparse  -o myCublasApp 

Whereas to compile against the static cuSPARSE library, the following command has to be used:

     
    gcc myCublasApp.c    libcusparse_static.a   libculibos.a  -o myCublasApp

2. Using the cuSPARSE API

This chapter describes how to use the cuSPARSE library API. It is not a reference for the cuSPARSE API data types and functions; that is provided in subsequent chapters.

2.1. Thread Safety

The library is thread safe and its functions can be called from multiple host threads.

2.2. Scalar Parameters

In the cuSPARSE API, the scalar parameters α and β can be passed by reference on the host or the device.

The few functions that return a scalar result, such as doti() and nnz(), return the resulting value by reference on the host or the device. Even though these functions return immediately, similarly to those that return matrix and vector results, the scalar result is not ready until execution of the routine on the GPU completes. This requires proper synchronization be used when reading the result from the host.

This feature allows the cuSPARSE library functions to execute completely asynchronously using streams, even when α and β are generated by a previous kernel. This situation arises, for example, when the library is used to implement iterative methods for the solution of linear systems and eigenvalue problems [3].

2.3. Parallelism with Streams

If the application performs several small independent computations, or if it makes data transfers in parallel with the computation, CUDA streams can be used to overlap these tasks.

The application can conceptually associate a stream with each task. To achieve the overlap of computation between the tasks, the developer should create CUDA streams using the function cudaStreamCreate() and set the stream to be used by each individual cuSPARSE library routine by calling cusparseSetStream() just before calling the actual cuSPARSE routine. Then, computations performed in separate streams would be overlapped automatically on the GPU, when possible. This approach is especially useful when the computation performed by a single task is relatively small and is not enough to fill the GPU with work, or when there is a data transfer that can be performed in parallel with the computation.

When streams are used, we recommend using the new cuSPARSE API with scalar parameters and results passed by reference in the device memory to achieve maximum computational overlap.

Although a developer can create many streams, in practice it is not possible to have more than 16 concurrent kernels executing at the same time.

3. cuSPARSE Indexing and Data Formats

The cuSPARSE library supports dense and sparse vector, and dense and sparse matrix formats.

3.1. Index Base Format

The library supports zero- and one-based indexing. The index base is selected through the cusparseIndexBase_t type, which is passed as a standalone parameter or as a field in the matrix descriptor cusparseMatDescr_t type.

3.2. Vector Formats

This section describes dense and sparse vector formats.

3.2.1. Dense Format

Dense vectors are represented with a single data array that is stored linearly in memory, such as the following 7 × 1 dense vector.

1.0 0.0 0.0 2.0 3.0 0.0 4.0

(This vector is referenced again in the next section.)

3.2.2. Sparse Format

Sparse vectors are represented with two arrays.

  • The data array has the nonzero values from the equivalent array in dense format.

  • The integer index array has the positions of the corresponding nonzero values in the equivalent array in dense format.

For example, the dense vector in section 3.2.1 can be stored as a sparse vector with one-based indexing.

1.0 2.0 3.0 4.0 1 .0 4 .0 5 .0 7 .0

It can also be stored as a sparse vector with zero-based indexing.

1.0 2.0 3.0 4.0 0 .0 3 .0 4 .0 6 .0

In each example, the top row is the data array and the bottom row is the index array, and it is assumed that the indices are provided in increasing order and that each index appears only once.

3.3. Matrix Formats

Dense and several sparse formats for matrices are discussed in this section.

3.3.1. Dense Format

The dense matrix X is assumed to be stored in column-major format in memory and is represented by the following parameters.

m (integer) The number of rows in the matrix.
n (integer) The number of columns in the matrix.
ldX (integer) The leading dimension of X, which must be greater than or equal to m. If ldX is greater than m, then X represents a sub-matrix of a larger matrix stored in memory
X (pointer) Points to the data array containing the matrix elements. It is assumed that enough storage is allocated for X to hold all of the matrix elements and that cuSPARSE library functions may access values outside of the sub-matrix, but will never overwrite them.

For example, m×n dense matrix X with leading dimension ldX can be stored with one-based indexing as shown.

X 1 , 1 X 1 , 2 X 1 , n X 2 , 1 X 2 , 2 X 2 , n X m , 1 X m , 2 X m , n X l d X , 1 X l d X , 2 X l d X , n

Its elements are arranged linearly in memory in the order below.

X 1 , 1 X 2 , 1 X m , 1 X l d X , 1 X 1 , n X 2 , n X m , n X l d X , n

Note: This format and notation are similar to those used in the NVIDIA CUDA cuBLAS library.

3.3.2. Coordinate Format (COO)

The m×n sparse matrix A is represented in COO format by the following parameters.

nnz (integer) The number of nonzero elements in the matrix.
cooValA (pointer) Points to the data array of length nnz that holds all nonzero values of A in row-major format.
cooRowIndA (pointer) Points to the integer array of length nnz that contains the row indices of the corresponding elements in array cooValA.
cooColIndA (pointer) Points to the integer array of length nnz that contains the column indices of the corresponding elements in array cooValA.

A sparse matrix in COO format is assumed to be stored in row-major format: the index arrays are first sorted by row indices and then within the same row by compressed column indices. It is assumed that each pair of row and column indices appears only once.

For example, consider the following 4 × 5 matrix A.

1.0 4.0 0.0 0.0 0.0 0.0 2.0 3.0 0.0 0.0 5.0 0.0 0.0 7.0 8.0 0.0 0.0 9.0 0.0 6.0

It is stored in COO format with zero-based indexing this way.

cooValA = 1.0 4.0 2.0 3.0 5.0 7.0 8.0 9.0 6.0 cooRowIndA = 0 .0 0 .0 1 .0 1 .0 2 .0 2 .0 2 .0 3 .0 3 .0 cooColIndA = 0 .0 1 .0 1 .0 2 .0 0 .0 3 .0 4 .0 2 .0 4 .0

In the COO format with one-based indexing, it is stored as shown.

cooValA = 1.0 4.0 2.0 3.0 5.0 7.0 8.0 9.0 6.0 cooRowIndA = 1 .0 1 .0 2 .0 2 .0 3 .0 3 .0 3 .0 4 .0 4 .0 cooColIndA = 1 .0 2 .0 2 .0 3 .0 1 .0 4 .0 5 .0 3 .0 5 .0

3.3.3. Compressed Sparse Row Format (CSR)

The only way the CSR differs from the COO format is that the array containing the row indices is compressed in CSR format. The m×n sparse matrix A is represented in CSR format by the following parameters.

nnz (integer) The number of nonzero elements in the matrix.
csrValA (pointer) Points to the data array of length nnz that holds all nonzero values of A in row-major format.
csrRowPtrA (pointer) Points to the integer array of length m+1 that holds indices into the arrays csrColIndA and csrValA. The first m entries of this array contain the indices of the first nonzero element in the ith row for i=i,...,m, while the last entry contains nnz+csrRowPtrA(0). In general, csrRowPtrA(0) is 0 or 1 for zero- and one-based indexing, respectively.
csrColIndA (pointer) Points to the integer array of length nnz that contains the column indices of the corresponding elements in array csrValA.

Sparse matrices in CSR format are assumed to be stored in row-major CSR format, in other words, the index arrays are first sorted by row indices and then within the same row by column indices. It is assumed that each pair of row and column indices appears only once.

Consider again the 4 × 5 matrixA.

1.0 4.0 0.0 0.0 0.0 0.0 2.0 3.0 0.0 0.0 5.0 0.0 0.0 7.0 8.0 0.0 0.0 9.0 0.0 6.0

It is stored in CSR format with zero-based indexing as shown.

csrValA = 1.0 4.0 2.0 3.0 5.0 7.0 8.0 9.0 6.0 csrRowPtrA = 0 .0 2 .0 4 .0 7 .0 9 .0 csrColIndA = 0 .0 1 .0 1 .0 2 .0 0 .0 3 .0 4 .0 2 .0 4 .0

This is how it is stored in CSR format with one-based indexing.

csrValA = 1.0 4.0 2.0 3.0 5.0 7.0 8.0 9.0 6.0 csrRowPtrA = 1 .0 3 .0 5 .0 8 .0 10 .0 csrColIndA = 1 .0 2 .0 2 .0 3 .0 1 .0 4 .0 5 .0 3 .0 5 .0

3.3.4. Compressed Sparse Column Format (CSC)

The CSC format is different from the COO format in two ways: the matrix is stored in column-major format, and the array containing the column indices is compressed in CSC format. The m×n matrix A is represented in CSC format by the following parameters.

nnz (integer) The number of nonzero elements in the matrix.
cscValA (pointer) Points to the data array of length nnz that holds all nonzero values of A in column-major format.
cscRowIndA (pointer) Points to the integer array of length nnz that contains the row indices of the corresponding elements in array cscValA.
cscColPtrA (pointer) Points to the integer array of length n+1 that holds indices into the arrays cscRowIndA and cscValA. The first n entries of this array contain the indices of the first nonzero element in the ith row for i=i,...,n, while the last entry contains nnz+cscColPtrA(0). In general, cscColPtrA(0) is 0 or 1 for zero- and one-based indexing, respectively.
Note: The matrix A in CSR format has exactly the same memory layout as its transpose in CSC format (and vice versa).

For example, consider once again the 4 × 5 matrix A.

1.0 4.0 0.0 0.0 0.0 0.0 2.0 3.0 0.0 0.0 5.0 0.0 0.0 7.0 8.0 0.0 0.0 9.0 0.0 6.0

It is stored in CSC format with zero-based indexing this way.

cscValA = 1.0 5.0 4.0 2.0 3.0 9.0 7.0 8.0 6.0 cscRowIndA = 0 .0 2 .0 0 .0 1 .0 1 .0 3 .0 2 .0 2 .0 3 .0 cscColPtrA = 0 .0 2 .0 4 .0 6 .0 7 .0 9 .0

In CSC format with one-based indexing, this is how it is stored.

cscValA = 1.0 5.0 4.0 2.0 3.0 9.0 7.0 8.0 6.0 cscRowIndA = 1 .0 3 .0 1 .0 2 .0 2 .0 4 .0 3 .0 3 .0 4 .0 cscColPtrA = 1 .0 3 .0 5 .0 7 .0 8 .0 10 .0

Each pair of row and column indices appears only once.

3.3.5. Ellpack-Itpack Format (ELL)

An m×n sparse matrix A with at most k nonzero elements per row is stored in the Ellpack-Itpack (ELL) format [2] using two dense arrays of dimension m×k. The first data array contains the values of the nonzero elements in the matrix, while the second integer array contains the corresponding column indices.

For example, consider the 4 × 5 matrix A.

1.0 4.0 0.0 0.0 0.0 0.0 2.0 3.0 0.0 0.0 5.0 0.0 0.0 7.0 8.0 0.0 0.0 9.0 0.0 6.0

This is how it is stored in ELL format with zero-based indexing.

data = 1.0 4.0 0.0 2.0 3.0 0.0 5.0 7.0 8.0 9.0 6.0 0.0 indices = 0 .0 1 .0 1 . 1 .0 2 .0 1 . 0 .0 3 .0 4 . 2 .0 4 .0 1 .

It is stored this way in ELL format with one-based indexing.

data = 1.0 4.0 0.0 2.0 3.0 0.0 5.0 7.0 8.0 9.0 6.0 0.0 indices = 1 .0 2 .0 1 . 2 .0 3 .0 1 . 1 .0 4 .0 5 . 3 .0 5 .0 1 .

Sparse matrices in ELL format are assumed to be stored in column-major format in memory. Also, rows with less than k nonzero elements are padded in the data and indices arrays with zero and 1 , respectively.

The ELL format is not supported directly, but it is used to store the regular part of the matrix in the HYB format that is described in the next section.

3.3.6. Hybrid Format (HYB)

The HYB sparse storage format is composed of a regular part, usually stored in ELL format, and an irregular part, usually stored in COO format [1]. The ELL and COO parts are always stored using zero-based indexing. HYB is implemented as an opaque data format that requires the use of a conversion operation to store a matrix in it. The conversion operation partitions the general matrix into the regular and irregular parts automatically or according to developer-specified criteria.

For more information, please refer to the description of cusparseHybPartition_t type, as well as the description of the conversion routines dense2hyb, csc2hyb and csr2hyb.

3.3.7. Block Compressed Sparse Row Format (BSR)

The only difference between the CSR and BSR formats is the format of the storage element. The former stores primitive data types (single, double, cuComplex, and cuDoubleComplex) whereas the latter stores a two-dimensional square block of primitive data types. The dimension of the square block is b l o c k D i m . The m×n sparse matrix A is equivalent to a block sparse matrix A b with m b = m + b l o c k D i m 1 b l o c k D i m block rows and n b = n + b l o c k D i m 1 b l o c k D i m block columns. If m or n is not multiple of b l o c k D i m , then zeros are filled into A b .

A is represented in BSR format by the following parameters.

blockDim (integer) Block dimension of matrix A.
mb (integer) The number of block rows of A.
nb (integer) The number of block columns of A.
nnzb (integer) The number of nonzero blocks in the matrix.
bsrValA (pointer) Points to the data array of length n n z b b l o c k D i m 2 that holds all elements of nonzero blocks of A. The block elements are stored in either column-major order or row-major order.
bsrRowPtrA (pointer) Points to the integer array of length mb+1 that holds indices into the arrays bsrColIndA and bsrValA. The first mb entries of this array contain the indices of the first nonzero block in the ith block row for i=1,...,mb, while the last entry contains nnzb+bsrRowPtrA(0). In general, bsrRowPtrA(0) is 0 or 1 for zero- and one-based indexing, respectively.
bsrColIndA (pointer) Points to the integer array of length nnzb that contains the column indices of the corresponding blocks in array bsrValA.

As with CSR format, (row, column) indices of BSR are stored in row-major order. The index arrays are first sorted by row indices and then within the same row by column indices.

For example, consider again the 4×5 matrix A.

1.0 4.0 0.0 0.0 0.0 0.0 2.0 3.0 0.0 0.0 5.0 0.0 0.0 7.0 8.0 0.0 0.0 9.0 0.0 6.0

If b l o c k D i m is equal to 2, then m b is 2, n b is 3, and matrix A is split into 2×3 block matrix A b . The dimension of A b is 4×6, slightly bigger than matrix A , so zeros are filled in the last column of A b . The element-wise view of A b is this.

1.0 4.0 0.0 0.0 0.0 0.0 0.0 2.0 3.0 0.0 0.0 0.0 5.0 0.0 0.0 7.0 8.0 0.0 0.0 0.0 9.0 0.0 6.0 0.0

Based on zero-based indexing, the block-wise view of A b can be represented as follows.

A b = A 00 A 01 A 02 A 10 A 11 A 12

The basic element of BSR is a nonzero A i j block, one that contains at least one nonzero element of A. Five of six blocks are nonzero in A b .

A 00 = 1 4 0 2 , A 01 = 0 0 3 0 , A 10 = 5 0 0 0 , A 11 = 0 7 9 0 , A 12 = 8 0 6 0

BSR format only stores the information of nonzero blocks, including block indices ( i , j ) and values A i j . Also row indices are compressed in CSR format.

bsrValA = A 00 A 01 A 10 A 11 A 12 bsrRowPtrA = 0 .0 2 .0 5 bsrColIndA = 0 .0 1 .0 0 .0 1 .0 2

There are two ways to arrange the data element of block A i j : row-major order and column-major order. Under column-major order, the physical storage of bsrValA is this.

b s r V a l A = [ 1 .0 0 .0 4 .0 2 .0 0 .0 3 .0 0 .0 0 .0 5 .0 0 .0 0 .0 0 .0 0 .0 9 .0 7 .0 0 .0 8 .0 6 .0 0 .0 0 .0 ]

Under row-major order, the physical storage of bsrValA is this.

b s r V a l A = [ 1 .0 4 .0 0 .0 2 .0 0 .0 0 .0 3 .0 0 .0 5 .0 0 .0 0 .0 0 .0 0 .0 7 .0 9 .0 0 .0 8 .0 0 .0 6 .0 0 .0 ]

Similarly, in BSR format with one-based indexing and column-major order, A can be represented by the following.

A b = A 11 A 12 A 13 A 21 A 22 A 23
b s r V a l A = [ 1 .0 0 .0 4 .0 2 .0 0 .0 3 .0 0 .0 0 .0 5 .0 0 .0 0 .0 0 .0 0 .0 9 .0 7 .0 0 .0 8 .0 6 .0 0 .0 0 .0 ]

bsrRowPtrA = 1 .0 3 .0 6 bsrColIndA = 1 .0 2 .0 1 .0 2 .0 3

Note: The general BSR format has two parameters, rowBlockDim and colBlockDim. rowBlockDim is number of rows within a block and colBlockDim is number of columns within a block. If rowBlockDim=colBlockDim, general BSR format is the same as BSR format. If rowBlockDim=colBlockDim=1, general BSR format is the same as CSR format. The conversion routine gebsr2gebsr is used to do conversion among CSR, BSR and general BSR.
Note: In the cuSPARSE Library, the storage format of blocks in BSR format can be column-major or row-major, independently of the base index. However, if the developer uses BSR format from the Math Kernel Library (MKL) and wants to directly interface with the cuSPARSE Library, then cusparseDirection_tCUSPARSE_DIRECTION_COLUMN should be used if the base index is one; otherwise, cusparseDirection_tCUSPARSE_DIRECTION_ROW should be used.

3.3.8. Extended BSR Format (BSRX)

BSRX is the same as the BSR format, but the array bsrRowPtrA is separated into two parts. The first nonzero block of each row is still specified by the array bsrRowPtrA, which is the same as in BSR, but the position next to the last nonzero block of each row is specified by the array bsrEndPtrA. Briefly, BSRX format is simply like a 4-vector variant of BSR format.

Matrix A is represented in BSRX format by the following parameters.

blockDim (integer) Block dimension of matrix A.
mb (integer) The number of block rows of A.
nb (integer) The number of block columns of A.
nnzb (integer) number of nonzero blocks in the matrix A.
bsrValA (pointer) Points to the data array of length n n z b b l o c k D i m 2 that holds all the elements of the nonzero blocks of A. The block elements are stored in either column-major order or row-major order.
bsrRowPtrA (pointer) Points to the integer array of length mb that holds indices into the arrays bsrColIndA and bsrValA; bsrRowPtrA(i) is the position of the first nonzero block of the ith block row in bsrColIndA and bsrValA.
bsrEndPtrA (pointer) Points to the integer array of length mb that holds indices into the arrays bsrColIndA and bsrValA; bsrRowPtrA(i) is the position next to the last nonzero block of the ith block row in bsrColIndA and bsrValA.
bsrColIndA (pointer) Points to the integer array of length nnzb that contains the column indices of the corresponding blocks in array bsrValA.

A simple conversion between BSR and BSRX can be done as follows. Suppose the developer has a 2×3 block sparse matrix A b represented as shown.

A b = A 00 A 01 A 02 A 10 A 11 A 12

Assume it has this BSR format.

bsrValA of BSR = A 00 A 01 A 10 A 11 A 12 bsrRowPtrA of BSR = 0 .0 2 .0 5 bsrColIndA of BSR = 0 .0 1 .0 0 .0 1 .0 2

The bsrRowPtrA of the BSRX format is simply the first two elements of the bsrRowPtrA BSR format. The bsrEndPtrA of BSRX format is the last two elements of the bsrRowPtrA of BSR format.

bsrRowPtrA of BSRX = 0 .0 2 bsrEndPtrA of BSRX = 2 .0 5

The advantage of the BSRX format is that the developer can specify a submatrix in the original BSR format by modifying bsrRowPtrA and bsrEndPtrA while keeping bsrColIndA and bsrValA unchanged.

For example, to create another block matrix A ˜ = O O O O A 11 O that is slightly different from A , the developer can keep bsrColIndA and bsrValA, but reconstruct A ˜ by properly setting of bsrRowPtrA and bsrEndPtrA. The following 4-vector characterizes A ˜ .

bsrValA of  A ˜ = A 00 A 01 A 10 A 11 A 12 bsrColIndA of  A ˜ = 0 .0 1 .0 0 .0 1 .0 2 bsrRowPtrA of  A ˜ = 0 .0 3 bsrEndPtrA of  A ˜ = 0 .0 4

4. cuSPARSE Types Reference

4.1. Data types

The float, double, cuComplex, and cuDoubleComplex data types are supported. The first two are standard C data types, while the last two are exported from cuComplex.h.

4.2. cusparseAction_t

This type indicates whether the operation is performed only on indices or on data and indices.

Value Meaning

CUSPARSE_ACTION_SYMBOLIC

the operation is performed only on indices.

CUSPARSE_ACTION_NUMERIC

the operation is performed on data and indices.

4.3. cusparseDirection_t

This type indicates whether the elements of a dense matrix should be parsed by rows or by columns (assuming column-major storage in memory of the dense matrix) in function cusparse[S|D|C|Z]nnz. Besides storage format of blocks in BSR format is also controlled by this type.

Value Meaning

CUSPARSE_DIRECTION_ROW

the matrix should be parsed by rows.

CUSPARSE_DIRECTION_COLUMN

the matrix should be parsed by columns.

4.4. cusparseHandle_t

This is a pointer type to an opaque cuSPARSE context, which the user must initialize by calling prior to calling cusparseCreate() any other library function. The handle created and returned by cusparseCreate() must be passed to every cuSPARSE function.

4.5. cusparseHybMat_t

This is a pointer type to an opaque structure holding the matrix in HYB format, which is created by cusparseCreateHybMat and destroyed by cusparseDestroyHybMat.

4.5.1. cusparseHybPartition_t

This type indicates how to perform the partitioning of the matrix into regular (ELL) and irregular (COO) parts of the HYB format.

The partitioning is performed during the conversion of the matrix from a dense or sparse format into the HYB format and is governed by the following rules. When CUSPARSE_HYB_PARTITION_AUTO is selected, the cuSPARSE library automatically decides how much data to put into the regular and irregular parts of the HYB format. When CUSPARSE_HYB_PARTITION_USER is selected, the width of the regular part of the HYB format should be specified by the caller. When CUSPARSE_HYB_PARTITION_MAX is selected, the width of the regular part of the HYB format equals to the maximum number of non-zero elements per row, in other words, the entire matrix is stored in the regular part of the HYB format.

The default is to let the library automatically decide how to split the data.

Value Meaning

CUSPARSE_HYB_PARTITION_AUTO

the automatic partitioning is selected (default).

CUSPARSE_HYB_PARTITION_USER

the user specified treshold is used.

CUSPARSE_HYB_PARTITION_MAX

the data is stored in ELL format.

4.6. cusparseMatDescr_t

This structure is used to describe the shape and properties of a matrix.

typedef struct {
    cusparseMatrixType_t MatrixType;
    cusparseFillMode_t FillMode;
    cusparseDiagType_t DiagType;
    cusparseIndexBase_t IndexBase;
} cusparseMatDescr_t;

4.6.1. cusparseDiagType_t

This type indicates if the matrix diagonal entries are unity. The diagonal elements are always assumed to be present, but if CUSPARSE_DIAG_TYPE_UNIT is passed to an API routine, then the routine assumes that all diagonal entries are unity and will not read or modify those entries. Note that in this case the routine assumes the diagonal entries are equal to one, regardless of what those entries are actually set to in memory.

Value Meaning

CUSPARSE_DIAG_TYPE_NON_UNIT

the matrix diagonal has non-unit elements.

CUSPARSE_DIAG_TYPE_UNIT

the matrix diagonal has unit elements.

4.6.2. cusparseFillMode_t

This type indicates if the lower or upper part of a matrix is stored in sparse storage.

Value Meaning

CUSPARSE_FILL_MODE_LOWER

the lower triangular part is stored.

CUSPARSE_FILL_MODE_UPPER

the upper triangular part is stored.

4.6.3. cusparseIndexBase_t

This type indicates if the base of the matrix indices is zero or one.

Value Meaning

CUSPARSE_INDEX_BASE_ZERO

the base index is zero.

CUSPARSE_INDEX_BASE_ONE

the base index is one.

4.6.4. cusparseMatrixType_t

This type indicates the type of matrix stored in sparse storage. Notice that for symmetric, Hermitian and triangular matrices only their lower or upper part is assumed to be stored.

Value Meaning

CUSPARSE_MATRIX_TYPE_GENERAL

the matrix is general.

CUSPARSE_MATRIX_TYPE_SYMMETRIC

the matrix is symmetric.

CUSPARSE_MATRIX_TYPE_HERMITIAN

the matrix is Hermitian.

CUSPARSE_MATRIX_TYPE_TRIANGULAR

the matrix is triangular.

4.7. cusparseOperation_t

This type indicates which operations need to be performed with the sparse matrix.

Value Meaning

CUSPARSE_OPERATION_NON_TRANSPOSE

the non-transpose operation is selected.

CUSPARSE_OPERATION_TRANSPOSE

the transpose operation is selected.

CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE

the conjugate transpose operation is selected.

4.8. cusparsePointerMode_t

This type indicates whether the scalar values are passed by reference on the host or device. It is important to point out that if several scalar values are passed by reference in the function call, all of them will conform to the same single pointer mode. The pointer mode can be set and retrieved using cusparseSetPointerMode() and cusparseGetPointerMode() routines, respectively.

Value Meaning

CUSPARSE_POINTER_MODE_HOST

the scalars are passed by reference on the host.

CUSPARSE_POINTER_MODE_DEVICE

the scalars are passed by reference on the device.

4.9. cusparseSolvePolicy_t

This type indicates whether level information is generated and used in csrsv2, csric02, csrilu02, bsrsv2, bsric02 and bsrilu02.

Value Meaning

CUSPARSE_SOLVE_POLICY_NO_LEVEL

no level information is generated and used.

CUSPARSE_SOLVE_POLICY_USE_LEVEL

generate and use level information.

4.10. cusparseSolveAnalysisInfo_t

This is a pointer type to an opaque structure holding the information collected in the analysis phase of the solution of the sparse triangular linear system. It is expected to be passed unchanged to the solution phase of the sparse triangular linear system.

4.11. csrsv2Info_t

This is a pointer type to an opaque structure holding the information used in csrsv2_bufferSize(), csrsv2_analysis(), and csrsv2_solve().

4.12. csric02Info_t

This is a pointer type to an opaque structure holding the information used in csric02_bufferSize(), csric02_analysis(), and csric02().

4.13. csrilu02Info_t

This is a pointer type to an opaque structure holding the information used in csrilu02_bufferSize(), csrilu02_analysis(), and csrilu02().

4.14. bsrsv2Info_t

This is a pointer type to an opaque structure holding the information used in bsrsv2_bufferSize(), bsrsv2_analysis(), and bsrsv2_solve().

4.15. bsric02Info_t

This is a pointer type to an opaque structure holding the information used in bsric02_bufferSize(), bsric02_analysis(), and bsric02().

4.16. bsrilu02Info_t

This is a pointer type to an opaque structure holding the information used in bsrilu02_bufferSize(), bsrilu02_analysis(), and bsrilu02().

4.17. cusparseStatus_t

This is a status type returned by the library functions and it can have the following values.

CUSPARSE_STATUS_SUCCESS

The operation completed successfully.

CUSPARSE_STATUS_NOT_INITIALIZED

The cuSPARSE library was not initialized. This is usually caused by the lack of a prior call, an error in the CUDA Runtime API called by the cuSPARSE routine, or an error in the hardware setup.

To correct: call cusparseCreate() prior to the function call; and check that the hardware, an appropriate version of the driver, and the cuSPARSE library are correctly installed.

CUSPARSE_STATUS_ALLOC_FAILED

Resource allocation failed inside the cuSPARSE library. This is usually caused by a cudaMalloc() failure.

To correct: prior to the function call, deallocate previously allocated memory as much as possible.

CUSPARSE_STATUS_INVALID_VALUE

An unsupported value or parameter was passed to the function (a negative vector size, for example).

To correct: ensure that all the parameters being passed have valid values.

CUSPARSE_STATUS_ARCH_MISMATCH

The function requires a feature absent from the device architecture; usually caused by the lack of support for atomic operations or double precision.

To correct: compile and run the application on a device with appropriate compute capability, which is 1.1 for 32-bit atomic operations and 1.3 for double precision.

CUSPARSE_STATUS_MAPPING_ERROR

An access to GPU memory space failed, which is usually caused by a failure to bind a texture.

To correct: prior to the function call, unbind any previously bound textures.

CUSPARSE_STATUS_EXECUTION_FAILED

The GPU program failed to execute. This is often caused by a launch failure of the kernel on the GPU, which can be caused by multiple reasons.

To correct: check that the hardware, an appropriate version of the driver, and the cuSPARSE library are correctly installed.

CUSPARSE_STATUS_INTERNAL_ERROR

An internal cuSPARSE operation failed. This error is usually caused by a cudaMemcpyAsync() failure.

To correct: check that the hardware, an appropriate version of the driver, and the cuSPARSE library are correctly installed. Also, check that the memory passed as a parameter to the routine is not being deallocated prior to the routine’s completion.

CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED

The matrix type is not supported by this function. This is usually caused by passing an invalid matrix descriptor to the function.

To correct: check that the fields in cusparseMatDescr_t descrA were set correctly.

5. cuSPARSE Helper Function Reference

The cuSPARSE helper functions are described in this section.

5.1. cusparseCreate()

cusparseStatus_t
cusparseCreate(cusparseHandle_t *handle)

This function initializes the cuSPARSE library and creates a handle on the cuSPARSE context. It must be called before any other cuSPARSE API function is invoked. It allocates hardware resources necessary for accessing the GPU.

Output
handle the pointer to the handle to the cuSPARSE context.
Status Returned
CUSPARSE_STATUS_SUCCESS the initialization succeeded.
CUSPARSE_STATUS_NOT_INITIALIZED the CUDA Runtime initialization failed.
CUSPARSE_STATUS_ALLOC_FAILED the resources could not be allocated.
CUSPARSE_STATUS_ARCH_MISMATCH the device compute capability (CC) is less than 1.1. The CC of at least 1.1 is required.

5.2. cusparseCreateHybMat()

cusparseStatus_t
cusparseCreateHybMat(cusparseHybMat_t *hybA)

This function creates and initializes the hybA opaque data structure.

Input
hybA the pointer to the hybrid format storage structure.
Status Returned
CUSPARSE_STATUS_SUCCESS the structure was initialized successfully.
CUSPARSE_STATUS_ALLOC_FAILED the resources could not be allocated.

5.3. cusparseCreateMatDescr()

cusparseStatus_t
cusparseCreateMatDescr(cusparseMatDescr_t *descrA)

This function initializes the matrix descriptor. It sets the fields MatrixType and IndexBase to the default values CUSPARSE_MATRIX_TYPE_GENERAL and CUSPARSE_INDEX_BASE_ZERO , respectively, while leaving other fields uninitialized.

Input
descrA the pointer to the matrix descriptor.
Status Returned
CUSPARSE_STATUS_SUCCESS the descriptor was initialized successfully.
CUSPARSE_STATUS_ALLOC_FAILED the resources could not be allocated.

5.4. cusparseCreateSolveAnalysisInfo()

cusparseStatus_t
cusparseCreateSolveAnalysisInfo(cusparseSolveAnalysisInfo_t *info)

This function creates and initializes the solve and analysis structure to default values.

Input
info the pointer to the solve and analysis structure.
Status Returned
CUSPARSE_STATUS_SUCCESS the structure was initialized successfully.
CUSPARSE_STATUS_ALLOC_FAILED the resources could not be allocated.

5.5. cusparseDestroy()

cusparseStatus_t
cusparseDestroy(cusparseHandle_t handle)

This function releases CPU-side resources used by the cuSPARSE library. The release of GPU-side resources may be deferred until the application shuts down.

Input
handle the handle to the cuSPARSE context.
Status Returned
CUSPARSE_STATUS_SUCCESS the shutdown succeeded.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.

5.6. cusparseDestroyHybMat()

cusparseStatus_t
cusparseDestroyHybMat(cusparseHybMat_t hybA)

This function destroys and releases any memory required by the hybA structure.

Input
hybA the hybrid format storage structure.
Status Returned
CUSPARSE_STATUS_SUCCESS the resources were released successfully.

5.7. cusparseDestroyMatDescr()

cusparseStatus_t
cusparseDestroyMatDescr(cusparseMatDescr_t descrA)

This function releases the memory allocated for the matrix descriptor.

Input
descrA the matrix descriptor.
Status Returned
CUSPARSE_STATUS_SUCCESS the resources were released successfully.

5.8. cusparseDestroySolveAnalysisInfo()

cusparseStatus_t
cusparseDestroySolveAnalysisInfo(cusparseSolveAnalysisInfo_t info)

This function destroys and releases any memory required by the structure.

Input

info the solve and analysis structure.

Status Returened

CUSPARSE_STATUS_SUCCESS the resources were released successfully.

5.9. cusparseGetLevelInfo()

cusparseStatus_t 
cusparseGetLevelInfo(cusparseHandle_t handle, 
                     cusparseSolveAnalysisInfo_t info, 
                     int *nlevels, 
                     int **levelPtr, 
                     int **levelInd)

This function returns the number of levels and the assignment of rows into the levels computed by either the csrsv_analysis, csrsm_analysis or hybsv_analysis routines.

Input
handle handle to the cuSPARSE library context.
info the pointer to the solve and analysis structure.
Output
nlevels number of levels.
levelPtr integer array of nlevels+1 elements that contains the start of every level and the end of the last level plus one.
levelInd integer array of m (number of rows in the matrix) elements that contains the row indices belonging to every level.
Status Returned
CUSPARSE_STATUS_SUCCESS the structure was initialized successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library or the solve analysis structure was not initialized.

5.10. cusparseGetMatDiagType()

cusparseDiagType_t
cusparseGetMatDiagType(const cusparseMatDescr_t descrA)

This function returns the DiagType field of the matrix descriptor descrA.

Input
descrA the matrix descriptor.
Returned
One of the enumerated diagType types.

5.11. cusparseGetMatFillMode()

cusparseFillMode_t
cusparseGetMatFillMode(const cusparseMatDescr_t descrA)

This function returns the FillMode field of the matrix descriptor descrA.

Input
descrA the matrix descriptor.
Returned
One of the enumerated fillMode types.

5.12. cusparseGetMatIndexBase()

cusparseIndexBase_t
cusparseGetMatIndexBase(const cusparseMatDescr_t descrA)

This function returns the IndexBase field of the matrix descriptor descrA.

Input
descrA the matrix descriptor.
Returned
One of the enumerated indexBase types.

5.13. cusparseGetMatType()

cusparseMatrixType_t
cusparseGetMatType(const cusparseMatDescr_t descrA)

This function returns the MatrixType field of the matrix descriptor descrA.

Input
descrA the matrix descriptor.
Returned
One of the enumerated matrix types.

5.14. cusparseGetPointerMode()

cusparseStatus_t
cusparseGetPointerMode(cusparseHandlet handle, 
                       cusparsePointerMode_t *mode)

This function obtains the pointer mode used by the cuSPARSE library. Please see the section on the cusparsePointerMode_t type for more details.

Input
handle the handle to the cuSPARSE context.
Output
mode One of the enumerated pointer mode types.
Status Returned
CUSPARSE_STATUS_SUCCESS the pointer mode was returned successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.

5.15. cusparseGetVersion()

cusparseStatus_t
cusparseGetVersion(cusparseHandle_t handle, int *version)

This function returns the version number of the cuSPARSE library.

Input
handle the handle to the cuSPARSE context.
Output
version the version number of the library.
Status Returned
CUSPARSE_STATUS_SUCCESS the version was returned successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.

5.16. cusparseSetMatDiagType()

cusparseStatus_t
cusparseSetMatDiagType(cusparseMatDescr_t descrA, 
                       cusparseDiagType_t diagType)

This function sets the DiagType field of the matrix descriptor descrA.

Input
diagType One of the enumerated diagType types.
Output
descrA the matrix descriptor.
Status Returned
CUSPARSE_STATUS_SUCCESS the field DiagType was set successfully.
CUSPARSE_STATUS_INVALID_VALUE An invalid diagType parameter was passed.

5.17. cusparseSetMatFillMode()

cusparseStatus_t
cusparseSetMatFillMode(cusparseMatDescr_t descrA, 
                       cusparseFillMode_t fillMode)

This function sets the FillMode field of the matrix descriptor descrA.

Input
fillMode One of the enumerated fillMode types.
Output
descrA the matrix descriptor.
Status Returned
CUSPARSE_STATUS_SUCCESS the FillMode field was set successfully.
CUSPARSE_STATUS_INVALID_VALUE An invalid fillMode parameter was passed.

5.18. cusparseSetMatIndexBase()

cusparseStatus_t
cusparseSetMatIndexBase(cusparseMatDescr_t descrA, 
                        cusparseIndexBase_t base)

This function sets the IndexBase field of the matrix descriptor descrA.

Input
base One of the enumerated indexBase types.
Output
descrA the matrix descriptor.
Status Returned
CUSPARSE_STATUS_SUCCESS the IndexBase field was set successfully.
CUSPARSE_STATUS_INVALID_VALUE An invalid base parameter was passed.

5.19. cusparseSetMatType()

cusparseStatus_t
cusparseSetMatType(cusparseMatDescr_t descrA, cusparseMatrixType_t type)

This function sets the MatrixType field of the matrix descriptor descrA.

Input
type One of the enumerated matrix types.
Output
descrA the matrix descriptor.
Status Returned
CUSPARSE_STATUS_SUCCESS the MatrixType field was set successfully.
CUSPARSE_STATUS_INVALID_VALUE An invalid type parameter was passed.

5.20. cusparseSetPointerMode()

cusparseStatus_t
cusparseSetPointerMode(cusparseHandle_t handle, 
                       cusparsePointerMode_t mode)

This function sets the pointer mode used by the cuSPARSE library. The default is for the values to be passed by reference on the host. Please see the section on the cublasPointerMode_t type for more details.

Input
handle the handle to the cuSPARSE context.
mode One of the enumerated pointer mode types.
Status Returned
CUSPARSE_STATUS_SUCCESS the pointer mode was set successfully.
CUSPARSE_STATUS_INVALID_VALUE the library was not initialized.

5.21. cusparseSetStream()

cusparseStatus_t
cusparseSetStream(cusparseHandle_t handle, cudaStream_t streamId)

This function sets the stream to be used by the cuSPARSE library to execute its routines.

Input
handle the handle to the cuSPARSE context.
streamId the stream to be used by the library.
Status Returned
CUSPARSE_STATUS_SUCCESS the stream was set successfully.
CUSPARSE_STATUS_INVALID_VALUE the library was not initialized.

5.22. cusparseCreateCsrsv2Info()

cusparseStatus_t 
cusparseCreateCsrsv2Info(csrsv2Info_t *info);

This function creates and initializes the solve and analysis structure of csrsv2 to default values.

Input
info the pointer to the solve and analysis structure of csrsv2.
Status Returned
CUSPARSE_STATUS_SUCCESS the structure was initialized successfully.
CUSPARSE_STATUS_ALLOC_FAILED the resources could not be allocated.

5.23. cusparseDestroyCsrsv2Info()

cusparseStatus_t 
cusparseDestroyCsrsv2Info(csrsv2Info_t info);

This function destroys and releases any memory required by the structure.

Input

info the solve (csrsv2_solve) and analysis (csrsv2_analysis) structure.

Status Returened

CUSPARSE_STATUS_SUCCESS the resources were released successfully.

5.24. cusparseCreateCsric02Info()

cusparseStatus_t 
cusparseCreateCsric02Info(csric02Info_t *info);

This function creates and initializes the solve and analysis structure of incomplete Cholesky to default values.

Input
info the pointer to the solve and analysis structure of incomplete Cholesky.
Status Returned
CUSPARSE_STATUS_SUCCESS the structure was initialized successfully.
CUSPARSE_STATUS_ALLOC_FAILED the resources could not be allocated.

5.25. cusparseDestroyCsric02Info()

cusparseStatus_t 
cusparseDestroyCsric02Info(csric02Info_t info);

This function destroys and releases any memory required by the structure.

Input

info the solve (csric02_solve) and analysis (csric02_analysis) structure.

Status Returened

CUSPARSE_STATUS_SUCCESS the resources were released successfully.

5.26. cusparseCreateCsrilu02Info()

cusparseStatus_t 
cusparseCreateCsrilu02Info(csrilu02Info_t *info);

This function creates and initializes the solve and analysis structure of incomplete LU to default values.

Input
info the pointer to the solve and analysis structure of incomplete LU.
Status Returned
CUSPARSE_STATUS_SUCCESS the structure was initialized successfully.
CUSPARSE_STATUS_ALLOC_FAILED the resources could not be allocated.

5.27. cusparseDestroyCsrilu02Info()

cusparseStatus_t 
cusparseDestroyCsrilu02Info(csrilu02Info_t info);

This function destroys and releases any memory required by the structure.

Input

info the solve (csrilu02_solve) and analysis (csrilu02_analysis) structure.

Status Returened

CUSPARSE_STATUS_SUCCESS the resources were released successfully.

6. cuSPARSE Level 1 Function Reference

This chapter describes sparse linear algebra functions that perform operations between dense and sparse vectors.

6.1. cusparse<t>axpyi()

cusparseStatus_t 
cusparseSaxpyi(cusparseHandle_t handle, int nnz, 
               const float           *alpha, 
               const float           *xVal, const int *xInd, 
               float           *y, cusparseIndexBase_t idxBase)
cusparseStatus_t 
cusparseDaxpyi(cusparseHandle_t handle, int nnz, 
               const double          *alpha, 
               const double          *xVal, const int *xInd, 
               double          *y, cusparseIndexBase_t idxBase)
cusparseStatus_t 
cusparseCaxpyi(cusparseHandle_t handle, int nnz, 
               const cuComplex       *alpha, 
               const cuComplex       *xVal, const int *xInd, 
               cuComplex       *y, cusparseIndexBase_t idxBase)
cusparseStatus_t 
cusparseZaxpyi(cusparseHandle_t handle, int nnz, 
               const cuDoubleComplex *alpha, 
               const cuDoubleComplex *xVal, const int *xInd, 
               cuDoubleComplex *y, cusparseIndexBase_t idxBase)

This function multiplies the vector x in sparse format by the constant α and adds the result to the vector y in dense format. This operation can be written as

y = y + α x

In other words,

for i=0 to nnz-1
    y[xInd[i]-idxBase] = y[xInd[i]-idxBase] + alpha*xVal[i]

This function requires no extra storage. It is executed asynchronously with respect to the host, and it may return control to the application on the host before the result is ready.

Input
handle handle to the cuSPARSE library context.
nnz number of elements in vector x.
alpha <type> scalar used for multiplication.
xVal <type> vector with nnz nonzero values of vector x.
xInd integer vector with nnz indices of the nonzero values of vector x.
y <type> vector in dense format.
idxBase CUSPARSE_INDEX_BASE_ZERO or CUSPARSE_INDEX_BASE_ONE.
Output
y <type> updated vector in dense format (that is unchanged if nnz == 0).
Status Returned
CUSPARSE_STATUS_SUCCESS the operation completed successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.
CUSPARSE_STATUS_INVALID_VALUE the idxBase is neither CUSPARSE_INDEX_BASE_ZERO nor CUSPARSE_INDEX_BASE_ONE.
CUSPARSE_STATUS_ARCH_MISMATCH the device does not support double precision.
CUSPARSE_STATUS_EXECUTION_FAILED the function failed to launch on the GPU.

6.2. cusparse<t>doti()

cusparseStatus_t 
cusparseSdoti(cusparseHandle_t handle, int nnz, 
              const float           *xVal, 
              const int *xInd, const float           *y, 
              float           *resultDevHostPtr, 
              cusparseIndexBase_t idxBase)
cusparseStatus_t 
cusparseDdoti(cusparseHandle_t handle, int nnz, 
              const double          *xVal, 
              const int *xInd, const double          *y, 
              double          *resultDevHostPtr, 
              cusparseIndexBase_t idxBase)
cusparseStatus_t 
cusparseCdoti(cusparseHandle_t handle, int nnz, 
              const cuComplex       *xVal, 
              const int *xInd, const cuComplex       *y, 
              cuComplex       *resultDevHostPtr, 
              cusparseIndexBase_t idxBase)
cusparseStatus_t 
cusparseZdoti(cusparseHandle_t handle, int nnz, const 
              cuDoubleComplex *xVal, 
              const int *xInd, const cuDoubleComplex *y, 
              cuDoubleComplex *resultDevHostPtr, 
              cusparseIndexBase_t idxBase)

This function returns the dot product of a vector x in sparse format and vector y in dense format. This operation can be written as

r e s u l t = y T x

In other words,

for i=0 to nnz-1
    resultDevHostPtr += xVal[i]*y[xInd[i-idxBase]]

This function requires some temporary extra storage that is allocated internally. It is executed asynchronously with respect to the host and may return control to the application on the host before the result is ready.

Input
handle handle to the cuSPARSE library context.
nnz number of elements in vector x.
xVal <type> vector with nnz nonzero values of vector x.
xInd integer vector with nnz indices of the nonzero values of vector x.
y <type> vector in dense format.
resultDevHostPtr pointer to the location of the result in the device or host memory.
idxBase CUSPARSE_INDEX_BASE_ZERO or CUSPARSE_INDEX_BASE_ONE.
Output
resultDevHostPtr scalar result in the device or host memory (that is zero if nnz == 0).
Status Returned
CUSPARSE_STATUS_SUCCESS the operation completed successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.
CUSPARSE_STATUS_ALLOC_FAILED the reduction buffer could not be allocated.
CUSPARSE_STATUS_INVALID_VALUE the idxBase is neither CUSPARSE_INDEX_BASE_ZERO nor CUSPARSE_INDEX_BASE_ONE.
CUSPARSE_STATUS_ARCH_MISMATCH the device does not support double precision.
CUSPARSE_STATUS_EXECUTION_FAILED the function failed to launch on the GPU.
CUSPARSE_STATUS_INTERNAL_ERROR an internal operation failed.

6.3. cusparse<t>dotci()

cusparseStatus_t 
cusparseCdotci(cusparseHandle_t handle, int nnz, 
               const cuComplex       *xVal, 
               const int *xInd, const cuComplex       *y, 
               cuComplex       *resultDevHostPtr, cusparseIndexBase_t idxBase)
cusparseStatus_t 
cusparseZdotci(cusparseHandle_t handle, int nnz, 
               const cuDoubleComplex *xVal, 
               const int *xInd, const cuDoubleComplex *y, 
               cuDoubleComplex *resultDevHostPtr, cusparseIndexBase_t idxBase)

This function returns the dot product of a complex conjugate of vector x in sparse format and vector y in dense format. This operation can be written as

r e s u l t = x H y

In other words,

for i=0 to nnz-1
	resultDevHostPtr += xVal[i]¯*y[xInd[i-idxBase]]

This function requires some temporary extra storage that is allocated internally. It is executed asynchronously with respect to the host and may return control to the application on the host before the result is ready.

Input
handle handle to the cuSPARSE library context.
nnz number of elements in vector x.
xVal <type> vector with nnz nonzero values of vector x.
xInd integer vector with nnz indices of the nonzero values of vector x.
y <type> vector in dense format.
resultDevHostPtr pointer to the location of the result in the device or host memory.
idxBase CUSPARSE_INDEX_BASE_ZERO or CUSPARSE_INDEX_BASE_ONE.
Output
resultDevHostPtr scalar result in the device or host memory (that is zero if nnz == 0).
Status Returned
CUSPARSE_STATUS_SUCCESS the operation completed successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.
CUSPARSE_STATUS_ALLOC_FAILED the reduction buffer could not be allocated.
CUSPARSE_STATUS_INVALID_VALUE the idxBase is neither CUSPARSE_INDEX_BASE_ZERO nor CUSPARSE_INDEX_BASE_ONE.
CUSPARSE_STATUS_ARCH_MISMATCH the device does not support double precision.
CUSPARSE_STATUS_EXECUTION_FAILED the function failed to launch on the GPU.
CUSPARSE_STATUS_INTERNAL_ERROR an internal operation failed.

6.4. cusparse<t>gthr()

cusparseStatus_t 
cusparseSgthr(cusparseHandle_t handle, int nnz, 
              const float           *y, 
              float           *xVal, const int *xInd, 
              cusparseIndexBase_t idxBase)
cusparseStatus_t 
cusparseDgthr(cusparseHandle_t handle, int nnz, 
              const double          *y, 
              double          *xVal, const int *xInd, 
              cusparseIndexBase_t idxBase)
cusparseStatus_t 
cusparseCgthr(cusparseHandle_t handle, int nnz, 
              const cuComplex       *y, 
              cuComplex       *xVal, const int *xInd, 
              cusparseIndexBase_t idxBase)
cusparseStatus_t 
cusparseZgthr(cusparseHandle_t handle, int nnz, 
              const cuDoubleComplex *y, 
              cuDoubleComplex *xVal, const int *xInd, 
              cusparseIndexBase_t idxBase)

This function gathers the elements of the vector y listed in the index array xInd into the data array xVal.

This function requires no extra storage. It is executed asynchronously with respect to the host and it may return control to the application on the host before the result is ready.

Input
handle handle to the cuSPARSE library context.
nnz number of elements in vector x.
y <type> vector in dense format (of size≥max(xInd)-idxBase+1).
xInd integer vector with nnz indices of the nonzero values of vector x.
idxBase CUSPARSE_INDEX_BASE_ZERO or CUSPARSE_INDEX_BASE_ONE.
Output
xVal <type> vector with nnz nonzero values that were gathered from vector y (that is unchanged if nnz == 0).
Status Returned
CUSPARSE_STATUS_SUCCESS the operation completed successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.
CUSPARSE_STATUS_INVALID_VALUE the idxBase is neither CUSPARSE_INDEX_BASE_ZERO nor CUSPARSE_INDEX_BASE_ONE.
CUSPARSE_STATUS_ARCH_MISMATCH the device does not support double precision.
CUSPARSE_STATUS_EXECUTION_FAILED the function failed to launch on the GPU.

6.5. cusparse<t>gthrz()

cusparseStatus_t 
cusparseSgthrz(cusparseHandle_t handle, int nnz, float           *y, 
               float           *xVal, const int *xInd, 
               cusparseIndexBase_t idxBase)
cusparseStatus_t 
cusparseDgthrz(cusparseHandle_t handle, int nnz, double          *y, 
               double          *xVal, const int *xInd, 
               cusparseIndexBase_t idxBase)
cusparseStatus_t 
cusparseCgthrz(cusparseHandle_t handle, int nnz, cuComplex       *y, 
               cuComplex       *xVal, const int *xInd, 
               cusparseIndexBase_t idxBase)
cusparseStatus_t 
cusparseZgthrz(cusparseHandle_t handle, int nnz, cuDoubleComplex *y, 
               cuDoubleComplex *xVal, const int *xInd, 
               cusparseIndexBase_t idxBase)

This function gathers the elements of the vector y listed in the index array xInd into the data array xVal. Also, it zeros out the gathered elements in the vector y.

This function requires no extra storage. It is executed asynchronously with respect to the host, and it may return control to the application on the host before the result is ready.

Input
handle handle to the cuSPARSE library context.
nnz number of elements in vector x.
y <type> vector in dense format (of size≥max(xInd)-idxBase+1).
xInd integer vector with nnz indices of the nonzero values of vector x.
idxBase CUSPARSE_INDEX_BASE_ZERO or CUSPARSE_INDEX_BASE_ONE.
Output
xVal <type> vector with nnz nonzero values that were gathered from vector y (that is unchanged if nnz == 0).
y <type> vector in dense format with elements indexed by xInd set to zero (it is unchanged if nnz == 0).
Status Returned
CUSPARSE_STATUS_SUCCESS the operation completed successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.
CUSPARSE_STATUS_INVALID_VALUE the idxBase is neither CUSPARSE_INDEX_BASE_ZERO nor CUSPARSE_INDEX_BASE_ONE.
CUSPARSE_STATUS_ARCH_MISMATCH the device does not support double precision.
CUSPARSE_STATUS_EXECUTION_FAILED the function failed to launch on the GPU.

6.6. cusparse<t>roti()

cusparseStatus_t 
cusparseSroti(cusparseHandle_t handle, int nnz, float  *xVal, 
              const int *xInd, 
              float  *y, const float  *c, const float  *s, 
              cusparseIndexBase_t idxBase)
cusparseStatus_t 
cusparseDroti(cusparseHandle_t handle, int nnz, double *xVal, 
              const int *xInd, 
              double *y, const double *c, const double *s, 
              cusparseIndexBase_t idxBase)

This function applies the Givens rotation matrix

G = c s s c

to sparse x and dense y vectors. In other words,

for i=0 to nnz-1
    y[xInd[i]-idxBase] = c * y[xInd[i]-idxBase] - s*xVal[i]
    x[i]               = c * xVal[i]            + s * y[xInd[i]-idxBase]
Input
handle handle to the cuSPARSE library context.
nnz number of elements in vector x.
xVal <type> vector with nnz nonzero values of vector x.
xInd integer vector with nnz indices of the nonzero values of vector x.
y <type> vector in dense format.
c cosine element of the rotation matrix.
s sine element of the rotation matrix.
idxBase CUSPARSE_INDEX_BASE_ZERO or CUSPARSE_INDEX_BASE_ONE.
Output
xVal <type> updated vector in sparse format (that is unchanged if nnz == 0).
y <type> updated vector in dense format (that is unchanged if nnz == 0).
Status Returned
CUSPARSE_STATUS_SUCCESS the operation completed successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.
CUSPARSE_STATUS_INVALID_VALUE the idxBase is neither CUSPARSE_INDEX_BASE_ZERO nor CUSPARSE_INDEX_BASE_ONE.
CUSPARSE_STATUS_ARCH_MISMATCH the device does not support double precision.
CUSPARSE_STATUS_EXECUTION_FAILED the function failed to launch on the GPU.

6.7. cusparse<t>sctr()

cusparseStatus_t 
cusparseSsctr(cusparseHandle_t handle, int nnz, 
              const float           *xVal, 
              const int *xInd, float           *y, 
              cusparseIndexBase_t idxBase)
cusparseStatus_t 
cusparseDsctr(cusparseHandle_t handle, int nnz, 
              const double          *xVal, 
              const int *xInd, double          *y, 
              cusparseIndexBase_t idxBase)
cusparseStatus_t 
cusparseCsctr(cusparseHandle_t handle, int nnz, 
              const cuComplex       *xVal, 
              const int *xInd, cuComplex       *y, 
              cusparseIndexBase_t idxBase)
cusparseStatus_t 
cusparseZsctr(cusparseHandle_t handle, int nnz, 
              const cuDoubleComplex *xVal, 
              const int *xInd, cuDoubleComplex *y, 
              cusparseIndexBase_t idxBase)

This function scatters the elements of the vector x in sparse format into the vector y in dense format. It modifies only the elements of y whose indices are listed in the array xInd.

This function requires no extra storage. It is executed asynchronously with respect to the host and may return control to the application on the host before the result is ready.

Input
handle handle to the cuSPARSE library context.
nnz number of elements in vector x.
xVal <type> vector with nnz nonzero values of vector x.
xInd integer vector with nnz indices of the nonzero values of vector x.
y <type> dense vector (of size≥max(xInd)-idxBase+1).
idxBase CUSPARSE_INDEX_BASE_ZERO or CUSPARSE_INDEX_BASE_ONE.
Output
y <type> vector with nnz nonzero values that were scattered from vector x (that is unchanged if nnz == 0).
Status Returned
CUSPARSE_STATUS_SUCCESS the operation completed successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.
CUSPARSE_STATUS_INVALID_VALUE the idxBase is neither CUSPARSE_INDEX_BASE_ZERO nor CUSPARSE_INDEX_BASE_ONE..
CUSPARSE_STATUS_ARCH_MISMATCH the device does not support double precision.
CUSPARSE_STATUS_EXECUTION_FAILED the function failed to launch on the GPU.

7. cuSPARSE Level 2 Function Reference

This chapter describes the sparse linear algebra functions that perform operations between sparse matrices and dense vectors.

In particular, the solution of sparse triangular linear systems is implemented in two phases. First, during the analysis phase, the sparse triangular matrix is analyzed to determine the dependencies between its elements by calling the appropriate csrsv_analysis() function. The analysis is specific to the sparsity pattern of the given matrix and to the selected cusparseOperation_t type. The information from the analysis phase is stored in the parameter of type cusparseSolveAnalysisInfo_t that has been initialized previously with a call to cusparseCreateSolveAnalysisInfo().

Second, during the solve phase, the given sparse triangular linear system is solved using the information stored in the cusparseSolveAnalysisInfo_t parameter by calling the appropriate csrsv_solve() function. The solve phase may be performed multiple times with different right-hand sides, while the analysis phase needs to be performed only once. This is especially useful when a sparse triangular linear system must be solved for a set of different right-hand sides one at a time, while its coefficient matrix remains the same.

Finally, once all the solves have completed, the opaque data structure pointed to by the cusparseSolveAnalysisInfo_t parameter can be released by calling cusparseDestroySolveAnalysisInfo(). For more information please refer to [3].

7.1. cusparse<t>bsrmv()

cusparseStatus_t
cusparseSbsrmv(cusparseHandle_t handle, cusparseDirection_t dir,
    cusparseOperation_t trans, int mb, int nb, int nnzb,
    const float *alpha, const cusparseMatDescr_t descr,
    const float *bsrVal, const int *bsrRowPtr, const int *bsrColInd,
    int  blockDim, const float *x, 
    const float *beta, float *y)
cusparseStatus_t
cusparseDbsrmv(cusparseHandle_t handle, cusparseDirection_t dir,
    cusparseOperation_t trans, int mb, int nb, int nnzb,
    const double *alpha, const cusparseMatDescr_t descr,
    const double *bsrVal, const int *bsrRowPtr, const int *bsrColInd,
    int  blockDim, const double *x, 
    const double *beta, double *y)
cusparseStatus_t
cusparseCbsrmv(cusparseHandle_t handle, cusparseDirection_t dir,
    cusparseOperation_t trans, int mb, int nb, int nnzb,
    const cuComplex *alpha, const cusparseMatDescr_t descr,
    const cuComplex *bsrVal, const int *bsrRowPtr, const int *bsrColInd,
    int  blockDim, const cuComplex *x, 
    const cuComplex *beta, cuComplex *y)
cusparseStatus_t
cusparseZbsrmv(cusparseHandle_t handle, cusparseDirection_t dir,
    cusparseOperation_t trans, int mb, int nb, int nnzb,
    const cuDoubleComplex *alpha, const cusparseMatDescr_t descr,
    const cuDoubleComplex *bsrVal, const int *bsrRowPtr, const int *bsrColInd,
    int  blockDim, const cuDoubleComplex *x, 
    const cuDoubleComplex *beta, cuDoubleComplex *y)

This function performs the matrix-vector operation

y = α op ( A ) x + β y

where A  is an   ( m b b l o c k D i m ) × ( n b b l o c k D i m ) sparse matrix that is defined in BSR storage format by the three arrays bsrVal, bsrRowPtr, and bsrColInd); x and y are vectors; α  and  β are scalars; and

op ( A ) = A if trans == CUSPARSE_OPERATION_NON_TRANSPOSE A T if trans == CUSPARSE_OPERATION_TRANSPOSE A H if trans == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE
Several comments on bsrmv():
  • Only CUSPARSE_OPERATION_NON_TRANSPOSE is supported, that is

    y = α A x + β y
  • Only CUSPARSE_MATRIX_TYPE_GENERAL is supported.
  • The size of vector x should be ( n b b l o c k D i m ) at least, and the size of vector y should be ( m b b l o c k D i m ) at least; otherwise, the kernel may return CUSPARSE_STATUS_EXECUTION_FAILED because of an out-of-bounds array.

For example, suppose the user has a CSR format and wants to try bsrmv(), the following code demonstrates how to use csr2bsr() conversion and bsrmv() multiplication in single precision.

// Suppose that A is m x n sparse matrix represented by CSR format, 
// hx is a host vector of size n, and hy is also a host vector of size m. 
// m and n are not multiple of blockDim.
// step 1: transform CSR to BSR with column-major order 
int base, nnz;
cusparseDirection_t dirA = CUSPARSE_DIRECTION_COLUMN;
int mb = (m + blockDim-1)/blockDim;
int nb = (n + blockDim-1)/blockDim;
cudaMalloc((void**)&bsrRowPtrC, sizeof(int) *(mb+1));
cusparseXcsr2bsrNnz(handle, dirA, m, n, 
        descrA, csrRowPtrA, csrColIndA, blockDim, 
        descrC, bsrRowPtrC);
cudaMemcpy(&nnzb, bsrRowPtrC+mb, sizeof(int), cudaMemcpyDeviceToHost);
cudaMemcpy(&base, bsrRowPtrC, sizeof(int), cudaMemcpyDeviceToHost);
nnzb -= base;
cudaMalloc((void**)&bsrColIndC, sizeof(int)*nnzb);
cudaMalloc((void**)&bsrValC, sizeof(float)*(blockDim*blockDim)*nnzb);
cusparseScsr2bsr(handle, dirA, m, n, 
        descrA, csrValA, csrRowPtrA, csrColIndA, blockDim,
        descrC, bsrValC, bsrRowPtrC, bsrColIndC);
// step 2: allocate vector x and vector y large enough for bsrmv 
cudaMalloc((void**)&x, sizeof(float)*(nb*blockDim));
cudaMalloc((void**)&y, sizeof(float)*(mb*blockDim));
cudaMemcpy(x, hx, sizeof(float)*n, cudaMemcpyHostToDevice);
cudaMemcpy(y, hy, sizeof(float)*m, cudaMemcpyHostToDevice);
// step 3: perform bsrmv
cusparseSbsrmv(handle, dirA, transA, mb, nb, alpha, descrC, bsrValC, bsrRowPtrC, bsrColIndC, blockDim, x, beta, y);
Input
handle handle to the cuSPARSE library context.
dir storage format of blocks, either CUSPARSE_DIRECTION_ROW or CUSPARSE_DIRECTION_COLUMN.
trans the operation op ( A ) . Only CUSPARSE_OPERATION_NON_TRANSPOSE is supported.
mb number of block rows of matrix A .
nb number of block columns of matrix A .
nnzb number of nonzero blocks of matrix A .
alpha <type> scalar used for multiplication.
descr the descriptor of matrix A . The supported matrix type is CUSPARSE_MATRIX_TYPE_GENERAL. Also, the supported index bases are CUSPARSE_INDEX_BASE_ZERO and CUSPARSE_INDEX_BASE_ONE.
bsrVal <type> array of nnz ( = csrRowPtrA(mb) - csrRowPtrA(0) ) nonzero blocks of matrix A .
bsrRowPtr integer array of mb + 1 elements that contains the start of every block row and the end of the last block row plus one.
bsrColInd integer array of nnz ( = csrRowPtrA(mb) - csrRowPtrA(0) ) column indices of the nonzero blocks of matrix A .
blockDim block dimension of sparse matrix A , larger than zero.
x <type> vector of n b b l o c k D i m elements.
beta <type> scalar used for multiplication. If beta is zero, y does not have to be a valid input.
y <type> vector of m b b l o c k D i m elements.
Output
y <type> updated vector.
Status Returned
CUSPARSE_STATUS_SUCCESS the operation completed successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.
CUSPARSE_STATUS_INVALID_VALUE invalid parameters were passed (m,n,nnz<0, trans != CUSPARSE_OPERATION_NON_TRANSPOSE, b l o c k D i m < 1 , dir is not row-major or column-major, or IndexBase of descr is not base-0 or base-1 ).
CUSPARSE_STATUS_ARCH_MISMATCH the device does not support double precision.
CUSPARSE_STATUS_EXECUTION_FAILED the function failed to launch on the GPU.
CUSPARSE_STATUS_INTERNAL_ERROR an internal operation failed.
CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED the matrix type is not supported.

7.2. cusparse<t>bsrxmv()

cusparseStatus_t 
cusparseSbsrxmv(cusparseHandle_t handle, 
                cusparseDirection_t dir,
                cusparseOperation_t trans, 
                int sizeOfMask,
                int mb, 
                int nb, 
                int nnzb,
                const float *alpha, 
                const cusparseMatDescr_t descr,
                const float *bsrVal, 
                const int *bsrMaskPtr, 
                const int *bsrRowPtr, 
                const int *bsrEndPtr, 
                const int *bsrColInd,
                int blockDim, 
                const float *x, 
                const float *beta, 
                float *y)
cusparseStatus_t 
cusparseDbsrxmv(cusparseHandle_t handle, 
                cusparseDirection_t dir,
                cusparseOperation_t trans, 
                int sizeOfMask,
                int mb, 
                int nb, 
                int nnzb,
                const double *alpha, 
                const cusparseMatDescr_t descr,
                const double *bsrVal, 
                const int *bsrMaskPtr,
                const int *bsrRowPtr, 
                const int *bsrEndPtr, 
                const int *bsrColInd,
                int blockDim, 
                const double *x, 
                const double *beta, 
                double *y)
cusparseStatus_t 
cusparseCbsrxmv(cusparseHandle_t handle, 
                cusparseDirection_t dir,
                cusparseOperation_t trans, 
                int sizeOfMask,
                int mb, 
                int nb, 
                int nnzb,
                const cuComplex *alpha, 
                const cusparseMatDescr_t descr,
                const cuComplex *bsrVal, 
                const int *bsrMaskPtr,
                const int *bsrRowPtr, 
                const int *bsrEndPtr, 
                const int *bsrColInd,
                int blockDim, 
                const cuComplex *x, 
                const cuComplex *beta, 
                cuComplex *y)
cusparseStatus_t 
cusparseZbsrxmv(cusparseHandle_t handle, 
                cusparseDirection_t dir,
                cusparseOperation_t trans, 
                int sizeOfMask,
                int mb, 
                int nb, 
                int nnzb,
                const cuDoubleComplex *alpha, 
                const cusparseMatDescr_t descr,
                const cuDoubleComplex *bsrVal, 
                const int *bsrMaskPtr,
                const int *bsrRowPtr, 
                const int *bsrEndPtr, 
                const int *bsrColInd,
                int blockDim, 
                const cuDoubleComplex *x, 
                const cuDoubleComplex *beta, 
                cuDoubleComplex *y)

This function performs a bsrmv and a mask operation

y(mask) = ( α op ( A ) x + β y ) (mask)

where A  is an  ( m b b l o c k D i m ) × ( n b b l o c k D i m ) sparse matrix that is defined in BSRX storage format by the four arrays bsrVal, bsrRowPtr, bsrEndPtr, and bsrColInd); x and y are vectors; α  and  β are scalars; and

op ( A ) = A if trans == CUSPARSE_OPERATION_NON_TRANSPOSE A T if trans == CUSPARSE_OPERATION_TRANSPOSE A H if trans == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE

The mask operation is defined by array bsrMaskPtr which contains updated block row indices of y . If row i is not specified in bsrMaskPtr, then bsrxmv() does not touch row block i of A and y .

For example, consider the 2 × 3 block matrix A :

A = A 11 A 12 O A 21 A 22 A 23

and its one-based BSR format (three vector form) is

bsrVal = A 11 A 12 A 21 A 22 A 23 bsrRowPtr = 1 .0 3 .0 6 bsrColInd = 1 .0 2 .0 1 .0 2 .0 3

Suppose we want to do the following bsrmv operation on a matrix A ¯ which is slightly different from A .

y 1 y 2 := a l p h a ( A ˜ = O O O O A 22 O ) x 1 x 2 x 3 + y 1 b e t a y 2

We don’t need to create another BSR format for the new matrix A ¯ , all that we should do is to keep bsrVal and bsrColInd unchanged, but modify bsrRowPtr and add an additional array bsrEndPtr which points to the last nonzero elements per row of A ¯ plus 1.

For example, the following bsrRowPtr and bsrEndPtr can represent matrix A ¯ :

bsrRowPtr = 1 .0 4 bsrEndPtr = 1 .0 5

Further we can use a mask operator (specified by array bsrMaskPtr) to update particular block row indices of y only because y 1 is never changed. In this case, bsrMaskPtr = [2] and sizeOfMask=1.

The mask operator is equivalent to the following operation:

? y 2 := a l p h a ? ? ? O A 22 O x 1 x 2 x 3 + b e t a ? y 2

If a block row is not present in the bsrMaskPtr, then no calculation is performed on that row, and the corresponding value in y is unmodified. The question mark "?" is used to inidcate row blocks not in bsrMaskPtr.

In this case, first row block is not present in bsrMaskPtr, so bsrRowPtr[0] and bsrEndPtr[0] are not touched also.

bsrRowPtr = ? .0 4 bsrEndPtr = ? .0 5
A couple of comments on bsrxmv():
  • Only CUSPARSE_OPERATION_NON_TRANSPOSE and CUSPARSE_MATRIX_TYPE_GENERAL are supported.
  • Parameters bsrMaskPtr, bsrRowPtr, bsrEndPtr and bsrColInd are consistent with base index, either one-based or zero-based. The above example is one-based.

Input
handle handle to the cuSPARSE library context.
dir storage format of blocks, either CUSPARSE_DIRECTION_ROW or CUSPARSE_DIRECTION_COLUMN.
trans the operation op ( A ) . Only CUSPARSE_OPERATION_NON_TRANSPOSE is supported.
sizeOfMask number of updated block rows of y .
mb number of block rows of matrix A .
nb number of block columns of matrix A .
nnzb number of nonzero blocks of matrix A .
alpha <type> scalar used for multiplication.
descr the descriptor of matrix A . The supported matrix type is CUSPARSE_MATRIX_TYPE_GENERAL. Also, the supported index bases are CUSPARSE_INDEX_BASE_ZERO and CUSPARSE_INDEX_BASE_ONE.
bsrVal <type> array of nnz nonzero blocks of matrix A .
bsrMaskPtr integer array of sizeOfMask elements that contains the indices corresponding to updated block rows.
bsrRowPtr integer array of mb elements that contains the start of every block row.
bsrEndPtr integer array of mb elements that contains the end of the every block row plus one.
bsrColInd integer array of nnzb column indices of the nonzero blocks of matrix A .
blockDim block dimension of sparse matrix A , larger than zero.
x <type> vector of n b b l o c k D i m elements.
beta <type> scalar used for multiplication. If beta is zero, y does not have to be a valid input.
y <type> vector of m b b l o c k D i m elements.
Status Returned
CUSPARSE_STATUS_SUCCESS the operation completed successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.
CUSPARSE_STATUS_INVALID_VALUE invalid parameters were passed (m,n,nnz<0, trans != CUSPARSE_OPERATION_NON_TRANSPOSE, b l o c k D i m < 1 , dir is not row-major or column-major, or IndexBase of descr is not base-0 or base-1 ).
CUSPARSE_STATUS_ARCH_MISMATCH the device does not support double precision.
CUSPARSE_STATUS_EXECUTION_FAILED the function failed to launch on the GPU.
CUSPARSE_STATUS_INTERNAL_ERROR an internal operation failed.
CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED the matrix type is not supported.

7.3. cusparse<t>csrmv()

cusparseStatus_t 
cusparseScsrmv(cusparseHandle_t handle, cusparseOperation_t transA, 
               int m, int n, int nnz, const float           *alpha, 
               const cusparseMatDescr_t descrA, 
               const float           *csrValA, 
               const int *csrRowPtrA, const int *csrColIndA,
               const float           *x, const float           *beta, 
               float           *y)
cusparseStatus_t 
cusparseDcsrmv(cusparseHandle_t handle, cusparseOperation_t transA, 
               int m, int n, int nnz, const double          *alpha, 
               const cusparseMatDescr_t descrA, 
               const double          *csrValA, 
               const int *csrRowPtrA, const int *csrColIndA,
               const double          *x, const double          *beta, 
               double          *y)
cusparseStatus_t 
cusparseCcsrmv(cusparseHandle_t handle, cusparseOperation_t transA, 
               int m, int n, int nnz, const cuComplex       *alpha, 
               const cusparseMatDescr_t descrA, 
               const cuComplex       *csrValA, 
               const int *csrRowPtrA, const int *csrColIndA,
               const cuComplex       *x, const cuComplex       *beta, 
               cuComplex       *y)
cusparseStatus_t 
cusparseZcsrmv(cusparseHandle_t handle, cusparseOperation_t transA, 
               int m, int n, int nnz, const cuDoubleComplex *alpha, 
               const cusparseMatDescr_t descrA, 
               const cuDoubleComplex *csrValA, 
               const int *csrRowPtrA, const int *csrColIndA, 
               const cuDoubleComplex *x, const cuDoubleComplex *beta, 
               cuDoubleComplex *y)

This function performs the matrix-vector operation

y = α op ( A ) x + β y

A is an m×n sparse matrix that is defined in CSR storage format by the three arrays csrValA, csrRowPtrA, and csrColIndA); x and y are vectors; α  and  β are scalars; and

op ( A ) = A if trans == CUSPARSE_OPERATION_NON_TRANSPOSE A T if trans == CUSPARSE_OPERATION_TRANSPOSE A H if trans == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE

When using the (conjugate) transpose of a general matrix or a Hermitian/symmetric matrix, this routine may produce slightly different results during different runs with the same input parameters. For these matrix types it uses atomic operations to compute the final result, consequently many threads may be adding floating point numbers to the same memory location without any specific ordering, which may produce slightly different results for each run.

If exactly the same output is required for any input when multiplying by the transpose of a general matrix, the following procedure can be used:

1. Convert the matrix from CSR to CSC format using one of the csr2csc() functions. Notice that by interchanging the rows and columns of the result you are implicitly transposing the matrix.

2. Call the csrmv() function with the cusparseOperation_t parameter set to CUSPARSE_OPERATION_NON_TRANSPOSE and with the interchanged rows and columns of the matrix stored in CSC format. This (implicitly) multiplies the vector by the transpose of the matrix in the original CSR format.

This function requires no extra storage for the general matrices when operation CUSPARSE_OPERATION_NON_TRANSPOSE is selected. It requires some extra storage for Hermitian/symmetric matrices and for the general matrices when an operation different than CUSPARSE_OPERATION_NON_TRANSPOSE is selected. It is executed asynchronously with respect to the host and may return control to the application on the host before the result is ready.

Input
handle handle to the cuSPARSE library context.
trans the operation op ( A ) .
m number of rows of matrix A.
n number of columns of matrix A.
nnz number of nonzero elements of matrix A.
alpha <type> scalar used for multiplication.
descrA the descriptor of matrix A. The supported matrix type is CUSPARSE_MATRIX_TYPE_GENERAL, CUSPARSE_MATRIX_TYPE_SYMMETRIC, and CUSPARSE_MATRIX_TYPE_HERMITIAN. Also, the supported index bases are CUSPARSE_INDEX_BASE_ZERO and CUSPARSE_INDEX_BASE_ONE.
csrValA <type> array of nnz ( = csrRowPtrA(m) - csrRowPtrA(0) ) nonzero elements of matrix A.
csrRowPtrA integer array of m+1 elements that contains the start of every row and the end of the last row plus one.
csrColIndA integer array of nnz ( = csrRowPtrA(m) - csrRowPtrA(0) ) column indices of the nonzero elements of matrix A.
x <type> vector of n elements if op ( A ) = A , and m elements if op ( A ) = A T or op ( A ) = A H
beta <type> scalar used for multiplication. If beta is zero, y does not have to be a valid input.
y <type> vector of m elements if op ( A ) = A , and n elements if op ( A ) = A T or op ( A ) = A H
Output
y <type> updated vector.
Status Returned
CUSPARSE_STATUS_SUCCESS the operation completed successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.
CUSPARSE_STATUS_ALLOC_FAILED the resources could not be allocated.
CUSPARSE_STATUS_INVALID_VALUE invalid parameters were passed (m,n,nnz<0).
CUSPARSE_STATUS_ARCH_MISMATCH the device does not support double precision (compute capability (c.c.) >= 1.3 required), symmetric/Hermitian matrix (c.c. >= 1.2 required), or transpose operation (c.c. >= 1.1 required).
CUSPARSE_STATUS_INTERNAL_ERROR an internal operation failed.
CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED the matrix type is not supported.

7.4. cusparse<t>bsrsv2_bufferSize()

cusparseStatus_t 
cusparseSbsrsv2_bufferSize(cusparseHandle_t handle,
                           cusparseDirection_t dirA,
                           cusparseOperation_t transA,
                           int mb,
                           int nnzb,
                           const cusparseMatDescr_t descrA,
                           float *bsrValA,
                           const int *bsrRowPtrA,
                           const int *bsrColIndA,
                           int blockDim,
                           bsrsv2Info_t info,
                           int *pBufferSizeInBytes);

cusparseStatus_t 
cusparseDbsrsv2_bufferSize(cusparseHandle_t handle,
                           cusparseDirection_t dirA,
                           cusparseOperation_t transA,
                           int mb,
                           int nnzb,
                           const cusparseMatDescr_t descrA,
                           double *bsrValA,
                           const int *bsrRowPtrA,
                           const int *bsrColIndA,
                           int blockDim,
                           bsrsv2Info_t info,
                           int *pBufferSizeInBytes);

cusparseStatus_t 
cusparseCbsrsv2_bufferSize(cusparseHandle_t handle,
                           cusparseDirection_t dirA,
                           cusparseOperation_t transA,
                           int mb,
                           int nnzb,
                           const cusparseMatDescr_t descrA,
                           cuComplex *bsrValA,
                           const int *bsrRowPtrA,
                           const int *bsrColIndA,
                           int blockDim,
                           bsrsv2Info_t info,
                           int *pBufferSizeInBytes);

cusparseStatus_t 
cusparseZbsrsv2_bufferSize(cusparseHandle_t handle,
                           cusparseDirection_t dirA,
                           cusparseOperation_t transA,
                           int mb,
                           int nnzb,
                           const cusparseMatDescr_t descrA,
                           cuDoubleComplex *bsrValA,
                           const int *bsrRowPtrA,
                           const int *bsrColIndA,
                           int blockDim,
                           bsrsv2Info_t info,
                           int *pBufferSizeInBytes);

This function returns size of the buffer used in bsrsv2, a new sparse triangular linear system op(A)*y = α x.

A is an (mb*blockDim)x(mb*blockDim) sparse matrix that is defined in BSR storage format by the three arrays bsrValA, bsrRowPtrA, and bsrColIndA); x and y are the right-hand-side and the solution vectors; α is a scalar; and

op ( A ) = A if trans == CUSPARSE_OPERATION_NON_TRANSPOSE A T if trans == CUSPARSE_OPERATION_TRANSPOSE A H if trans == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE

Although there are six combinations in terms of parameter trans and the upper (lower) triangular part of A, bsrsv2_bufferSize() returns the maximum size buffer among these combinations. The buffer size depends on the dimensions mb, blockDim, and the number of nonzero blocks of the matrix nnzb. If the user changes the matrix, it is necessary to call bsrsv2_bufferSize() again to have the correct buffer size; otherwise a segmentation fault may occur.

Input
handle handle to the cuSPARSE library context.
dirA storage format of blocks, either CUSPARSE_DIRECTION_ROW or CUSPARSE_DIRECTION_COLUMN.
transA the operation op ( A ) .
mb number of block rows of matrix A.
nnzb number of nonzero blocks of matrix A.
descrA the descriptor of matrix A. The supported matrix type is CUSPARSE_MATRIX_TYPE_GENERAL, while the supported diagonal types are CUSPARSE_DIAG_TYPE_UNIT and CUSPARSE_DIAG_TYPE_NON_UNIT.
bsrValA <type> array of nnzb ( = bsrRowPtrA(mb) - bsrRowPtrA(0) ) nonzero blocks of matrix A.
bsrRowPtrA integer array of mb + 1 elements that contains the start of every block row and the end of the last block row plus one.
bsrColIndA integer array of nnzb ( = bsrRowPtrA(mb) - bsrRowPtrA(0) ) column indices of the nonzero blocks of matrix A.
blockDim block dimension of sparse matrix A; must be larger than zero.
Output
info record of internal states based on different algorithms.
pBufferSizeInBytes number of bytes of the buffer used in the bsrsv2_analysis() and bsrsv2_solve().
Status Returned
CUSPARSE_STATUS_SUCCESS the operation completed successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.
CUSPARSE_STATUS_INVALID_VALUE invalid parameters were passed (mb,nnzb<=0), base index is not 0 or 1.
CUSPARSE_STATUS_ARCH_MISMATCH the device only supports compute capability 2.0 and above.
CUSPARSE_STATUS_EXECUTION_FAILED the function failed to launch on the GPU.
CUSPARSE_STATUS_INTERNAL_ERROR an internal operation failed.
CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED the matrix type is not supported.

7.5. cusparse<t>bsrsv2_analysis()

cusparseStatus_t 
cusparseSbsrsv2_analysis(cusparseHandle_t handle,
                         cusparseDirection_t dirA,
                         cusparseOperation_t transA,
                         int mb,
                         int nnzb,
                         const cusparseMatDescr_t descrA,
                         const float *bsrValA,
                         const int *bsrRowPtrA,
                         const int *bsrColIndA,
                         int blockDim,
                         bsrsv2Info_t info,
                         cusparseSolvePolicy_t policy,
                         void *pBuffer);

cusparseStatus_t 
cusparseDbsrsv2_analysis(cusparseHandle_t handle,
                         cusparseDirection_t dirA,
                         cusparseOperation_t transA,
                         int mb,
                         int nnzb,
                         const cusparseMatDescr_t descrA,
                         const double *bsrValA,
                         const int *bsrRowPtrA,
                         const int *bsrColIndA,
                         int blockDim,
                         bsrsv2Info_t info,
                         cusparseSolvePolicy_t policy,
                         void *pBuffer);

cusparseStatus_t 
cusparseCbsrsv2_analysis(cusparseHandle_t handle,
                         cusparseDirection_t dirA,
                         cusparseOperation_t transA,
                         int mb,
                         int nnzb,
                         const cusparseMatDescr_t descrA,
                         const cuComplex *bsrValA,
                         const int *bsrRowPtrA,
                         const int *bsrColIndA,
                         int blockDim,
                         bsrsv2Info_t info,
                         cusparseSolvePolicy_t policy,
                         void *pBuffer);

cusparseStatus_t 
cusparseZbsrsv2_analysis(cusparseHandle_t handle,
                         cusparseDirection_t dirA,
                         cusparseOperation_t transA,
                         int mb,
                         int nnzb,
                         const cusparseMatDescr_t descrA,
                         const cuDoubleComplex *bsrValA,
                         const int *bsrRowPtrA,
                         const int *bsrColIndA,
                         int blockDim,
                         bsrsv2Info_t info,
                         cusparseSolvePolicy_t policy,
                         void *pBuffer);

This function performs the analysis phase of bsrsv2, a new sparse triangular linear system op(A)*y = α x.

A is an (mb*blockDim)x(mb*blockDim) sparse matrix that is defined in BSR storage format by the three arrays bsrValA, bsrRowPtrA, and bsrColIndA); x and y are the right-hand side and the solution vectors; α is a scalar; and

op ( A ) = A if trans == CUSPARSE_OPERATION_NON_TRANSPOSE A T if trans == CUSPARSE_OPERATION_TRANSPOSE A H if trans == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE

The block of BSR format is of size blockDim*blockDim, stored as column-major or row-major as determined by parameter dirA, which is either CUSPARSE_DIRECTION_COLUMN or CUSPARSE_DIRECTION_ROW. The matrix type must be CUSPARSE_MATRIX_TYPE_GENERAL, and the fill mode and diagonal type are ignored.

It is expected that this function will be executed only once for a given matrix and a particular operation type.

This function requires a buffer size returned by bsrsv2_bufferSize(). The address of pBuffer must be multiple of 128 bytes. If it is not, CUSPARSE_STATUS_INVALID_VALUE is returned.

Function bsrsv2_analysis() reports a structural zero and computes level information, which stored in the opaque structure info. The level information can extract more parallelism for a triangular solver. However bsrsv2_solve() can be done without level information. To disable level information, the user needs to specify the policy of the triangular solver as CUSPARSE_SOLVE_POLICY_NO_LEVEL.

Function bsrsv2_analysis() always reports the first structural zero, even when parameter policy is CUSPARSE_SOLVE_POLICY_NO_LEVEL. No structural zero is reported if CUSPARSE_DIAG_TYPE_UNIT is specified, even if block A(j,j) is missing for some j. The user needs to call cusparseXbsrsv2_zeroPivot() to know where the structural zero is.

It is the user's choice whether to call bsrsv2_solve() if bsrsv2_analysis() reports a structural zero. In this case, the user can still call bsrsv2_solve(), which will return a numerical zero at the same position as a structural zero. However the result x is meaningless.

Input
handle handle to the cuSPARSE library context.
dirA storage format of blocks, either CUSPARSE_DIRECTION_ROW or CUSPARSE_DIRECTION_COLUMN.
transA the operation op ( A ) .
mb number of block rows of matrix A.
nnzb number of nonzero blocks of matrix A.
descrA the descriptor of matrix A. The supported matrix type is CUSPARSE_MATRIX_TYPE_GENERAL, while the supported diagonal types are CUSPARSE_DIAG_TYPE_UNIT and CUSPARSE_DIAG_TYPE_NON_UNIT.
bsrValA <type> array of nnzb ( = bsrRowPtrA(mb) - bsrRowPtrA(0) ) nonzero blocks of matrix A.
bsrRowPtrA integer array of mb + 1 elements that contains the start of every block row and the end of the last block row plus one.
bsrColIndA integer array of nnzb ( = bsrRowPtrA(mb) - bsrRowPtrA(0) ) column indices of the nonzero blocks of matrix A.
blockDim block dimension of sparse matrix A, larger than zero.
info structure initialized using cusparseCreateBsrsv2Info().
policy the supported policies are CUSPARSE_SOLVE_POLICY_NO_LEVEL and CUSPARSE_SOLVE_POLICY_USE_LEVEL.
pBuffer buffer allocated by the user, the size is return by bsrsv2_bufferSize().
Output
info structure filled with information collected during the analysis phase (that should be passed to the solve phase unchanged).
Status Returned
CUSPARSE_STATUS_SUCCESS the operation completed successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.
CUSPARSE_STATUS_ALLOC_FAILED the resources could not be allocated.
CUSPARSE_STATUS_INVALID_VALUE invalid parameters were passed (mb,nnzb<=0).
CUSPARSE_STATUS_ARCH_MISMATCH the device only supports compute capability 2.0 and above.
CUSPARSE_STATUS_EXECUTION_FAILED the function failed to launch on the GPU.
CUSPARSE_STATUS_INTERNAL_ERROR an internal operation failed.
CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED the matrix type is not supported.

7.6. cusparse<t>bsrsv2_solve()

cusparseStatus_t 
cusparseSbsrsv2_solve(cusparseHandle_t handle,
                      cusparseDirection_t dirA,
                      cusparseOperation_t transA,
                      int mb,
                      int nnzb,
                      const float *alpha,
                      const cusparseMatDescr_t descrA,
                      const float *bsrValA,
                      const int *bsrRowPtrA,
                      const int *bsrColIndA,
                      int blockDim,
                      bsrsv2Info_t info,
                      const float *x,
                      float *y,
                      cusparseSolvePolicy_t policy,
                      void *pBuffer);

cusparseStatus_t 
cusparseDbsrsv2_solve(cusparseHandle_t handle,
                      cusparseDirection_t dirA,
                      cusparseOperation_t transA,
                      int mb,
                      int nnzb,
                      const double *alpha,
                      const cusparseMatDescr_t descrA,
                      const double *bsrValA,
                      const int *bsrRowPtrA,
                      const int *bsrColIndA,
                      int blockDim,
                      bsrsv2Info_t info,
                      const double *x,
                      double *y,
                      cusparseSolvePolicy_t policy,
                      void *pBuffer);

cusparseStatus_t 
cusparseCbsrsv2_solve(cusparseHandle_t handle,
                      cusparseDirection_t dirA,
                      cusparseOperation_t transA,
                      int mb,
                      int nnzb,
                      const cuComplex *alpha,
                      const cusparseMatDescr_t descrA,
                      const cuComplex *bsrValA,
                      const int *bsrRowPtrA,
                      const int *bsrColIndA,
                      int blockDim,
                      bsrsv2Info_t info,
                      const cuComplex *x,
                      cuComplex *y,
                      cusparseSolvePolicy_t policy,
                      void *pBuffer);

cusparseStatus_t 
cusparseZbsrsv2_solve(cusparseHandle_t handle,
                      cusparseDirection_t dirA,
                      cusparseOperation_t transA,
                      int mb,
                      int nnzb,
                      const cuDoubleComplex *alpha,
                      const cusparseMatDescr_t descrA,
                      const cuDoubleComplex *bsrValA,
                      const int *bsrRowPtrA,
                      const int *bsrColIndA,
                      int blockDim,
                      bsrsv2Info_t info,
                      const cuDoubleComplex *x,
                      cuDoubleComplex *y,
                      cusparseSolvePolicy_t policy,
                      void *pBuffer);

This function performs the solve phase of bsrsv2, a new sparse triangular linear system op(A)*y = α x.

A is an (mb*blockDim)x(mb*blockDim) sparse matrix that is defined in BSR storage format by the three arrays bsrValA, bsrRowPtrA, and bsrColIndA); x and y are the right-hand-side and the solution vectors; α is a scalar; and

op ( A ) = A if trans == CUSPARSE_OPERATION_NON_TRANSPOSE A T if trans == CUSPARSE_OPERATION_TRANSPOSE A H if trans == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE

The block in BSR format is of size blockDim*blockDim, stored as column-major or row-major as determined by parameter dirA, which is either CUSPARSE_DIRECTION_COLUMN or CUSPARSE_DIRECTION_ROW. The matrix type must be CUSPARSE_MATRIX_TYPE_GENERAL, and the fill mode and diagonal type are ignored. Function bsrsv02_solve() can support an arbitrary blockDim.

This function may be executed multiple times for a given matrix and a particular operation type.

This function requires a buffer size returned by bsrsv2_bufferSize(). The address of pBuffer must be multiple of 128 bytes. If it is not, CUSPARSE_STATUS_INVALID_VALUE is returned.

Although bsrsv2_solve() can be done without level information, the user still needs to be aware of consistency. If bsrsv2_analysis() is called with policy CUSPARSE_SOLVE_POLICY_USE_LEVEL, bsrsv2_solve() can be run with or without levels. On the other hand, if bsrsv2_analysis() is called with CUSPARSE_SOLVE_POLICY_NO_LEVEL, bsrsv2_solve() can only accept CUSPARSE_SOLVE_POLICY_NO_LEVEL; otherwise, CUSPARSE_STATUS_INVALID_VALUE is returned.

The level information may not improve the performance, but may spend extra time doing analysis. For example, a tridiagonal matrix has no parallelism. In this case, CUSPARSE_SOLVE_POLICY_NO_LEVEL performs better than CUSPARSE_SOLVE_POLICY_USE_LEVEL. If the user has an iterative solver, the best approach is to do bsrsv2_analysis() with CUSPARSE_SOLVE_POLICY_USE_LEVEL once. Then do bsrsv2_solve() with CUSPARSE_SOLVE_POLICY_NO_LEVEL in the first run, and with CUSPARSE_SOLVE_POLICY_USE_LEVEL in the second run, and pick the fastest one to perform the remaining iterations.

Function bsrsv02_solve() has the same behavior as csrsv02_solve(). That is, bsr2csr(bsrsv02(A)) = csrsv02(bsr2csr(A)). The numerical zero of csrsv02_solve() means there exists some zero A(j,j). The numerical zero of bsrsv02_solve() means there exists some block A(j,j) that is not invertible.

Function bsrsv2_solve() reports the first numerical zero, including a structural zero. No numerical zero is reported if CUSPARSE_DIAG_TYPE_UNIT is specified, even if A(j,j) is not invertible for some j. The user needs to call cusparseXbsrsv2_zeroPivot() to know where the numerical zero is.

For example, suppose L is a lower triangular matrix with unit diagonal, then the following code solves L*y=x by level information.

// Suppose that L is m x m sparse matrix represented by BSR format, 
// The number of block rows/columns is mb, and 
// the number of nonzero blocks is nnzb.
// L is lower triangular with unit diagonal. 
// Assumption:
// - dimension of matrix L is m(=mb*blockDim),
// - matrix L has nnz(=nnzb*blockDim*blockDim) nonzero elements,
// - handle is already created by cusparseCreate(),
// - (d_bsrRowPtr, d_bsrColInd, d_bsrVal) is BSR of L on device memory,
// - d_x is right hand side vector on device memory.
// - d_y is solution vector on device memory.
// - d_x and d_y are of size m.
cusparseMatDescr_t descr = 0;
bsrsv2Info_t info = 0;
int pBufferSize;
char *pBuffer = 0;
int structural_zero;
int numerical_zero;
const double alpha = 1.;
const cusparseSolvePolicy_t policy = CUSPARSE_SOLVE_POLICY_USE_LEVEL;
const cusparseOperation_t trans = CUSPARSE_OPERATION_NON_TRANSPOSE;
const cusparseDirection_t dir = CUSPARSE_DIRECTION_COLUMN;

// step 1: create a descriptor which contains
// - matrix L is base-1
// - matrix L is lower triangular
// - matrix L has unit diagonal, specified by parameter CUSPARSE_DIAG_TYPE_UNIT
//   (L may not have all diagonal elements.) 
cusparseCreateMatDescr(&descr);
cusparseSetMatIndexBase(descr, CUSPARSE_INDEX_BASE_ONE);
cusparseSetMatFillMode(descr, CUSPARSE_FILL_MODE_LOWER);
cusparseSetMatDiagType(descr, CUSPARSE_DIAG_TYPE_UNIT);

// step 2: create a empty info structure
cusparseCreateBsrsv2Info(&info);

// step 3: query how much memory used in bsrsv2, and allocate the buffer
cusparseDbsrsv2_bufferSize(handle, dir, trans, mb, nnzb, descr,
    d_bsrVal, d_bsrRowPtr, d_bsrColInd, blockDim, &pBufferSize);

// pBuffer returned by cudaMalloc is automatically aligned to 128 bytes.
cudaMalloc((void**)&pBuffer, pBufferSize);

// step 4: perform analysis 
cusparseDbsrsv2_analysis(handle, dir, trans, mb, nnzb, descr, 
    d_bsrVal, d_bsrRowPtr, d_bsrColInd, blockDim,
    info, policy, pBuffer);
// L has unit diagonal, so no structural zero is reported.
status = cusparseXbsrsv2_zeroPivot(handle, info, &structural_zero);
if (CUSPARSE_STATUS_ZERO_PIVOT == status){
   printf("L(%d,%d) is missing\n", structural_zero, structural_zero);
}

// step 5: solve L*y = x
cusparseDbsrsv2_solve(handle, dir, trans, mb, nnzb, &alpha, descr,
   d_bsrVal, d_bsrRowPtr, d_bsrColInd, blockDim, info,
   d_x, d_y, policy, pBuffer);
// L has unit diagonal, so no numerical zero is reported.
status = cusparseXbsrsv2_zeroPivot(handle, info, &numerical_zero);
if (CUSPARSE_STATUS_ZERO_PIVOT == status){
   printf("L(%d,%d) is zero\n", numerical_zero, numerical_zero);
}

// step 6: free resources
cudaFree(pBuffer);
cusparseDestroyBsrsv2Info(info);
cusparseDestroyMatDescr(descr);
cusparseDestroy(handle);
Input
handle handle to the cuSPARSE library context.
dirA storage format of blocks, either CUSPARSE_DIRECTION_ROW or CUSPARSE_DIRECTION_COLUMN.
transA the operation op ( A ) .
mb number of block rows and block columns of matrix A.
alpha <type> scalar used for multiplication.
descrA the descriptor of matrix A. The supported matrix type is CUSPARSE_MATRIX_TYPE_GENERAL, while the supported diagonal types are CUSPARSE_DIAG_TYPE_UNIT and CUSPARSE_DIAG_TYPE_NON_UNIT.
bsrValA <type> array of nnzb ( = bsrRowPtrA(mb) - bsrRowPtrA(0) ) nonzero blocks of matrix A.
bsrRowPtrA integer array of mb + 1 elements that contains the start of every block row and the end of the last block row plus one.
bsrColIndA integer array of nnzb ( = bsrRowPtrA(mb) - bsrRowPtrA(0) ) column indices of the nonzero blocks of matrix A.
blockDim block dimension of sparse matrix A, larger than zero.
info structure with information collected during the analysis phase (that should have been passed to the solve phase unchanged).
x <type> right-hand-side vector of size m.
policy the supported policies are CUSPARSE_SOLVE_POLICY_NO_LEVEL and CUSPARSE_SOLVE_POLICY_USE_LEVEL.
pBuffer buffer allocated by the user, the size is returned by bsrsv2_bufferSize().
Output
y <type> solution vector of size m.
Status Returned
CUSPARSE_STATUS_SUCCESS the operation completed successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.
CUSPARSE_STATUS_INVALID_VALUE invalid parameters were passed (mb,nnzb<=0).
CUSPARSE_STATUS_ARCH_MISMATCH the device only supports compute capability 2.0 and above.
CUSPARSE_STATUS_MAPPING_ERROR the texture binding failed.
CUSPARSE_STATUS_EXECUTION_FAILED the function failed to launch on the GPU.
CUSPARSE_STATUS_INTERNAL_ERROR an internal operation failed.
CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED the matrix type is not supported.

cusparseXbsrsv2_zeroPivot()

cusparseStatus_t 
cusparseXbsrsv2_zeroPivot(cusparseHandle_t handle,
                          bsrsv2Info_t info,
                          int *position);

If the returned error code is CUSPARSE_STATUS_ZERO_PIVOT, position=j means A(j,j) is either structural zero or numerical zero (singular block). Otherwise position=-1.

The position can be 0-based or 1-based, the same as the matrix.

Function cusparseXbsrsv2_zeroPivot() is a blocking call. It calls cudaDeviceSynchronize() to make sure all previous kernels are done.

The position can be in the host memory or device memory. The user can set the proper mode with cusparseSetPointerMode().

Input
handle handle to the cuSPARSE library context.
info info contains a structural zero or numerical zero if the user already called bsrsv2_analysis() or bsrsv2_solve().
Output
position if no structural or numerical zero, position is -1; otherwise if A(j,j) is missing or U(j,j) is zero, position=j.
Status Returned
CUSPARSE_STATUS_SUCCESS the operation completed successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.
CUSPARSE_STATUS_INVALID_VALUE info is not valid.
CUSPARSE_STATUS_INTERNAL_ERROR an internal operation failed.

7.8. cusparse<t>csrsv_analysis()

cusparseStatus_t 
cusparseScsrsv_analysis(cusparseHandle_t handle, 
                        cusparseOperation_t transA, 
                        int m, int nnz, const cusparseMatDescr_t descrA,
                        const float           *csrValA, 
                        const int *csrRowPtrA, const int *csrColIndA, 
                        cusparseSolveAnalysisInfo_t info)    
cusparseStatus_t 
cusparseDcsrsv_analysis(cusparseHandle_t handle, 
                        cusparseOperation_t transA, 
                        int m, int nnz, const cusparseMatDescr_t descrA,
                        const double          *csrValA, 
                        const int *csrRowPtrA, const int *csrColIndA, 
                        cusparseSolveAnalysisInfo_t info)    
cusparseStatus_t 
cusparseCcsrsv_analysis(cusparseHandle_t handle, 
                        cusparseOperation_t transA, 
                        int m, int nnz, const cusparseMatDescr_t descrA,
                        const cuComplex       *csrValA, 
                        const int *csrRowPtrA, const int *csrColIndA, 
                        cusparseSolveAnalysisInfo_t info)    
cusparseStatus_t 
cusparseZcsrsv_analysis(cusparseHandle_t handle, 
                        cusparseOperation_t transA, 
                        int m, int nnz, const cusparseMatDescr_t descrA,
                        const cuDoubleComplex *csrValA, 
                        const int *csrRowPtrA, const int *csrColIndA, 
                        cusparseSolveAnalysisInfo_t info)

This function performs the analysis phase of the solution of a sparse triangular linear system

op ( A ) y = α x

where A is an m×m sparse matrix that is defined in CSR storage format by the three arrays csrValA, csrRowPtrA, and csrColIndA); x and y are the right-hand-side and the solution vectors; α is a scalar; and

op ( A ) = A if trans == CUSPARSE_OPERATION_NON_TRANSPOSE A T if trans == CUSPARSE_OPERATION_TRANSPOSE A H if trans == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE

It is expected that this function will be executed only once for a given matrix and a particular operation type.

This function requires a significant amount of extra storage that is proportional to the matrix size. It is executed asynchronously with respect to the host and may return control to the application on the host before the result is ready.

Input
handle handle to the cuSPARSE library context.
trans the operation op ( A )
m number of rows of matrix A.
nnz number of nonzero elements of matrix A.
descrA the descriptor of matrix A . The supported matrix types are CUSPARSE_MATRIX_TYPE_TRIANGULAR and CUSPARSE_MATRIX_TYPE_GENERAL, while the supported diagonal types are CUSPARSE_DIAG_TYPE_UNIT and CUSPARSE_DIAG_TYPE_NON_UNIT.
csrValA <type> array of nnz ( = csrRowPtrA(m) - csrRowPtrA(0) ) nonzero elements of matrix A.
csrRowPtrA integer array of m + 1 elements that contains the start of every row and the end of the last row plus one.
csrColIndA integer array of nnz ( = csrRowPtrA(m) - csrRowPtrA(0) ) column indices of the nonzero elements of matrix A.
info structure initialized using cusparseCreateSolveAnalysisInfo.
Output
info structure filled with information collected during the analysis phase (that should be passed to the solve phase unchanged).
Status Returned
CUSPARSE_STATUS_SUCCESS the operation completed successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.
CUSPARSE_STATUS_ALLOC_FAILED the resources could not be allocated.
CUSPARSE_STATUS_INVALID_VALUE invalid parameters were passed (m,nnz<0).
CUSPARSE_STATUS_ARCH_MISMATCH the device does not support double precision.
CUSPARSE_STATUS_EXECUTION_FAILED the function failed to launch on the GPU.
CUSPARSE_STATUS_INTERNAL_ERROR an internal operation failed.
CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED the matrix type is not supported.

7.9. cusparse<t>csrsv_solve()

cusparseStatus_t 
cusparseScsrsv_solve(cusparseHandle_t handle, 
                     cusparseOperation_t transA, 
                     int m, const float           *alpha, 
                     const cusparseMatDescr_t descrA,
                     const float           *csrValA, 
                     const int *csrRowPtrA, const int *csrColIndA, 
                     cusparseSolveAnalysisInfo_t info,
                     const float           *x, float           *y)
cusparseStatus_t 
cusparseDcsrsv_solve(cusparseHandle_t handle, 
                     cusparseOperation_t transA, 
                     int m, const double          *alpha, 
                     const cusparseMatDescr_t descrA,
                     const double          *csrValA, 
                     const int *csrRowPtrA, const int *csrColIndA, 
                     cusparseSolveAnalysisInfo_t info,
                     const double          *x, double          *y)
cusparseStatus_t 
cusparseCcsrsv_solve(cusparseHandle_t handle, 
                     cusparseOperation_t transA, 
                     int m, const cuComplex       *alpha, 
                     const cusparseMatDescr_t descrA,
                     const cuComplex       *csrValA, 
                     const int *csrRowPtrA, const int *csrColIndA, 
                     cusparseSolveAnalysisInfo_t info,
                     const cuComplex       *x, cuComplex       *y)
cusparseStatus_t 
cusparseZcsrsv_solve(cusparseHandle_t handle, 
                     cusparseOperation_t transA, 
                     int m, const cuDoubleComplex *alpha, 
                     const cusparseMatDescr_t descrA,
                     const cuDoubleComplex *csrValA, 
                     const int *csrRowPtrA, const int *csrColIndA, 
                     cusparseSolveAnalysisInfo_t info,
                     const cuDoubleComplex *x, cuDoubleComplex *y)

This function performs the solve phase of the solution of a sparse triangular linear system

op ( A ) y = α x

where A is an m×m sparse matrix that is defined in CSR storage format by the three arrays csrValA, csrRowPtrA, and csrColIndA); x and y are the right-hand-side and the solution vectors; α is a scalar; and

op ( A ) = A if trans == CUSPARSE_OPERATION_NON_TRANSPOSE A T if trans == CUSPARSE_OPERATION_TRANSPOSE A H if trans == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE

This function may be executed multiple times for a given matrix and a particular operation type.

This function requires no extra storage. It is executed asynchronously with respect to the host and may return control to the application on the host before the result is ready.

Input
handle handle to the cuSPARSE library context.
trans the operation op ( A )
m number of rows and columns of matrix A.
alpha <type> scalar used for multiplication.
descrA the descriptor of matrix A. The supported matrix types are CUSPARSE_MATRIX_TYPE_TRIANGULAR and CUSPARSE_MATRIX_TYPE_GENERAL, while the supported diagonal types are CUSPARSE_DIAG_TYPE_UNIT and CUSPARSE_DIAG_TYPE_NON_UNIT.
csrValA <type> array of nnz ( = csrRowPtrA(m) - csrRowPtrA(0) ) nonzero elements of matrix A.
csrRowPtrA integer array of m + 1 elements that contains the start of every row and the end of the last row plus one.
csrColIndA integer array of nnz ( = csrRowPtrA(m) - csrRowPtrA(0) ) column indices of the nonzero elements of matrix A.
info structure with information collected during the analysis phase (that should have been passed to the solve phase unchanged).
x <type> right-hand-side vector of size m.
Output
y <type> solution vector of size m.
Status Returned
CUSPARSE_STATUS_SUCCESS the operation completed successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.
CUSPARSE_STATUS_INVALID_VALUE invalid parameters were passed (m<0).
CUSPARSE_STATUS_ARCH_MISMATCH the device does not support double precision.
CUSPARSE_STATUS_MAPPING_ERROR the texture binding failed.
CUSPARSE_STATUS_EXECUTION_FAILED the function failed to launch on the GPU.
CUSPARSE_STATUS_INTERNAL_ERROR an internal operation failed.
CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED the matrix type is not supported.

7.10. cusparse<t>csrsv2_bufferSize()

cusparseStatus_t 
cusparseScsrsv2_bufferSize(cusparseHandle_t handle,
                           cusparseOperation_t transA,
                           int m,
                           int nnz,
                           const cusparseMatDescr_t descrA,
                           float *csrValA,
                           const int *csrRowPtrA,
                           const int *csrColIndA,
                           csrsv2Info_t info,
                           int *pBufferSizeInBytes);

cusparseStatus_t 
cusparseDcsrsv2_bufferSize(cusparseHandle_t handle,
                           cusparseOperation_t transA,
                           int m,
                           int nnz,
                           const cusparseMatDescr_t descrA,
                           double *csrValA,
                           const int *csrRowPtrA,
                           const int *csrColIndA,
                           csrsv2Info_t info,
                           int *pBufferSizeInBytes);

cusparseStatus_t 
cusparseCcsrsv2_bufferSize(cusparseHandle_t handle,
                           cusparseOperation_t transA,
                           int m,
                           int nnz,
                           const cusparseMatDescr_t descrA,
                           cuComplex *csrValA,
                           const int *csrRowPtrA,
                           const int *csrColIndA,
                           csrsv2Info_t info,
                           int *pBufferSizeInBytes);

cusparseStatus_t 
cusparseZcsrsv2_bufferSize(cusparseHandle_t handle,
                           cusparseOperation_t transA,
                           int m,
                           int nnz,
                           const cusparseMatDescr_t descrA,
                           cuDoubleComplex *csrValA,
                           const int *csrRowPtrA,
                           const int *csrColIndA,
                           csrsv2Info_t info,
                           int *pBufferSizeInBytes);

This function returns the size of the buffer used in csrsv2, a new sparse triangular linear system op(A)*y = α x.

A is an m×m sparse matrix that is defined in CSR storage format by the three arrays csrValA, csrRowPtrA, and csrColIndA); x and y are the right-hand-side and the solution vectors; α is a scalar; and

op ( A ) = A if trans == CUSPARSE_OPERATION_NON_TRANSPOSE A T if trans == CUSPARSE_OPERATION_TRANSPOSE A H if trans == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE

Although there are six combinations in terms of the parameter trans and the upper (lower) triangular part of A, csrsv2_bufferSize() returns the maximum size buffer of these combinations. The buffer size depends on the dimension and the number of nonzero elements of the matrix. If the user changes the matrix, it is necessary to call csrsv2_bufferSize() again to have the correct buffer size; otherwise, a segmentation fault may occur.

Input
handle handle to the cuSPARSE library context.
transA the operation op ( A ) .
m number of rows of matrix A.
nnz number of nonzero elements of matrix A.
descrA the descriptor of matrix A. The supported matrix type is CUSPARSE_MATRIX_TYPE_GENERAL, while the supported diagonal types are CUSPARSE_DIAG_TYPE_UNIT and CUSPARSE_DIAG_TYPE_NON_UNIT.
csrValA <type> array of nnz ( = csrRowPtrA(m) - csrRowPtrA(0) ) nonzero elements of matrix A.
csrRowPtrA integer array of m + 1 elements that contains the start of every row and the end of the last row plus one.
csrColIndA integer array of nnz ( = csrRowPtrA(m) - csrRowPtrA(0) ) column indices of the nonzero elements of matrix A.
Output
info record of internal states based on different algorithms.
pBufferSizeInBytes number of bytes of the buffer used in the csrsv2_analysis and csrsv2_solve.
Status Returned
CUSPARSE_STATUS_SUCCESS the operation completed successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.
CUSPARSE_STATUS_INVALID_VALUE invalid parameters were passed (m,nnz<=0), base index is not 0 or 1.
CUSPARSE_STATUS_ARCH_MISMATCH the device only supports compute capability 2.0 and above.
CUSPARSE_STATUS_EXECUTION_FAILED the function failed to launch on the GPU.
CUSPARSE_STATUS_INTERNAL_ERROR an internal operation failed.
CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED the matrix type is not supported.

7.11. cusparse<t>csrsv2_analysis()

cusparseStatus_t 
cusparseScsrsv2_analysis(cusparseHandle_t handle,
                         cusparseOperation_t transA,
                         int m,
                         int nnz,
                         const cusparseMatDescr_t descrA,
                         const float *csrValA,
                         const int *csrRowPtrA,
                         const int *csrColIndA,
                         csrsv2Info_t info,
                         cusparseSolvePolicy_t policy,
                         void *pBuffer);

cusparseStatus_t 
cusparseDcsrsv2_analysis(cusparseHandle_t handle,
                         cusparseOperation_t transA,
                         int m,
                         int nnz,
                         const cusparseMatDescr_t descrA,
                         const double *csrValA,
                         const int *csrRowPtrA,
                         const int *csrColIndA,
                         csrsv2Info_t info,
                         cusparseSolvePolicy_t policy,
                         void *pBuffer);

cusparseStatus_t 
cusparseCcsrsv2_analysis(cusparseHandle_t handle,
                         cusparseOperation_t transA,
                         int m,
                         int nnz,
                         const cusparseMatDescr_t descrA,
                         const cuComplex *csrValA,
                         const int *csrRowPtrA,
                         const int *csrColIndA,
                         csrsv2Info_t info,
                         cusparseSolvePolicy_t policy,
                         void *pBuffer);

cusparseStatus_t 
cusparseZcsrsv2_analysis(cusparseHandle_t handle,
                         cusparseOperation_t transA,
                         int m,
                         int nnz,
                         const cusparseMatDescr_t descrA,
                         const cuDoubleComplex *csrValA,
                         const int *csrRowPtrA,
                         const int *csrColIndA,
                         csrsv2Info_t info,
                         cusparseSolvePolicy_t policy,
                         void *pBuffer);

This function performs the analysis phase of csrsv2, a new sparse triangular linear system op(A)*y = α x.

A is an m×m sparse matrix that is defined in CSR storage format by the three arrays csrValA, csrRowPtrA, and csrColIndA); x and y are the right-hand-side and the solution vectors; α is a scalar; and

op ( A ) = A if trans == CUSPARSE_OPERATION_NON_TRANSPOSE A T if trans == CUSPARSE_OPERATION_TRANSPOSE A H if trans == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE

It is expected that this function will be executed only once for a given matrix and a particular operation type.

This function requires a buffer size returned by csrsv2_bufferSize(). The address of pBuffer must be multiple of 128 bytes. If it is not, CUSPARSE_STATUS_INVALID_VALUE is returned.

Function csrsv2_analysis() reports a structural zero and computes level information that is stored in opaque structure info. The level information can extract more parallelism for a triangular solver. However csrsv2_solve() can be done without level information. To disable level information, the user needs to specify the policy of the triangular solver as CUSPARSE_SOLVE_POLICY_NO_LEVEL.

Function csrsv2_analysis() always reports the first structural zero, even if the policy is CUSPARSE_SOLVE_POLICY_NO_LEVEL. No structural zero is reported if CUSPARSE_DIAG_TYPE_UNIT is specified, even if A(j,j) is missing for some j. The user needs to call cusparseXcsrsv2_zeroPivot() to know where the structural zero is.

It is the user's choice whether to call csrsv2_solve() if csrsv2_analysis() reports a structural zero. In this case, the user can still call csrsv2_solve() which will return a numerical zero in the same position as the structural zero. However the result x is meaningless.

Input
handle handle to the cuSPARSE library context.
transA the operation op ( A ) .
m number of rows of matrix A.
nnz number of nonzero elements of matrix A.
descrA the descriptor of matrix A. The supported matrix type is CUSPARSE_MATRIX_TYPE_GENERAL, while the supported diagonal types are CUSPARSE_DIAG_TYPE_UNIT and CUSPARSE_DIAG_TYPE_NON_UNIT.
csrValA <type> array of nnz ( = csrRowPtrA(m) - csrRowPtrA(0) ) nonzero elements of matrix A.
csrRowPtrA integer array of m + 1 elements that contains the start of every row and the end of the last row plus one.
csrColIndA integer array of nnz ( = csrRowPtrA(m) - csrRowPtrA(0) ) column indices of the nonzero elements of matrix A.
info structure initialized using cusparseCreateCsrsv2Info().
policy The supported policies are CUSPARSE_SOLVE_POLICY_NO_LEVEL and CUSPARSE_SOLVE_POLICY_USE_LEVEL.
pBuffer buffer allocated by the user, the size is returned by csrsv2_bufferSize().
Output
info structure filled with information collected during the analysis phase (that should be passed to the solve phase unchanged).
Status Returned
CUSPARSE_STATUS_SUCCESS the operation completed successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.
CUSPARSE_STATUS_ALLOC_FAILED the resources could not be allocated.
CUSPARSE_STATUS_INVALID_VALUE invalid parameters were passed (m,nnz<=0).
CUSPARSE_STATUS_ARCH_MISMATCH the device only supports compute capability 2.0 and above.
CUSPARSE_STATUS_EXECUTION_FAILED the function failed to launch on the GPU.
CUSPARSE_STATUS_INTERNAL_ERROR an internal operation failed.
CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED the matrix type is not supported.

7.12. cusparse<t>csrsv2_solve()

cusparseStatus_t 
cusparseScsrsv2_solve(cusparseHandle_t handle,
                      cusparseOperation_t transA,
                      int m,
                      int nnz,
                      const float *alpha,
                      const cusparseMatDescr_t descra,
                      const float *csrValA,
                      const int *csrRowPtrA,
                      const int *csrColIndA,
                      csrsv2Info_t info,
                      const float *x,
                      float *y,
                      cusparseSolvePolicy_t policy,
                      void *pBuffer);

cusparseStatus_t 
cusparseDcsrsv2_solve(cusparseHandle_t handle,
                      cusparseOperation_t transA,
                      int m,
                      int nnz,
                      const double *alpha,
                      const cusparseMatDescr_t descra,
                      const double *csrValA,
                      const int *csrRowPtrA,
                      const int *csrColIndA,
                      csrsv2Info_t info,
                      const double *x,
                      double *y,
                      cusparseSolvePolicy_t policy,
                      void *pBuffer);

cusparseStatus_t 
cusparseCcsrsv2_solve(cusparseHandle_t handle,
                      cusparseOperation_t transA,
                      int m,
                      int nnz,
                      const cuComplex *alpha,
                      const cusparseMatDescr_t descra,
                      const cuComplex *csrValA,
                      const int *csrRowPtrA,
                      const int *csrColIndA,
                      csrsv2Info_t info,
                      const cuComplex *x,
                      cuComplex *y,
                      cusparseSolvePolicy_t policy,
                      void *pBuffer);

cusparseStatus_t 
cusparseZcsrsv2_solve(cusparseHandle_t handle,
                      cusparseOperation_t transA,
                      int m,
                      int nnz,
                      const cuDoubleComplex *alpha,
                      const cusparseMatDescr_t descra,
                      const cuDoubleComplex *csrValA,
                      const int *csrRowPtrA,
                      const int *csrColIndA,
                      csrsv2Info_t info,
                      const cuDoubleComplex *x,
                      cuDoubleComplex *y,
                      cusparseSolvePolicy_t policy,
                      void *pBuffer);

This function performs the solve phase of csrsv2, a new sparse triangular linear system op(A)*y = α x.

A is an m×m sparse matrix that is defined in CSR storage format by the three arrays csrValA, csrRowPtrA, and csrColIndA); x and y are the right-hand-side and the solution vectors; α is a scalar; and

op ( A ) = A if trans == CUSPARSE_OPERATION_NON_TRANSPOSE A T if trans == CUSPARSE_OPERATION_TRANSPOSE A H if trans == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE

This function may be executed multiple times for a given matrix and a particular operation type.

This function requires the buffer size returned by csrsv2_bufferSize(). The address of pBuffer must be multiple of 128 bytes. If it is not, CUSPARSE_STATUS_INVALID_VALUE is returned.

Although csrsv2_solve() can be done without level information, the user still needs to be aware of consistency. If csrsv2_analysis() is called with policy CUSPARSE_SOLVE_POLICY_USE_LEVEL, csrsv2_solve() can be run with or without levels. On the contrary, if csrsv2_analysis() is called with CUSPARSE_SOLVE_POLICY_NO_LEVEL, csrsv2_solve() can only accept CUSPARSE_SOLVE_POLICY_NO_LEVEL; otherwise, CUSPARSE_STATUS_INVALID_VALUE is returned.

The level information may not improve the performance but spend extra time doing analysis. For example, a tridiagonal matrix has no parallelism. In this case, CUSPARSE_SOLVE_POLICY_NO_LEVEL performs better than CUSPARSE_SOLVE_POLICY_USE_LEVEL. If the user has an iterative solver, the best approach is to do csrsv2_analysis() with CUSPARSE_SOLVE_POLICY_USE_LEVEL once. Then do csrsv2_solve() with CUSPARSE_SOLVE_POLICY_NO_LEVEL in the first run and with CUSPARSE_SOLVE_POLICY_USE_LEVEL in the second run, picking faster one to perform the remaining iterations.

Function csrsv2_solve() reports the first numerical zero, including a structural zero. If status is 0, no numerical zero was found. Furthermore, no numerical zero is reported if CUSPARSE_DIAG_TYPE_UNIT is specified, even if A(j,j) is zero for some j. The user needs to call cusparseXcsrsv2_zeroPivot() to know where the numerical zero is.

For example, suppose L is a lower triangular matrix with unit diagonal, the following code solves L*y=x by level information.

// Suppose that L is m x m sparse matrix represented by CSR format, 
// L is lower triangular with unit diagonal. 
// Assumption:
// - dimension of matrix L is m,
// - matrix L has nnz number zero elements,
// - handle is already created by cusparseCreate(),
// - (d_csrRowPtr, d_csrColInd, d_csrVal) is CSR of L on device memory,
// - d_x is right hand side vector on device memory,
// - d_y is solution vector on device memory.

cusparseMatDescr_t descr = 0;
csrsv2Info_t info = 0;
int pBufferSize;
char *pBuffer = 0;
int structural_zero;
int numerical_zero;
const double alpha = 1.;
const cusparseSolvePolicy_t policy = CUSPARSE_SOLVE_POLICY_USE_LEVEL;
const cusparseOperation_t trans = CUSPARSE_OPERATION_NON_TRANSPOSE;

// step 1: create a descriptor which contains
// - matrix L is base-1
// - matrix L is lower triangular
// - matrix L has unit diagonal, specified by parameter CUSPARSE_DIAG_TYPE_UNIT
//   (L may not have all diagonal elements.) 
cusparseCreateMatDescr(&descr);
cusparseSetMatIndexBase(descr, CUSPARSE_INDEX_BASE_ONE);
cusparseSetMatFillMode(descr, CUSPARSE_FILL_MODE_LOWER);
cusparseSetMatDiagType(descr, CUSPARSE_DIAG_TYPE_UNIT);

// step 2: create a empty info structure
cusparseCreateCsrsv2Info(&info);

// step 3: query how much memory used in csrsv2, and allocate the buffer
cusparseDcsrsv2_bufferSize(handle, trans, m, nnz, descr,
    d_csrVal, d_csrRowPtr, d_csrColInd, &pBufferSize);
// pBuffer returned by cudaMalloc is automatically aligned to 128 bytes.
cudaMalloc((void**)&pBuffer, pBufferSize);

// step 4: perform analysis 
cusparseDcsrsv2_analysis(handle, trans, m, nnz, descr, 
    d_csrVal, d_csrRowPtr, d_csrColInd,
    info, policy, pBuffer);
// L has unit diagonal, so no structural zero is reported.
status = cusparseXcsrsv2_zeroPivot(handle, info, &structural_zero);
if (CUSPARSE_STATUS_ZERO_PIVOT == status){
   printf("L(%d,%d) is missing\n", structural_zero, structural_zero);
}

// step 5: solve L*y = x
cusparseDcsrsv2_solve(handle, trans, m, nnz, &alpha, descr,
   d_csrVal, d_csrRowPtr, d_csrColInd, info,
   d_x, d_y, policy, pBuffer);
// L has unit diagonal, so no numerical zero is reported.
status = cusparseXcsrsv2_zeroPivot(handle, info, &numerical_zero);
if (CUSPARSE_STATUS_ZERO_PIVOT == status){
   printf("L(%d,%d) is zero\n", numerical_zero, numerical_zero);
}

// step 6: free resources
cudaFree(pBuffer);
cusparseDestroyCsrsv2Info(info);
cusparseDestroyMatDescr(descr);
cusparseDestroy(handle);
Input
handle handle to the cuSPARSE library context.
transA the operation op ( A ) .
m number of rows and columns of matrix A.
alpha <type> scalar used for multiplication.
descrA the descriptor of matrix A. The supported matrix type is CUSPARSE_MATRIX_TYPE_GENERAL, while the supported diagonal types are CUSPARSE_DIAG_TYPE_UNIT and CUSPARSE_DIAG_TYPE_NON_UNIT.
csrValA <type> array of nnz ( = csrRowPtrA(m) - csrRowPtrA(0) ) nonzero elements of matrix A.
csrRowPtrA integer array of m + 1 elements that contains the start of every row and the end of the last row plus one.
csrColIndA integer array of nnz ( = csrRowPtrA(m) - csrRowPtrA(0) ) column indices of the nonzero elements of matrix A.
info structure with information collected during the analysis phase (that should have been passed to the solve phase unchanged).
x <type> right-hand-side vector of size m.
policy The supported policies are CUSPARSE_SOLVE_POLICY_NO_LEVEL and CUSPARSE_SOLVE_POLICY_USE_LEVEL.
pBuffer buffer allocated by the user, the size is return by csrsv2_bufferSize.
Output
y <type> solution vector of size m.
Status Returned
CUSPARSE_STATUS_SUCCESS the operation completed successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.
CUSPARSE_STATUS_INVALID_VALUE invalid parameters were passed (m,nnz<=0).
CUSPARSE_STATUS_ARCH_MISMATCH the device only supports compute capability 2.0 and above.
CUSPARSE_STATUS_MAPPING_ERROR the texture binding failed.
CUSPARSE_STATUS_EXECUTION_FAILED the function failed to launch on the GPU.
CUSPARSE_STATUS_INTERNAL_ERROR an internal operation failed.
CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED the matrix type is not supported.

cusparseXcsrsv2_zeroPivot()

cusparseStatus_t 
cusparseXcsrsv2_zeroPivot(cusparseHandle_t handle,
                          csrsv2Info_t info,
                          int *position);

If the returned error code is CUSPARSE_STATUS_ZERO_PIVOT, position=j means A(j,j) has either a structural zero or a numerical zero. Otherwise position=-1.

The position can be 0-based or 1-based, the same as the matrix.

Function cusparseXcsrsv2_zeroPivot() is a blocking call. It calls cudaDeviceSynchronize() to make sure all previous kernels are done.

The position can be in the host memory or device memory. The user can set the proper mode with cusparseSetPointerMode().

Input
handle handle to the cuSPARSE library context.
info info contains structural zero or numerical zero if the user already called csrsv2_analysis() or csrsv2_solve().
Output
position if no structural or numerical zero, position is -1; otherwise, if A(j,j) is missing or U(j,j) is zero, position=j.
Status Returned
CUSPARSE_STATUS_SUCCESS the operation completed successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.
CUSPARSE_STATUS_INVALID_VALUE info is not valid.
CUSPARSE_STATUS_INTERNAL_ERROR an internal operation failed.

7.14. cusparse<t>hybmv()

cusparseStatus_t
cusparseShybmv(cusparseHandle_t handle, cusparseOperation_t transA,
               const float           *alpha, 
               const cusparseMatDescr_t descrA,
               const cusparseHybMat_t hybA, const float           *x,
               const float           *beta, float           *y)
cusparseStatus_t
cusparseDhybmv(cusparseHandle_t handle, cusparseOperation_t transA,
               const double          *alpha, 
               const cusparseMatDescr_t descrA,
               const cusparseHybMat_t hybA, const double          *x,
               const double          *beta, double          *y)
cusparseStatus_t
cusparseChybmv(cusparseHandle_t handle, cusparseOperation_t transA,
               const cuComplex       *alpha, 
               const cusparseMatDescr_t descrA,
               const cusparseHybMat_t hybA, const cuComplex       *x,
               const cuComplex       *beta, cuComplex       *y)
cusparseStatus_t
cusparseZhybmv(cusparseHandle_t handle, cusparseOperation_t transA,
               const cuDoubleComplex *alpha, 
               const cusparseMatDescr_t descrA,
               const cusparseHybMat_t hybA, const cuDoubleComplex *x,
               const cuDoubleComplex *beta, cuDoubleComplex *y)

This function performs the matrix-vector operation

y = α op ( A ) x + β y

A is an m×n sparse matrix that is defined in the HYB storage format by an opaque data structure hybA, x and y are vectors, α  and  β are scalars, and

op ( A ) = A  if transA == CUSPARSE_OPERATION_NON_TRANSPOSE

Notice that currently only op ( A ) = A is supported.

This function requires no extra storage. It is executed asynchronously with respect to the host and may return control to the application on the host before the result is ready.

Input
handle handle to the cuSPARSE library context.
transA the operation op ( A ) (currently only op ( A ) = A is supported).
m number of rows of matrix A.
n number of columns of matrix A.
alpha <type> scalar used for multiplication.
descrA the descriptor of matrix A. The supported matrix type is CUSPARSE_MATRIX_TYPE_GENERAL.
hybA the matrix A in HYB storage format.
x <type> vector of n elements.
beta <type> scalar used for multiplication. If beta is zero, y does not have to be a valid input.
y <type> vector of m elements.
Output
y <type> updated vector.
Status Returned
CUSPARSE_STATUS_SUCCESS the operation completed successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.
CUSPARSE_STATUS_ALLOC_FAILED the resources could not be allocated.
CUSPARSE_STATUS_INVALID_VALUE the internally stored HYB format parameters are invalid.
CUSPARSE_STATUS_ARCH_MISMATCH the device does not support double precision.
CUSPARSE_STATUS_EXECUTION_FAILED the function failed to launch on the GPU.
CUSPARSE_STATUS_INTERNAL_ERROR an internal operation failed.
CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED the matrix type is not supported.

7.15. cusparse<t>hybsv_analysis()

cusparseStatus_t
cusparseShybsv_analysis(cusparseHandle_t handle, 
                        cusparseOperation_t transA,
                        const cusparseMatDescr_t descrA, 
                        cusparseHybMat_t hybA,
                        cusparseSolveAnalysisInfo_t info)    
cusparseStatus_t
cusparseDhybsv_analysis(cusparseHandle_t handle, 
                        cusparseOperation_t transA,
                        const cusparseMatDescr_t descrA, 
                        cusparseHybMat_t hybA,
                        cusparseSolveAnalysisInfo_t info)    
cusparseStatus_t
cusparseChybsv_analysis(cusparseHandle_t handle, 
                        cusparseOperation_t transA,
                        const cusparseMatDescr_t descrA, 
                        cusparseHybMat_t hybA,
                        cusparseSolveAnalysisInfo_t info)    
cusparseStatus_t
cusparseZhybsv_analysis(cusparseHandle_t handle, 
                        cusparseOperation_t transA,
                        const cusparseMatDescr_t descrA, 
                        cusparseHybMat_t hybA,
                        cusparseSolveAnalysisInfo_t info) 

This function performs the analysis phase of the solution of a sparse triangular linear system

op ( A ) y = α x

A is an m×m sparse matrix that is defined in HYB storage format by an opaque data structure hybA, x and y are the right-hand-side and the solution vectors, α is a scalar, and

op ( A ) = A  if transA == CUSPARSE_OPERATION_NON_TRANSPOSE

Notice that currently only op ( A ) = A is supported.

It is expected that this function will be executed only once for a given matrix and a particular operation type.

This function requires a significant amount of extra storage that is proportional to the matrix size. It is executed asynchronously with respect to the host and may return control to the application on the host before the result is ready.

Input
handle handle to the cuSPARSE library context.
transA the operation op ( A ) (currently only op ( A ) = A is supported).
descrA the descriptor of matrix A. The supported matrix type is CUSPARSE_MATRIX_TYPE_TRIANGULAR and diagonal type USPARSE_DIAG_TYPE_NON_UNIT.
hybA the matrix A in HYB storage format.
info structure initialized using cusparseCreateSolveAnalysisInfo().
Output
info structure filled with information collected during the analysis phase (that should be passed to the solve phase unchanged).
Status Returned
CUSPARSE_STATUS_SUCCESS the operation completed successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.
CUSPARSE_STATUS_ALLOC_FAILED the resources could not be allocated.
CUSPARSE_STATUS_INVALID_VALUE the internally stored HYB format parameters are invalid.
CUSPARSE_STATUS_ARCH_MISMATCH the device does not support double precision.
CUSPARSE_STATUS_EXECUTION_FAILED the function failed to launch on the GPU.
CUSPARSE_STATUS_INTERNAL_ERROR an internal operation failed.
CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED the matrix type is not supported.

7.16. cusparse<t>hybsv_solve()

cusparseStatus_t 
cusparseShybsv_solve(cusparseHandle_t handle, 
                     cusparseOperation_t transA,
                     const float           *alpha, 
                     const cusparseMatDescr_t descrA,
                     cusparseHybMat_t hybA, 
                     cusparseSolveAnalysisInfo_t info,
                     const float           *x, float           *y)
cusparseStatus_t 
cusparseDhybsv_solve(cusparseHandle_t handle, 
                     cusparseOperation_t transA,
                     const double          *alpha, 
                     const cusparseMatDescr_t descrA,
                     cusparseHybMat_t hybA, 
                     cusparseSolveAnalysisInfo_t info,
                     const double          *x, double          *y)
cusparseStatus_t 
cusparseChybsv_solve(cusparseHandle_t handle, 
                     cusparseOperation_t transA,
                     const cuComplex       *alpha, 
                     const cusparseMatDescr_t descrA,
                     cusparseHybMat_t hybA, 
                     cusparseSolveAnalysisInfo_t info,
                     const cuComplex       *x, cuComplex       *y)
cusparseStatus_t 
cusparseZhybsv_solve(cusparseHandle_t handle, 
                     cusparseOperation_t transA,
                     const cuDoubleComplex *alpha, 
                     const cusparseMatDescr_t descrA,
                     cusparseHybMat_t hybA, 
                     cusparseSolveAnalysisInfo_t info,
                     const cuDoubleComplex *x, cuDoubleComplex *y)

This function performs the solve phase of the solution of a sparse triangular linear system:

op ( A ) y = α x

A is an m×m sparse matrix that is defined in HYB storage format by an opaque data structure hybA, x and y are the right-hand-side and the solution vectors, α is a scalar, and

op ( A ) = A  if transA == CUSPARSE_OPERATION_NON_TRANSPOSE

Notice that currently only op ( A ) = A is supported.

This function may be executed multiple times for a given matrix and a particular operation type.

This function requires no extra storage. It is executed asynchronously with respect to the host and may return control to the application on the host before the result is ready.

Input
handle handle to the cuSPARSE library context.
transA the operation op ( A ) (currently only op ( A ) = A is supported).
alpha <type> scalar used for multiplication.
descrA the descriptor of matrix A. The supported matrix type is CUSPARSE_MATRIX_TYPE_TRIANGULAR and the diagonal type is CUSPARSE_DIAG_TYPE_NON_UNIT.
hybA the matrix A in HYB storage format.
info structure with information collected during the analysis phase (that should be passed to the solve phase unchanged).
x <type> right-hand-side vector of size m.
Output
y <type> solution vector of size m.
Status Returned
CUSPARSE_STATUS_SUCCESS the operation completed successfully.
CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized.
CUSPARSE_STATUS_INVALID_VALUE the internally stored hyb format parameters are invalid.
CUSPARSE_STATUS_ARCH_MISMATCH the device does not support double precision.
CUSPARSE_STATUS_MAPPING_ERROR the texture binding failed.
CUSPARSE_STATUS_EXECUTION_FAILED the function failed to launch on the GPU.
CUSPARSE_STATUS_INTERNAL_ERROR an internal operation failed.
CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED the matrix type is not supported.

8. cuSPARSE Level 3 Function Reference

This chapter describes sparse linear algebra functions that perform operations between sparse and (usually tall) dense matrices.

In particular, the solution of sparse triangular linear systems with multiple right-hand sides is implemented in two phases. First, during the analysis phase, the sparse triangular matrix is analyzed to determine the dependencies between its elements by calling the appropriate csrsm_analysis() function. The analysis is specific to the sparsity pattern of the given matrix and to the selected cusparseOperation_t type. The information from the analysis phase is stored in the parameter of type cusparseSolveAnalysisInfo_t that has been initialized previously with a call to cusparseCreateSolveAnalysisInfo().

Second, during the solve phase, the given sparse triangular linear system is solved using the information stored in the cusparseSolveAnalysisInfo_t parameter by calling the appropriate csrsm_solve() function. The solve phase may be performed multiple times with different multiple right-hand sides, while the analysis phase needs to be performed only once. This is especially useful when a sparse triangular linear system must be solved for different sets of multiple right-hand sides one at a time, while its coefficient matrix remains the same.

Finally, once all the solves have completed, the opaque data structure pointed to by the cusparseSolveAnalysisInfo_t parameter can be released by calling cusparseDestroySolveAnalysisInfo(). For more information please refer to [3].

8.1. cusparse<t>csrmm()

cusparseStatus_t 
cusparseScsrmm(cusparseHandle_t handle, 
    cusparseOperation_t transA, 
    int m, 
    int n, 
    int k, 
    int nnz, 
    const float *alpha, 
    const cusparseMatDescr_t descrA, 
    const float *csrValA, 
    const int *csrRowPtrA, 
    const int *csrColIndA,
    const float *B, 
    int ldb,
    const float *beta, 
    float *C, 
    int ldc)
cusparseStatus_t 
cusparseDcsrmm(cusparseHandle_t handle, 
    cusparseOperation_t transA, 
    int m, 
    int n, 
    int k, 
    int nnz, 
    const double *alpha, 
    const cusparseMatDescr_t descrA, 
    const double *csrValA, 
    const int *csrRowPtrA, 
    const int *csrColIndA,
    const double *B, 
    int ldb,
    const double *beta,
    double *C, 
    int ldc)
cusparseStatus_t 
cusparseCcsrmm(cusparseHandle_t handle, 
    cusparseOperation_t transA, 
    int m, 
    int n, 
    int k, 
    int nnz, 
    const cuComplex *alpha, 
    const cusparseMatDescr_t descrA, 
    const cuComplex *csrValA, 
    const int *csrRowPtrA, 
    const int *csrColIndA,
    const cuComplex *B, 
    int ldb,
    const cuComplex *beta, 
    cuComplex *C, 
    int ldc)
cusparseStatus_t 
cusparseZcsrmm(cusparseHandle_t handle, 
    cusparseOperation_t transA, 
    int m, 
    int n, 
    int k, 
    int nnz, 
    const cuDoubleComplex *alpha, 
    const cusparseMatDescr_t descrA, 
    const cuDoubleComplex *csrValA, 
    const int *csrRowPtrA, 
    const int *csrColIndA,
    const cuDoubleComplex *B, 
    int ldb,
    const cuDoubleComplex *beta,
    cuDoubleComplex *C, 
    int ldc)

This function performs one of the following matrix-matrix operations:

C = α op ( A ) B + β C

A is an m×k sparse matrix that is defined in CSR storage format by the three arrays csrValA, csrRowPtrA, and csrColIndA); B and C are dense matrices; α  and  β are scalars; and

op ( A ) = A if trans == CUSPARSE_OPERATION_NON_TRANSPOSE A T if trans == CUSPARSE_OPERATION_TRANSPOSE A H if trans == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE

When using the (conjugate) transpose of a general matrix or a Hermitian/symmetric matrix, this routine may produce slightly different results with the same input parameters during different runs of this function. For these matrix types it uses atomic operations to compute the final result; consequently, many threads may be adding floating point numbers to the same memory location without any specific ordering, which may produce slightly different results for each run.

If exactly the same output is required for any input when multiplying by the transpose of a general matrix, the following procedure can be used:

1. Convert the matrix from CSR to CSC format using one of the csr2csc() functions. Notice that by interchanging the rows and columns of the result you are implicitly transposing the matrix.

2. Call the csrmm() function with the cusparseOperation_t parameter set to CUSPARSE_OPERATION_NON_TRANSPOSE and with the interchanged rows and columns of the matrix stored in CSC format. This (implicitly) multiplies the vector by the transpose of the matrix in the original CSR format.

This function requires no extra storage for the general matrices when operation CUSPARSE_OPERATION_NON_TRANSPOSE is selected. It requires some extra storage for Hermitian/symmetric matrices and for the general matrices when an operation different from CUSPARSE_OPERATION_NON_TRANSPOSE is selected. It is executed asynchronously with respect to the host and may return control to the application on the host before the result is ready.

Input
handle handle to the cuSPARSE library context.
transA the operation op ( A )
m number of rows of sparse matrix A.
n number of columns of dense matrices B and C.
k number of columns of sparse matrix A.
nnz number of nonzero elements of sparse matrix A.
alpha <type> scalar used for multiplication.
descrA the descriptor of matrix A. The supported matrix types are CUSPARSE_MATRIX_TYPE_GENERAL, CUSPARSE_MATRIX_TYPE_SYMMETRIC, and CUSPARSE_MATRIX_TYPE_HERMITIAN. Also, the supported index bases are CUSPARSE_INDEX_BASE_ZERO and CUSPARSE_INDEX_BASE_ONE.
csrValA <type> array of nnz ( = csrRowPtrA(m)