# LIbrary for complex numbers in device kernel

**URL:** <https://forums.developer.nvidia.com/t/library-for-complex-numbers-in-device-kernel/46690>\
**Category:** CUDA Programming and Performance\
**Created:** [December 9, 2016, 2:05pm UTC](https://forums.developer.nvidia.com/t/library-for-complex-numbers-in-device-kernel/46690 "2016-12-09T14:05:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![idg101](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@idg101](https://forums.developer.nvidia.com/u/idg101)\
**Post date:** [December 9, 2016, 2:05pm UTC](https://forums.developer.nvidia.com/t/library-for-complex-numbers-in-device-kernel/46690/1 "2016-12-09T14:05:54Z")

</div>

What is the best library to use for complex number algebra for device kernels? I need capability beyond multiplication and addition like powers of complex numbers and division.

I see that there is thrust::complex but it looks dated. Is there something like std::complex?

---

<div class="post-metadata">

**Author:** ![episteme](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/episteme/32/10803_2.png) [@episteme](https://forums.developer.nvidia.com/u/episteme)\
**Post date:** [December 9, 2016, 2:43pm UTC](https://forums.developer.nvidia.com/t/library-for-complex-numbers-in-device-kernel/46690/2 "2016-12-09T14:43:54Z")

</div>

take a look cuComplex.h : definition of float/double complex and various of functions/operations.

---

<div class="post-metadata">

**Author:** ![idg101](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@idg101](https://forums.developer.nvidia.com/u/idg101)\
**Post date:** [December 9, 2016, 2:49pm UTC](https://forums.developer.nvidia.com/t/library-for-complex-numbers-in-device-kernel/46690/3 "2016-12-09T14:49:32Z")

</div>

@episteme, it looks like there are no power functions in there (sqrt, pow(complex, float), etc).

---

<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:** [December 9, 2016, 4:35pm UTC](https://forums.developer.nvidia.com/t/library-for-complex-numbers-in-device-kernel/46690/4 "2016-12-09T16:35:54Z")

</div>

You might want to take a look at the Bluebird Library ([http://www.orangeowlsolutions.com/bluebird](http://www.orangeowlsolutions.com/bluebird)) which is under GPL. I just checked, it does have support for complex pow(), although a comment in the code says “to be optimized”, so it might not be particularly fast. I have not used this library myself, so can only provide a pointer here, you will have to make your own determination as to suitability for your use case.

You may also consider filing an enhancement request with NVIDIA to add complex support to the CUDA standard math library. Enhancement requests can be filed through the bug reporting form linked from the CUDA registered developer website, simply prefix the synopsis with “RFE:”.

---

<div class="post-metadata">

**Author:** ![idg101](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@idg101](https://forums.developer.nvidia.com/u/idg101)\
**Post date:** [December 9, 2016, 5:43pm UTC](https://forums.developer.nvidia.com/t/library-for-complex-numbers-in-device-kernel/46690/5 "2016-12-09T17:43:05Z")

</div>

@njuffa thank you! Im not so concerned about speed right now but numerical accuracy is important.

I will file a “bug request” next week.

---

<div class="post-metadata">

**Author:** ![MutantJohn](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@MutantJohn](https://forums.developer.nvidia.com/u/MutantJohn)\
**Post date:** [December 9, 2016, 6:33pm UTC](https://forums.developer.nvidia.com/t/library-for-complex-numbers-in-device-kernel/46690/6 "2016-12-09T18:33:51Z")

</div>

Not sure if this is naive or not but if numerical accuracy is your true desire, it might be more worthwhile to stick with the CPU. There are already arbitrary precision libraries out there and that kind of computation maps better to the CPU than it does to the GPU. Keep in mind, CUDA pushed for a 16 bit floating point data type so if that’s any indication…

---

<div class="post-metadata">

**Author:** ![idg101](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@idg101](https://forums.developer.nvidia.com/u/idg101)\
**Post date:** [December 9, 2016, 6:40pm UTC](https://forums.developer.nvidia.com/t/library-for-complex-numbers-in-device-kernel/46690/7 "2016-12-09T18:40:51Z")

</div>

@mutantjohn NVIDIA GPUs support IEEE standard. That should be fine.

---

<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:** [December 9, 2016, 7:01pm UTC](https://forums.developer.nvidia.com/t/library-for-complex-numbers-in-device-kernel/46690/8 "2016-12-09T19:01:03Z")

</div>

CUDA features are first and foremost driven by customer demand, and that applies to FP16 (half precision) as well. Filing enhancement requests is one way of expressing customer demand.

Since NVIDIA is a for-profit enterprise that provides CUDA and its associated libraries free of charge, one can naturally assume that the ability to drive GPU sales is one important criterion in the prioritization of feature requests by customers. As deep learning is a multi-billion dollar sales opportunity, NVIDIA’s push with respect to FP16 and machine learning libraries that make use of it is therefore easily explained.

As for a multiple-precision library for CUDA, I would suggest looking at the third-party library CAMPARY: [url][http://homepages.laas.fr/mmjoldes/campary/[/url]](http://homepages.laas.fr/mmjoldes/campary/%5B/url%5D). Again, I have not used the library myself.

CAMPARY paper: [url][https://hal.archives-ouvertes.fr/hal-01312858/[/url]](https://hal.archives-ouvertes.fr/hal-01312858/%5B/url%5D)  
related paper on algorithms: [url][Arithmetic Algorithms for Extended Precision Using Floating-Point Expansions | IEEE Journals & Magazine | IEEE Xplore](http://ieeexplore.ieee.org/document/7118139/%5B/url%5D)

CAMPARY presentation slide deck: [url][http://perso.ens-lyon.fr/valentina.popescu/Talks/RAIM15.pdf[/url]](http://perso.ens-lyon.fr/valentina.popescu/Talks/RAIM15.pdf%5B/url%5D)  
older slide deck: [url][http://perso.ens-lyon.fr/valentina.popescu/Talks/CAPA14.pdf[/url]](http://perso.ens-lyon.fr/valentina.popescu/Talks/CAPA14.pdf%5B/url%5D)

---

<div class="post-metadata">

**Author:** ![MutantJohn](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@MutantJohn](https://forums.developer.nvidia.com/u/MutantJohn)\
**Post date:** [December 9, 2016, 7:21pm UTC](https://forums.developer.nvidia.com/t/library-for-complex-numbers-in-device-kernel/46690/9 "2016-12-09T19:21:49Z")

</div>

Wow, I’ve actually never been happier to be wrong! Thanks, njuffa! That link is awesome.
