CUDA推力库中计数迭代器的目的和使用

CUDA推力库中计数迭代器的目的和使用,cuda,iterator,thrust,Cuda,Iterator,Thrust,我无法理解CUDA的推力库中的计数迭代器。其目的是什么?如何使用?在其他编程语言中是否可用,例如C++?< P>计数迭代器只是一个迭代器,它从每次迭代迭代器时递增的序列返回下一个值。最简单的例子如下: #include <iostream> #include <thrust/iterator/counting_iterator.h> int main(void) { int n = 10; thrust::counting_iterator<in

我无法理解CUDA的推力库中的
计数迭代器
。其目的是什么?如何使用?在其他编程语言中是否可用,例如C++?

< P>计数迭代器只是一个迭代器,它从每次迭代迭代器时递增的序列返回下一个值。最简单的例子如下:

#include <iostream>
#include <thrust/iterator/counting_iterator.h>

int main(void)
{
    int n = 10;

    thrust::counting_iterator<int>x(1);

    for(int i=0; i<n; ++i, ++x) {
        std::cout << *x << std::endl;
    }

    return 0;
}
即,计数迭代器是用一个值初始化的,每次迭代器递增,我们都会得到计数序列中的下一个值,即1,2,3,4,5

在“推力”中,计数增量很方便,只要您需要一个序列来填充向量或在变换迭代器或函子中进行操作。计数迭代器不再需要显式地创建向量并用所需的序列填充向量。例如(从我的回答到):

#包括
#包括
#包括
#包括
#包括
#包括
内部主(空)
{
常数N=18,M=3;
推力::设备_向量myvector(N);
推力::变换(推力::生成计数迭代器(0),
推力::使_计数_迭代器(N),
推力:使常数迭代器(M),
myvector.begin(),
推力::分裂();

对于(int i=0;i作为进一步的示例,它是@talonmies答案的一个轻微修改,您可以通过以下代码模拟Matlab的
linspace
命令

#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>

void main()
{ 
    const int N = 20;

    float a     = 3.87f;
    float b     = 7.11f;

    float Dx    = (b-a)/(float)(N-1);

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

    thrust::transform(thrust::make_counting_iterator(a/Dx),
                  thrust::make_counting_iterator((b+1.f)/Dx),
                  thrust::make_constant_iterator(Dx),
                  myvector.begin(),
                  thrust::multiplies<float>());

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

    getchar();

}
例如,正如
linspace(3.87,7.11,20)

Boost有一个。
#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;
}
#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>

void main()
{ 
    const int N = 20;

    float a     = 3.87f;
    float b     = 7.11f;

    float Dx    = (b-a)/(float)(N-1);

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

    thrust::transform(thrust::make_counting_iterator(a/Dx),
                  thrust::make_counting_iterator((b+1.f)/Dx),
                  thrust::make_constant_iterator(Dx),
                  myvector.begin(),
                  thrust::multiplies<float>());

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

    getchar();

}
0 3.870000
1 4.040526
2 4.211052
3 4.381579
4 4.552105
5 4.722631
6 4.893158
7 5.063684
8 5.234210
9 5.404737
10 5.575263
11 5.745790
12 5.916316
13 6.086842
14 6.257369
15 6.427895
16 6.598421
17 6.768948
18 6.939474
19 7.110000