使用 thrust::transform 对 thrust::complex 类型进行操作

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

我正在尝试使用 thrust::transformthrust:complex<float> 类型的向量进行操作,但没有成功。下面的例子在编译过程中爆炸了,有几页错误。

#include <cuda.h>
#include <cuda_runtime.h>
#include <cufft.h>

#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/transform.h>
#include <thrust/complex.h>

int main(int argc, char *argv[]) {

  thrust::device_vector< thrust::complex<float> > d_vec1(4);
  thrust::device_vector<float> d_vec2(4);

  thrust::fill(d_vec1.begin(), d_vec1.end(), thrust::complex<float>(1,1));

  thrust::transform(d_vec1.begin(), d_vec1.end(), d_vec2.begin(), thrust::abs< thrust::complex<float> >() );
}

我在 Ubuntu Xenial 上使用 CUDA 8.0,并使用 nvcc --std=c++11 main.cpp -o main.

使用 clang 3.8.0-2ubuntu4 进行编译

主要错误似乎是:

main.cpp: In function ‘int main(int, char**)’:
main.cpp:17:105: error: no matching function for call to ‘abs()’
 gin(), d_vec1.end(), d_vec2.begin(), thrust::abs< thrust::complex<float> >() );

/usr/local/cuda-8.0/bin/../targets/x86_64-linux/include/thrust/detail/complex/arithmetic.h:143:20: note:   template argument deduction/substitution failed:
main.cpp:17:105: note:   candidate expects 1 argument, 0 provided
 gin(), d_vec1.end(), d_vec2.begin(), thrust::abs< thrust::complex<float> >() );
                                                                            ^

处理真正的浮点数没有问题,但处理复杂的浮点数就没有问题了。我在想我遗漏了一个类型错误,但我仍然处于 Thrust 和模板学习曲线的陡峭部分。

错误消息描述性很强:

thrust::abs<thrust::complex<...>> 是一个 函数,需要 一个 参数,参见 thrust/detail/complex/arithmetic.h#L143:

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

对于您的用例,您需要用 functor:

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

最后,在这里使用那个函子:

thrust::transform(d_vec1.begin(),
                  d_vec1.end(),
                  d_vec2.begin(),
                  complex_abs_functor());