2012-06-13 37 views
1

我使用推力::序列 - 如何提高每个N元素后一步

thrust::sequence(myvector.begin(), myvector.end(), 0, 1) 

并取得良好的有序列表,如:

0, 1, 2, 3, 4 

我的问题是如何实现这样的下面列举(最好的方法是什么?)

0, 0, 0, 1, 1, 1, 2, 2 ,2, 3, 3, 3 

我知道如何与函子做,所以请不要试图用函子来回答。我想了解,如果有一个优化的方式为它的推力,还是我失去了一个简单的方法..

回答

4

事情是这样的:

thrust::device_vector<int> myvector(N); 

thrust::transform(thrust::make_counting_iterator(0), 
        thrust::make_counting_iterator(N), 
        thrust::make_constant_iterator(3), 
        myvector.begin(), 
        thrust::divides<int>()); 

(免责声明写在浏览器中,从来没有编译或测试,请自担风险)

应通过计算[0..N]//3并在myvector上输出结果来给出您要查找的序列。


看到,因为你有麻烦编译您的版本,这里是编译和完整的示例运行:

#include <thrust/device_vector.h> 
#include <thrust/transform.h> 
#include <thrust/functional.h> 
#include <thrust/iterator/counting_iterator.h> 
#include <thrust/iterator/constant_iterator.h> 
#include <cstdio> 

int main(void) 
{ 
    const int N = 18, M = 3; 
    thrust::device_vector<int> myvector(N); 

    thrust::transform( thrust::make_counting_iterator(0), 
         thrust::make_counting_iterator(N), 
         thrust::make_constant_iterator(M), 
         myvector.begin(), 
         thrust::divides<int>()); 

    for(int i=0; i<N; i++) { 
     int val = myvector[i]; 
     printf("%d %d\n", i, val); 
    } 
    return 0; 
} 
+0

尼斯的答案,但我不能编译:xyz.cu(544) :error:没有重载函数的实例“thrust :: transform”匹配参数列表参数类型是:(thrust :: counting_iterator ,thrust :: counting_iterator ,thrust :: constant_iterator ,thrust :: detail :: normal_iterat或>,thrust :: divides ) – phoad

+0

您是否阅读过错误信息?你已经为常量和计数迭代器参数混合了'unsigned int'和'int'类型。我已经编辑了一个可编辑的例子,它可以正确运行,供你学习。 – talonmies

+1

是的,我很惊讶,因为得到这样的错误,并错过了unsigned int vs int不匹配。很好的结果,答案和错误。谢谢。 – phoad

相关问题