CUDA-如何处理复数? [英] CUDA - How to work with complex numbers?
问题描述
如果我想使用复数并对内核本身中的这些复数双数执行简单的数学运算(加法和乘法),我应该在程序中包括哪些CUDA标头?
What CUDA headers should I include in my programme if I want to work with complex numbers and do simple maths operations (addition and multiplication) to these complex double numbers within the kernel itself?
在C ++中,我可以将一个常数乘以复数double>,只要它们都是double即可.但是在CUDA中,当我尝试对复杂double>进行简单的数学运算时,只要它不与另一个复杂double>一起使用,我都会遇到很多错误.我想念什么?
In C++ I can multiply a constant number with a complex double> as long as they are both double. However in CUDA I get lots of errors when I try to do simple maths operations to complex double>s whenever it isn't with another complex double>. What am I missing?
谢谢!
推荐答案
要包含的标头是:
#include <cuComplex.h>
在标准Linux CUDA安装中,它位于:
On a standard linux CUDA install, it is located in:
/usr/local/cuda/include
您将需要检查该头文件,并使用其中定义的功能来操作设备上的复数.
You will need to inspect that header file and use the functions defined in it to manipulate complex numbers on the device.
要将(双)复数乘以实数,我将:
To multiply a (double) complex number by a real number, I would:
#include <cuComplex.h>
...
double cr = 1;
double ci = 2;
double r = 3;
cuDoubleComplex c = make_cuDoubleComplex(cr, ci);
cuDoubleComplex result = cuCmul(c, make_cuDoubleComplex(r, 0));
编辑:使用CUDA 7 RC中最新发布的Thrust v1.8,可以在推力代码或 CUDA设备代码中使用推力::: complex.这样就可以编写看起来更自然的操作,例如:
EDIT: With the recently released Thrust v1.8 in CUDA 7 RC, it is possible to use thrust::complex in either thrust code or CUDA device code. This makes it possible to write more natural-looking operations such as:
#include <thrust/complex.h>
...
thrust::complex<float> c = thrust::complex<float>(2.0f, 5.0f);
thrust::complex<float> c2 = c*c;
float r = c2.real();
这篇关于CUDA-如何处理复数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!