This document includes math equations (highlighted in red) which are best viewed with Firefox version 4.0 or higher, or another MathML-aware browser. There is also a PDF version of this document.
CUSPARSE (PDF) - CUDA Toolkit v5.5 (older) - Last updated July 19, 2013 - Send Feedback

## 1. Introduction

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

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

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

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

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

### 1.1. Naming Conventions

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

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

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

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

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

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

### 1.2. Asynchronous Execution

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

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

## 2. Using the CUSPARSE API

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

### 2.1. Thread Safety

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

### 2.2. Scalar Parameters

In the CUSPARSE API, the scalar parameters $\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 storage format of blocks in BSR format can be column-major or row-major, independently of the base index. However, if the developer has BSR format from the Math Kernel Library (MKL) and wants to directly copy it to BSR in CUSPARSE, then cusparseDirection_t is CUSPARSE_DIRECTION_COLUMN if the base index is one; otherwise, cusparseDirection_t is CUSPARSE_DIRECTION_ROW.

### 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) The size of bsrColIndA and bsrValA; nnzb is greater than or equal to the 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; bsrRowPtr(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; bsrRowPtr(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 power 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 will assume 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 actuall set to in memory.

Value Meaning

CUSPARSE_DIAG_TYPE_NON_UNIT

the matrix diagonal has non-unit elements.

CUSPARSE_DIAG_TYPE_UNIT

the matrix diagonal has unit elements.

### 4.6.2. cusparseFillMode_t

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

Value Meaning

CUSPARSE_FILL_MODE_LOWER

the lower triangular part is stored.

CUSPARSE_FILL_MODE_UPPER

the upper triangular part is stored.

### 4.6.3. cusparseIndexBase_t

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

Value Meaning

CUSPARSE_INDEX_BASE_ZERO

the base index is zero.

CUSPARSE_INDEX_BASE_ONE

the base index is one.

### 4.6.4. cusparseMatrixType_t

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

Value Meaning

CUSPARSE_MATRIX_TYPE_GENERAL

the matrix is general.

CUSPARSE_MATRIX_TYPE_SYMMETRIC

the matrix is symmetric.

CUSPARSE_MATRIX_TYPE_HERMITIAN

the matrix is Hermitian.

CUSPARSE_MATRIX_TYPE_TRIANGULAR

the matrix is triangular.

### 4.7. cusparseOperation_t

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

Value Meaning

CUSPARSE_OPERATION_NON_TRANSPOSE

the non-transpose operation is selected.

CUSPARSE_OPERATION_TRANSPOSE

the transpose operation is selected.

CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE

the conjugate transpose operation is selected.

### 4.8. cusparsePointerMode_t

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

Value Meaning

CUSPARSE_POINTER_MODE_HOST

the scalars are passed by reference on the host.

CUSPARSE_POINTER_MODE_DEVICE

the scalars are passed by reference on the device.

### 4.9. 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.10. cusparseStatus_t

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

 CUSPARSE_STATUS_SUCCESS The operation completed successfully. CUSPARSE_STATUS_NOT_INITIALIZED The CUSPARSE library was not initialized. This is usually caused by the lack of a prior call, an error in the CUDA Runtime API called by the CUSPARSE routine, or an error in the hardware setup. To correct: call cusparseCreate() prior to the function call; and check that the hardware, an appropriate version of the driver, and the CUSPARSE library are correctly installed. CUSPARSE_STATUS_ALLOC_FAILED Resource allocation failed inside the CUSPARSE library. This is usually caused by a cudaMalloc() failure. To correct: prior to the function call, deallocate previously allocated memory as much as possible. CUSPARSE_STATUS_INVALID_VALUE An unsupported value or parameter was passed to the function (a negative vector size, for example). To correct: ensure that all the parameters being passed have valid values. CUSPARSE_STATUS_ARCH_MISMATCH The function requires a feature absent from the device architecture; usually caused by the lack of support for atomic operations or double precision. To correct: compile and run the application on a device with appropriate compute capability, which is 1.1 for 32-bit atomic operations and 1.3 for double precision. CUSPARSE_STATUS_MAPPING_ERROR An access to GPU memory space failed, which is usually caused by a failure to bind a texture. To correct: prior to the function call, unbind any previously bound textures. CUSPARSE_STATUS_EXECUTION_FAILED The GPU program failed to execute. This is often caused by a launch failure of the kernel on the GPU, which can be caused by multiple reasons. To correct: check that the hardware, an appropriate version of the driver, and the CUSPARSE library are correctly installed. CUSPARSE_STATUS_INTERNAL_ERROR An internal CUSPARSE operation failed. This error is usually caused by a cudaMemcpyAsync() failure. To correct: check that the hardware, an appropriate version of the driver, and the CUSPARSE library are correctly installed. Also, check that the memory passed as a parameter to the routine is not being deallocated prior to the routine’s completion. CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED The matrix type is not supported by this function. This is usually caused by passing an invalid matrix descriptor to the function. To correct: check that the fields in cusparseMatDescr_t descrA were set correctly.

## 5. CUSPARSE Helper Function Reference

The CUSPARSE helper functions are described in this section.

### 5.1. cusparseCreate()

```cusparseStatus_t
cusparseCreate(cusparseHandle_t *handle)```

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

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

### 5.2. cusparseCreateHybMat()

```cusparseStatus_t
cusparseCreateHybMat(cusparseHybMat_t *hybA)```

This function creates and initializes the hybA opaque data structure.

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

### 5.3. cusparseCreateMatDescr()

```cusparseStatus_t
cusparseCreateMatDescr(cusparseMatDescr_t *descrA)```

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

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

### 5.4. cusparseCreateSolveAnalysisInfo()

```cusparseStatus_t
cusparseCreateSolveAnalysisInfo(cusparseSolveAnalysisInfo_t *info)```

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

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

### 5.5. cusparseDestroy()

```cusparseStatus_t
cusparseDestroy(cusparseHandle_t handle)```

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

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

### 5.6. cusparseDestroyHybMat()

```cusparseStatus_t
cusparseDestroyHybMat(cusparseHybMat_t hybA)
```

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

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

### 5.7. cusparseDestroyMatDescr()

```cusparseStatus_t
cusparseDestroyMatDescr(cusparseMatDescr_t descrA)
```

This function releases the memory allocated for the matrix descriptor.

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

### 5.8. cusparseDestroySolveAnalysisInfo()

```cusparseStatus_t
cusparseDestroySolveAnalysisInfo(cusparseSolveAnalysisInfo_t info)```

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

Input

 info the solve and analysis structure.

Status Returened

 CUSPARSE_STATUS_SUCCESS the resources were released successfully.

### 5.9. cusparseGetLevelInfo()

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

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

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

### 5.10. cusparseGetMatDiagType()

```cusparseDiagType_t
cusparseGetMatDiagType(const cusparseMatDescr_t descrA)
```

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

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

### 5.11. cusparseGetMatFillMode()

```cusparseFillMode_t
cusparseGetMatFillMode(const cusparseMatDescr_t descrA)
```

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

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

### 5.12. cusparseGetMatIndexBase()

```cusparseIndexBase_t
cusparseGetMatIndexBase(const cusparseMatDescr_t descrA)
```

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

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

### 5.13. cusparseGetMatType()

```cusparseMatrixType_t
cusparseGetMatType(const cusparseMatDescr_t descrA)
```

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

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

### 5.14. cusparseGetPointerMode()

```cusparseStatus_t
cusparseGetPointerMode(cusparseHandlet handle,
cusparsePointerMode_t *mode)
```

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

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

### 5.15. cusparseGetVersion()

```cusparseStatus_t
cusparseGetVersion(cusparseHandle_t handle, int *version)
```

This function returns the version number of the CUSPARSE library.

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

### 5.16. cusparseSetMatDiagType()

```cusparseStatus_t
cusparseSetMatDiagType(cusparseMatDescr_t descrA,
cusparseDiagType_t diagType)
```

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

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

### 5.17. cusparseSetMatFillMode()

```cusparseStatus_t
cusparseSetMatFillMode(cusparseMatDescr_t descrA,
cusparseFillMode_t fillMode)
```

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

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

### 5.18. cusparseSetMatIndexBase()

```cusparseStatus_t
cusparseSetMatIndexBase(cusparseMatDescr_t descrA,
cusparseIndexBase_t base)
```

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

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

### 5.19. cusparseSetMatType()

```cusparseStatus_t
cusparseSetMatType(cusparseMatDescr_t descrA, cusparseMatrixType_t type)
```

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

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

### 5.20. cusparseSetPointerMode()

```cusparseStatus_t
cusparseSetPointerMode(cusparseHandle_t handle,
cusparsePointerMode_t mode)
```

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

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

### 5.21. cusparseSetStream()

```cusparseStatus_t
cusparseSetStream(cusparseHandle_t handle, cudaStream_t streamId)
```

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

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

## 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 non-zero values of vector x. xInd integer vector with nnz indices of the non-zero 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 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. xVal vector with nnz non-zero values of vector x. xInd integer vector with nnz indices of the non-zero 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={y}^{H}x$

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 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. xVal vector with nnz non-zero values of vector x. xInd integer vector with nnz indices of the non-zero 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 non-zero values of vector x. idxBase CUSPARSE_INDEX_BASE_ZERO or CUSPARSE_INDEX_BASE_ONE
Output
 xVal vector with nnz non-zero 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 zeroes 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 non-zero values of vector x. idxBase CUSPARSE_INDEX_BASE_ZERO or CUSPARSE_INDEX_BASE_ONE
Output
 xVal vector with nnz non-zero 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 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 non-zero values of vector x. xInd integer vector with nnz indices of the non-zero 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 fomat (that is unchanged if nnz == 0). y updated vector in dense fomat (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 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. xVal vector with nnz non-zero values of vector x. xInd integer vector with nnz indices of the non-zero 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 non-zero 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\$

Several comments on bsrmv:

1. Only CUSPARSE_OPERATION_NON_TRANSPOSE is supported, i.e.

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

2. Only CUSPARSE_MATRIX_TYPE_GENERAL is supported.

3. 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 out-of-array-bound.

Example: suppose the user has a CSR format and wants to try bsrmv, the following code demonstrates csr2csc and csrmv on single precision.

```// Suppose that A is m x n sparse matrix represented by CSR format,
// hx is a host vector of size n, and hy is also a host vector of size m.
// m and n are not multiple of blockDim.
// step 1: transform CSR to BSR with column-major order
int base, nnz;
cusparseDirection_t dirA = CUSPARSE_DIRECTION_COLUMN;
int mb = (m + blockDim-1)/blockDim;
int nb = (n + blockDim-1)/blockDim;
cudaMalloc((void**)&bsrRowPtrC, sizeof(int) *(mb+1));
cusparseXcsr2bsrNnz(handle, dirA, m, n,
descrA, csrRowPtrA, csrColIndA, blockDim,
descrC, bsrRowPtrC);
cudaMemcpy(&nnzb, bsrRowPtrC+mb, sizeof(int), cudaMemcpyDeviceToHost);
cudaMemcpy(&base, bsrRowPtrC   , sizeof(int), cudaMemcpyDeviceToHost);
nnzb -= base;
cudaMalloc((void**)&bsrColIndC, sizeof(int)*nnzb);
cudaMalloc((void**)&bsrValC, sizeof(float)*(blockDim*blockDim)*nnzb);
cusparseScsr2bsr(handle, dirA, m, n,
descrA, csrValA, csrRowPtrA, csrColIndA, blockDim,
descrC, bsrValC, bsrRowPtrC, bsrColIndC);
// step 2: allocate vector x and vector y large enough for bsrmv
cudaMalloc((void**)&x, sizeof(float)*(nb*blockDim));
cudaMalloc((void**)&y, sizeof(float)*(mb*blockDim));
cudaMemcpy(x, hx, sizeof(float)*n, cudaMemcpyHostToDevice);
cudaMemcpy(y, hy, sizeof(float)*m, cudaMemcpyHostToDevice);
// step 3: perform bsrmv
cusparseSbsrmv(handle, dirA, transA, mb, nb, alpha, descrC, bsrValC, bsrRowPtrC, bsrColIndC, blockDim, x, beta, y);```
Input
 handle handle to the CUSPARSE library context. dir storage format of blocks, either CUSPARSE_DIRECTION_ROW or CUSPARSE_DIRECTION_COLUMN . trans the operation $\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 nonz-zero 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)$ non-zero 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 non-zero 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 sizeOfMask,
int mb, int nb, int nnzb,
const float *alpha, const cusparseMatDescr_t descr,
const float *bsrVal, const int *bsrMaskPtr,
const int *bsrRowPtr, const int *bsrEndPtr, const int *bsrColInd,
int blockDim, const float *x,
const float *beta, float *y)
cusparseStatus_t
cusparseDbsrxmv(cusparseHandle_t handle, cusparseDirection_t dir,
cusparseOperation_t trans, int sizeOfMask,
int mb, int nb, int nnzb,
const double *alpha, const cusparseMatDescr_t descr,
const double *bsrVal, const int *bsrMaskPtr,
const int *bsrRowPtr, const int *bsrEndPtr, const int *bsrColInd,
int blockDim, const double *x,
const double *beta, double *y)
cusparseStatus_t
cusparseCbsrxmv(cusparseHandle_t handle, cusparseDirection_t dir,
cusparseOperation_t trans, int sizeOfMask,
int mb, int nb, int nnzb,
const cuComplex *alpha, const cusparseMatDescr_t descr,
const cuComplex *bsrVal, const int *bsrMaskPtr,
const int *bsrRowPtr, const int *bsrEndPtr, const int *bsrColInd,
int blockDim, const cuComplex *x,
const cuComplex *beta, cuComplex *y)
cusparseStatus_t
cusparseZbsrxmv(cusparseHandle_t handle, cusparseDirection_t dir,
cusparseOperation_t trans, int sizeOfMask,
int mb, int nb, int nnzb,
const cuDoubleComplex *alpha, const cusparseMatDescr_t descr,
const cuDoubleComplex *bsrVal, const int *bsrMaskPtr,
const int *bsrRowPtr, const int *bsrEndPtr, const int *bsrColInd,
int blockDim, const cuDoubleComplex *x,
const cuDoubleComplex *beta, cuDoubleComplex *y)```

This function performs a bsrmv and a mask operation

 $\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 row indices of $y$. If row $i$ is not specified in bsrMaskPtr, then bsrxmv does not touch row block $i$ of $A$ and $y\left[i\right]$ .

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 additional array bsrEndPtr which points to 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 mask operator (specified by array bsrMaskPtr) to update particular row indices of $y$ only because ${y}_{1}$ is never changed. In this case, bsrMaskPtr$=$ [2]

The mask operator is equivalent to the following operation (? stands for don’t care)

 $\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]$

In other words, bsrRowPtr[0] and bsrEndPtr[0] are don’t care.

 $\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}$

Several comments on bsrxmv:

Only CUSPARSE_OPERATION_NON_TRANSPOSE and CUSPARSE_MATRIX_TYPE_GENERAL are supported.

bsrMaskPtr, bsrRowPtr, bsrEndPtr and bsrColInd are consistent with base index, either one-based or zero-based. 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 rows of $y$. mb number of block rows of matrix $A$. nb number of block columns of matrix $A$. nnzb number of nonz-zero 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 non-zero blocks of matrix $A$. bsrRowPtr integer array of mb elements that contains the start of every block row and the end of the last block row plus one. 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 non-zero 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}$

where $A$ is 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 of this function 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 operation different than CUSPARSE_OPERATION_NON_TRANSPOSE is selected. 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. 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 nonz-zero 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)$ non-zero 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 non-zero 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), symmetric/Hermitian matrix (c.c. >= 1.2) or transpose operation (c.c. >= 1.1). CUSPARSE_STATUS_INTERNAL_ERROR an internal operation failed. CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED the matrix type is not supported.

### 7.4. 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 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\$

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

This function requires significant amount of extra storage that is proportional to the matrix size. 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. trans the operation $\text{op}\left(A\right)$ m number of rows of matrix $A$. nnz number of nonz-zero elements of matrix $A$. descrA the descriptor of matrix $A$. The supported matrix types are CUSPARSE_MATRIX_TYPE_TRIANGULAR and CUSPARSE_MATRIX_TYPE_GENERAL, while the supported diagonal types are CUSPARSE_DIAG_TYPE_UNIT and CUSPARSE_DIAG_TYPE_NON_UNIT. csrValA array of nnz$\left(=$csrRowPtrA(m)$-$csrRowPtrA(0)$\right)$ non-zero 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 non-zero 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.5. cusparse<t>csrsv_solve

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

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

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

where $A$ is 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\$

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 it 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. csrValA array of nnz$\left(=$csrRowPtrA(m)$-$csrRowPtrA(0)$\right)$ non-zero 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 non-zero elements of matrix $A$. 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.
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 (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.6. cusparse<t>hybmv

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

This function performs the matrix-vector operation

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

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

Notice that currently only $\text{op}\left(A\right)=A$ is supported.

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. transA the operation $\text{op}\left(A\right)$ (currently only $\text{op}\left(A\right)=A$ is supported). m number of rows of matrix $A$. n number of columns of matrix $A$. alpha scalar used for multiplication. descrA the descriptor of matrix $A$. The supported matrix type is CUSPARSE_MATRIX_TYPE_GENERAL. hybA the matrix $A$ in HYB storage format. x vector of n elements. beta scalar used for multiplication. If beta is zero, y does not have to be a valid input. y vector of m 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_ALLOC_FAILED the resources could not be allocated. CUSPARSE_STATUS_INVALID_VALUE the internally stored hyb format parameters are invalid. CUSPARSE_STATUS_ARCH_MISMATCH the device does not support double precision. CUSPARSE_STATUS_EXECUTION_FAILED the function failed to launch on the GPU CUSPARSE_STATUS_INTERNAL_ERROR an internal operation failed. CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED the matrix type is not supported.

### 7.7. cusparse<t>hybsv_analysis

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

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

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

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

Notice that currently only $\text{op}\left(A\right)=A$ is supported.

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

This function requires significant amount of extra storage that is proportional to the matrix size. 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. transA the operation $\text{op}\left(A\right)$ (currently only $\text{op}\left(A\right)=A$ is supported). descrA the descriptor of matrix $A$. The supported matrix type is CUSPARSE_MATRIX_TYPE_TRIANGULAR and diagonal type USPARSE_DIAG_TYPE_NON_UNIT. hybA the matrix $A$ in HYB storage format. info structure initialized using cusparseCreateSolveAnalysisInfo.
Output
 info structure filled with information collected during the analysis phase (that should be passed to the solve phase unchanged).
Status Returned
 CUSPARSE_STATUS_SUCCESS the operation completed successfully. CUSPARSE_STATUS_NOT_INITIALIZED the library was not initialized. CUSPARSE_STATUS_ALLOC_FAILED the resources could not be allocated. CUSPARSE_STATUS_INVALID_VALUE the internally stored hyb format parameters are invalid. CUSPARSE_STATUS_ARCH_MISMATCH the device does not support double precision. CUSPARSE_STATUS_EXECUTION_FAILED the function failed to launch on the GPU CUSPARSE_STATUS_INTERNAL_ERROR an internal operation failed. CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED the matrix type is not supported.

### 7.8. cusparse<t>hybsv_solve

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

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

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

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

Notice that currently only $\text{op}\left(A\right)=A$ is supported.

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

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

Input
 handle handle to the CUSPARSE library context. transA the operation $\text{op}\left(A\right)$ (currently only $\text{op}\left(A\right)=A$ is supported). alpha scalar used for multiplication. descrA the descriptor of matrix $A$. The supported matrix type is CUSPARSE_MATRIX_TYPE_TRIANGULAR and diagonal type CUSPARSE_DIAG_TYPE_NON_UNIT. hybA the matrix $A$ in HYB storage format. info structure with information collected during the analysis phase (that should be passed to the solve phase unchanged). x right-hand-side vector of size m.
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 the internally stored hyb format parameters are invalid. CUSPARSE_STATUS_ARCH_MISMATCH the device does not support double precision. CUSPARSE_STATUS_MAPPING_ERROR the texture binding failed. CUSPARSE_STATUS_EXECUTION_FAILED the function failed to launch on the GPU CUSPARSE_STATUS_INTERNAL_ERROR an internal operation failed. CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED the matrix type is not supported.

## 8. CUSPARSE Level 3 Function Reference

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

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

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

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

### 8.1. cusparse<t>csrmm

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

This function performs one of the following matrix-matrix operation

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

where $A$ is m×k sparse matrix (that is defined in CSR storage format by the three arrays csrValA, csrRowPtrA, and csrColIndA), are dense matrices, 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 of this function 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 csrmm() function with the cusparseOperation_t parameter set to CUSPARSE_OPERATION_NON_TRANSPOSE and with the interchanged rows and columns of the matrix stored in CSC format. This (implicitly) multiplies the vector by the transpose of the matrix in the original CSR format.

This function requires no extra storage for the general matrices when operation CUSPARSE_OPERATION_NON_TRANSPOSE is selected. It requires some extra storage for Hermitian/symmetric matrices and for the general matrices when operation different than CUSPARSE_OPERATION_NON_TRANSPOSE is selected. 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. transA the operation $\text{op}\left(A\right)$ m number of rows of sparse matrix $A$. n number of columns of dense matrix $B$ and $C$. k number of columns of sparse matrix $A$. nnz number of nonz-zero elements of sparse 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)$ non-zero 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 non-zero elements of matrix $A$. B array of dimensions (ldb, n). ldb leading dimension of B. It must be at least $max\text{(1, k)}$ if $\text{op}\left(A\right)=A$ and at least $max\text{(1, m)}$ otherwise. beta scalar used for multiplication. If beta is zero, C does not have to be a valid input. C array of dimensions (ldc, n). ldc leading dimension of C. It must be at least $max\text{(1, k)}$ if $\text{op}\left(A\right)=A$ and at least $max\text{(1, m)}$ otherwise.
Output
 C updated array of dimensions (ldc, n).
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,k,nnz<0 or ldb and ldc are incorrect). 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.

### 8.2. cusparse<t>csrmm2

```cusparseStatus_t
cusparseScsrmm2(cusparseHandle_t         handle,
cusparseOperation_t      transA,
cusparseOperation_t      transB,
int                      m,
int                      n,
int                      k,
int                      nnz,
const float              *alpha,
const cusparseMatDescr_t descrA,
const float              *csrValA,
const int                *csrRowPtrA,
const int                *csrColIndA,
const float              *B,
int                      ldb,
const float              *beta,
float                    *C,
int                      ldc)
cusparseStatus_t
cusparseDcsrmm2(cusparseHandle_t         handle,
cusparseOperation_t      transA,
cusparseOperation_t      transB,
int                      m,
int                      n,
int                      k,
int                      nnz,
const double             *alpha,
const cusparseMatDescr_t descrA,
const double             *csrValA,
const int                *csrRowPtrA,
const int                *csrColIndA,
const double             *B,
int                      ldb,
const double             *beta,
double                   *C,
int                      ldc)
cusparseStatus_t
cusparseCcsrmm2(cusparseHandle_t         handle,
cusparseOperation_t      transA,
cusparseOperation_t      transB,
int                      m,
int                      n,
int                      k,
int                      nnz,
const cuComplex          *alpha,
const cusparseMatDescr_t descrA,
const cuComplex          *csrValA,
const int                *csrRowPtrA,
const int                *csrColIndA,
const cuComplex          *B,
int                      ldb,
const cuComplex          *beta,
cuComplex                *C,
int                      ldc)
cusparseStatus_t
cusparseZcsrmm2(cusparseHandle_t         handle,
cusparseOperation_t      transA,
cusparseOperation_t      transB,
int                      m,
int                      n,
int                      k,
int                      nnz,
const cuDoubleComplex    *alpha,
const cusparseMatDescr_t descrA,
const cuDoubleComplex    *csrValA,
const int                *csrRowPtrA,
const int                *csrColIndA,
const cuDoubleComplex    *B,
int                      ldb,
const cuDoubleComplex    *beta,
cuDoubleComplex          *C,
int                      ldc)```

This function performs one of the following matrix-matrix operation

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

where $A$ is m×k sparse matrix (that is defined in CSR storage format by the three arrays csrValA, csrRowPtrA, and csrColIndA), are dense matrices, are scalars, and

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

,

$\text{op}\left(B\right)=\left\{\begin{array}{ll}B& \text{if transB == CUSPARSE_OPERATION_NON_TRANSPOSE}\\ {B}^{T}& \text{if transB == CUSPARSE_OPERATION_TRANSPOSE}\\ {B}^{H}& \text{not supported}\end{array}\right\$

If op(B)=B, cusparse<t>csrmm2 is the same as cusparse<t>csrmm. Otherwise only op(A)=A is supported and matrix type must be CUSPARSE_MATRIX_TYPE_GENERAL.

The motivation of transpose(B) is to improve memory access of matrix B . The computational pattern of A*transpose(B) with matrix B in column-major order is equivalent to A*B with matrix B in row-mjor order.

In practice, no operation in iterative solver or eigenvalue solver uses A*transpose(B). However we can perform A*transpose(transpose(B)) which is the same as A*B. For example, suppose A is m*k, B is k*n and C is m*n, the following code shows usage of cusparseDcsrmm.

```// A is m*k, B is k*n and C is m*n
const int ldb_B = k ; // leading dimension of B
const int ldc   = m ; // leading dimension of C
// perform C:=alpha*A*B + beta*C
cusparseSetMatType(descrA, CUSPARSE_MATRIX_TYPE_GENERAL ) ;
cusparseDcsrmm2(cusparse_handle,
CUSPARSE_OPERATION_NON_TRANSPOSE,
m, n, k, nnz, alpha,
descrA, csrValA, csrRowPtrA, csrColIndA,
B, ldb_B,
beta, C, ldc);
```

Instead of using A*B, our proposal is to transpose B to Bt first by calling cublas<t>geam, then to perform A*transpose(Bt).

```// step 1: Bt := transpose(B)
double *Bt;
const int ldb_Bt = n ; // leading dimension of Bt
cudaMalloc((void**)&Bt, sizeof(double)*ldb_Bt*k);
double one  = 1.0;
double zero = 0.0;
cublasSetPointerMode(cublas_handle, CUBLAS_POINTER_MODE_HOST);
cublasDgeam(cublas_handle, CUBLAS_OP_T, CUBLAS_OP_T,
n, k, &one, B, int ldb_B, &zero, B, int ldb_B, Bt, ldb_Bt);

// step 2: perform C:=alpha*A*transpose(Bt) + beta*C
cusparseDcsrmm2(cusparse_handle,
CUSPARSE_OPERATION_NON_TRANSPOSE,
CUSPARSE_OPERATION_TRANSPOSE
m, n, k, nnz, alpha,
descrA, csrValA, csrRowPtrA, csrColIndA,
Bt, ldb_Bt,
beta, C, ldc);
```

Remark 1: cublas<t>geam and cusparse<t>csrmm2 are memory-bound. The complexity of cublas<t>geam is 2*n*k and the minimum complexity of cusparse<t>csrmm2 is about (nnz + nnz*n + 2*m*n). If nnz per column (=nnz/k) is large, it is worth paying extra cost on transposition because 'A*transpose(B)' may be 2x faster than 'A*B' if sparsity pattern of A is not good.

Remark 2: A*transpose(B) is only supported on compute capability 2.0 and above.

Input
 handle handle to the CUSPARSE library context. transA the operation $\text{op}\left(A\right)$ transB the operation $\text{op}\left(B\right)$ m number of rows of sparse matrix $A$. n number of columns of dense matrix $\mathrm{op\left(B\right)}$ and $C$. k number of columns of sparse matrix $A$. nnz number of nonz-zero elements of sparse 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)$ non-zero 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 non-zero elements of matrix $A$. B array of dimensions (ldb, n) if op(B)=B and (ldb, k) otherwise. ldb leading dimension of B. If op(B)=B, it must be at least $max\text{(1, k)}$ if $\text{op}\left(A\right)=A$ and at least $max\text{(1, m)}$ otherwise. If op(B) != B, it must be at least max(1, n). beta scalar used for multiplication. If beta is zero, C does not have to be a valid input. C array of dimensions (ldc, n). ldc leading dimension of C. It must be at least $max\text{(1, k)}$ if $\text{op}\left(A\right)=A$ and at least $max\text{(1, m)}$ otherwise.
Output
 C updated array of dimensions (ldc, n).
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,k,nnz<0 or ldb and ldc are incorrect). CUSPARSE_STATUS_ARCH_MISMATCH if op(B)=B the device does not support double precision or if op(B)=transpose(B) the device is below compute capability 2.0. 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 CUSPARSE_MATRIX_TYPE_TRIANGULAR is not supported if op(B)=B and only CUSPARSE_MATRIX_TYPE_GENERAL is supported otherwise.

### 8.3. cusparse<t>csrsm_analysis

```cusparseStatus_t
cusparseScsrsm_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
cusparseDcsrsm_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
cusparseCcsrsm_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
cusparseZcsrsm_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}$

with multiple right-hand-sides, where $A$ is m×m sparse matrix (that is defined in CSR storage format by the three arrays csrValA, csrRowPtrA, and csrColIndA), are the right-hand-side and the solution dense matrices, $\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\$

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

This function requires significant amount of extra storage that is proportional to the matrix size. 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. transA the operation $\text{op}\left(A\right)$ m number of rows of matrix $A$. nnz number of nonz-zero elements of matrix $A$. descrA the descriptor of matrix $A$. The supported matrix types are CUSPARSE_MATRIX_TYPE_TRIANGULAR and CUSPARSE_MATRIX_TYPE_GENERAL, while the supported diagonal types are CUSPARSE_DIAG_TYPE_UNIT and CUSPARSE_DIAG_TYPE_NON_UNIT. csrValA array of nnz$\left(=$csrRowPtrA(m)$-$csrRowPtrA(0)$\right)$ non-zero 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 non-zero 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.

### 8.4. cusparse<t>csrsm_solve

```cusparseStatus_t
cusparseScsrsm_solve(cusparseHandle_t handle,
cusparseOperation_t transA,
int m, int n, const float *alpha,
const cusparseMatDescr_t descrA,
const float           *csrValA,
const int *csrRowPtrA, const int *csrColIndA,
cusparseSolveAnalysisInfo_t info,
const float           *X, int ldx,
float           *Y, int ldy)
cusparseStatus_t
cusparseDcsrsm_solve(cusparseHandle_t handle,
cusparseOperation_t transA,
int m, int n, const double *alpha,
const cusparseMatDescr_t descrA,
const double          *csrValA,
const int *csrRowPtrA, const int *csrColIndA,
cusparseSolveAnalysisInfo_t info,
const double          *X, int ldx,
double          *Y, int ldy)
cusparseStatus_t
cusparseCcsrsm_solve(cusparseHandle_t handle,
cusparseOperation_t transA,
int m, int n, const cuComplex *alpha,
const cusparseMatDescr_t descrA,
const cuComplex       *csrValA,
const int *csrRowPtrA, const int *csrColIndA,
cusparseSolveAnalysisInfo_t info,
const cuComplex       *X, int ldx,
cuComplex       *Y, int ldy)
cusparseStatus_t
cusparseZcsrsm_solve(cusparseHandle_t handle,
cusparseOperation_t transA,
int m, int n, const cuDoubleComplex *alpha,
const cusparseMatDescr_t descrA,
const cuDoubleComplex *csrValA,
const int *csrRowPtrA, const int *csrColIndA,
cusparseSolveAnalysisInfo_t info,
const cuDoubleComplex *X, int ldx,
cuDoubleComplex *Y, int ldy)```

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

 $\text{op}\left(A\right)\ast \text{Y}=\alpha \ast \text{X}$

with multiple right-hand-sides, where $A$ is m×n sparse matrix (that is defined in CSR storage format by the three arrays csrValA, csrRowPtrA, and csrColIndA), are the right-hand-side and the solution dense matrices, $\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 it may return control to the application on the host before the result is ready.

Input
 handle handle to the CUSPARSE library context. transA the operation $\text{op}\left(A\right)$ m number of rows and columns of matrix $A$. n number of columns of matrix $X$ and $Y$. 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. csrValA array of nnz$\left(=$csrRowPtrA(m)$-$csrRowPtrA(0)$\right)$ non-zero 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 non-zero elements of matrix $A$. info structure with information collected during the analysis phase (that should be passed to the solve phase unchanged). X right-hand-side array of dimensions (ldx, n). ldx leading dimension of X. (that is ≥ $max\text{(1, m)}$ ).
Output
 Y solution array of dimensions (ldy, n). ldy leading dimension of Y. (that is ≥ $max\text{(1, 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.

## 9. CUSPARSE Extra Function Reference

This chapter describes the extra routines used to manipulate sparse matrices.

### 9.1. cusparse<t>csrgeam

```cusparseStatus_t
cusparseXcsrgeamNnz(cusparseHandle_t handle, int m, int n,
const cusparseMatDescr_t descrA, int nnzA,
const int *csrRowPtrA, const int *csrColIndA,
const cusparseMatDescr_t descrB, int nnzB,
const int *csrRowPtrB, const int *csrColIndB,
const cusparseMatDescr_t descrC, int *csrRowPtrC,
int *nnzTotalDevHostPtr)
cusparseStatus_t
cusparseScsrgeam(cusparseHandle_t handle, int m, int n,
const float *alpha,
const cusparseMatDescr_t descrA, int nnzA,
const float *csrValA, const int *csrRowPtrA, const int *csrColIndA,
const float *beta,
const cusparseMatDescr_t descrB, int nnzB,
const float *csrValB, const int *csrRowPtrB, const int *csrColIndB,
const cusparseMatDescr_t descrC,
float *csrValC, int *csrRowPtrC, int *csrColIndC)
cusparseStatus_t
cusparseDcsrgeam(cusparseHandle_t handle, int m, int n,
const double *alpha,
const cusparseMatDescr_t descrA, int nnzA,
const double *csrValA, const int *csrRowPtrA, const int *csrColIndA,
const double *beta,
const cusparseMatDescr_t descrB, int nnzB,
const double *csrValB, const int *csrRowPtrB, const int *csrColIndB,
const cusparseMatDescr_t descrC,
double *csrValC, int *csrRowPtrC, int *csrColIndC)
cusparseStatus_t
cusparseCcsrgeam(cusparseHandle_t handle, int m, int n,
const cuComplex *alpha,
const cusparseMatDescr_t descrA, int nnzA,
const cuComplex *csrValA, const int *csrRowPtrA, const int *csrColIndA,
const cuComplex *beta,
const cusparseMatDescr_t descrB, int nnzB,
const cuComplex *csrValB, const int *csrRowPtrB, const int *csrColIndB,
const cusparseMatDescr_t descrC,
cuComplex *csrValC, int *csrRowPtrC, int *csrColIndC)
cusparseStatus_t
cusparseZcsrgeam(cusparseHandle_t handle, int m, int n,
const cuDoubleComplex *alpha,
const cusparseMatDescr_t descrA, int nnzA,
const cuDoubleComplex *csrValA, const int *csrRowPtrA,
const int *csrColIndA,
const cuDoubleComplex *beta,
const cusparseMatDescr_t descrB, int nnzB,
const cuDoubleComplex *csrValB, const int *csrRowPtrB,
const int *csrColIndB,
const cusparseMatDescr_t descrC,
cuDoubleComplex *csrValC, int *csrRowPtrC, int *csrColIndC)```

This function performs following matrix-matrix operation

 $C=\alpha \ast A+\beta \ast B$

where $A$, $B$ and $C$ are m×n sparse matrices (defined in CSR storage format by the three arrays csrValA|csrValB|csrValC, csrRowPtrA|csrRowPtrB|csrRowPtrC, and csrColIndA|csrColIndB|csrcolIndC respectively), and are scalars. Since $A$ and $B$ have different sparsity patterns, CUSPARSE adopts two-step approach to complete sparse matrix C. In the first step, the user allocates csrRowPtrC of m+1elements and uses function cusparseXcsrgeamNnz to determine csrRowPtrC and total number of nonzero elements. In the second step, the user gathers nnzC (number of non-zero elements of matrix C) from either (nnzC=*nnzTotalDevHostPtr) or (nnzC=csrRowPtrC(m)-csrRowPtrC(0)) and allocates csrValC, csrColIndC of nnzC elements respectively, then finally calls function cusparse[S|D|C|Z]csrgeam to complete matrix C.

The general procedure is as follows:

```int baseC, nnzC;
// nnzTotalDevHostPtr points to host memory
int *nnzTotalDevHostPtr = &nnzC;
cusparseSetPointerNode(handle, CUSPARSE_POINTER_MODE_HOST);
cudaMalloc((void**)&csrRowPtrC, sizeof(int)*(m+1));
cusparseXcsrgeamNnz(handle, m, n,
descrA, nnzA, csrRowPtrA, csrColIndA,
descrB, nnzB, csrRowPtrB, csrColIndB,
descrC, csrRowPtrC, nnzTotalDevHostPtr);
if (NULL != nnzTotalDevHostPtr){
nnzC = *nnzTotalDevHostPtr;
}else{
cudaMemcpy(&nnzC , csrRowPtrC+m, sizeof(int), cudaMemcpyDeviceToHost);
cudaMemcpy(&baseC, csrRowPtrC  , sizeof(int), cudaMemcpyDeviceToHost);
nnzC -= baseC;
}
cudaMalloc((void**)&csrColIndC, sizeof(int)*nnzC);
cudaMalloc((void**)&csrValC   , sizeof(float)*nnzC);
cusparseScsrgeam(handle, m, n,
alpha,
descrA, nnzA,
csrValA, csrRowPtrA, csrColIndA,
beta,
descrB, nnzB,
csrValB, csrRowPtrB, csrColIndB,
descrC,
csrValC, csrRowPtrC, csrColIndC);```

Several comments on csrgeam:

1. CUSPARSE does not support other three combinations, NT, TN and TT. In order to do any one of above three, the user should use the routine csr2csc to convert $A$| $B$ to ${A}^{T}$| ${B}^{T}$.

2. Only CUSPARSE_MATRIX_TYPE_GENERAL is supported, if either $A$ or $B$ is symmetric or hermitian, then the user must extend the matrix to a full one and reconfigure MatrixType field of descriptor to CUSPARSE_MATRIX_TYPE_GENERAL.

3. If the sparsity pattern of matrix C is known, then the user can skip the call to function cusparseXcsrgeamNnz. For example, suppose that the user has an iterative algorithm which would update $A$ and $B$ iteratively but keep sparsity patterns. The user can call function cusparseXcsrgeamNnz once to setup sparsity pattern of C, then call function cusparse[S|D|C|Z]geam only for each iteration.

4. The pointers, alpha and beta, must be valid.

5. CUSPARSE would not consider special case when alpha or beta is zero. The sparsity pattern of C is independent of value of alpha and beta. If the user want $C=0×A+1×{B}^{T}$ , then csr2csc is better than csrgeam.

Input
 handle handle to the CUSPARSE library context. m number of rows of sparse matrix A,B,C. n number of columns of sparse matrix A,B,C. alpha scalar used for multiplication. descrA the descriptor of matrix $A$. The supported matrix type is CUSPARSE_MATRIX_TYPE_GENERAL only. nnzA number of nonz-zero elements of sparse matrix A. csrValA array of nnzA$\left(=$csrRowPtrA(m)$-$csrRowPtrA(0)$\right)$ non-zero 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 nnzA$\left(=$csrRowPtrA(m)$-$csrRowPtrA(0)$\right)$ column indices of the non-zero elements of matrix $A$. beta scalar used for multiplication. If beta is zero, y does not have to be a valid input. descrB the descriptor of matrix $B$. The supported matrix type is CUSPARSE_MATRIX_TYPE_GENERAL only. nnzB number of nonz-zero elements of sparse matrix B. csrValB array of nnzB$\left(=$csrRowPtrB(m)$-$csrRowPtrB(0)$\right)$ non-zero elements of matrix $B$. csrRowPtrB integer array of m$+1$ elements that contains the start of every row and the end of the last row plus one. csrColIndB integer array of nnzB$\left(=$csrRowPtrB(m)$-$csrRowPtrB(0)$\right)$ column indices of the non-zero elements of matrix $B$. descrC the descriptor of matrix $C$. The supported matrix type is CUSPARSE_MATRIX_TYPE_GENERAL only.
Output
 csrValC array of nnzC$\left(=$csrRowPtrC(m)$-$csrRowPtrC(0)$\right)$ non-zero elements of matrix $C$. csrRowPtrC integer array of m$+1$ elements that contains the start of every row and the end of the last row plus one. csrColIndC integer array of nnzC$\left(=$csrRowPtrC(m)$-$csrRowPtrC(0)$\right)$ column indices of the non-zero elements of matrix $C$. nnzTotalDevHostPtr total number of nonzero elements in device or host memory.It is equal to (csrRowPtrC(m)-csrRowPtrC(0)).
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, IndexBase of descrA,descrB,descrC is not base-0 or base-1, or alpha or beta is nil )). 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_MATRIX_TYPE_NOT_SUPPORTED the matrix type is not supported. CUSPARSE_STATUS_INTERNAL_ERROR an internal operation failed.

### 9.2. cusparse<t>csrgemm

```cusparseStatus_t
cusparseXcsrgemmNnz(cusparseHandle_t handle,
cusparseOperation_t transA, cusparseOperation_t transB,
int m, int n, int k,
const cusparseMatDescr_t descrA, const int nnzA,
const int *csrRowPtrA, const int *csrColIndA,
const cusparseMatDescr_t descrB, const int nnzB,
const int *csrRowPtrB, const int *csrColIndB,
const cusparseMatDescr_t descrC, int *csrRowPtrC,
int *nnzTotalDevHostPtr )
cusparseStatus_t
cusparseScsrgemm(cusparseHandle_t handle,
cusparseOperation_t transA, cusparseOperation_t transB,
int m, int n, int k,
const cusparseMatDescr_t descrA, const int nnzA,
const float *csrValA,
const int *csrRowPtrA, const int *csrColIndA,
const cusparseMatDescr_t descrB, const int nnzB,
const float *csrValB,
const int *csrRowPtrB, const int *csrColIndB,
const cusparseMatDescr_t descrC,
float *csrValC,
const int *csrRowPtrC, int *csrColIndC )
cusparseStatus_t
cusparseDcsrgemm(cusparseHandle_t handle,
cusparseOperation_t transA, cusparseOperation_t transB,
int m, int n, int k,
const cusparseMatDescr_t descrA, const int nnzA,
const double *csrValA,
const int *csrRowPtrA, const int *csrColIndA,
const cusparseMatDescr_t descrB, const int nnzB,
const double *csrValB,
const int *csrRowPtrB, const int *csrColIndB,
const cusparseMatDescr_t descrC,
double *csrValC,
const int *csrRowPtrC, int *csrColIndC )
cusparseStatus_t
cusparseCcsrgemm(cusparseHandle_t handle,
cusparseOperation_t transA, cusparseOperation_t transB,
int m, int n, int k,
const cusparseMatDescr_t descrA, const int nnzA,
const cuComplex *csrValA,
const int *csrRowPtrA, const int *csrColIndA,
const cusparseMatDescr_t descrB, const int nnzB,
const cuComplex *csrValB,
const int *csrRowPtrB, const int *csrColIndB,
const cusparseMatDescr_t descrC,
cuComplex *csrValC,
const int *csrRowPtrC, int *csrColIndC )
cusparseStatus_t
cusparseZcsrgemm(cusparseHandle_t handle,
cusparseOperation_t transA, cusparseOperation_t transB,
int m, int n, int k,
const cusparseMatDescr_t descrA, const int nnzA,
const cuDoubleComplex *csrValA,
const int *csrRowPtrA, const int *csrColIndA,
const cusparseMatDescr_t descrB, const int nnzB,
const cuDoubleComplex *csrValB,
const int *csrRowPtrB, const int *csrColIndB,
const cusparseMatDescr_t descrC,
cuDoubleComplex *csrValC,
const int *csrRowPtrC, int *csrColIndC )```

This function performs following matrix-matrix operation

 $\text{C}=\text{op}\left(A\right)\ast \text{op}\left(B\right)$

where $\text{op}\left(A\right)$ , $\text{op}\left(B\right)$ and $C$ are m×k, k×n, and m×n sparse matrices (defined in CSR storage format by the three arrays csrValA|csrValB|csrValC, csrRowPtrA|csrRowPtrB|csrRowPtrC, and csrColIndA|csrColIndB|csrcolIndC respectively. The operation is defined by

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

There are four versions, NN, NT, TN and TT. NN stands for $C=A*B$ , NT stands for $C=A*{B}^{T}$ , TN stands for $C={A}^{T}*B$ and TT stands for $C={A}^{T}*{A}^{T}$ .

CUSPARSE adopts two-step approach to complete sparse matrix . In the first step, the user allocates csrRowPtrC of m+1 elements and uses function cusparseXcsrgemmNnz to determine csrRowPtrC and total number of nonzero elements. In the second step, the user gathers nnzC (number of nonzero elements of matrix C) from either (nnzC=*nnzTotalDevHostPtr) or (nnzC=csrRowPtrC(m)-csrRowPtrC(0)) and allocates csrValC, csrColIndC of nnzC elements respectively, then finally calls function cusparse[S|D|C|Z]csrgemm to complete matrix C.

The general procedure is as follows:

```int baseC, nnzC;
// nnzTotalDevHostPtr points to host memory
int *nnzTotalDevHostPtr = &nnzC;
cusparseSetPointerMode(handle, CUSPARSE_POINTER_MODE_HOST);
cudaMalloc((void**)&csrRowPtrC, sizeof(int)*(m+1));
cusparseXcsrgemmNnz(handle, m, n, k,
descrA, nnzA, csrRowPtrA, csrColIndA,
descrB, nnzB, csrRowPtrB, csrColIndB,
descrC, csrRowPtrC, nnzTotalDevHostPtr );
if (NULL != nnzTotalDevHostPtr){
nnzC = *nnzTotalDevHostPtr;
}else{
cudaMemcpy(&nnzC , csrRowPtrC+m, sizeof(int), cudaMemcpyDeviceToHost);
cudaMemcpy(&baseC, csrRowPtrC  , sizeof(int), cudaMemcpyDeviceToHost);
nnzC -= baseC;
}
cudaMalloc((void**)&csrColIndC, sizeof(int)*nnzC);
cudaMalloc((void**)&csrValC   , sizeof(float)*nnzC);
cusparseScsrgemm(handle, transA, transB, m, n, k,
descrA, nnzA,
csrValA, csrRowPtrA, csrColIndA,
descrB, nnzB,
csrValB, csrRowPtrB, csrColIndB,
descrC,
csrValC, csrRowPtrC, csrColIndC);```

Several comments on csrgemm:

1. Only NN version is implemented. For NT version, matrix $B$ is converted to ${B}^{T}$ by csr2csc and call NN version. The same technique applies to TN and TT. The csr2csc routine would allocate working space implicitly, if the user needs memory management, then NN version is better.

2. NN version needs working space of size nnzA integers at least.

3. Only CUSPARSE_MATRIX_TYPE_GENERAL is supported, if either $A$ or $B$ is symmetric or hermitian, then the user must extend the matrix to a full one and reconfigure MatrixType field of descriptor to CUSPARSE_MATRIX_TYPE_GENERAL.

4. Only support devices of compute capability 2.0 or above.

Input
 handle handle to the CUSPARSE library context. transA the operation $\text{op}\left(A\right)$ transB the operation $\text{op}\left(B\right)$ m number of rows of sparse matrix $\text{op}\left(A\right)$ and C. n number of columns of sparse matrix $\text{op}\left(B\right)$ and C. k number of columns/rows of sparse matrix $\text{op}\left(A\right)$ / $\text{op}\left(B\right)$ . descrA the descriptor of matrix $A$. The supported matrix type is CUSPARSE_MATRIX_TYPE_GENERAL only. nnzA number of nonz-zero elements of sparse matrix A. csrValA array of nnzA$\left(=$csrRowPtrA(m)$-$csrRowPtrA(0)$\right)$ non-zero elements of matrix $A$. csrRowPtrA integer array of $\stackrel{˜}{m}+1$ elements that contains the start of every row and the end of the last row plus one. $\stackrel{˜}{m}=m$ if transA == CUSPARSE_OPERATION_NON_TRANSPOSE, otherwise $\stackrel{˜}{m}=k$ . csrColIndA integer array of nnzA$\left(=$csrRowPtrA(m)$-$csrRowPtrA(0)$\right)$ column indices of the non-zero elements of matrix $A$. descrB the descriptor of matrix $B$. The supported matrix type is CUSPARSE_MATRIX_TYPE_GENERAL only. nnzB number of nonz-zero elements of sparse matrix B. csrValB array of nnzB non-zero elements of matrix $B$. csrRowPtrB integer array of $\stackrel{˜}{k}+1$ elements that contains the start of every row and the end of the last row plus one. $\stackrel{˜}{k}=k$ if transB == CUSPARSE_OPERATION_NON_TRANSPOSE, otherwise $\stackrel{˜}{k}=n$ csrColIndB integer array of nnzB column indices of the non-zero elements of matrix $B$. descrC the descriptor of matrix $C$. The supported matrix type is CUSPARSE_MATRIX_TYPE_GENERAL only.
Output
 csrValC array of nnzC$\left(=$csrRowPtrC(m)$-$csrRowPtrC(0)$\right)$ non-zero elements of matrix $C$. csrRowPtrC integer array of m+1 elements that contains the start of every row and the end of the last row plus one. csrColIndC integer array of nnzC$\left(=$csrRowPtrC(m)$-$csrRowPtrC(0)$\right)$ column indices of the non-zero elements of matrix $C$. nnzTotalDevHostPtr total number of nonzero elements in device or host memory. It is equal to (csrRowPtrC(m)-csrRowPtrC(0)).
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,k<0, IndexBase of descrA,descrB,descrC is not base-0 or base-1, or alpha or beta is nil )). 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_MATRIX_TYPE_NOT_SUPPORTED the matrix type is not supported. CUSPARSE_STATUS_INTERNAL_ERROR an internal operation failed.

## 10. CUSPARSE Preconditioners Reference

This chapter describes the routines that implement different preconditioners.

In particular, the incomplete factorizations are 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 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 numerical factorization phase, the given coefficient matrix is factorized using the information stored in the cusparseSolveAnalysisInfo_t parameter by calling the appropriate csrilu0 or csric0 function.

The analysis phase is shared across the sparse triangular solve and the incomplete factorization and must be performed only once. While the resulting information can be passed to the numerical factorization and the sparse triangular solve multiple times.

Finally, once the incomplete factorization and all the sparse triangular solves have completed, the opaque data structure pointed to by the cusparseSolveAnalysisInfo_t parameter can be released by calling cusparseDestroySolveAnalysisInfo().

### 10.1. cusparse<t>csric0

```cusparseStatus_t
cusparseScsric0(cusparseHandle_t handle, cusparseOperation_t trans,
int m, const cusparseMatDescr_t descrA,
float           *csrValM,
const int *csrRowPtrA, const int *csrColIndA,
cusparseSolveAnalysisInfo_t info)
cusparseStatus_t
cusparseDcsric0(cusparseHandle_t handle, cusparseOperation_t trans,
int m, const cusparseMatDescr_t descrA,
double          *csrValM,
const int *csrRowPtrA, const int *csrColIndA,
cusparseSolveAnalysisInfo_t info)
cusparseStatus_t
cusparseCcsric0(cusparseHandle_t handle, cusparseOperation_t trans,
int m, const cusparseMatDescr_t descrA,
cuComplex       *csrValM,
const int *csrRowPtrA, const int *csrColIndA,
cusparseSolveAnalysisInfo_t info)
cusparseStatus_t
cusparseZcsric0(cusparseHandle_t handle, cusparseOperation_t trans,
int m, const cusparseMatDescr_t descrA,
cuDoubleComplex *csrValM,
const int *csrRowPtrA, const int *csrColIndA,
cusparseSolveAnalysisInfo_t info)```

This function computes the incomplete-Cholesky factorization with $0$ fill-in and no pivoting

 $op\left(A\right)\approx {R}^{T}R$

where $A$ is m$×$m Hermitian/symmetric positive definite sparse matrix (that is defined in CSR storage format by the three arrays csrValM, csrRowPtrA and csrColIndA) 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\$

Notice that only a lower or upper Hermitian/symmetric part of the matrix $A$ is actually stored. It is overwritten by the lower or upper triangular factor ${R}^{T}$ or $R$ , respectively.

A call to this routine must be preceeded by a call to the csrsv_analysis routine.

This function requires some 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. trans the operation op$\left(A\right)$ m number of rows and columns of matrix $A$. descrA the descriptor of matrix $A$. The supported matrix type is CUSPARSE_MATRIX_TYPE_SYMMETRIC and CUSPARSE_MATRIX_TYPE_HERMITIAN. Also, the supported index bases are CUSPARSE_INDEX_BASE_ZERO and CUSPARSE_INDEX_BASE_ONE. csrValM array of nnz$\left(=$csrRowPtrA(m)$-$csrRowPtrA(0)$\right)$ non-zero 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 non-zero elements of matrix $A$. info structure with information collected during the analysis phase (that should have been passed to the solve phase unchanged).
Output
 csrValM matrix containg the incomplete-Cholesky lower or upper triangular factor.
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<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.

### 10.2. cusparse<t>csrilu0

```cusparseStatus_t
cusparseScsrilu0(cusparseHandle_t handle, cusparseOperation_t trans,
int m, const cusparseMatDescr_t descrA,
float           *csrValM,
const int *csrRowPtrA, const int *csrColIndA,
cusparseSolveAnalysisInfo_t info)
cusparseStatus_t
cusparseDcsrilu0(cusparseHandle_t handle, cusparseOperation_t trans,
int m, const cusparseMatDescr_t descrA,
double          *csrValM,
const int *csrRowPtrA, const int *csrColIndA,
cusparseSolveAnalysisInfo_t info)
cusparseStatus_t
cusparseCcsrilu0(cusparseHandle_t handle, cusparseOperation_t trans,
int m, const cusparseMatDescr_t descrA,
cuComplex       *csrValM,
const int *csrRowPtrA, const int *csrColIndA,
cusparseSolveAnalysisInfo_t info)
cusparseStatus_t
cusparseZcsrilu0(cusparseHandle_t handle, cusparseOperation_t trans,
int m, const cusparseMatDescr_t descrA,
cuDoubleComplex *csrValM,
const int *csrRowPtrA, const int *csrColIndA,
cusparseSolveAnalysisInfo_t info)```

This function computes the incomplete-LU factorization with $0$ fill-in and no pivoting

 $op\left(A\right)\approx LU$

where $A$ is m$×$m sparse matrix (that is defined in CSR storage format by the three arrays csrValM, csrRowPtrA and csrColIndA) 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\$

Notice that the diagonal of lower triangular factor $L$ is unitary and need not be stored. Therefore the input matrix is ovewritten with the resulting lower and upper triangular factor $L$ and $U$, respectively.

A call to this routine must be preceeded by a call to the csrsv_analysis routine.

This function requires some 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. trans the operation op$\left(A\right)$ m number of rows and columns of matrix $A$. descrA 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. csrValM array of nnz$\left(=$csrRowPtrA(m)$-$csrRowPtrA(0)$\right)$ non-zero 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 non-zero elements of matrix $A$. info structure with information collected during the analysis phase (that should have been passed to the solve phase unchanged).
Output
 csrValM matrix containg the incomplete-LU lower and upper triangular factors.
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<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.

### 10.3. cusparse<t>gtsv

```cusparseStatus_t
cusparseSgtsv(cusparseHandle_t handle, int m, int n,
const float           *dl, const float            *d,
const float           *du, float *B, int ldb)
cusparseStatus_t
cusparseDgtsv(cusparseHandle_t handle, int m, int n,
const double          *dl, const double           *d,
const double          *du, double *B, int ldb)
cusparseStatus_t
cusparseCgtsv(cusparseHandle_t handle, int m, int n,
const cuComplex       *dl, const cuComplex        *d,
const cuComplex       *du, cuComplex       *B, int ldb)
cusparseStatus_t
cusparseZgtsv(cusparseHandle_t handle, int m, int n,
const cuDoubleComplex *dl, const cuDoubleComplex  *d,
const cuDoubleComplex *du, cuDoubleComplex *B, int ldb)```

This function computes the solution of a tridiagonal linear system

 $A\ast Y=\alpha \ast X$

with multiple right-hand-sides.

The coefficient matrix $A$ of each of these tri-diagonal linear system is defined with three vectors corresponding to its lower (ld), main (d) and upper (ud) matrix diagonals, while the right-hand-sides are stored in the dense matrix $X$. Notice that the solutions $Y$ overwrite the right-hand-sides $X$ on exit.

The routine does perform pivoting, which usually results in more accurate and more stable results than cusparse<t>gtsv_nopivot at the expense of some execution time

This routine requires significant amount of temporary extra storage (min(m,8) ×(3+n)×sizeof(<type>)). 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. m the size of the linear system (must be ≥ 3). n number of right-hand-sides, columns of matrix B. dl dense array containing the lower diagonal of the tri-diagonal linear system. The first element of each lower diagonal must be zero. d dense array containing the main diagonal of the tri-diagonal linear system. du dense array containing the upper diagonal of the tri-diagonal linear system. The last element of each upper diagonal must be zero. B dense right-hand-side array of dimensions (ldb, n). ldb leading dimension of B. (that is ≥ $max\text{(1, m))}$
Output
 B dense solution array of dimensions (ldb, n).
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<3, n<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.

### 10.4. cusparse<t>gtsv_nopivot

```cusparseStatus_t
cusparseSgtsv_nopivot(cusparseHandle_t handle, int m, int n,
const float           *dl, const float            *d,
const float           *du, float *B, int ldb)
cusparseStatus_t
cusparseDgtsv_nopivot(cusparseHandle_t handle, int m, int n,
const double          *dl, const double           *d,
const double          *du, double *B, int ldb)
cusparseStatus_t
cusparseCgtsv_nopivot(cusparseHandle_t handle, int m, int n,
const cuComplex       *dl, const cuComplex        *d,
const cuComplex       *du, cuComplex       *B, int ldb)
cusparseStatus_t
cusparseZgtsv_nopivot(cusparseHandle_t handle, int m, int n,
const cuDoubleComplex *dl, const cuDoubleComplex  *d,
const cuDoubleComplex *du, cuDoubleComplex *B, int ldb)```

This function computes the solution of a tridiagonal linear system

 $A\ast Y=\alpha \ast X$

with multiple right-hand-sides.

The coefficient matrix $A$ of each of these tri-diagonal linear system is defined with three vectors corresponding to its lower (ld), main (d) and upper (ud) matrix diagonals, while the right-hand-sides are stored in the dense matrix $X$. Notice that the solutions $Y$ overwrite the right-hand-sides $X$ on exit.

The routine does not perform any pivoting and uses a combination of the Cyclic Reduction (CR) and Parallel Cyclic Reduction (PCR) algorithms to find the solution. It achieves better performance when m is a power of 2.

This routine requires significant amount of temporary extra storage (m×(3+n)×sizeof(<type>)). 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. m the size of the linear system (must be ≥ 3). n number of right-hand-sides, columns of matrix B. dl dense array containing the lower diagonal of the tri-diagonal linear system. The first element of each lower diagonal must be zero. d dense array containing the main diagonal of the tri-diagonal linear system. du dense array containing the upper diagonal of the tri-diagonal linear system. The last element of each upper diagonal must be zero. B dense right-hand-side array of dimensions (ldb, n). ldb leading dimension of B. (that is ≥ $max\text{(1, m))}$
Output
 B dense solution array of dimensions (ldb, n).
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<3, n<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.

### 10.5. cusparse<t>gtsvStridedBatch

```cusparseStatus_t
cusparseSgtsvStridedBatch(cusparseHandle_t handle, int m,
const float           *dl,
const float            *d,
const float           *du, float           *x,
int batchCount, int batchStride)
cusparseStatus_t
cusparseDgtsvStridedBatch(cusparseHandle_t handle, int m,
const double          *dl,
const double           *d,
const double          *du, double          *x,
int batchCount, int batchStride)
cusparseStatus_t
cusparseCgtsvStridedBatch(cusparseHandle_t handle, int m,
const cuComplex       *dl,
const cuComplex        *d,
const cuComplex       *du, cuComplex       *x,
int batchCount, int batchStride)
cusparseStatus_t
cusparseZgtsvStridedBatch(cusparseHandle_t handle, int m,
const cuDoubleComplex *dl,
const cuDoubleComplex  *d,
const cuDoubleComplex *du, cuDoubleComplex *x,
int batchCount, int batchStride)```

This function computes the solution of multiple tridiagonal linear systems

 ${A}^{\left(i\right)}\ast {\text{y}}^{\left(i\right)}=\alpha \ast {\text{x}}^{\left(i\right)}$

for i=0,\ldots,batchCount.

The coefficient matrix $A$ of each of these tri-diagonal linear system is defined with three vectors corresponding to its lower (ld), main (d) and upper (ud) matrix diagonals, while the right-hand-side is stored in the vector x. Notice that the solution y overwrites the right-hand-side x on exit. The different matrices are assumed to be of the same size and are stored with a fixed batchStride in memory.

The routine does not perform any pivoting and uses a combination of the Cyclic Reduction (CR) and Parallel Cyclic Reduction (PCR) algorithms to find the solution. It achieves better performance when m is a power of 2.

This routine requires significant amount of temporary extra storage ((batchCount×(4×m+2048)×sizeof(<type>))). 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. m the size of the linear system (must be ≥ 3). dl dense array containing the lower diagonal of the tri-diagonal linear system. The lower diagonal $d{l}^{\left(i\right)}$ that corresponds to the ith linear system starts at location dl+batchStride×i in memory. Also, the first element of each lower diagonal must be zero. d dense array containing the main diagonal of the tri-diagonal linear system. The main diagonal ${d}^{\left(i\right)}$ that corresponds to the ith linear system starts at location d+batchStride×i in memory. du dense array containing the upper diagonal of the tri-diagonal linear system. The upper diagonal $d{u}^{\left(i\right)}$ that corresponds to the ith linear system starts at location du+batchStride×i in memory. Also, the last element of each upper diagonal must be zero. x dense array that contains the right-hand-side of the tri-diagonal linear system. The right-hand-side ${x}^{\left(i\right)}$ that corresponds to the ith linear system starts at location x+batchStride×iin memory. batchCount Number of systems to solve. batchStride stride (number of elements) that separates the vectors of every system (must be at least m).
Output
 x dense array that contains the solution of the tri-diagonal linear system. The solution ${x}^{\left(i\right)}$ that corresponds to the ith linear system starts at location x+batchStride×iin memory.
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<3, batchCount≤0, batchStride

## 11. CUSPARSE Format Conversion Reference

This chapter describes the conversion routines between different sparse and dense storage formats.

### 11.1. cusparse<t>bsr2csr

```cusparseStatus_t
cusparseSbsr2csr(cusparseHandle_t handle, cusparseDirection_t dirA,
int mb, int nb,
const cusparseMatDescr_t descrA, const float *bsrValA,
const int *bsrRowPtrA, const int *bsrColIndA,
int blockDim,
const cusparseMatDescr_t descrC,
float *csrValC, int *csrRowPtrC, int *csrColIndC)
cusparseStatus_t
cusparseDbsr2csr(cusparseHandle_t handle, cusparseDirection_t dirA,
int mb, int nb,
const cusparseMatDescr_t descrA, const double *bsrValA,
const int *bsrRowPtrA, const int *bsrColIndA,
int blockDim,
const cusparseMatDescr_t descrC,
double *csrValC, int *csrRowPtrC, int *csrColIndC)
cusparseStatus_t
cusparseCbsr2csr(cusparseHandle_t handle, cusparseDirection_t dirA,
int mb, int nb,
const cusparseMatDescr_t descrA, const cuComplex *bsrValA,
const int *bsrRowPtrA, const int *bsrColIndA,
int blockDim,
const cusparseMatDescr_t descrC,
cuComplex *csrValC, int *csrRowPtrC, int *csrColIndC)
cusparseStatus_t
cusparseZbsr2csr(cusparseHandle_t handle, cusparseDirection_t dirA,
int mb, int nb,
const cusparseMatDescr_t descrA, const cuDoubleComplex *bsrValA,
const int *bsrRowPtrA, const int *bsrColIndA,
int blockDim,
const cusparseMatDescr_t descrC,
cuDoubleComplex *csrValC, int *csrRowPtrC, int *csrColIndC)```

This function converts a sparse matrix in BSR format (that is defined by the three arrays bsrValA, bsrRowPtrA, and bsrColIndA) into a sparse matrix in CSR format (that is defined by arrays csrValC, csrRowPtrC, and csrColIndC).

Let $m\left(=mb\ast blockDim\right)$ be number of rows of $A$ and $n\left(=nb\ast blockDim\right)$ be number of columns of $A$, then $A$ and $C$ are m×n sparse matricies. BSR format of $A$ contains $nnzb\left(=$csrRowPtrC(mb) − csrRowPtrC(0)$\right)$ non-zero blocks whereas sparse matrix $A$ contains $nnz\left(=nnzb\ast blockDi{m}^{2}\right)$ elements. The user must allocate enough space for arrays csrRowPtrC, csrColIndC and csrValC. The requirements are

csrRowPtrC of m+1 elements,

csrValC of nnz elements, and

csrColIndC of nnz elements.

The general procedure is as follows:

```// Given BSR format (bsrRowPtrA, bsrcolIndA, bsrValA) and
// blocks of BSR format are stored in column-major order.
cusparseDirection_t dirA = CUSPARSE_DIRECTION_COLUMN;
int m = mb*blockDim;
int nnzb = bsrRowPtrA[mb] - bsrRowPtrA[0]; // number of blocks
int nnz  = nnzb * blockDim * blockDim; // number of elements
cudaMalloc((void**)&csrRowPtrC, sizeof(int)*(m+1));
cudaMalloc((void**)&csrColIndC, sizeof(int)*nnz);
cudaMalloc((void**)&csrValC   , sizeof(float)*nnz);
cusparseSbsr2csr(handle, dirA, mb, nb,
descrA,
bsrValA, bsrRowPtrA, bsrColIndA,
blockDim,
descrC,
csrValC, csrRowPtrC, csrColIndC);```
Input
 handle handle to the CUSPARSE library context. dirA storage format of blocks, either CUSPARSE_DIRECTION_ROW or CUSPARSE_DIRECTION_COLUMN. mb number of block rows of sparse matrix A. The number of rows of sparse matrix C is m (= mb * blockDim) nb number of block columns of sparse matrix A. The number of columns of sparse matrix C is n (= nb * blockDim) descrA the descriptor of matrix $A$. bsrValA array of nnzb* $blockDi{m}^{2}$ non-zero elements of matrix A. bsrRowPtrA integer array ofmb+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 column indices of the non-zero blocks of matrix A. blockDim block dimension of sparse matrix A, larger than zero. descrC the descriptor of matrix $C$.
Output
 csrValC array of nnz$\left(=$csrRowPtrA(m)$-$csrRowPtrA(0)$\right)$ non-zero elements of matrix $C$. csrRowPtrC integer array of m+1 elements that contains the start of every row and the end of the last row plus one. csrColIndC integer array of nnz column indices of the non-zero elements of matrix $C$.
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,nb<0, IndexBase of descrA, descrC is not base-0 or base-1, dirA is not row-major or column-major, or blockDim<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_MATRIX_TYPE_NOT_SUPPORTED the matrix type is not supported. CUSPARSE_STATUS_INTERNAL_ERROR an internal operation failed.

### 11.2. cusparse<t>coo2csr

```cusparseStatus_t
cusparseXcoo2csr(cusparseHandle_t handle, const int *cooRowInd,
int nnz, int m, int *csrRowPtr, cusparseIndexBase_t idxBase)```

This function converts the array containing the uncompressed row indices (corresponding to COO format) into an array of compressed row pointers (corresponding to CSR format).

It can also be used to convert the array containing the uncompressed column indices (corresponding to COO format) into an array of column pointers (corresponding to CSC format).

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