无法了解CUSP中的CSR表示形式的输出 [英] Not able to understand output of CSR Representation in CUSP

查看:106
本文介绍了无法了解CUSP中的CSR表示形式的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用CUSP库.我正在读取基本上是稀疏COO表示形式的.txt文件.我正在使用CUSP转换为CSR格式.

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.

当我用cusp::print()打印矩阵时,它将为COO表示打印正确的结果.但是,当我将矩阵转换为CSR时,我已经编写了自己的打印功能,但结果却不是我想要的.

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.

这是代码段

      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";

    }

假设fill2DCooMatrixFromFile填写以下矩阵

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  

我无法理解作为输出的RowOffset.

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

推荐答案

RowOffset累计指定有多少个条目.它将始终以0开头,并以稀疏矩阵中包含的非零数字结束.

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

RO :: 0 2 3 3 4 5

因此,您应该将行读为:稀疏矩阵的第一行之前,有零个条目RO [0].在第一行中,有两个条目RO [1],它们由CI [0] -CI [1]索引,并用V [0] -V [1]的值填充.在矩阵的第二行中,还有一个条目,因此RO [2] == 3,它位于列CI [2]中,值为V [2].

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].

如您所见,RO不会在表示矩阵中为空行的第三个数字与第四个数字之间改变值.

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

希望阐明CSR矩阵格式的工作方式.否则请问更多.

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

这篇关于无法了解CUSP中的CSR表示形式的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆