1

我在用

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 中是否有优化的方法,或者我错过了一个简单的方法..

4

1 回答 1

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;
}
于 2012-06-13T14:53:30.290 回答