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;
}