thrust :: sequence - 如何在每个N元素之后增加步长

时间:2012-06-13 09:11:48

标签: cuda copy sequence thrust

我正在使用

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

我知道如何用仿函数制作它,所以请不要尝试用仿函数来回答它。我想知道Thrust中是否有优化的方法,或者我错过了一个简单的方法..

1 个答案:

答案 0 :(得分: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;
}