推力::序列 - 如何在每个 N 个元素之后增加步长 [英] thrust::sequence - how to increase the step after each N elements
问题描述
我正在使用
thrust::sequence(myvector.begin(), myvector.end(), 0, 1)
并获得良好的有序列表,例如:
and achieve good ordered list like:
0, 1, 2, 3, 4
我的问题是如何实现下面的这样一个列表(最好的方法?)
My question is how can I achieve such a list below (the best way?)
0, 0, 0, 1, 1, 1, 2, 2 ,2, 3, 3, 3
我知道怎么用函子来做,所以请不要试图用函子来回答.我想了解在 Thrust 中是否有针对它的优化方法,或者我是否缺少一种简单的方法..
I know how to make it with functors, so please do not try to answer it with functor. I want to learn if there is an optimized way for it in Thrust, or am I missing a simple way..
推荐答案
类似这样的:
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>() );
(免责声明,用浏览器编写,从未编译或测试过,使用风险自负)
(disclaimer, written in browser, never compiled or tested, use at own risk)
应该通过计算 [0..N]//3
并在 myvector
上输出结果来为您提供您正在寻找的序列.
should give you the sequence you are looking for by computing [0..N]//3
and outputting the result on myvector
.
鉴于您在编译版本时遇到问题,这里有一个完整的编译和运行示例:
Seeing as you are having trouble compiling your version, here is a complete example which compiles and runs:
#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
", i, val);
}
return 0;
}
这篇关于推力::序列 - 如何在每个 N 个元素之后增加步长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!