首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >cusp::io::write_matrix_market_file()给出了访问冲突异常

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

Stack Overflow用户
提问于 2013-02-22 05:25:35
回答 2查看 341关注 0票数 1

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

无论如何,我现在面临的问题是,在我读取array2d中的数据并尝试使用cusp::write_matrix_market_file()将其写入.mtx文件后,我收到了“未处理的异常:写入位置的访问冲突”错误消息。在尝试将它们写入文件之前,我尝试打印来自array2d的一些值,只是为了确保所有内容都已就绪,并且这些值似乎存储在array2d中。我还尝试将类型更改为float,但错误仍然存在。下面是我完成这项任务的代码:

代码语言:javascript
复制
     #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 >,float>(std::basic_ofstream >& output={...},const float & value=)第116行+ 0x5字节C++

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

代码语言:javascript
复制
      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文件时,这对不同格式的构造函数有何影响

EN

回答 2

Stack Overflow用户

发布于 2013-02-22 06:16:58

这段代码对我很有效,演示了如何直接从array2d转到coo或ell:

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

代码语言:javascript
复制
 #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
EN

Stack Overflow用户

发布于 2013-02-22 12:38:02

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

代码语言:javascript
复制
       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";
          }
        }
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15012529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档