Not able to understand output of CSR Representation in CUSP

社会主义新天地 提交于 2019-12-12 00:08:56

问题


I am trying to use the CUSP library. I am reading .txt files which are basically sparse COO representation. I am using CUSP to convert into CSR format.

When I print the matrix with cusp::print() it prints the correct outcome for COO representation. However when I convert the matrix into CSR, I have written my own function for printing but the outcome is not what I want.

Here is the snippet

      main()
        {
        //.
        //bla bla
        //..
        //create a 2d coo matrix
    cusp::coo_matrix<int, int, cusp::host_memory> D(nRows_data, nCols_data, nnz_data);

            // Load data from file into sparse matrices  
            //fill 2D coo matrix
            fill2DCooMatrixFromFile( fNameData, D );


            std::cout<<"\n----------------------------\n";
            cusp::print( D );

            cusp::csr_matrix<int, int, cusp::host_memory> csrD = D;
            std::cout<<"\n----------------------------\n";
            printCSRMatrix( csrD );
        }



        //print csr matrix
        void printCSRMatrix( cusp::csr_matrix<int, int, cusp::host_memory> csr )
        {
            std::cout<<"csr matrix <"<<csr.num_rows<<", "<<csr.num_cols<<"> with  <csr.num_entries<<" enteries\n";

            std::cout<<"V  :: ";
            for( int i=0 ; i<csr.values.size() ; i++ )
                std::cout<<csr.values[i]<<"  ";
            std::cout<<"\n";


            std::cout<<"CI :: ";
            for( in

t i=0 ; i<csr.column_indices.size() ; i++ )
            std::cout<<csr.column_indices[i]<<"  ";
        std::cout<<"\n";


        std::cout<<"RO :: ";
        for( int i=0 ; i<csr.row_offsets.size() ; i++ )
            std::cout<<csr.row_offsets[i]<<"  ";
        std::cout<<"\n";

    }

Assume that fill2DCooMatrixFromFile fills in the following matrix

1 0 1 0 0
0 0 0 1 0
0 0 0 0 0
0 1 0 0 0
0 0 0 1 0

Following is the output I get with the code

sparse matrix <5, 5> with 5 entries
              0              0              1
              0              2              1
              1              3              1
              3              1              1
              4              3              1

----------------------------
csr matrix <5, 5> with 5 enteries
V  :: 1  1  1  1  1  
CI :: 0  2  3  1  3  
RO :: 0  2  3  3  4  5  

I am not able to understand the RowOffset that is the output.


回答1:


The RowOffset specifies cumulative how many entries there are. It will always start with 0 and end with the number of nonzero contained in the sparse matrix.

RO :: 0 2 3 3 4 5

Hence, you should read the line as: before the first row of your sparse matrix, there are zero entries RO[0]. In the first row there are two entries RO[1], these are indexed by CI[0]-CI[1] and filled with the values of V[0]-V[1]. In the second row of your matrix, there are one more entry hence RO[2] == 3, and it is located at column CI[2] with value V[2].

As you can see the RO does not change value between the third and fourth number that indicates an empty row in the matrix.

Hope that clarifies how the CSR matrix format works. Otherwise feel free to ask more.



来源:https://stackoverflow.com/questions/16315727/not-able-to-understand-output-of-csr-representation-in-cusp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!