# Complex arithmetics in CUDA

**URL:** <https://forums.developer.nvidia.com/t/complex-arithmetics-in-cuda/29954>\
**Category:** CUDA Programming and Performance\
**Created:** [July 2, 2013, 9:38am UTC](https://forums.developer.nvidia.com/t/complex-arithmetics-in-cuda/29954 "2013-07-02T09:38:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![JFSebastian](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/jfsebastian/32/75602_2.png) [@JFSebastian](https://forums.developer.nvidia.com/u/JFSebastian)\
**Post date:** [July 2, 2013, 9:38am UTC](https://forums.developer.nvidia.com/t/complex-arithmetics-in-cuda/29954/1 "2013-07-02T09:38:18Z")

</div>

I frequently use complex arithmetics in CUDA and need to define my own implementations of, for example, transcendental functions (sin, cos, exp, …) on complex numbers. I want now to definitely solve the problem by writing a library of such functions so to consider a “large” number of cases and avoiding being pit-stopped any time for new implementations.

I’m aware that for some of them, code writing could be non-trivial from the point of view of trade-off between accuracy and computational complexity. Nevertheless, I’m also aware that C++ has its own complex math library.

Is there any possibility to “recycle” already existing C++ solutions for CUDA purposes?

---

<div class="post-metadata">

**Author:** ![vacaloca](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@vacaloca](https://forums.developer.nvidia.com/u/vacaloca)\
**Post date:** [July 2, 2013, 9:42am UTC](https://forums.developer.nvidia.com/t/complex-arithmetics-in-cuda/29954/2 "2013-07-02T09:42:44Z")

</div>

Cam you be more explicit as to why the native functions don’t work for you? Is this for float? double? even higher than double precision? Would something like this work?  
[url][http://mplapack.sourceforge.net/[/url]](http://mplapack.sourceforge.net/%5B/url%5D)

---

<div class="post-metadata">

**Author:** ![JFSebastian](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/jfsebastian/32/75602_2.png) [@JFSebastian](https://forums.developer.nvidia.com/u/JFSebastian)\
**Post date:** [July 2, 2013, 10:59am UTC](https://forums.developer.nvidia.com/t/complex-arithmetics-in-cuda/29954/3 "2013-07-02T10:59:51Z")

</div>

Mathematical **device** functions like sin, cos, sinh, atan, log, exp etc. are defined on double precision arguments, if I correctly understand the NVIDIA CUDA Library Documentation:

[url][http://developer.download.nvidia.com/compute/cuda/4\_2/rel/toolkit/docs/online/group\_\_CUDA\_\_MATH\_\_DOUBLE\_g3ebbca20a2937d1fe51329402880df85.html#g3ebbca20a2937d1fe51329402880df85[/url]](http://developer.download.nvidia.com/compute/cuda/4_2/rel/toolkit/docs/online/group __CUDA__ MATH__DOUBLE_g3ebbca20a2937d1fe51329402880df85.html#g3ebbca20a2937d1fe51329402880df85%5B/url%5D)

I’m searching for implementations thereof on complex numbers.

---

<div class="post-metadata">

**Author:** ![vacaloca](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@vacaloca](https://forums.developer.nvidia.com/u/vacaloca)\
**Post date:** [July 2, 2013, 11:54am UTC](https://forums.developer.nvidia.com/t/complex-arithmetics-in-cuda/29954/4 "2013-07-02T11:54:20Z")

</div>

Okay, I get it now. Now here’s my suggestion. Why not just keep parallel arrays of the imaginary and real parts? I suppose if this is production code that has some spec that _needs_ some sort of robust complex operations to make it ‘user/coder friendly’ as part of a deliverable then it makes sense. Just throwing some ideas out there.

See: [https://devtalk.nvidia.com/default/topic/414672/complex-library-in-cuda/](https://devtalk.nvidia.com/default/topic/414672/complex-library-in-cuda/) and [http://en.wikipedia.org/wiki/Euler%27s\_formula#Relationship\_to\_trigonometry](http://en.wikipedia.org/wiki/Euler%27s_formula#Relationship_to_trigonometry)

For example, here is a complex product function prototype keeping real and imaginary parts separate:

```auto
__device__ void ComplexProd(double *a_R, double *a_I, double *b_R, double *b_I,
                 double *c_R, double *c_I)
/* Calculates c=a*b */
{
    *c_R=a_R[0]*b_R[0]-a_I[0]*b_I[0];
    *c_I=a_R[0]*b_I[0]+a_I[0]*b_R[0];
}

```

---

<div class="post-metadata">

**Author:** ![JFSebastian](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/jfsebastian/32/75602_2.png) [@JFSebastian](https://forums.developer.nvidia.com/u/JFSebastian)\
**Post date:** [July 2, 2013, 1:13pm UTC](https://forums.developer.nvidia.com/t/complex-arithmetics-in-cuda/29954/5 "2013-07-02T13:13:16Z")

</div>

Thanks for your suggestions. I have already my own implementation of a wrapper complex type class as well as related overloaded operators. I try to better explain myself.

C/C++ has its own implementations of sin, cos, exp etc. on real numbers. They are optimized for accuracy/efficiency according to some numerical schemes (polynomial expansions, etc.).

C/C++ has also complex implementations of those functions. For many of them (e.g., trigonometric functions) the implementations seems rather simple: they are combination of mathematical functions on real argument. If, for example, the source files of those functions on complex arguments would be available, then one could port them to CUDA as easily as adding a **device** keywork in front. Of course, in this simple scenario invoking sin, cos, exp etc. would mean to invoke native CUDA mathematical functions.

---

<div class="post-metadata">

**Author:** ![vacaloca](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@vacaloca](https://forums.developer.nvidia.com/u/vacaloca)\
**Post date:** [July 2, 2013, 2:57pm UTC](https://forums.developer.nvidia.com/t/complex-arithmetics-in-cuda/29954/6 "2013-07-02T14:57:29Z")

</div>

Here is a bit more insight, now that you have clarified the question:  
[url][How does C compute sin() and other math functions? - Stack Overflow](http://stackoverflow.com/questions/2284860/how-does-c-compute-sin-and-other-math-functions%5B/url%5D)

From that link above I got found the fdlibm sources link:  
[url][http://gnuwin32.sourceforge.net/packages/fdlibm.htm[/url]](http://gnuwin32.sourceforge.net/packages/fdlibm.htm%5B/url%5D)

I should mention that if you choose to go that route (to build a library from scratch in CUDA), it is no small feat. There might be other libraries that are cleaner and not as bloated and better suited for porting to CUDA, that is just the first one I found that had source code.

My solution to this would be… since pretty much any of the trig function real/imaginary parts can be derived from complex exponentials (exp, expf) or other functions (sin,cos)… just use the provided CUDA functions, and use your complex wrapper function to calculate each real and imaginary argument separately. I don’t think you’d want to re-invent the wheel when CUDA already provides device functions that are already tested and work. Just adapt your wrapper class to calculate the arguments independently and return a CComplex struct/output or however you do it.

It’s very much possible that you could optimize single functions further from the method I have just described, building them up from a ‘from scratch’ implementation from some library sources… but I really think that would not be the most productive thing to do.

Hopefully the fdlibm library sources give you an idea of how to use the CUDA native sin,cos,exp, etc to build the functions in your template that would work on complex numbers… that would be the ideal way to do it, I think.

---

<div class="post-metadata">

**Author:** ![njuffa](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@njuffa](https://forums.developer.nvidia.com/u/njuffa)\
**Post date:** [July 2, 2013, 4:25pm UTC](https://forums.developer.nvidia.com/t/complex-arithmetics-in-cuda/29954/7 "2013-07-02T16:25:25Z")

</div>

I am not aware of a package of complex math functions for CUDA C. I believe CUDA Fortran provides the customary complex math in device code, but my experience with CUDA Fortran is limited and does not extend to complex arithmetic. If CUDA Fortran is an option, you may want to investigate that.

As far as support for complex math functions in CUDA C is concerned, consider filing an enhancement request through the bug reporting form linked from the registered developer website. If you decide to do so, please prefix the synopsis (i.e. subject line) with “RFE:” to mark this as a “request for enhancement” rather than a bug.

---

<div class="post-metadata">

**Author:** ![vacaloca](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@vacaloca](https://forums.developer.nvidia.com/u/vacaloca)\
**Post date:** [July 3, 2013, 8:09pm UTC](https://forums.developer.nvidia.com/t/complex-arithmetics-in-cuda/29954/8 "2013-07-03T20:09:29Z")

</div>

Someone bumped up this thread a while ago:  
[url][A new implementation of C++ std::complex for complex numbers on CUDA - CUDA Programming and Performance - NVIDIA Developer Forums](https://devtalk.nvidia.com/default/topic/517156/a-new-implementation-of-c-std-complex-for-complex-numbers-on-cuda/%5B/url%5D)

See:  
[url][https://github.com/jtravs/cuda\_complex[/url]](https://github.com/jtravs/cuda_complex%5B/url%5D)

It’s not being maintained, but would be a good starting point.

---

<div class="post-metadata">

**Author:** ![JFSebastian](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/jfsebastian/32/75602_2.png) [@JFSebastian](https://forums.developer.nvidia.com/u/JFSebastian)\
**Post date:** [July 4, 2013, 8:45am UTC](https://forums.developer.nvidia.com/t/complex-arithmetics-in-cuda/29954/9 "2013-07-04T08:45:30Z")

</div>

First of all, let me thank vacaloca for all his efforts.

Concerning the link

[url][A new implementation of C++ std::complex for complex numbers on CUDA - CUDA Programming and Performance - NVIDIA Developer Forums](https://devtalk.nvidia.com/default/topic/517156/a-new-implementation-of-c-std-complex-for-complex-numbers-on-cuda/%5B/url%5D)

let me say that I have already implemented something similar.

Regarding

[url][How does C compute sin() and other math functions? - Stack Overflow](http://stackoverflow.com/questions/2284860/how-does-c-compute-sin-and-other-math-functions%5B/url%5D)

my opinion is the same as yours: exploit as much as possible what CUDA already has in terms of mathematical functions and develop only what is needed, especially on complex arguments.

I have taken a look at

[url][http://gnuwin32.sourceforge.net/packages/fdlibm.htm[/url]](http://gnuwin32.sourceforge.net/packages/fdlibm.htm%5B/url%5D)

and I think it would be a very good starting point to possibly develop new mathematical functions in CUDA if NVIDIA will not make them available in future.

So, finally, thank you very much again.

Let me also thank njuffa for his usually kind answer. I have filed a “request for enhancement” as suggested.

---

<div class="post-metadata">

**Author:** ![JFSebastian](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/jfsebastian/32/75602_2.png) [@JFSebastian](https://forums.developer.nvidia.com/u/JFSebastian)\
**Post date:** [July 9, 2013, 8:49pm UTC](https://forums.developer.nvidia.com/t/complex-arithmetics-in-cuda/29954/10 "2013-07-09T20:49:39Z")

</div>

On the Stackoverflow

[url][c++ - Complex arithmetics in CUDA - Stack Overflow](http://stackoverflow.com/questions/17405715/complex-arithmetics-in-cuda/17495149?noredirect=1#17495149%5B/url%5D)

someone was directing me to the link

[url][https://github.com/jtravs/cuda\_complex/blob/master/cuda\_complex.hpp[/url]](https://github.com/jtravs/cuda_complex/blob/master/cuda_complex.hpp%5B/url%5D)

which is almost exactly what I was searching for.
