在设备矢量上存储推力reduce_by_key的返回值

时间:2014-02-20 12:06:17

标签: cuda return-value device reduce thrust

我一直在尝试在设备向量上使用推力函数reduce_by_key。在文档中,他们在host_vectors上给出了示例,而不是任何设备向量。我得到的主要问题是存储函数的返回值。更具体的是我的代码:

 thrust::device_vector<int> hashValueVector(10)
 thrust::device_vector<int> hashKeysVector(10)

 thrust::device_vector<int> d_pathId(10);
 thrust::device_vector<int> d_freqError(10); //EDITED 

 thrust::pair<thrust::detail::normal_iterator<thrust::device_ptr<int> >,thrust::detail::normal_iterator<thrust::device_ptr<int> > >  new_end; //THE PROBLEM
 new_end = thrust::reduce_by_key(hashKeysVector.begin(),hashKeysVector.end(),hashValueVector.begin(),d_pathId.begin(),d_freqError.begin());

我尝试在定义中首先将它们声明为device_ptr,因为对于host_vectors,它们也在文档中的定义中使用了指针。但是当我尝试那时我得到编译错误然后我读了错误语句并将声明转换为上面,这是编译正常但我不确定这是否是正确的定义方式。

我还有其他标准/干净方式来声明(“new_end”变量)吗?如果我的问题在某处不明确,请发表评论。

编辑:我编辑了d_freqError的声明。它应该是int我错误地将它写成hashElem,对不起。

1 个答案:

答案 0 :(得分:0)

reduce_by_key操作的设置存在问题:

new_end = thrust::reduce_by_key(hashKeysVector.begin(),hashKeysVector.end(),hashValueVector.begin(),d_pathId.begin(),d_freqError.begin());

请注意,在documentation中声明:

  

OutputIterator2是输出迭代器的模型,而InputIterator2的value_type可以转换为OutputIterator2的value_type。

InputIterator2(即hashValueVector.begin())的值类型为int。您OutputIterator2的值类型为struct hashElem。 Thrust不知道如何将int转换为struct hashElem

关于您的问题,从reduce_by_key捕获返回实体应该不难。根据文档,它是两个迭代器的推力对,并且这些迭代器应该分别与键迭代器类型和值迭代器类型一致(即,具有相同的向量类型和值类型)。

根据您发布的内容,这是一个更新的示例,它可以干净地编译:

$ cat t353.cu
#include <iostream>
#include <thrust/device_vector.h>
#include <thrust/pair.h>
#include <thrust/reduce.h>
#include <thrust/sequence.h>
#include <thrust/fill.h>
#include <thrust/copy.h>

typedef thrust::device_vector<int>::iterator  dIter;

int main(){

  thrust::device_vector<int> hashValueVector(10);
  thrust::device_vector<int> hashKeysVector(10);

  thrust::device_vector<int> d_pathId(10);
  thrust::device_vector<int> d_freqError(10);

  thrust::sequence(hashValueVector.begin(), hashValueVector.end());
  thrust::fill(hashKeysVector.begin(), hashKeysVector.begin()+5, 1);
  thrust::fill(hashKeysVector.begin()+6, hashKeysVector.begin()+10, 2);

  thrust::pair<dIter, dIter>  new_end;
  new_end = thrust::reduce_by_key(hashKeysVector.begin(),hashKeysVector.end(),hashValueVector.begin(),d_pathId.begin(),d_freqError.begin());
  std::cout << "Number of results are: " << new_end.first - d_pathId.begin() << std::endl;
  thrust::copy(d_pathId.begin(), new_end.first, std::ostream_iterator<int>(std::cout, "\n"));
  thrust::copy(d_freqError.begin(), new_end.second, std::ostream_iterator<int>(std::cout, "\n"));
}

$ nvcc -arch=sm_20 -o t353 t353.cu
$ ./t353
Number of results are: 3
1
0
2
10
5
30
$
相关问题