This example illustrates the use of the rocSPARSE
level 2 sparse matrix-vector multiplication using BSR storage format.
The operation calculates the following product:
where
-
$\alpha$ and$\beta$ are scalars -
$\mathbf{x}$ and$\mathbf{y}$ are dense vectors -
$A'$ is a sparse matrix in BSR format withrocsparse_operation
and described below.
- Setup a sparse matrix in BSR format. Allocate an x and a y vector and set up
$\alpha$ and$\beta$ scalars. - Setup a handle, a matrix descriptor and a matrix info.
- Allocate device memory and copy input matrix and vectors from host to device.
- Compute a sparse matrix multiplication, using BSR storage format.
- Copy the result vector from device to host.
- Clear rocSPARSE allocations on device.
- Clear device arrays.
- Print result to the standard output.
The Block Compressed Sparse Row (BSR) storage format describes a sparse matrix using three arrays. The idea behind this storage format is to split the given sparse matrix into equal sized blocks of dimension bsr_dim
and store those using the CSR format. Because the CSR format only stores non-zero elements, the BSR format introduces the concept of non-zero block: a block that contains at least one non-zero element. Note that all elements of non-zero blocks are stored, even if some of them are equal to zero.
Therefore, defining
mb
: number of rows of blocksnb
: number of columns of blocksnnzb
: number of non-zero blocksbsr_dim
: dimension of each block
we can describe a sparse matrix using the following arrays:
-
bsr_val
: contains the elements of the non-zero blocks of the sparse matrix. The elements are stored block by block in column- or row-major order. That is, it is an array of sizennzb
$\cdot$ bsr_dim
$\cdot$ bsr_dim
. -
bsr_row_ptr
: given$i \in [0, mb]$ - if
$0 \leq i < mb$ ,bsr_row_ptr[i]
stores the index of the first non-zero block in row$i$ of the block matrix - if
$i = mb$ ,bsr_row_ptr[i]
storesnnzb
.
This way, row
$j \in [0, mb)$ contains the non-zero blocks of indices frombsr_row_ptr[j]
tobsr_row_ptr[j+1]-1
. The corresponding values inbsr_val
can be accessed frombsr_row_ptr[j] * bsr_dim * bsr_dim
to(bsr_row_ptr[j+1]-1) * bsr_dim * bsr_dim
. - if
-
bsr_col_ind
: given$i \in [0, nnzb-1]$ ,bsr_col_ind[i]
stores the column of the$i^{th}$ non-zero block in the block matrix.
Note that, for a given
For instance, consider a sparse matrix as
Taking
with the following non-zero blocks:
and the zero matrix:
Therefore, the BSR representation of
bsr_val = { 8, 0, 7, 2, 0, 3, 0, 5, 2, 0, 1, 0, 0, 0, 0, 0 // A_{00}
4, 7, 0, 0, 0, 7, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0 // A_{10}
0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // A_{12}
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 5, 0 // A_{20}
0, 9, 6, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0 } // A_{21}
bsr_row_ptr = { 0, 1, 3, 4 }
bsr_col_ind = { 0, 0, 2, 0, 1 }
-
rocsparse_[dscz]bsrmv_ex(...)
is the solver with four different function signatures depending on the type of the input matrix:-
d
double-precision real (double
) -
s
single-precision real (float
) -
c
single-precision complex (rocsparse_float_complex
) -
z
double-precision complex (rocsparse_double_complex
)
-
-
rocsparse_operation trans
: matrix operation type with the following options:-
rocsparse_operation_none
: identity operation:$A' = A$ -
rocsparse_operation_transpose
: transpose operation:$A' = A^\mathrm{T}$ -
rocsparse_operation_conjugate_transpose
: Hermitian operation:$A' = A^\mathrm{H}$
Currently, only
rocsparse_operation_none
is supported. -
-
rocsparse_mat_descr
: descriptor of the sparse BSR matrix. -
rocsparse_direction
block storage major direction with the following options:rocsparse_direction_column
rocsparse_direction_row
rocsparse_create_handle
rocsparse_create_mat_descr
rocsparse_create_mat_info
rocsparse_dbsrmv_ex
rocsparse_destroy_handle
rocsparse_destroy_mat_descr
rocsparse_destroy_mat_info
rocsparse_direction
rocsparse_direction_column
rocsparse_handle
rocsparse_int
rocsparse_mat_descr
rocsparse_mat_info
rocsparse_operation
rocsparse_operation_none
hipFree
hipMalloc
hipMemcpy
hipMemcpyDeviceToHost
hipMemcpyHostToDevice