next up previous contents index
Next: Compressed Column Storage Up: Sparse Matrix Storage Formats Previous: Sparse Matrix Storage Formats   Contents   Index


Compressed Row Storage

The compressed row and column (in the next section) storage formats are the most general: they make absolutely no assumptions about the sparsity structure of the matrix, and they don't store any unnecessary elements. On the other hand, they are not very efficient, needing an indirect addressing step for every single scalar operation in a matrix-vector product or preconditioner solve.

The compressed row storage (CRS) format puts the subsequent nonzeros of the matrix rows in contiguous memory locations. Assuming we have a nonsymmetric sparse matrix $A$, we create three vectors: one for floating point numbers (val) and the other two for integers (col_ind, row_ptr). The val vector stores the values of the nonzero elements of the matrix $A$ as they are traversed in a row-wise fashion. The col_ind vector stores the column indexes of the elements in the val vector. That is, if ${\tt val(k)}=a_{i,j}$, then ${\tt col\_ind(k)}=j$. The row_ptr vector stores the locations in the val vector that start a row; that is, if ${\tt val(k)}=a_{i,j}$, then ${\tt row\_ptr(i)}\leq k<{\tt row\_ptr(i+1)}$. By convention, we define ${\tt row\_ptr(n+1)} = nnz+1$, where $nnz$ is the number of nonzeros in the matrix $A$. The storage savings for this approach is significant. Instead of storing $n^2$ elements, we need only $2nnz+n+1$ storage locations.

As an example, consider the nonsymmetric matrix $A$ defined by

\begin{displaymath}
A =
\left[\begin{array}{rrrrrr}
10 & 0 & 0 & 0 &-2 & 0 \\
...
... 9 & 9 & 13 \\
0 & 4 & 0 & 0 & 2 & -1
\end{array}\right] ~.
\end{displaymath} (269)

The CRS format for this matrix is then specified by the arrays {val, col_ind, row_ptr} given below:

val 10 -2 3 9 3 7 8 7 3 $\cdots$ 9 13 4 2 -1    
col_ind 1 5 1 2 6 2 3 4 1 $\cdots$ 5 6 2 5 6    


row_ptr 1 3 6 9 13 17 20

If the matrix $A$ is symmetric, we need only store the upper (or lower) triangular portion of the matrix. The tradeoff is a more complicated algorithm with a somewhat different pattern of data access.


next up previous contents index
Next: Compressed Column Storage Up: Sparse Matrix Storage Formats Previous: Sparse Matrix Storage Formats   Contents   Index
Susan Blackford 2000-11-20