I know this is an old thread, but, assuming the COO data is (i,j) ordered/sorted, as you show, a sequential algorithm to convert from COO to CSR is:
int main()
{
// Example from Wikipedia (https://en.wikipedia.org/wiki/Sparse_matrix)
// Matrix:
// 10 20 0 0 0 0
// 0 30 0 40 0 0
// 0 0 50 60 70 0
// 0 0 0 0 0 80
// Expected output:
// csr_val: 10 20 30 40 50 60 70 80
// csr_col: 0 1 1 3 2 3 4 5
// csr_row: 0 2 4 7 8
const int nnz = 8; // number of non-zero elements
const int rows = 4; // number of matrix rows
const int cols = 6; // number of matrix columns
// coo data:
double coo_val[nnz] = { 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0 };
int coo_row[nnz] = { 0, 0, 1, 1, 2, 2, 2, 3 };
int coo_col[nnz] = { 0, 1, 1, 3, 2, 3, 4, 5 };
// coo to csr:
double csr_val[nnz] = { 0 };
int csr_col[nnz] = { 0 };
int csr_row[rows + 1] = { 0 };
for (int i = 0; i < nnz; i++)
{
csr_val[i] = coo_val[i];
csr_col[i] = coo_col[i];
csr_row[coo_row[i] + 1]++;
}
for (int i = 0; i < rows; i++)
{
csr_row[i + 1] += csr_row[i];
}
}
Notice that, assuming the COO data is (i,j) ordered/sorted, csr_col = coo_col
and csr_val = coo_val
, so you just need to obtain csr_row
, and that is as simples as:
int csr_row[rows + 1] = { 0 };
for (int i = 0; i < nnz; i++)
csr_row[coo_row[i] + 1]++;
for (int i = 0; i < rows; i++)
csr_row[i + 1] += csr_row[i];
So, in conclusion, you can easily achieve what you want with the 5 lines of code presented above.
Final note:
The proposed method has no considerations for duplicated COO entries.