0

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 ?

talonmies
  • 70,661
  • 34
  • 192
  • 269
Prz9
  • 33
  • 7
  • 1
    Your matrix is transposed. The dense matrix is assumed to be stored in column-major form. If you are using the array that you posted (in this form), then this suggests that you are using the row-major form. – Marco13 Dec 27 '17 at 16:34
  • Do u mean that in cusparse , dense matrix is stored in column major format ? – Prz9 Dec 28 '17 at 02:22

1 Answers1

1

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.

Prz9
  • 33
  • 7
  • Um... it's hard to tell what exactly you are doing there. But in the question, the first **column** was `0 0 0 0` and the second one was `1 6 11 16`. So if the same matrix was supposed to be stored in a flat, column-major array, then the array should start with `[0,0,0,0,1,6,11,6...]` - However, the *general* issue was the row/column mixup, regardless of how you (or someone else) now actually resolves it... – Marco13 Dec 28 '17 at 13:59
  • Yes @Marco13 you are right. But if you see closely , the input we give is an one dimensional array ie., the matrix in the question is actually given as an array of `{0,1,0,3,0,0,6,0,8,0,0,11,0,13,0,0,16,0,18,0}` Therefore the array gets stored not in a row-major format but rather in a column-major format by which we get the matrix as I described in the answer. Is it clear now ? – Prz9 Dec 29 '17 at 04:23
  • Technically, it's clear. The point is that a flat array of 20 values can either be a 5x4 matrix or a 4x5 matrix, and the row/column issue may always be fiddly and cause some confusion (particularly when only the flat array is shown, and it's not clear whether it was *supposed* to be row/column major). This is even more difficult because the different CUSPARSE matrix formats are using different conventions (!), and sometimes even the functions are receiving a `cusparseDirection_t` (i.e. `CUSPARSE_DIRECTION_ROW` or `COLUMN`) to say which format is actually used. – Marco13 Dec 29 '17 at 12:18
  • @Marco13 , the rows are columns of the matrices are given as input parameters to the function. So , we , and the program are sure that the rows are gonna be "m" and columns are gonna be "n". The only issue is wrt the order. In my question , I assumed dense matrix to be of row major , whereas the reality is dense matrices are of column major. My question had pre-assumed that rows n columns are 4 & 5 respectively. Sorry that I didnt mention them explicitly. :) – Prz9 Jan 02 '18 at 04:24