Operating on thrust::complex types with thrust::transform

前端 未结 1 492
深忆病人
深忆病人 2021-01-25 05:05

I\'m trying to use thrust::transform to operate on vectors of type thrust:complex without success. The following example blows up during c

相关标签:
1条回答
  • 2021-01-25 05:39

    The error message is quite descriptive:

    thrust::abs<thrust::complex<...>> is a function which expects exactly one parameter, see thrust/detail/complex/arithmetic.h#L143:

    template <typename ValueType>
      __host__ __device__
      inline ValueType abs(const complex<ValueType>& z){
      return hypot(z.real(),z.imag());
    }
    

    For your use case, you need to wrap that function by a functor:

    struct complex_abs_functor
    {
        template <typename ValueType>
        __host__ __device__
        ValueType operator()(const thrust::complex<ValueType>& z)
        {
            return thrust::abs(z);
        }
    };
    

    Finally, employ that functor here:

    thrust::transform(d_vec1.begin(),
                      d_vec1.end(),
                      d_vec2.begin(),
                      complex_abs_functor());
    
    0 讨论(0)
提交回复
热议问题