Compressed Sparse Row (CSR) : Kiarash Torkian
Compressed Sparse Row (CSR) : Kiarash Torkian
Abstract—Paper focuses on highlighting the work and research B. Compressed Sparse Row
that was put into comparing operations done on a regular spare
matrix vs. a compressed one. A library of operations were written
A compressed sparse row is one of the many ways of
using the Java language to compare the two and is publicly compressing a matrix by holding onto any value that is nonzero
available. A compressed matrix will benefit from not having to (val), the column index of values (col ind), and a row pointer
explicitly perform operations on zeros which won’t have any effect that represents the start of a new row (row ptr). There includes
on the final value while requiring a substantially less amount of many other famous methods such as compressed column
space to store. storage (CCS) that instead holds onto the row index (row ind)
and a column pointer (col ptr), and coordinate format (COO)
that instead of having a pointer vector, it will simply hold
I. I NTRODUCTION onto the column index (col ind) and the row index (col ind).
When it comes to matrices, having to write methods to
perform operations on them are not that difficult, since they
really just boil down to only two or more nested loops. Now
in terms of memory and performance, this won’t be a huge
problem as long as the matrices themselves are not very
large. But lets not forget that two nested loops is still an O2
algorithm and will quadratically take more memory and be
slower in performance as the matrices grow larger and larger.
Which happens to be very frequent in real life examples.
One thing that we can say for sure is having zeros in a matrix
can make calculating it easier. But not for a computer, if we
don’t make an new algorithm that works around this since it Figure 1: A compressed sparse row can represent a matrix by
will still go over the zero values anyway. So what if we found holding onto only the nonzero values and two more arrays to
a way to compress our matrices in a format that contained only understand their previous positions. This figure is a represen-
the nonzero values without changing the matrix itself? Turns tation of matrix (1)
out we can do this in multiple ways, but in all cases it won’t
work too well unless we have a spare matrix.
Consider nnz is the number of nonzero elements in a matrix
II. BACKGROUND and n is the length of the row ptr vector. The memory
required to store these elements is equivalent to
A. Spare Matrix
Spare matrix is a type of matrix that is mostly filled with 2nnz + n + 1
zero values and is therefore opposite of a dense matrix, which
Where as to store a matrix in all its formality, one would
contains mostly nonzero values. A spare matrix can be vastly
require m ∗ n of memory where n and m represent number
improved in terms of space needed and performance required
of rows and columns respectively. Even though memory is
since majority of its data can be removed and its operations
something we seem to worry about less and less as time goes
skipped. This may not be so true for a dense matrix however
on since it is not as limited as it used to be, it is however,
for having to take the extra time of converting a matrix into
not something we can ignore when working with large data
a compressed format when we would only save on merely a
such as matrices. We can still reach our limits very soon. The
couple of steps. Here we see a simple example of a spare
benefit however, that comes from compressing matrices is not
matrix.
just memory.
1 0 6 5 3 0 0 0
III. A LGORITHMS
2 1 0 0 0 3 0 3
0
0 9 3 0 0 0 0 Now that we have gotten rid of zero values, our operations
0 0 0 4 0 0 0 0 will no longer need to be considered about looping over
A=
0
(1) zero values that will have no affect in the end result of
0 0 0 5 0 0 0
the operation. In this section we will be going over some
0 0 0 0 0 1 0 0
0 0 0 0 4 7 5 0
of the main implemented functions done for this program.
0 0 0 0 0 0 0 6
MARCH 2016 2
A. Matrix-Vector Multiplication
Lets compare the below two algorithms, where
one operates on a regular matrix while the other,
on a compressed sparse row. Consider the following:
m = N umber of rows
n = N umber of columns
C. Symmetrical
A = AT
A−1 AT = I
R EFERENCES
[1] M. Mccourt, B. Smith, and H. Zhang, EFFICIENT SPARSE MATRIX-
MATRIX PRODUCTS USING COLORINGS. [Online]. Available at:
http://www.mcs.anl.gov/papers/p5007-0813 1.pdf
[2] S. Y. Cheung, Working with Sparse Matrices, Work-
ing with Sparse Matrices. [Online]. Available at:
http://www.mathcs.emory.edu/c̃heung/courses/561/syllabus/3-
c/sparse.html
[3] Compressed Sparse Row Format: CSR, Compressed
Sparse Row Format: CSR. [Online]. Available at:
http://www5.in.tum.de/lehre/vorlesungen/parnum/ws10/parnum 6.pdf
[4] J. Dongarra , Compressed Column Storage (CCS), Compressed
Column Storage (CCS), 20-Nov-1995. [Online]. Available at:
http://netlib.org/linalg/html templates/node92.html
[5] J. Fineman, M. Frigo, J. Gilbert, and C. Leiserson, Parallel Sparse Matrix-
Vector and MatrixTranspose-Vector Multiplication using Compressed
Sparse Blocks, Parallel Sparse Matrix-Vector and MatrixTranspose-
Vector Multiplication using Compressed Sparse Blocks. [Online]. Avail-
able at: http://gauss.cs.ucsb.edu/ aydin/talks/csb-spaa.pdf
[6] W. H. Press, Numerical recipes: the art of scientific
computing. Cambridge, UK: Cambridge University Press,
2007. P. Stathis, D. Cheresiz, S. Vassiliadis, and B.
Juurlink, Sparse Matrix Transpose Unit. [Online]. Available
at: https://www.aes.tu-berlin.de/fileadmin/fg196/publication/old-
juurlink/sparse matrix transpose unit.pdf
[7] W. Townsend, “Irregular Applications Sparse Matrix Vector Multiplica-
tion”, http://slideplayer.com/slide/9793914/, 2016
[8] V. der Vorst, Iterative Krylov Methods for Large Linear Systems.
Cambridge University Press