Cuda Thrust Custom function

爷,独闯天下 提交于 2019-12-01 13:48:18

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));
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!