CUDA Thrust库中counting_iterators的用途和用法 [英] Purpose and usage of counting_iterators in CUDA Thrust library

查看:685
本文介绍了CUDA Thrust库中counting_iterators的用途和用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解CUDA的推力库中的 counting_iterator 。它的目的是什么,如何使用?计数迭代器只是一个迭代器,它返回下一个值,并返回一个值。每次迭代器递增时提前的序列。最简单的例子是这样的:

  #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 std :: cout< * x<< std :: endl;
}

return 0;
}

在编译时执行此操作:

  $ / usr / local / cuda / bin / nvcc counting.cc 
$ ./a.out
1
2
3
4
5
6
7
8
9
10

ie。计数迭代器被初始化为值1,并且每当迭代器递增时,我们得到计数序列中的下一个值,即。 1,2,3,4,5 ....



其中计数增量在推力中派上用场是任何时候你需要一个序列来填充一个向量或操纵变换迭代器或函子。计数迭代器不需要显式创建和填充所需的序列的向量。例如(从我的回答到此Stack Overflow问题):

  #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 int val = myvector [i];
printf(%d%d \\\
,i,val);
}
return 0;
}

这会产生一个填充序列0,0,0,1 ,1,1,2,2,2,3,3,3。


I have trouble understanding counting_iterator in thrust library for CUDA. What is its purpose and how is it used? Is it available in other programming languages such as C++ also?

解决方案

A counting iterator is just an iterator which returns the next value from a sequence which is advanced each time the iterator is incremented. The simplest possible example is something like this:

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

which does this when compiled and run:

$ /usr/local/cuda/bin/nvcc counting.cc 
$ ./a.out
1
2
3
4
5
6
7
8
9
10

ie. the counting iterator was initialised with a value of one, and each time the iterator is incremented, we get the next value in the counting sequence, ie. 1,2,3,4,5....

Where counting increments come in handy in thrust is any time you need a sequence to fill a vector or manipulate in a transform iterator or functor. The counting iterator removes the need to explicitly create and fill a vector with the sequence you require. For example (from my answer to this Stack Overflow question):

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

which produces a device vector filled with the sequence 0, 0, 0, 1, 1, 1, 2, 2 ,2, 3, 3, 3.

这篇关于CUDA Thrust库中counting_iterators的用途和用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆