我一直在尝试将行索引,列索引和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());
}
这是一个推力向量指针,它已经在以前的代码中分配了内存,在这里没有关系。
代码编译,但在运行时因错误而失败:
在抛出'thrust :: system :: system_error'的实例后终止调用 what():无效的参数 中止(核心倾销)
有人能告诉我我做错了什么吗?
由于 阿克沙伊
答案 0 :(得分:2)
您的编码有几处错误。正如已经指出的那样,访问行矩阵的索引,列索引和值的方法将不起作用。此外,您无法创建零大小的d_Rows
,d_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;
}