Cuda Thrust Custom function

后端 未结 1 1656
傲寒
傲寒 2021-01-16 18:17

How can I impliment this function in Thrust?

for (i=0;i
相关标签:
1条回答
  • 2021-01-16 19:17

    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));
    
    0 讨论(0)
提交回复
热议问题