// double every k-th element in given array; k obtained from command
// line

#include <stdio.h>

#include <thrust/device_vector.h>
#include <thrust/sequence.h>
#include <thrust/remove.h>  // for copy_if()

// functor
struct ismultk
{
   const int increm;  // k in above comments
   const  thrust::device_vector<int>::iterator w;  // "pointer" to our array
   int *wd;
   // get "pointer," k 
   ismultk(thrust::device_vector<int>::iterator _w, int _increm):
      w(_w), increm(_increm) {
        wd = thrust::raw_pointer_cast(&w[0]);
      }
   __device__
   bool operator()(const int i)  // bool is phony, but void doesn't work
   // {  if (i != 0 && (i % increm) == 0) wd[i] = 2 * wd[i];
   {  if (i != 0 && (i % increm) == 0) w[i] = 2 * w[i];
   }
};

// test
int main(int argc, char **argv)
{  // test case:
   int x[15] = {6,12,5,13,3,5,4,5,8,88,1,11,9,22,168};
   int n=15;
   thrust::device_vector<int> dx(x,x+n);
   thrust::device_vector<int> seq(n);
   thrust::sequence(seq.begin(),seq.end(),0);
   thrust::device_vector<int> out(n);
   int incr = atoi(argv[1]);  // k
   // for each i in seq, call ismultk() on this i, and if get a true
   // result, put 0 in dx[i] 
   thrust::copy_if(dx.begin(),dx.end(),seq.begin(),out.begin(),
      ismultk(dx.begin(),incr));
   // did it work?
   thrust::copy(dx.begin(), dx.end(),
      std::ostream_iterator<int>(std::cout, " "));
   std::cout << "\n";
}
