Search code examples
matrixsparse-matrixcusolver

cusparseSdense2csr conversion


I am trying to convert a dense matrix to sparse using the cusparseSdense2csr API, the dense matrix is as follows :

[ 0 1  0 3  0
  0 6  0 8  0
  0 11 0 13 0
  0 16 0 18 0 ]

The expected resultant sparse matrix should be :

csrValA = {1,3,6,8,11,13,16,18}
csrRowPtrA = {0,2,4,6,8}
csrColIndA = {1,3,1,3,1,3,1,3}

But the output I get is

csrValA = {8,16,1,13,6,18,3,11}
csrRowPtrA = {0,2,4,6,8}
csrColIndA = {2,4,0,3,1,4,0,2}

Why is this happening ? whats the reason behind ?


Solution

  • Alright , It seems cusparse stores Dense matrices in Column Major Format. I found it in the documentation of the same

    http://docs.nvidia.com/cuda/cusparse/index.html#dense-format2

    So , inspite our input matrix being as mentioned in the question , cusparse stores this dense matrix in column major format in memory. So ideally , our input matrix in memory becomes something similar to this :

    [ 0  0  8  0  16
      1  0  0  13 0
      0  6  0  0  18
      3  0  11 0  0 ]
    

    This explains the output I got.