Compressed Sparse Column (CSC)
Another common sparse format is the CSC (sometimes called 3-array CSC or CSC3) format that is represented by scalar sizes (nrows, ncols, nnz), as well as three data arrays: col_ptr, row_ind and values, and the index_base parameter.
CSC Matrix Format Elements |
Description |
|---|---|
nrows |
Number of rows in the sparse matrix. |
ncols |
Number of columns in the sparse matrix. |
nnz |
Number of stored elements (sometimes called number of non-zeros) in the sparse matrix. |
index |
Parameter that is used to specify whether the matrix has zero or one-based indexing. |
values |
An array that contains the nnz stored element values of the sparse matrix stored compressed column by compressed column. |
row_ind |
An integer array of nnz row indices for the stored elements stored in the values array, such that row_ind[i] is the row number (using zero- or one-based indexing) of the element of the sparse matrix stored in values[i]. |
col_ptr |
An integer array of size equal to ncols + 1. Element j of this integer array gives the position of the element in the values array that is first non-zero element in a column j of A. Note that this position is equal to col_ptr[j] - index. The last element of the col_ptr array (col_ptr[ncols]) stores the sum of nnz and index. That is, nnz = col_ptr[ncols] - index. |
The 3-array CSC format that oneMKL supports has sorted columns by definition. However, the row indices within each column may or may not be sorted (in ascending order) leading to a possibility of unsorted data. The CSC format (with no repeated row indices that oneMKL currently requires) has a unique sorted form. However, in order to provide complete functional support for CSC matrices in either situation, oneMKL assumes CSC matrix handles to be unsorted by default. If the CSC format arrays are known to be in sorted form where all row indices within each column are sorted in ascending order, then users are strongly encouraged to provide that information to oneMKL through the sparse::set_matrix_property() API to possibly allow better algorithm choices and optimizations to deliver better performance. This is particularly important for APIs like triangular solve or sparse matrix addition.
A summary of sorted states for CSC format is given in the table below.
CSC Sortedness State |
Description |
Appropriate oneMKL sparse::property |
|---|---|---|
Unsorted |
Row indices within each column may not be ordered |
None (default) |
Sorted |
Sorted by columns and sorted by rows within each col |
sparse::property::sorted |
Examples of CSC format
The following 3 examples show how the sparse CSC format can be used:
CSC Case 2: Sorted rectangular matrix with one-based indexing and an empty row
CSC Case 3: Unsorted rectangular matrix with zero-based indexing
CSC Case 1: Sorted square matrix with zero-based indexing
Assuming zero-based indexing and a real square matrix.
nrows |
3 |
||||
ncols |
3 |
||||
nnz |
5 |
||||
index |
0 |
||||
col_ptr |
0 |
2 |
3 |
5 |
|
row_ind |
0 |
2 |
1 |
0 |
1 |
values |
1.0 |
3.0 |
-1.0 |
2.0 |
4.0 |
CSC Case 2: Sorted rectangular matrix with one-based indexing and an empty row
Assuming one-based indexing and real rectangular matrix with an empty row.
nrows |
4 |
||||||
ncols |
5 |
||||||
nnz |
7 |
||||||
index |
1 |
||||||
col_ptr |
1 |
3 |
4 |
6 |
7 |
8 |
|
row_ind |
1 |
4 |
2 |
1 |
2 |
4 |
2 |
values |
1.0 |
2.0 |
-1.0 |
2.0 |
4.0 |
1.0 |
1.0 |
CSC Case 3: Unsorted rectangular matrix with zero-based indexing
Unsorted CSC example: Assuming zero-based indexing and a real rectangular matrix, we note that the CSC format does not require row indices to be sorted within a given column, but values and row_ind arrays must be consistent with each other. Having the sorted property is not necessary, but can lead to better performance in actual runs due to better algorithms and data locality being enabled. See sparse::set_matrix_property() and sparse::sort_matrix for more details on how one could set the sorted property when it is applicable or reorder the matrix to achieve it when desired.
nrows |
4 |
|||||||||
ncols |
5 |
|||||||||
nnz |
10 |
|||||||||
index |
0 |
|||||||||
col_ptr |
0 |
3 |
5 |
8 |
9 |
10 |
||||
row_ind |
0 |
2 |
3 |
1 |
2 |
0 |
1 |
2 |
2 |
1 |
values |
1.0 |
1.0 |
3.0 |
-1.0 |
2.0 |
2.0 |
4.0 |
3.0 |
4.0 |
1.0 |