推力::顺序 - 如何增加每个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
给出您要查找的序列, code> 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\n", i, val);
}
return 0;
}
这篇关于推力::顺序 - 如何增加每个N元素后的步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!