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!
We can create complex number class in C++, that can hold the real and imaginary part of the complex number as member elements. There will be some member functions that are used to handle this class. In this example we are creating one complex type class, a function to display the complex number into correct format.
Java does not have a standard complex number class, but there exist a number of incompatible free implementations of a complex number class: The Apache Commons Math library provides complex numbers for Java with its Complex class.
Since C++ is newer that C, it has included the complex datatype since it was first standardized, and doesn't need to resort to the underscored version.
Complex numbers are especially useful in electronics, optics, and quantum theory for describing waves and any periodic phenomena. Fourier transforms use complex numbers and are the key to working with wavefunctions, designing filters, signal integrity in digital electronics, radio astronomy, and on and on...
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With