Complex Numbers How best to represent?

Hi,
I’ve just started a project which i plan on porting to CUDA, most of the operations involve mixed math on complex and real numbers. I’ve never really used complex numbers before in any of my code so i’m not quite sure what’s the best way to start, especially since i will immediately be working with cuda and so don’t want to use a type which is not even cuda supported.

My options appear to be → the c++ complex class template (which is pretty crap), using the gsl complex numbers which is a bit annoying as it’s all macro tastic but probably pretty solid, using the C99 complex.h specificiation which seems to be very good but i’m not sure how this will mix with nvcc or just knocking together my own typedef, basic fns etc.

Anyone else done much with complex numbers? Any thoughts or advice?

Thanks

You will probably need to roll your complex struct to manipulate complex numbers from global and device functions. Other libraries are not going to work from the GPU, probably not even purely template libraries like STL.

nvcc does support operator overloading, so you can easily write stuff like this:

typedef struct __align__(8) { 

    float real; 

    float img; 

} complex;

__host__ __device__ complex operator+(const complex &a, const complex &b)

{

  complex result;

  result.real = a.real + b.real;

  result.img = a.img + b.img;

  return result;

}

there are some examples in CUDA SDK that operate with complex numbers, I believe, I recomend to look at those from the performance point of view

Great stuff! I didn’t really know how much c++ nvcc actually supports, if i can overload some operators then this shouldn’t be too hard at all (he says…).

Thanks for the help guys

What support is there for complex arithmetic like that in math.h and complex.h ? I see that convolutionFFT2D in the SDK uses the Complex data type (float2) and I see cublasCgemm() in the CUBLAS documentation, but I was curious about the status of the math library.

-jm

In cuComplex.h, any reason why cuCabsf() is only defined for the host and not the device like all the other functions?

-jm

Does anyone know if this is a bug in cuComplex.h ?

-jm

I just added the device to the declaration, works fine… Hopefully this will be fixed in the next release, I don’t see any reason why this shouldn’t just be a bug.

I’m also using my own Complex struct. But here comes the point. I seperated my cuda code from my C code in my .cu file will be my kernel etc. I pass my struct to the kernel as an argument but I also want to use the struct inside my C code. How can I manage to do that?

Without make 2 typedefs with exactly the same name but without the host device bla bla bla…

I hope someone can comment on this.

Thank,
Jordy

Can’t you just declare every function as host device , unless you need specific performance-optimized version for the device only?

I have declared them like that. But that is done inside my .cu file how can I use it in my C files then?

Just include the .cu or make an header that goes with the .cu? I think that is the only right way to do it the

I have declared them like that. But that is done inside my .cu file how can I use it in my C files then?

Just include the .cu or make an header that goes with the .cu? I think that is the only right way to do it the

I have declared them like that. But that is done inside my .cu file how can I use it in my C files then?

Just include the .cu or make an header that goes with the .cu? I think that is the only right way to do it the

I have declared them like that. But that is done inside my .cu file how can I use it in my C files then?

Just include the .cu or make an header that goes with the .cu? I think that is the only right way to do it the

I have declared them like that. But that is done inside my .cu file how can I use it in my C files then?

Just include the .cu or make an header that goes with the .cu? I think that is the only right way to do it the

Sheesh, you replied 4 times to this thread.

You can use a define before including it for CUDA that defines a macro like this

define CUDAFUNCTION host device

every of your math functions in the header gets defined in this way:

double CUDAFUNCTION sin(double x);

before using it in C/C++, define CUDAFUNCTION as nothing (just make sure it IS defined)

before using it from .cu, define CUDAFUNCTION as host device

Christian

hay, currently i am studying programming using CUDA. there are some functions I don’t understand. can you tell me about the cuCabsf() function.