Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose and usage of counting_iterators in CUDA Thrust library

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?

like image 593
Rahul Bansal Avatar asked Oct 31 '25 01:10

Rahul Bansal


1 Answers

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.

like image 151
talonmies Avatar answered Nov 02 '25 18:11

talonmies