【问题标题】:storing return value of thrust reduce_by_key on device vectors在设备向量上存储推力 reduce_by_key 的返回值
【发布时间】:2014-02-20 12:06:17
【问题描述】:

我一直在尝试在设备向量上使用推力函数 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,抱歉。

【问题讨论】:

    标签: cuda return-value device reduce thrust


    【解决方案1】:

    你的reduce_by_key操作设置有问题:

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

    请注意,documentation 中声明:

    OutputIterator2 是 OutputIterator 的模型,并且 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
    $
    

    【讨论】:

    • 我错误地放置了 hashElem,它应该是 int。我已经在代码中进行了编辑。是的,它现在工作正常。非常感谢您的帮助。
    猜你喜欢
    • 2011-12-02
    • 2014-03-12
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    • 2015-11-09
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    相关资源
    最近更新 更多