cusp :: io :: write_matrix_market_file()给出了访问冲突异常

时间:2013-02-21 21:25:35

标签: matlab matrix cuda cusp-library

我正在尝试使用CUSP从Matlab读取272 x 544双矩阵到ELLPACK格式稀疏矩阵。到目前为止我唯一能找到的方法是将数据读入mxArray,将mxArray读入double *数组(使用mxGetPr()),然后将值复制到cusp :: array2d中。到那里之后,我需要将array2d写入.mtx文件,然后将文件读入coo_matrix,最后将其转换为ell_matrix。这对我来说听起来很愚蠢,但鉴于CUSP的文档很少,这是我能想到的最好的。如果有人建议采用更好的方法,我会非常感激。

无论如何,我现在面临的问题是,在我读取array2d中的数据并尝试使用cusp :: write_matrix_market_file()将其写入.mtx文件后,我得到一个“未处理的异常:写入位置的访问冲突“ 错误信息。我尝试将它们写入文件之前尝试从array2d打印一些值,只是为了确保一切都到位并且值似乎在array2d中存储得很好。我也尝试将类型更改为float,但错误仍然存​​在。 这是我执行此任务的代码:

     #include <thrust/version.h>
     #include <cusp/version.h>
     #include <cusp/ell_matrix.h>
     #include <cusp/print.h>
     #include <cusp/io/matrix_market.h>
     #include <cusp/array2d.h>
     #include <cusp/coo_matrix.h>

     int main(int argc, char** argv)
     {
      const mxArray* mx_g_tra;
      //Reading data from Matlab   
       mx_g_tra = openMatFile2("C:\\out.mat", ndir, "out.tra");
   double* g_tra = mxGetPr(mx_g_tra);
   sizeG_tra=(int)mxGetNumberOfElements(mx_g_tra);
   const mwSize* traDims= mxGetDimensions(mx_g_tra);

      //get matrix dimensions
       tra_W= traDims[0];
   tra_H= traDims[1];
   printf("\nAcquired %d TRA elements as %d x %d from Maltab.\n", sizeG_tra, tra_W, tra_H);
       cusp::array2d<float, cusp::host_memory> TRA(traDims[0], traDims[1]);
       if(g_tra != NULL)
       {
       for(int i=0; i< traDims[0]; i++)
        {
         for(int j=0; j<traDims[1]; j++) 
          {TRA(i,j) = (float)g_tra[i*traDims[1]+j];
           if(TRA(i,j) != 0) printf("\nTRA(%d, %d) = %g", i, j, TRA(i,j));}
        }
       }

 cusp::io::write_matrix_market_file(TRA, "C:\\TRA.mtx"); 

当我得到异常时,调用堆栈指向:     cusp :: io :: detail :: write_value&gt;,float&gt;(std :: basic_ofstream&gt;&amp; output = {...},const float&amp; value =)第116行+ 0x5字节C ++

编辑: 快速更新:我通过修改文件“matrix_market.inl”摆脱了异常他们有一个奇怪的嵌套循环,通过循环遍历矩阵cols然后再次cols将值写入文件(从不考虑num_rows)显然,因为我的矩阵不是正方形,代码试图访问未存在的位置..这是修改后的代码:

      for(size_t j = 0; j < mtx.num_cols; j++)
       {
    // Modified below: mtx.num_cols --> mtx.num_rows
         for(size_t i = 0; i < mtx.num_rows; i++)
         {
         write_value(output, mtx(i,j));
         output << "\n";
          }
         }

我不确定当从这个.mtx文件读取时,这会如何影响不同的格式构造函数

2 个答案:

答案 0 :(得分:1)

此代码对我有用,演示如何直接从array2d转到coo或ell:

编辑更新了代码示例以显示cusp::is_valid_matrix()

的使用情况
 #include <stdio.h>
 #include <cusp/verify.h>
 #include <cusp/ell_matrix.h>
 #include <cusp/io/matrix_market.h>
 #include <cusp/array2d.h>
 #include <cusp/coo_matrix.h>

 int main(int argc, char** argv)
 {
  // initial matrix
   cusp::array2d<float, cusp::host_memory> E(4, 3);
   E(0,0) =  1.000e+00; E(0,1) =  0.000e+00; E(0,2) =  0.000e+00;
   E(1,0) =  0.000e+00; E(1,1) =  1.050e+01; E(1,2) =  0.000e+00;
   E(2,0) =  0.000e+00; E(2,1) =  0.000e+00; E(2,2) =  2.500e-01;
   E(3,0) =  0.000e+00; E(3,1) =  2.505e+02; E(3,2) =  0.000e+00;

   cusp::coo_matrix<int, float, cusp::host_memory> coo(E);
   cusp::ell_matrix<int, float, cusp::host_memory> ell(E);
   if (!cusp::is_valid_matrix(coo)) {printf("Invalid COO\n"); return 1;}
   if (!cusp::is_valid_matrix(ell)) {printf("Invalid ELL\n"); return 1;}

   cusp::io::write_matrix_market_file(coo, "COO.mtx");
   cusp::io::write_matrix_market_file(ell, "ELL.mtx");
   return 0;
 }

答案 1 :(得分:0)

只要行数大于cols数,就没有异常,代码也没有问题。我遇到的问题是因为行数是cols数量的一半。因此,当文件“matrix_market.inl”中的write_value假定我的矩阵是一个方阵并且在两个循环中只考虑num_cols时,我得到了错误。事实上,抛出异常时i的值是273(我的272 x 544矩阵中不存在的第一行)。 我通过编辑“matrix_market.inl”代码中的write_matrix_market_stream来修复它,如下所示:

       template <typename Matrix, typename Stream>
       void write_matrix_market_stream(const Matrix& mtx, Stream& output, cusp::array2d_format)
       {
        typedef typename Matrix::value_type ValueType;

        bool is_complex = thrust::detail::is_same<ValueType, cusp::complex<typename                   norm_type<ValueType>::type> >::value;

        if (is_complex)
        output << "%%MatrixMarket matrix array complex general\n";
        else
        output << "%%MatrixMarket matrix array real general\n";

        output << "\t" << mtx.num_rows << "\t" << mtx.num_cols << "\n";

        for(size_t j = 0; j < mtx.num_cols; j++)
        {
         // EDIT needed here
     // Modified below: mtx.num_cols --> mtx.num_rows
        for(size_t i = 0; i < mtx.num_rows; i++)
          {
             write_value(output, mtx(i,j));
            output << "\n";
          }
        }
        }