# How to accumulated the COO format matrix element?

Hi, I am trying to use CUSPARSE to do some matrix multiplication. Before that, I need to transfer the COO format sparse matrix to CSR format. I found my COO matrix has some replicated element with same row and column index. Is there any function to accumulate them by index in CUSPARSE or CUDA? In CUSPARSE, I only found the coosort function, which only sorted not accumulated. Could you give me some suggestions?
e.g.

``````A = (1,1,0.1;
1,2,0.2; % replicated
3,2,0.5;
2,2,1.0;
3,3,3.1;
1,2,0.7;} % replicated
``````

In this example, the [1,2,0.2] and[1,2,0.7] need to be accumulated.

Hi, cuSPARSE does not provide such functionality. You can obtain your final matrix by following these steps:

1. Merge row and column indices in an array of structures (instead of a structure of arrays). We can call it COO2
2. Use the CUB library for sorting COO2 elements (row/col) by key
3. Use the CUB library for performing a segmented reduction by key (row/col)
4. Use the CUB library for removing duplicate COO2 elements

I would like to ensure your may point clearly.
“Merge row and column indices in an array of structures (instead of a structure of arrays).”
Does this first step means converting the two indices, row and col, into one index, like the linear index of the matrix element as below?

``````index = row*N(col)+col;
``````

The other three step could be achieved by `thrust` library, I think.

you can just use something like
`coo_indices[i] = int2{coo_row[i], coo_column[i]};`