Question

I have been trying to copy the row indices, column indices and values of a COO matrix into separate thrust vectors, but I find that I am unable to do so.

Below is the code

    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 is a thrust vector pointer that has been allocated memory in some previous code and not relevant here.

The code compiles, but fails at run time with the error:

terminate called after throwing an instance of 'thrust::system::system_error' what(): invalid argument Aborted (core dumped)

Could someone tell me what am I doing wrong?

Thanks Akshay

Was it helpful?

Solution

There are several errors in your coding. As already pointed out, your method of accessing the row indices, column indices and values of the coo matrix won't work. Furthermore, you can't create thrust vectors like d_Rows, d_Vals of zero size, and then copy other vectors to them.

the following code works for me and illustrates one way of extracting the row indices, column indices, and values into separate thrust vectors:

 #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;
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top