问题
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?
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?
Thank you!
回答1:
The header to include is:
#include <cuComplex.h>
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));
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();
来源:https://stackoverflow.com/questions/17473826/cuda-how-to-work-with-complex-numbers