## 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, and compression of csr matrices.

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 2.0 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:

```    nvcc myCusparseApp.c  -lcusparse  -o myCusparseApp
```

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

```
nvcc myCusparseApp.c  -lcusparse_static   -lculibos -o myCusparseApp
```

It is also possible to use the native Host C++ compiler. Depending on the Host Operating system, some additional libraries like pthread or dl might be needed on the linking line. The following command on Linux is suggested :

```
g++ myCusparseApp.c  -lcusparse_static   -lculibos -lcudart_static -lpthread -ldl -I <cuda-toolkit-path>/include -L <cuda-toolkit-path>/lib64 -o myCusparseApp
```

Note that in the latter case, the library cuda is not needed. The CUDA Runtime will try to open explicitly the cuda library if needed. In the case of a system which does not have the CUDA driver installed, this allows the application to gracefully manage this issue and potentially run if a CPU-only path is available.

## 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.

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 $\alpha$ and $\beta$ 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 $\alpha$ and $\beta$ 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.

 $\left[\begin{array}{ccccccc}1.0& 0.0& 0.0& 2.0& 3.0& 0.0& 4.0\end{array}\right]$

(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.

 $\begin{array}{rcl}& & \left[\begin{array}{cccc}1.0& 2.0& 3.0& 4.0\end{array}\right]\\ & & \left[\begin{array}{cccc}1\phantom{.0}& 4\phantom{.0}& 5\phantom{.0}& 7\phantom{.0}\end{array}\right]\end{array}$

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

 $\begin{array}{rcl}& & \left[\begin{array}{cccc}1.0& 2.0& 3.0& 4.0\end{array}\right]\\ & & \left[\begin{array}{cccc}0\phantom{.0}& 3\phantom{.0}& 4\phantom{.0}& 6\phantom{.0}\end{array}\right]\end{array}$

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.

 $\left[\begin{array}{cccc}{X}_{1,1}& {X}_{1,2}& \cdots & {X}_{1,n}\\ {X}_{2,1}& {X}_{2,2}& \cdots & {X}_{2,n}\\ ⋮& ⋮& \ddots & ⋮\\ {X}_{m,1}& {X}_{m,2}& \cdots & {X}_{m,n}\\ ⋮& ⋮& \ddots & ⋮\\ {X}_{ldX,1}& {X}_{ldX,2}& \cdots & {X}_{ldX,n}\end{array}\right]$

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

 $\left[\begin{array}{ccccccccccccc}{X}_{1,1}& {X}_{2,1}& \cdots & {X}_{m,1}& \cdots & {X}_{ldX,1}& \cdots & {X}_{1,n}& {X}_{2,n}& \cdots & {X}_{m,n}& \cdots & {X}_{ldX,n}\end{array}\right]$
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.

 $\left[\begin{array}{ccccc}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\end{array}\right]$

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

 $\begin{array}{rcl}\text{cooValA}& =& \left[\begin{array}{ccccccccc}1.0& 4.0& 2.0& 3.0& 5.0& 7.0& 8.0& 9.0& 6.0\end{array}\right]\\ \text{cooRowIndA}& =& \left[\begin{array}{ccccccccc}0\phantom{.0}& 0\phantom{.0}& 1\phantom{.0}& 1\phantom{.0}& 2\phantom{.0}& 2\phantom{.0}& 2\phantom{.0}& 3\phantom{.0}& 3\phantom{.0}\end{array}\right]\\ \text{cooColIndA}& =& \left[\begin{array}{ccccccccc}0\phantom{.0}& 1\phantom{.0}& 1\phantom{.0}& 2\phantom{.0}& 0\phantom{.0}& 3\phantom{.0}& 4\phantom{.0}& 2\phantom{.0}& 4\phantom{.0}\end{array}\right]\end{array}$

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

 $\begin{array}{rcl}\text{cooValA}& =& \left[\begin{array}{ccccccccc}1.0& 4.0& 2.0& 3.0& 5.0& 7.0& 8.0& 9.0& 6.0\end{array}\right]\\ \text{cooRowIndA}& =& \left[\begin{array}{ccccccccc}1\phantom{.0}& 1\phantom{.0}& 2\phantom{.0}& 2\phantom{.0}& 3\phantom{.0}& 3\phantom{.0}& 3\phantom{.0}& 4\phantom{.0}& 4\phantom{.0}\end{array}\right]\\ \text{cooColIndA}& =& \left[\begin{array}{ccccccccc}1\phantom{.0}& 2\phantom{.0}& 2\phantom{.0}& 3\phantom{.0}& 1\phantom{.0}& 4\phantom{.0}& 5\phantom{.0}& 3\phantom{.0}& 5\phantom{.0}\end{array}\right]\end{array}$

### 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.

 $\left[\begin{array}{ccccc}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\end{array}\right]$

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

 $\begin{array}{rcl}\text{csrValA}& =& \left[\begin{array}{ccccccccc}1.0& 4.0& 2.0& 3.0& 5.0& 7.0& 8.0& 9.0& 6.0\end{array}\right]\\ \text{csrRowPtrA}& =& \left[\begin{array}{ccccc}0\phantom{.0}& 2\phantom{.0}& 4\phantom{.0}& 7\phantom{.0}& 9\phantom{.0}\end{array}\right]\\ \text{csrColIndA}& =& \left[\begin{array}{ccccccccc}0\phantom{.0}& 1\phantom{.0}& 1\phantom{.0}& 2\phantom{.0}& 0\phantom{.0}& 3\phantom{.0}& 4\phantom{.0}& 2\phantom{.0}& 4\phantom{.0}\end{array}\right]\end{array}$

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

 $\begin{array}{rcl}\text{csrValA}& =& \left[\begin{array}{ccccccccc}1.0& 4.0& 2.0& 3.0& 5.0& 7.0& 8.0& 9.0& 6.0\end{array}\right]\\ \text{csrRowPtrA}& =& \left[\begin{array}{ccccc}1\phantom{.0}& 3\phantom{.0}& 5\phantom{.0}& 8\phantom{.0}& 10\phantom{.0}\end{array}\right]\\ \text{csrColIndA}& =& \left[\begin{array}{ccccccccc}1\phantom{.0}& 2\phantom{.0}& 2\phantom{.0}& 3\phantom{.0}& 1\phantom{.0}& 4\phantom{.0}& 5\phantom{.0}& 3\phantom{.0}& 5\phantom{.0}\end{array}\right]\end{array}$

### 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.

 $\left[\begin{array}{ccccc}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\end{array}\right]$

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

 $\begin{array}{rcl}\text{cscValA}& =& \left[\begin{array}{ccccccccc}1.0& 5.0& 4.0& 2.0& 3.0& 9.0& 7.0& 8.0& 6.0\end{array}\right]\\ \text{cscRowIndA}& =& \left[\begin{array}{ccccccccc}0\phantom{.0}& 2\phantom{.0}& 0\phantom{.0}& 1\phantom{.0}& 1\phantom{.0}& 3\phantom{.0}& 2\phantom{.0}& 2\phantom{.0}& 3\phantom{.0}\end{array}\right]\\ \text{cscColPtrA}& =& \left[\begin{array}{cccccc}0\phantom{.0}& 2\phantom{.0}& 4\phantom{.0}& 6\phantom{.0}& 7\phantom{.0}& 9\phantom{.0}\end{array}\right]\end{array}$

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

 $\begin{array}{rcl}\text{cscValA}& =& \left[\begin{array}{ccccccccc}1.0& 5.0& 4.0& 2.0& 3.0& 9.0& 7.0& 8.0& 6.0\end{array}\right]\\ \text{cscRowIndA}& =& \left[\begin{array}{ccccccccc}1\phantom{.0}& 3\phantom{.0}& 1\phantom{.0}& 2\phantom{.0}& 2\phantom{.0}& 4\phantom{.0}& 3\phantom{.0}& 3\phantom{.0}& 4\phantom{.0}\end{array}\right]\\ \text{cscColPtrA}& =& \left[\begin{array}{cccccc}1\phantom{.0}& 3\phantom{.0}& 5\phantom{.0}& 7\phantom{.0}& 8\phantom{.0}& 10\phantom{.0}\end{array}\right]\end{array}$

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.

 $\left[\begin{array}{ccccc}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\end{array}\right]$

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

 $\begin{array}{rcl}\text{data}& =& \left[\begin{array}{ccc}1.0& 4.0& 0.0\\ 2.0& 3.0& 0.0\\ 5.0& 7.0& 8.0\\ 9.0& 6.0& 0.0\end{array}\right]\\ \text{indices}& =& \left[\begin{array}{ccc}0\phantom{.0}& 1\phantom{.0}& -1\phantom{.}\\ 1\phantom{.0}& 2\phantom{.0}& -1\phantom{.}\\ 0\phantom{.0}& 3\phantom{.0}& \phantom{-}4\phantom{.}\\ 2\phantom{.0}& 4\phantom{.0}& -1\phantom{.}\end{array}\right]\end{array}$

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

 $\begin{array}{rcl}\text{data}& =& \left[\begin{array}{ccc}1.0& 4.0& 0.0\\ 2.0& 3.0& 0.0\\ 5.0& 7.0& 8.0\\ 9.0& 6.0& 0.0\end{array}\right]\\ \text{indices}& =& \left[\begin{array}{ccc}1\phantom{.0}& 2\phantom{.0}& -1\phantom{.}\\ 2\phantom{.0}& 3\phantom{.0}& -1\phantom{.}\\ 1\phantom{.0}& 4\phantom{.0}& \phantom{-}5\phantom{.}\\ 3\phantom{.0}& 5\phantom{.0}& -1\phantom{.}\end{array}\right]\end{array}$

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 $blockDim$ . The m×n sparse matrix A is equivalent to a block sparse matrix ${A}_{b}$ with $mb=\frac{m+blockDim-1}{blockDim}$ block rows and $nb=\frac{n+blockDim-1}{blockDim}$ block columns. If $m$ or $n$ is not multiple of $blockDim$ , 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 $nnzb\ast blockDi{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.

 $\left[\begin{array}{ccccc}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\end{array}\right]$

If $blockDim$ is equal to 2, then $mb$ is 2, $nb$ 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.

 $\left[\begin{array}{cccccc}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\end{array}\right]$

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

 ${A}_{b}=\left[\begin{array}{ccc}{A}_{00}& {A}_{01}& {A}_{02}\\ {A}_{10}& {A}_{11}& {A}_{12}\end{array}\right]$

The basic element of BSR is a nonzero ${A}_{ij}$ block, one that contains at least one nonzero element of A. Five of six blocks are nonzero in ${A}_{b}$ .

 ${A}_{00}=\left[\begin{array}{cc}1& 4\\ 0& 2\end{array}\right],{A}_{01}=\left[\begin{array}{cc}0& 0\\ 3& 0\end{array}\right],{A}_{10}=\left[\begin{array}{cc}5& 0\\ 0& 0\end{array}\right],{A}_{11}=\left[\begin{array}{cc}0& 7\\ 9& 0\end{array}\right],{A}_{12}=\left[\begin{array}{cc}8& 0\\ 6& 0\end{array}\right]$

BSR format only stores the information of nonzero blocks, including block indices $\left(i,j\right)$ and values ${A}_{ij}$ . Also row indices are compressed in CSR format.

 $\begin{array}{rcl}\text{bsrValA}& =& \left[\begin{array}{ccccc}{A}_{00}& {A}_{01}& {A}_{10}& {A}_{11}& {A}_{12}\end{array}\right]\\ \text{bsrRowPtrA}& =& \left[\begin{array}{ccc}0\phantom{.0}& 2\phantom{.0}& 5\end{array}\right]\\ \text{bsrColIndA}& =& \left[\begin{array}{ccccc}0\phantom{.0}& 1\phantom{.0}& 0\phantom{.0}& 1\phantom{.0}& 2\end{array}\right]\end{array}$

There are two ways to arrange the data element of block ${A}_{ij}$ : row-major order and column-major order. Under column-major order, the physical storage of bsrValA is this.

 $\begin{array}{ccccc}bsrValA=\left[1\phantom{.0}0\phantom{.0}4\phantom{.0}2\phantom{.0}& 0\phantom{.0}3\phantom{.0}0\phantom{.0}0\phantom{.0}& 5\phantom{.0}0\phantom{.0}0\phantom{.0}0\phantom{.0}& 0\phantom{.0}9\phantom{.0}7\phantom{.0}0\phantom{.0}& 8\phantom{.0}6\phantom{.0}0\phantom{.0}0\phantom{.0}\right]\end{array}$

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

 $\begin{array}{ccccc}bsrValA=\left[1\phantom{.0}4\phantom{.0}0\phantom{.0}2\phantom{.0}& 0\phantom{.0}0\phantom{.0}3\phantom{.0}0\phantom{.0}& 5\phantom{.0}0\phantom{.0}0\phantom{.0}0\phantom{.0}& 0\phantom{.0}7\phantom{.0}9\phantom{.0}0\phantom{.0}& 8\phantom{.0}0\phantom{.0}6\phantom{.0}0\phantom{.0}\right]\end{array}$

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

 ${A}_{b}=\left[\begin{array}{ccc}{A}_{11}& {A}_{12}& {A}_{13}\\ {A}_{21}& {A}_{22}& {A}_{23}\end{array}\right]$
 $\begin{array}{ccccc}bsrValA=\left[1\phantom{.0}0\phantom{.0}4\phantom{.0}2\phantom{.0}& 0\phantom{.0}3\phantom{.0}0\phantom{.0}0\phantom{.0}& 5\phantom{.0}0\phantom{.0}0\phantom{.0}0\phantom{.0}& 0\phantom{.0}9\phantom{.0}7\phantom{.0}0\phantom{.0}& 8\phantom{.0}6\phantom{.0}0\phantom{.0}0\phantom{.0}\right]\end{array}$
 $\begin{array}{rcl}\text{bsrRowPtrA}& =& \left[\begin{array}{ccc}1\phantom{.0}& 3\phantom{.0}& 6\end{array}\right]\\ \text{bsrColIndA}& =& \left[\begin{array}{ccccc}1\phantom{.0}& 2\phantom{.0}& 1\phantom{.0}& 2\phantom{.0}& 3\end{array}\right]\end{array}$
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 $nnzb\ast blockDi{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}=\left[\begin{array}{ccc}{A}_{00}& {A}_{01}& {A}_{02}\\ {A}_{10}& {A}_{11}& {A}_{12}\end{array}\right]$

Assume it has this BSR format.

 $\begin{array}{rcl}\text{bsrValA of BSR}& =& \left[\begin{array}{ccccc}{A}_{00}& {A}_{01}& {A}_{10}& {A}_{11}& {A}_{12}\end{array}\right]\\ \text{bsrRowPtrA of BSR}& =& \left[\begin{array}{ccc}0\phantom{.0}& 2\phantom{.0}& 5\end{array}\right]\\ \text{bsrColIndA of BSR}& =& \left[\begin{array}{ccccc}0\phantom{.0}& 1\phantom{.0}& 0\phantom{.0}& 1\phantom{.0}& 2\end{array}\right]\end{array}$

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.

 $\begin{array}{rcl}\text{bsrRowPtrA of BSRX}& =& \left[\begin{array}{cc}0\phantom{.0}& 2\end{array}\right]\\ \text{bsrEndPtrA of BSRX}& =& \left[\begin{array}{cc}2\phantom{.0}& 5\end{array}\right]\end{array}$

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 $\stackrel{˜}{A}=\left[\begin{array}{ccc}O& O& O\\ O& {A}_{11}& O\end{array}\right]$ that is slightly different from $A$ , the developer can keep bsrColIndA and bsrValA, but reconstruct $\stackrel{˜}{A}$ by properly setting of bsrRowPtrA and bsrEndPtrA. The following 4-vector characterizes $\stackrel{˜}{A}$ .

## 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.

The whole idea of matrix type and fill mode is to keep minimum storage for symmetric/Hermitian matrix, and also to take advantage of symmetric property on SpMV (Sparse Matrix Vector multiplication). To compute y=A*x when A is symmetric and only lower triangular part is stored, two steps are needed. First step is to compute y=(L+D)*x and second step is to compute y=L^T*x + y. Given the fact that the transpose operation y=L^T*x is 10x slower than non-transpose version y=L*x, the symmetric property does not show up any performance gain. It is better for the user to extend the symmetric matrix to a general matrix and apply y=A*x with matrix type CUSPARSE_MATRIX_TYPE_GENERAL.

In general, SpMV, preconditioners (incomplete Cholesky or incomplete LU) and triangular solver are combined together in iterative solvers, for example PCG and GMRES. If the user always uses general matrix (instead of symmetric matrix), there is no need to support other than general matrix in preconditioners. Therefore the new routines, [bsr|csr]sv2 (triangular solver), [bsr|csr]ilu02 (incomplete LU) and [bsr|csr]ic02 (incomplete Cholesky), only support matrix type CUSPARSE_MATRIX_TYPE_GENERAL.

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.

### 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.12. csrsv2Info_t

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

### 4.13. csric02Info_t

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

### 4.14. csrilu02Info_t

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

### 4.15. bsrsv2Info_t

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

### bsrsm2Info_t

This is a pointer type to an opaque structure holding the information used in bsrsm2_bufferSize(), bsrsm2_analysis(), and bsrsm2_solve().

### 4.17. bsric02Info_t

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

### 4.18. bsrilu02Info_t

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

### 4.19. csrgemm2Info_t

This is a pointer type to an opaque structure holding the information used in csrgemm2_bufferSizeExt(), and csrgemm2().

### 4.20. 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.5. 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.3. 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.4. 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.

### 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.6. 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.10. 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.8. 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.9. 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.

### 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.11. 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.12. 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.13. 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.14. 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.15. 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.16. 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.17. 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.18. 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.19. 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.20. 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.21. 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.22. 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.23. 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.24. cusparseGetStream()

```cusparseStatus_t
cusparseGetStream(cusparseHandle_t handle, cudaStream_t *streamId)
```

This function gets the cuSPARSE library stream, which is being used to to execute all calls to the cuSPARSE library functions. If the cuSPARSE library stream is not set, all kernels use the default NULL stream.

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

### 5.25. 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.26. 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.27. 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.28. 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.29. 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.30. 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.

### 5.31. cusparseCreateBsrsv2Info()

```cusparseStatus_t
cusparseCreateBsrsv2Info(bsrsv2Info_t *info);
```

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

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

### 5.32. cusparseDestroyBsrsv2Info()

```cusparseStatus_t
cusparseDestroyBsrsv2Info(bsrsv2Info_t info);
```

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

Input

 info the solve (bsrsv2_solve) and analysis (bsrsv2_analysis) structure.

Status Returened

 CUSPARSE_STATUS_SUCCESS the resources were released successfully.

### 5.33. cusparseCreateBsrsm2Info()

```cusparseStatus_t
cusparseCreateBsrsm2Info(bsrsm2Info_t *info);
```

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

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

### 5.34. cusparseDestroyBsrsm2Info()

```cusparseStatus_t
cusparseDestroyBsrsm2Info(bsrsm2Info_t info);
```

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

Input

 info the solve (bsrsm2_solve) and analysis (bsrsm2_analysis) structure.

Status Returened

 CUSPARSE_STATUS_SUCCESS the resources were released successfully.

### 5.35. cusparseCreateBsric02Info()

```cusparseStatus_t
cusparseCreateBsric02Info(bsric02Info_t *info);
```

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

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

### 5.36. cusparseDestroyBsric02Info()

```cusparseStatus_t
cusparseDestroyBsric02Info(bsric02Info_t info);
```

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

Input

 info the solve (bsric02_solve) and analysis (bsric02_analysis) structure.

Status Returened

 CUSPARSE_STATUS_SUCCESS the resources were released successfully.

### 5.37. cusparseCreateBsrilu02Info()

```cusparseStatus_t
cusparseCreateBsrilu02Info(bsrilu02Info_t *info);
```

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

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

### 5.38. cusparseDestroyBsrilu02Info()

```cusparseStatus_t
cusparseDestroyBsrilu02Info(bsrilu02Info_t info);
```

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

Input

 info the solve (bsrilu02_solve) and analysis (bsrilu02_analysis) structure.

Status Returened

 CUSPARSE_STATUS_SUCCESS the resources were released successfully.

### 5.39. cusparseCreateCsrgemm2Info()

```cusparseStatus_t
cusparseCreateCsrgemm2Info(csrgemm2Info_t *info);
```

This function creates and initializes analysis structure of general sparse matrix-matrix multiplication.

Input
 info the pointer to the analysis structure of general sparse matrix-matrix multiplication.
Status Returned
 CUSPARSE_STATUS_SUCCESS the structure was initialized successfully. CUSPARSE_STATUS_ALLOC_FAILED the resources could not be allocated.

### 5.40. cusparseDestroyCsrgemm2Info()

```cusparseStatus_t
cusparseDestroyCsrgemm2Info(csrgemm2Info_t info);
```

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

Input

 info opaque structure of csrgemm2.

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 $\alpha$ and adds the result to the vector y in dense format. This operation can be written as

 $y=y+\alpha \ast 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 scalar used for multiplication. xVal vector with nnz nonzero values of vector x. xInd integer vector with nnz indices of the nonzero values of vector x. y vector in dense format. idxBase CUSPARSE_INDEX_BASE_ZERO or CUSPARSE_INDEX_BASE_ONE.
Output
 y 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

 $result={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 vector with nnz nonzero values of vector x. xInd integer vector with nnz indices of the nonzero values of vector x. y 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

 $result={x}^{H}y$

In other words,

```for i=0 to nnz-1
resultDevHostPtr += $\overline{\text{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 vector with nnz nonzero values of vector x. xInd integer vector with nnz indices of the nonzero values of vector x. y 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 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 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 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 vector with nnz nonzero values that were gathered from vector y (that is unchanged if nnz == 0). y 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=\left(\begin{array}{rr}c& s\\ -s& c\end{array}\right)$

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 vector with nnz nonzero values of vector x. xInd integer vector with nnz indices of the nonzero values of vector x. y 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 updated vector in sparse format (that is unchanged if nnz == 0). y 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 vector with nnz nonzero values of vector x. xInd integer vector with nnz indices of the nonzero values of vector x. y dense vector (of size≥max(xInd)-idxBase+1). idxBase CUSPARSE_INDEX_BASE_ZERO or CUSPARSE_INDEX_BASE_ONE.
Output
 y 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

 $\text{y}=\alpha \ast \text{op}\left(A\right)\ast \text{x}+\beta \ast \text{y}$

where sparse matrix that is defined in BSR storage format by the three arrays bsrVal, bsrRowPtr, and bsrColInd); x and y are vectors; are scalars; and

$\text{op}\left(A\right)=\left\{\begin{array}{ll}A& \text{if trans == CUSPARSE_OPERATION_NON_TRANSPOSE}\\ {A}^{T}& \text{if trans == CUSPARSE_OPERATION_TRANSPOSE}\\ {A}^{H}& \text{if trans == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE}\end{array}\right\$
• Only CUSPARSE_OPERATION_NON_TRANSPOSE is supported, that is

 $\text{y}=\alpha \ast A\ast \text{x}+\beta \ast \text{y}$
• Only CUSPARSE_MATRIX_TYPE_GENERAL is supported.
• The size of vector x should be $\left(nb\ast blockDim\right)$ at least, and the size of vector y should be $\left(mb\ast blockDim\right)$ 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;
int nnzb;
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, &nnzb);
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, nnzb, &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 $\text{op}\left(A\right)$ . 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 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 array of nnz$\left(=$csrRowPtrA(mb)$-$csrRowPtrA(0)$\right)$ 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$\left(=$csrRowPtrA(mb)$-$csrRowPtrA(0)$\right)$ column indices of the nonzero blocks of matrix $A$. blockDim block dimension of sparse matrix $A$, larger than zero. x vector of $nb\ast blockDim$ elements. beta scalar used for multiplication. If beta is zero, y does not have to be a valid input. y vector of $mb\ast blockDim$ elements.
Output
 y 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, $blockDim<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 mb,
int nb,
int nnzb,
const float *alpha,
const cusparseMatDescr_t descr,
const float *bsrVal,
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 mb,
int nb,
int nnzb,
const double *alpha,
const cusparseMatDescr_t descr,
const double *bsrVal,
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 mb,
int nb,
int nnzb,
const cuComplex *alpha,
const cusparseMatDescr_t descr,
const cuComplex *bsrVal,
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 mb,
int nb,
int nnzb,
const cuDoubleComplex *alpha,
const cusparseMatDescr_t descr,
const cuDoubleComplex *bsrVal,
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

 $\text{y(mask)}=\left(\alpha \ast \text{op}\left(A\right)\ast \text{x}+\beta \ast \text{y}\right)\text{(mask)}$

where sparse matrix that is defined in BSRX storage format by the four arrays bsrVal, bsrRowPtr, bsrEndPtr, and bsrColInd); x and y are vectors; are scalars; and

$\text{op}\left(A\right)=\left\{\begin{array}{ll}A& \text{if trans == CUSPARSE_OPERATION_NON_TRANSPOSE}\\ {A}^{T}& \text{if trans == CUSPARSE_OPERATION_TRANSPOSE}\\ {A}^{H}& \text{if trans == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE}\end{array}\right\$

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$:

 $\begin{array}{r}A=\left[\begin{array}{ccc}{A}_{11}& {A}_{12}& O\\ {A}_{21}& {A}_{22}& {A}_{23}\end{array}\right]\end{array}$

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

 $\begin{array}{rcl}\text{bsrVal}& =& \left[\begin{array}{ccccc}{A}_{11}& {A}_{12}& {A}_{21}& {A}_{22}& {A}_{23}\end{array}\right]\\ \text{bsrRowPtr}& =& \left[\begin{array}{ccc}1\phantom{.0}& 3\phantom{.0}& 6\end{array}\right]\\ \text{bsrColInd}& =& \left[\begin{array}{ccccc}1\phantom{.0}& 2\phantom{.0}& 1\phantom{.0}& 2\phantom{.0}& 3\end{array}\right]\end{array}$

Suppose we want to do the following bsrmv operation on a matrix $\overline{A}$ which is slightly different from $A$ .

 $\left[\begin{array}{c}{y}_{1}\\ {y}_{2}\end{array}\right]:=alpha\ast \left(\stackrel{˜}{A}=\left[\begin{array}{ccc}O& O& O\\ O& {A}_{22}& O\end{array}\right]\right)\ast \left[\begin{array}{c}{x}_{1}\\ {x}_{2}\\ {x}_{3}\end{array}\right]+\left[\begin{array}{c}{y}_{1}\\ beta\ast {y}_{2}\end{array}\right]$

We don’t need to create another BSR format for the new matrix $\overline{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 $\overline{A}$ plus 1.

For example, the following bsrRowPtr and bsrEndPtr can represent matrix $\overline{A}$ :

 $\begin{array}{rcl}\text{bsrRowPtr}& =& \left[\begin{array}{cc}1\phantom{.0}& 4\end{array}\right]\\ \text{bsrEndPtr}& =& \left[\begin{array}{cc}1\phantom{.0}& 5\end{array}\right]\end{array}$

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:

 $\left[\begin{array}{c}?\\ {y}_{2}\end{array}\right]:=alpha\ast \left[\begin{array}{ccc}?& ?& ?\\ O& {A}_{22}& O\end{array}\right]\ast \left[\begin{array}{c}{x}_{1}\\ {x}_{2}\\ {x}_{3}\end{array}\right]+beta\ast \left[\begin{array}{c}?\\ {y}_{2}\end{array}\right]$

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.

 $\begin{array}{rcl}\text{bsrRowPtr}& =& \left[\begin{array}{cc}?\phantom{.0}& 4\end{array}\right]\\ \text{bsrEndPtr}& =& \left[\begin{array}{cc}?\phantom{.0}& 5\end{array}\right]\end{array}$
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 $\text{op}\left(A\right)$ . 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 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 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 vector of $nb\ast blockDim$ elements. beta scalar used for multiplication. If beta is zero, y does not have to be a valid input. y vector of $mb\ast blockDim$ 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, $blockDim<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

 $\text{y}=\alpha \ast \text{op}\left(A\right)\ast \text{x}+\beta \ast \text{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; are scalars; and

$\text{op}\left(A\right)=\left\{\begin{array}{ll}A& \text{if trans == CUSPARSE_OPERATION_NON_TRANSPOSE}\\ {A}^{T}& \text{if trans == CUSPARSE_OPERATION_TRANSPOSE}\\ {A}^{H}& \text{if trans == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE}\end{array}\right\$

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 $\text{op}\left(A\right)$ . m number of rows of matrix A. n number of columns of matrix A. nnz number of nonzero elements of matrix A. alpha 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 array of nnz$\left(=$csrRowPtrA(m)$-$csrRowPtrA(0)$\right)$ 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$\left(=$csrRowPtrA(m)$-$csrRowPtrA(0)$\right)$ column indices of the nonzero elements of matrix A. x vector of n elements if $\text{op}\left(A\right)=A$ , and m elements if $\text{op}\left(A\right)={A}^{T}$ or $\text{op}\left(A\right)={A}^{H}$ beta scalar used for multiplication. If beta is zero, y does not have to be a valid input. y vector of m elements if $\text{op}\left(A\right)=A$ , and n elements if $\text{op}\left(A\right)={A}^{T}$ or $\text{op}\left(A\right)={A}^{H}$
Output
 y 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. cusparseCsrmvEx()

```cusparseStatus_t
cusparseCsrmvEx(cusparseHandle_t handle,
cusparseAlgMode_t alg,
cusparseOperation_t transA,
int m, int n, int nnz,
const cusparseMatDescr_t descrA,
const int *csrRowPtrA,
const int *csrColIndA,
void* buffer);
```

This function is an extended version of cusparse<t>csrmv() which performs the matrix-vector multiply operation. For detailed description of the functionality, see cusparse<t>csrmv().

For half-precision execution type, the minimum GPU architecture is SM_53. Also, for both half-precision IO and execution, only CUSPARSE_MATRIX_TYPE_GENERAL and CUSPARSE_OPERATION_NON_TRANSPOSE are supported.

All pointers should be aligned with 128 bytes.

Input specifically required by cusparseCsrmvEx
 alg Not currently used. Will be used for future algorithms such as MergePath or Multi-GPU. alphatype Data type of alpha. csrValAtype Data type of csrValA. xtype Data type of x. betatype Data type of beta. ytype Data type of y. executiontype Data type used for computation. buffer Pointer to workspace buffer. Not currently used, however must be allocated and be aligned with word boundaries. Recommended to always use cusparseCsrmvEx_bufferSize to obtain the right size for this buffer.

### cusparseCsrmvEx_bufferSize()

```cusparseCsrmvEx_bufferSize(cusparseHandle_t handle,
cusparseAlgMode_t alg,
cusparseOperation_t transA,
int m, int n, int nnz,
const cusparseMatDescr_t descrA,
const int *csrRowPtrA,
const int *csrColIndA,
size_t *bufferSizeInBytes);
```

This function returns the size of the workspace needed by cusparseCsrmvEx. User needs to allocate a buffer of this size and give that buffer to cusparseCsrmvEx as an argument. All the arguments are similar to cusparseCsrmvEx except the following output argument.

Output specifically required by cusparseCsrmvEx_bufferSize
 bufferSizeInBytes Pointer to a size_t variable, which will be assigned with the size of workspace needed by cusparseCsrmv. Since workspace is not currently used by cusparseCsrmv, this function returns the number 16 as an arbitrary number.

### 7.6. cusparse<t>csrmv_mp()

```cusparseStatus_t
cusparseScsrmv_mp(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_mp(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_mp(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_mp(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 a load-balanced matrix-vector operation

 $\text{y}=\alpha \ast \text{op}\left(A\right)\ast \text{x}+\beta \ast \text{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; are scalars; and

$\text{op}\left(A\right)=\left\{\begin{array}{ll}A& \text{if trans == CUSPARSE_OPERATION_NON_TRANSPOSE}\\ {A}^{T}& \text{if trans == CUSPARSE_OPERATION_TRANSPOSE}\\ {A}^{H}& \text{if trans == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE}\end{array}\right\$

This routine was introduced specifically to address some of the loss of performance in the regular csrmv() code due to irregular sparsity patterns and transpose operations. The core kernels are based on the "MergePath" approach created by Duanne Merril. By using this approach, we are able to provide performance independent of a sparsity pattern, transpose or non-transpose, across data types.

Input
 handle handle to the cuSPARSE library context. trans the operation $\text{op}\left(A\right)$ . m number of rows of matrix A. n number of columns of matrix A. nnz number of nonzero elements of matrix A. alpha 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 array of nnz$\left(=$csrRowPtrA(m)$-$csrRowPtrA(0)$\right)$ 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$\left(=$csrRowPtrA(m)$-$csrRowPtrA(0)$\right)$ column indices of the nonzero elements of matrix A. x vector of n elements if $\text{op}\left(A\right)=A$ , and m elements if $\text{op}\left(A\right)={A}^{T}$ or $\text{op}\left(A\right)={A}^{H}$ beta scalar used for multiplication. If beta is zero, y does not have to be a valid input. y vector of m elements if $\text{op}\left(A\right)=A$ , and n elements if $\text{op}\left(A\right)={A}^{T}$ or $\text{op}\left(A\right)={A}^{H}$
Output
 y 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.7. cusparse<t>gemvi()

```cusparseStatus_t
cusparseSgemvi(cusparseHandle_t handle, cusparseOperation_t transA,
int m, int n, const float           *alpha,
const float *A,
int lda, int nnz,
const float           *x,
const int             *xInd,
const float           *beta,
float                 *y,
cusparseIndexBase_t    idxBase,
void                  *pBuffer)
cusparseStatus_t
cusparseDgemvi(cusparseHandle_t handle, cusparseOperation_t transA,
int m, int n, const double           *alpha,
const double *A,
int lda, int nnz,
const double          *x,
const int             *xInd,
const double          *beta,
double                *y,
cusparseIndexBase_t    idxBase,
void                  *pBuffer)
cusparseStatus_t
cusparseCgemvi(cusparseHandle_t handle, cusparseOperation_t transA,
int m, int n, const cuComplex           *alpha,
const cuComplex *A,
int lda, int nnz,
const cuComplex       *x,
const int             *xInd,
const cuComplex       *beta,
cuComplex             *y,
cusparseIndexBase_t    idxBase,
void                  *pBuffer)
cusparseStatus_t
cusparseZgemvi(cusparseHandle_t handle, cusparseOperation_t transA,
int m, int n, const cuDoubleComplex           *alpha,
const cuDoubleComplex *A,
int lda, int nnz,
const cuDoubleComplex       *x,
const int             *xInd,
const cuDoubleComplex       *beta,
cuDoubleComplex             *y,
cusparseIndexBase_t    idxBase,
void                  *pBuffer)
```

This function performs the matrix-vector operation

 $\text{y}=\alpha \ast \text{op}\left(A\right)\ast \text{x}+\beta \ast \text{y}$

A is an m×n dense matrix and a sparse vector x that is defined in a sparse storage format by the two arrays xVal, xInd of length nnz, and y is a dense vector; are scalars; and

$\text{op}\left(A\right)=\left\{\begin{array}{ll}A& \text{if trans == CUSPARSE_OPERATION_NON_TRANSPOSE}\\ {A}^{T}& \text{if trans == CUSPARSE_OPERATION_TRANSPOSE}\\ {A}^{H}& \text{if trans == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE}\end{array}\right\$

To simplify the implementation, we have not (yet) optimized the transpose multiple case. We recommend the following for users interested in this case.

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 gemvi() 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 $\text{op}\left(A\right)$ . m number of rows of matrix A. n number of columns of matrix A. alpha scalar used for multiplication. A the pointer to dense matrix A. lda size of the leading dimension of A. nnz number of nonzero elements of vector x. x sparse vector of nnz elements of size n if $\text{op}\left(A\right)=A$ , and size m if $\text{op}\left(A\right)={A}^{T}$ or $\text{op}\left(A\right)={A}^{H}$ xInd Indices of non-zero values in x beta scalar used for multiplication. If beta is zero, y does not have to be a valid input. y dense vector of m elements if $\text{op}\left(A\right)=A$ , and n elements if $\text{op}\left(A\right)={A}^{T}$ or $\text{op}\left(A\right)={A}^{H}$ idxBase 0 or 1, for 0 based or 1 based indexing, respectively pBuffer working space buffer, of size given by Xgemvi_getBufferSize()
Output
 y updated dense 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.8. cusparse<t>gemvi_bufferSize()

```cusparseStatus_t
cusparseSgemvi_bufferSize(cusparseHandle_t handle,
cusparseOperation_t transA,
int m,
int n,
int nnz,
int *pBufferSize)

cusparseStatus_t
cusparseDgemvi_bufferSize(cusparseHandle_t handle,
cusparseOperation_t transA,
int m,
int n,
int nnz,
int *pBufferSize)

cusparseStatus_t
cusparseCgemvi_bufferSize(cusparseHandle_t handle,
cusparseOperation_t transA,
int m,
int n,
int nnz,
int *pBufferSize)

cusparseStatus_t
cusparseZgemvi_bufferSize(cusparseHandle_t handle,
cusparseOperation_t transA,
int m,
int n,
int nnz,
int *pBufferSize)
```

This function returns size of buffer used in gemvi()

A is an (m)x(n) dense matrix.

$\text{op}\left(A\right)=\left\{\begin{array}{ll}A& \text{if trans == CUSPARSE_OPERATION_NON_TRANSPOSE}\\ {A}^{T}& \text{if trans == CUSPARSE_OPERATION_TRANSPOSE}\\ {A}^{H}& \text{if trans == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE}\end{array}\right\$
Input
 handle handle to the cuSPARSE library context. transA the operation op(A). m number of rows of matrix A. n number of columns of matrix Y. nnz number of nonzero entries of vector x multiplying A.
Output
 pBufferSize number of elements needed the buffer used in gemvi().
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) 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.9. 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 =$\alpha$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; $\alpha$ is a scalar; and

$\text{op}\left(A\right)=\left\{\begin{array}{ll}A& \text{if trans == CUSPARSE_OPERATION_NON_TRANSPOSE}\\ {A}^{T}& \text{if trans == CUSPARSE_OPERATION_TRANSPOSE}\\ {A}^{H}& \text{if trans == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE}\end{array}\right\$

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 $\text{op}\left(A\right)$ . 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 array of nnzb$\left(=$bsrRowPtrA(mb)$-$bsrRowPtrA(0)$\right)$ 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$\left(=$bsrRowPtrA(mb)$-$bsrRowPtrA(0)$\right)$ 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.10. 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 =$\alpha$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; $\alpha$ is a scalar; and

$\text{op}\left(A\right)=\left\{\begin{array}{ll}A& \text{if trans == CUSPARSE_OPERATION_NON_TRANSPOSE}\\ {A}^{T}& \text{if trans == CUSPARSE_OPERATION_TRANSPOSE}\\ {A}^{H}& \text{if trans == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE}\end{array}\right\$

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 $\text{op}\left(A\right)$ . 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 array of nnzb$\left(=$bsrRowPtrA(mb)$-$bsrRowPtrA(0)$\right)$ 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$\left(=$bsrRowPtrA(mb)$-$bsrRowPtrA(0)$\right)$ 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.11. 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 =$\alpha$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; $\alpha$ is a scalar; and

$\text{op}\left(A\right)=\left\{\begin{array}{ll}A& \text{if trans == CUSPARSE_OPERATION_NON_TRANSPOSE}\\ {A}^{T}& \text{if trans == CUSPARSE_OPERATION_TRANSPOSE}\\ {A}^{H}& \text{if trans == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE}\end{array}\right\$

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;
void *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 $\text{op}\left(A\right)$ . mb number of block rows and block columns of matrix A. alpha 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 array of nnzb$\left(=$bsrRowPtrA(mb)$-$bsrRowPtrA(0)$\right)$ 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$\left(=$bsrRowPtrA(mb)$-$bsrRowPtrA(0)$\right)$ 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 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 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.13. 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

 $\text{op}\left(A\right)\ast \text{y}=\alpha \ast \text{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; $\alpha$ is a scalar; and

$\text{op}\left(A\right)=\left\{\begin{array}{ll}A& \text{if trans == CUSPARSE_OPERATION_NON_TRANSPOSE}\\ {A}^{T}& \text{if trans == CUSPARSE_OPERATION_TRANSPOSE}\\ {A}^{H}& \text{if trans == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE}\end{array}\right\$

The routine csrsv_analysis supports analysis phase of csrsv_solve, csric0 and csrilu0. The user has to be careful of which routine is called after csrsv_analysis. The matrix descriptor must be the same for csrsv_analysis and its subsequent call to csrsv_solve, csric0 and csrilu0.

For csrsv_solve, the matrix type must be CUSPARSE_MATRIX_TYPE_TRIANGULAR or CUSPARSE_MATRIX_TYPE_GENERAL.

For csrilu0, the matrix type must be CUSPARSE_MATRIX_TYPE_GENERAL.

For csric0, the matrix type must be CUSPARSE_MATRIX_TYPE_SYMMETRIC or CUSPARSE_MATRIX_TYPE_HERMITIAN.

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 $\text{op}\left(A\right)$ 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 for csrsv_solve, CUSPARSE_MATRIX_TYPE_SYMMETRIC and CUSPARSE_MATRIX_TYPE_HERMITIAN for csric0, CUSPARSE_MATRIX_TYPE_GENERAL for csrilu0, while the supported diagonal types are CUSPARSE_DIAG_TYPE_UNIT and CUSPARSE_DIAG_TYPE_NON_UNIT. csrValA array of nnz$\left(=$csrRowPtrA(m)$-$csrRowPtrA(0)$\right)$ 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$\left(=$csrRowPtrA(m)$-$csrRowPtrA(0)$\right)$ 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.14. cusparseCsrsv_analysisEx()

```cusparseStatus_t cusparseCsrsv_analysisEx(cusparseHandle_t handle,
cusparseOperation_t transA,
int m, int nnz,
const cusparseMatDescr_t descrA,
const int *csrSortedRowPtrA,
const int *csrSortedColIndA,
cusparseSolveAnalysisInfo_t info,
```

This function is an extended version of cusparse<t>csrsv_analysis(). For detailed description of the functionality, see cusparse<t>csrsv_analysis().

This function does not support half-precision execution type, but it supports half-precision IO with single precision execution.

Input specifically required by cusparseCsrsv_analysisEx
 csrSortedValAtype Data type of csrSortedValA. executiontype Data type used for computation.

### 7.15. 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 *csrSortedValA,
const int *csrSortedRowPtrA, const int *csrSortedColIndA,
cusparseSolveAnalysisInfo_t info,
const float *f, float *x);

cusparseStatus_t cusparseDcsrsv_solve(cusparseHandle_t handle,
cusparseOperation_t transA,
int m, const double *alpha,
const cusparseMatDescr_t descrA,
const double *csrSortedValA,
const int *csrSortedRowPtrA, const int *csrSortedColIndA,
cusparseSolveAnalysisInfo_t info,
const double *f, double *x);

cusparseStatus_t cusparseCcsrsv_solve(cusparseHandle_t handle,
cusparseOperation_t transA,
int m, const cuComplex *alpha,
const cusparseMatDescr_t descrA,
const cuComplex *csrSortedValA,
const int *csrSortedRowPtrA, const int *csrSortedColIndA,
cusparseSolveAnalysisInfo_t info,
const cuComplex *f, cuComplex *x);

cusparseStatus_t cusparseZcsrsv_solve(cusparseHandle_t handle,
cusparseOperation_t transA,
int m, const cuDoubleComplex *alpha,
const cusparseMatDescr_t descrA,
const cuDoubleComplex *csrSortedValA,
const int *csrSortedRowPtrA, const int *csrSortedColIndA,
cusparseSolveAnalysisInfo_t info,
const cuDoubleComplex *f, cuDoubleComplex *x);
```

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

 $\text{op}\left(A\right)\ast \text{x}=\alpha \ast \text{f}$

where A is an m×m sparse matrix that is defined in CSR storage format by the three arrays csrSortedValA, csrSortedRowPtrA, and csrSortedColIndA); f and x are the right-hand-side and the solution vectors; $\alpha$ is a scalar; and

$\text{op}\left(A\right)=\left\{\begin{array}{ll}A& \text{if trans == CUSPARSE_OPERATION_NON_TRANSPOSE}\\ {A}^{T}& \text{if trans == CUSPARSE_OPERATION_TRANSPOSE}\\ {A}^{H}& \text{if trans == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE}\end{array}\right\$

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 $\text{op}\left(A\right)$ m number of rows and columns of matrix A. alpha 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. csrSortedValA array of nnz$\left(=$csrSortedRowPtrA(m)$-$csrSortedRowPtrA(0)$\right)$ nonzero elements of matrix A. csrSortedRowPtrA integer array of m$+1$ elements that contains the start of every row and the end of the last row plus one. csrSortedColIndA integer array of nnz$\left(=$csrSortedRowPtrA(m)$-$csrSortedRowPtrA(0)$\right)$ 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). f right-hand-side vector of size m.
Output
 x 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.16. cusparseCsrsv_solveEx()

```cusparseStatus_t cusparseCsrsv_solveEx(cusparseHandle_t handle,
cusparseOperation_t transA,
int m,
const cusparseMatDescr_t descrA,
const int *csrSortedRowPtrA,
const int *csrSortedColIndA,
cusparseSolveAnalysisInfo_t info,
```

This function is an extended version of cusparse<t>csrsv_solve(). For detailed description of the functionality, see cusparse<t>csrsv_solve().

This function does not support half-precision execution type, but it supports half-precision IO with single precision execution.

Input specifically required by cusparseCsrsv_solveEx
 alphatype Data type of alpha. csrSortedValAtype Data type of csrSortedValA. ftype Data type of f. xtype Data type of x. executiontype Data type used for computation.

### 7.17. 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 =$\alpha$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; $\alpha$ is a scalar; and

$\text{op}\left(A\right)=\left\{\begin{array}{ll}A& \text{if trans == CUSPARSE_OPERATION_NON_TRANSPOSE}\\ {A}^{T}& \text{if trans == CUSPARSE_OPERATION_TRANSPOSE}\\ {A}^{H}& \text{if trans == CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE}\end{array}\right\$

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 $\text{op}\left(A\right)$ . 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 array of nnz$\left(=$csrRowPtrA(m)$-$csrRowPtrA(0)$\right)$ 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.