对推力::具有推力::变换的复杂类型进行操作 [英] Operating on thrust::complex types with thrust::transform
问题描述
我正在尝试使用 thrust :: transform
对类型为 thrust:complex< float>
没有成功。以下示例在编译过程中因几页错误而崩溃。
I'm trying to use thrust::transform
to operate on vectors of type thrust:complex<float>
without success. The following example blows up during compilation with several pages of errors.
#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
。
I'm using CUDA 8.0 on Ubuntu Xenial and compiling with clang 3.8.0-2ubuntu4 using 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> >() );
和
/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& amp;模板。
No problem working on real floats, but no such with complex ones. I'm thinking there's a type error that I'm missing, but I'm very much still on the steep part of the learning curve with Thrust & templates.
推荐答案
错误消息具有很强的描述性:
The error message is quite descriptive:
thrust :: abs< thrust :: complex< ...>>
是函数,它期望恰好是一个参数,请参见 thrust / detail / complex / arithmetic。 h#L143 :
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());
这篇关于对推力::具有推力::变换的复杂类型进行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!