简体   繁体   中英

Not able to understand output of CSR Representation in CUSP

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM