NVIDIA / CUDALibrarySamples

CUDA Library Samples
Other
1.5k stars 311 forks source link

sparse matrix transpose question #149

Closed jinz2014 closed 11 months ago

jinz2014 commented 11 months ago

Is there a cuSparse function that transposes a sparse matrix in CSR to another one in CSR ? Thanks.

Reference https://www.intel.com/content/www/us/en/docs/onemkl/developer-reference-dpcpp/2023-1/oneapi-mkl-sparse-omatcopy.html

fbusato commented 11 months ago

Yes, cuSPARSE provides cusparseCsr2cscEx2() https://docs.nvidia.com/cuda/cusparse/index.html#cusparsecsr2cscex2 to transpose a CSR matrix to CSC (which is CSR with columns/rows swapped).

jinz2014 commented 11 months ago

I suppose that users need to convert the outputs in CSC to the outputs in CSR after calling cusparseCsr2cscEx2(). Thanks.

jinz2014 commented 11 months ago

I have a follow-up question. Will cuSparse consider adding a function whose output is also in CSR format ?

qanhpham commented 11 months ago

Hi @jinz2014. Sorry, I don't get your question. A matrix in CSR is equivalent to its transposed matrix stored in CSC. You can think of the output of cusparseCsr2cscEx2() as a CSR matrix; then, this function acts as a transpose operation.

jinz2014 commented 11 months ago

Suppose the input matrix is "m" rows and "n" cols. After the transpose operation, the output matrix is "n" rows and "m" cols. When the output is stored in CSC, the "csrColPtr" array contains "m+1" elements. However, the document shows correctly that it contains "n+1" elements. Do you think the document could say that the output is still a CSR matrix (like the description of the Intel sparse omatcopy). This may be clearer to users of the function. Thanks.

cscColPtr | Integer array of size n + 1 that containes the CSC column offsets -- | --
qanhpham commented 11 months ago

In the document, it is said that "The resulting matrix can also be seen as the transpose of the original sparse matrix." Yes, I agree we should make it clearer that it's a transpose if we see the output as a CSR matrix (i.e. cscColPtr is used as csrRowPtr).