0

我一直在尝试将 COO 矩阵的行索引、列索引和值复制到单独的推力向量中,但我发现我无法这样做。

下面是代码

    cusp::coo_matrix <unsigned int, float, cusp::device_memory> *sim_mat;
    sim_mat = new cusp::coo_matrix <unsigned int, float, cusp::device_memory>; 
    /* Code to fill up sim_mat: runs fine
    ...
    */
    {
         thrust::device_ptr <unsigned int> d_rows = &((sim_mat->row_indices));
         thrust::device_ptr <unsigned int> d_cols = &((sim_mat->column_indices)[0]);
         thrust::device_ptr <float>        d_vals = &((sim_mat->values)[0]);
         unsigned int size_nn = (sim_mat->row_indices).size();
         thrust::device_vector <unsigned int> d_Rows;
         thrust::device_vector <float>        d_Vals;
         thrust::device_vector <unsigned int> reduced_Rows;

         // Code fails below this point
         thrust::copy_n (d_rows, size_nn, d_Rows.begin());
         thrust::copy_n (d_vals, size_nn, d_Vals.begin());
         cout << size_nn << std::endl;

         if (!(sim_mat->is_sorted_by_row()))
             thrust::sort_by_key(d_Rows.begin(), d_Rows.end(), d_Vals.begin());
         thrust::reduce_by_key(d_Rows.begin(), d_Rows.end(), d_Vals.begin(), reduced_Rows.begin(), sim_row->begin());

    }

Ithe sim_row 是一个推力向量指针,已在之前的一些代码中分配了内存,此处不相关。

代码编译,但在运行时失败并出现错误:

在抛出“thrust::system::system_error”实例后调用终止 what(): invalid argument Aborted (core dumped)

有人可以告诉我我做错了什么吗?

谢谢阿克谢

4

1 回答 1

2

您的编码中有几个错误。正如已经指出的那样,您访问 coo 矩阵的行索引、列索引和值的方法将不起作用。此外,您不能创建大小为零的推力向量d_Rowsd_Vals然后将其他向量复制到它们。

以下代码对我有用,并说明了将行索引、列索引和值提取到单独的推力向量中的一种方法:

 #include <stdio.h>
 #include <cusp/verify.h>
 #include <cusp/array2d.h>
 #include <cusp/coo_matrix.h>
 #include <thrust/host_vector.h>
 #include <thrust/device_vector.h>

 int main()
 {
  typedef cusp::device_memory MemorySpace;
  // initial matrix

   cusp::array2d<float, MemorySpace> 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, MemorySpace> coo(E);

   if (!cusp::is_valid_matrix(coo)) {printf("Invalid COO\n"); return 1;}

   thrust::device_vector<int> row_ind(coo.row_indices.size());
   thrust::device_vector<int> col_ind(coo.column_indices.size());
   thrust::device_vector<float> values(coo.values.size());

   thrust::copy(coo.row_indices.begin(), coo.row_indices.end(), row_ind.begin());
   thrust::copy(coo.column_indices.begin(), coo.column_indices.end(), col_ind.begin());
   thrust::copy(coo.values.begin(), coo.values.end(), values.begin());

   thrust::host_vector<int> h_row_ind = row_ind;
   thrust::host_vector<int> h_col_ind = col_ind;
   thrust::host_vector<float> h_values = values;

   printf("COO row indices: \n");
   for (int i = 0; i < h_row_ind.size(); i++)
     printf("%d \n", h_row_ind[i]);


   printf("COO column indices: \n");
   for (int i = 0; i < h_col_ind.size(); i++)
     printf("%d \n", h_col_ind[i]);


   printf("COO values: \n");
   for (int i = 0; i < h_values.size(); i++)
     printf("%f \n", h_values[i]);

   return 0;
 }
于 2013-02-27T03:08:31.860 回答