1

CUSP を使用して、Matlab から 272 x 544 の double 行列を 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 >,float>(std::basic_ofstream > & output={...}, const float & value=) Line を指しています116 + 0x5 バイト C++

編集:クイックアップデート:ファイル「matrix_market.inl」を変更して例外を取り除きました。行列の列をループしてから再度列をループすることでファイルに値を書き込む奇妙なネストされたループがありました(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 ファイルから読み取るときに、これがさまざまな形式コンストラクターにどのように影響するかはわかりません

4

2 に答える 2

1

このコードは私にとってはうまくいき、array2d から coo または ell に直接移動する方法を示しました。

EDIT:の使用法を示すためにコードサンプルを更新しました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;
 }
于 2013-02-21T22:16:58.130 に答える
0

行数が列数よりも多い限り、例外はなく、コードに問題はありません。私が抱えていた問題は、行数が列数の半分であるためです。そのため、ファイル "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";
          }
        }
        }
于 2013-02-22T04:38:01.737 に答える