Understanding Sparse Matrix Formats: COO, CSR, and CSC
1. Introduction: The Problem of “Zero”
In many fields of computational science—Deep Learning, Physics Simulations, and Graph Theory—matrices are often massive but mostly empty. We call a matrix dense if most elements are non-zero, and sparse if most elements are zero.
Consider a matrix representing a social network of 1 million users. A dense representation would store entries (mostly zeros, since no one knows everyone). Storing these zeros is a waste of memory and computation.
Sparse formats solve this by storing only the non-zero values ().
The Example Matrix
Throughout this report, we will use the following matrix to demonstrate each format. Note that it has 5 non-zero values ().
2. Coordinate List (COO)
The Intuitive Approach
COO is the simplest format. It represents the matrix as a list of tuples . It is often called the “triplet” format.
Data Structure
It requires three arrays of length :
row: The row index of the element.col: The column index of the element.data: The actual value of the element.
Representation of
| Index | row | col | data | Interpretation |
|---|---|---|---|---|
| 0 | 0 | 0 | 10 | |
| 1 | 1 | 1 | 20 | |
| 2 | 1 | 2 | 30 | |
| 3 | 2 | 0 | 40 | |
| 4 | 2 | 2 | 50 |
Pros and Cons
- Pros: Flexible. The order does not matter (you can append new values to the end easily). Excellent for initially constructing a matrix.
- Cons: Inefficient for arithmetic. To perform , the computer must jump randomly around memory to find which row it is currently processing.
- Memory Cost: integers/floats.
3. Compressed Sparse Row (CSR)
The Standard for Arithmetic
CSR is the industry standard for deep learning (e.g., PyTorch, CUDA) because it optimizes for row-based operations, such as Matrix-Vector multiplication. It compresses the row array from COO.
Data Structure
data: The non-zero values (size ). Must be sorted by row.indices: The column indices for the values (size ).indptr(Index Pointer): An array of size . This is the “bookmark” array.
Understanding indptr (Row Offsets)
The indptr array defines the range of indices in the data array that belong to a specific row.
Representation of
data:indices: (The columns associated with the data above)indptr:
Decoding indptr:
- Row 0: Look at
indptr[0]toindptr[1]range .- Take index 0 from
data: 10.
- Take index 0 from
- Row 1: Look at
indptr[1]toindptr[2]range .- Take indices 1 and 2 from
data: 20, 30.
- Take indices 1 and 2 from
- Row 2: Look at
indptr[2]toindptr[3]range .- Take indices 3 and 4 from
data: 40, 50.
- Take indices 3 and 4 from
Pros and Cons
- Pros: Fast arithmetic. The CPU/GPU can stream through
datalinearly to calculate row dot-products. Memory efficient. - Cons: Rigid. Adding a new non-zero value requires shifting all subsequent data in the arrays.
4. Compressed Sparse Column (CSC)
The Column-Major Alternative
CSC is identical to CSR, but transposed. It compresses columns instead of rows. It is the default in systems like MATLAB.
Data Structure
data: The non-zero values (size ). Must be sorted by column.indices: The row indices for the values (size ).indptr: An array of size . Defines column boundaries.
Representation of
Note: We must re-order the data to traverse down columns (Column 0, then Column 1, etc).
data:- (Col 0 has 10, 40. Col 1 has 20. Col 2 has 30, 50)
indices:- (Row indices for the data above)
indptr:
Pros and Cons
- Pros: Extremely fast for column slicing (e.g., “Get all data for Feature X”) or matrix addition.
- Cons: Slow for row-based operations.
5. Comparative Analysis
Memory Efficiency (The Math)
Let be the number of rows/cols (assuming square) and be the number of non-zeros.
Let be the size of a value (e.g., 64-bit float) and be the size of an index (e.g., 32-bit int).
-
COO Memory:
- Explanation: We store (row, col) for every value.
-
CSR/CSC Memory:
- Explanation: We store only one index (col or row) per value, plus the small
indptrarray.
- Explanation: We store only one index (col or row) per value, plus the small
Conclusion: CSR/CSC is more memory efficient than COO as long as the matrix is sufficiently sparse. Specifically, CSR wins when:
Since most Deep Learning matrices have millions of non-zeros and only thousands of rows, CSR saves roughly 33% of the index memory overhead.
Summary Table
| Feature | COO | CSR | CSC |
|---|---|---|---|
| Compression | None | Row Compressed | Column Compressed |
| Data Order | Arbitrary | Row-Major | Col-Major |
| Insertion | (Fast) | (Slow) | (Slow) |
| Access | Slow | Fast (Rows) | Fast (Cols) |
| Best For | Construction | Training/Math | Slicing |