Why not just multiply the variable iby Min your functor?
If it is known at compile time M, it could be:
struct functor
{
__host__ __device__ void operator() (const int my_i)
{
int i = my_i *M;
}
};
thrust::counting_iterator<int> it1(0);
thrust::counting_iterator<int> it2 = it1 + N;
thrust::for_each (it1 , it2 , functor());
If it Mis known only at run time, we can pass it as an initialization parameter to the functor:
struct functor
{
int my_M;
functor(int _M) : my_M(_M) ();
__host__ __device__ void operator() (const int my_i)
{
int i = my_i *my_M;
}
};
thrust::counting_iterator<int> it1(0);
thrust::counting_iterator<int> it2 = it1 + N;
thrust::for_each (it1 , it2 , functor(M));
, M:
struct functor
{
__host__ __device__ void operator() (const int i)
{
}
};
using namespace thrust::placeholders;
thrust::counting_iterator<int> it1(0);
thrust::counting_iterator<int> it2 = it1 + N;
thrust::for_each (make_transform_iterator(it1, _1 * M) , thrust::make_transform_iterator(it2, _1 * M) , functor());
, , .
, 3 :
$ cat t492.cu
#include <stdio.h>
#include <thrust/transform.h>
#include <thrust/for_each.h>
#include <thrust/execution_policy.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/iterator/transform_iterator.h>
#include <thrust/host_vector.h>
#include <thrust/functional.h>
#define N 5
#define M 4
using namespace thrust::placeholders;
struct my_functor_1
{
__host__ __device__ void operator() (const int i)
{
printf("functor 1 value: %d\n", i);
}
};
struct my_functor_2
{
__host__ __device__ void operator() (const int my_i)
{
int i = my_i*M;
printf("functor 2 value: %d\n", i);
}
};
struct my_functor_3
{
int my_M;
my_functor_3(int _M) : my_M(_M) {};
__host__ __device__ void operator() (const int my_i)
{
int i = my_i *my_M;
printf("functor 3 value: %d\n", i);
}
};
int main(){
thrust::counting_iterator<int> it1(0);
thrust::counting_iterator<int> it2 = it1 + N;
thrust::for_each(thrust::host, it1, it2, my_functor_1());
thrust::for_each(thrust::host, it1, it2, my_functor_2());
thrust::for_each(thrust::host, it1, it2, my_functor_3(M));
thrust::for_each(thrust::host, thrust::make_transform_iterator(it1, _1 * M), thrust::make_transform_iterator(it2, _1 * M), my_functor_1());
return 0;
}
$ nvcc -arch=sm_20 -o t492 t492.cu
$ ./t492
functor 1 value: 0
functor 1 value: 1
functor 1 value: 2
functor 1 value: 3
functor 1 value: 4
functor 2 value: 0
functor 2 value: 4
functor 2 value: 8
functor 2 value: 12
functor 2 value: 16
functor 3 value: 0
functor 3 value: 4
functor 3 value: 8
functor 3 value: 12
functor 3 value: 16
functor 1 value: 0
functor 1 value: 4
functor 1 value: 8
functor 1 value: 12
functor 1 value: 16
$