Saturday, June 8, 2013

Thrust: where the counting iterators reduced, at CPU or GPU

Thrust: where the counting iterators reduced, at CPU or GPU

The following code has no information that may lead it to run at CPU or GPU. I wonder where is the "reduce" operation executed?
#include <thrust/iterator/counting_iterator.h>
...
// create iterators
thrust::counting_iterator<int> first(10);
thrust::counting_iterator<int> last = first + 3;

first[0]   // returns 10
first[1]   // returns 11
first[100] // returns 110

// sum of [first, last)
thrust::reduce(first, last);   // returns 33 (i.e. 10 + 11 + 12)
Furthermore,
thrust::transform_reduce(
    thrust::counting_iterator<unsigned int>(0),
    thrust::counting_iterator<unsigned int>(N),
    MyOperation(data), 0 ,thrust::plus<unsigned int>())
Even though data is defined as thrust::host_vector, this function tries to be executed at GPU (compiler gives related errors, because the filename ends with .cpp). How may I make the code to run at CPU. Or should I look for another way to perform the same operation, e.g. not using counting_iterator?