Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CUDA - How to work with complex numbers?

Tags:

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?

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!

like image 317
user2550888 Avatar asked Jul 04 '13 15:07

user2550888


People also ask

Can C++ handle complex numbers?

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.

Does Java support complex numbers?

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.

Does C++ have complex data type?

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.

What is the use of complex numbers in programming?

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...


1 Answers

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();
like image 50
Robert Crovella Avatar answered Oct 04 '22 03:10

Robert Crovella