使用 Thrust::transform 对推力:​​:复杂类型进行操作

2023-11-29

我正在尝试使用thrust::transform对类型向量进行操作thrust: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 并使用 clang 3.8.0-2ubuntu4 进行编译nvcc --std=c++11 main.cpp -o main.

主要错误似乎是:

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> >() );

and

/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<...>> is a function期望正好一个参数,参见推力/细节/复杂/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());
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Thrust::transform 对推力:​​:复杂类型进行操作 的相关文章

随机推荐