C++11 使用推力::转换操作推力::复杂类型
我试图使用C++11 使用推力::转换操作推力::复杂类型,c++11,cuda,thrust,C++11,Cuda,Thrust,我试图使用推力::变换对推力:复杂类型的向量进行操作,但没有成功。下面的示例在编译过程中出现了几页错误 #include <cuda.h> #include <cuda_runtime.h> #include <cufft.h> #include <thrust/device_vector.h> #include <thrust/host_vector.h> #include <thrust/transform.h> #i
推力::变换
对推力:复杂类型的向量进行操作,但没有成功。下面的示例在编译过程中出现了几页错误
#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> >() );
}
及
/usr/local/cuda-8.0/bin/。/targets/x86_64-linux/include/推力/detail/complex/算术。h:143:20:注意:模板参数推断/替换失败:
main.cpp:17:105:注意:候选者需要1个参数,提供0个参数
gin(),d_vec1.end(),d_vec2.begin(),推力::abs<推力::复合>();
^
在真正的浮动上工作没有问题,但在复杂的浮动上没有问题。我想我遗漏了一个类型错误,但我仍然处于使用推力和模板的学习曲线的陡峭部分。错误信息非常具有描述性:
推力::abs
是一个功能,它只需要一个参数,请参见:
这就是诀窍,但是为什么
推力::否定()
会进入推力::转换()
调用,但是推力::abs()
需要在函子中包装?关于期望一个参数的抱怨并不是推力::变换()
对输入向量设置所提供的一元运算的并行执行,将结果写入输出向量?@andreasonkopolus negate已经是一个函子,因此不需要包装。您所做的是调用推力::abs()
,但在推力::变换
中没有参数,但它要求您提供一个函子(=函数对象,提供运算符()
)的类/结构的实例。查看了推力/functional.h和推力/complex.h中的定义,现在就有意义了。
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> >() );
^
template <typename ValueType>
__host__ __device__
inline ValueType abs(const complex<ValueType>& z){
return hypot(z.real(),z.imag());
}
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());