0

How can I impliment this function in Thrust?

for (i=0;i<n;i++)
    if (i==pos)
        h1[i]=1/h1[i];
    else
        h1[i]=-h1[i]/value;

In CUDA I did it like:

__global__ void inverse_1(double* h1, double value, int pos, int N)
{
    int i = blockDim.x * blockIdx.x + threadIdx.x;
    if (i < N){
        if (i == pos)
            h1[i] = 1 / h1[i];
        else
            h1[i] = -h1[i] / value;
    }
}

Thanks!

1
  • You're going to need to provide more info than that! What are you trying to do? Commented Sep 20, 2011 at 11:07

1 Answer 1

4

You need to create a binary functor to apply the operation, then use a counting iterator as the second input. You can pass pos and value into the functor's constructor. It'd look something like:

struct inv1_functor
{
  const int pos;
  const double value;

  inv1_functor(double _value, int _pos) : value(_value), pos(_pos) {}

  __host__ __device__
  double operator()(const double &x, const int &i) const {
    if (i == pos)
      return 1.0/x;
    else
      return -x/value;
  }
};

//...

thrust::transform(d_vec.begin(), d_vec.end(), thrust::counting_iterator<int>(),  d_vec.begin(), inv1_functor(value, pos));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.