Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there templated math functions in cuda? [duplicate]

Tags:

c++

cuda

I have been looking for templated math function in cuda and I can't seem to find one. In normal c++ if I call std::sqrt it is templated and will execute a different version based on if the argument is a float or double.

I want something like this for CUDA device code. My kernels have the real type passed as a template parameter and right now I have to choose between using sqrtf for float and sqrt for double. I thought thrust might have this feature but it only does for complex numbers.

like image 678
chasep255 Avatar asked Nov 08 '22 17:11

chasep255


1 Answers

[Turning comments, a deleted answer, and some additional history into an answer to get this off the unanswered queue for the CUDA tag, please edit and modify as you see fit]

TLDR; Yes

The original Open64 based toolchain had decent template support added during 2008-2009 (making things like Komrade and later Thrust possible), and the modern front end is really a proper subset of C++. Because template support and host C++ compilation has been baked into the toolchain since somewhere in CUDA 2 development cycle, the support code and math libraries have evolved in a fully template based overload system.

As a result, the standard math functions in CUDA are overloaded based on argument type, so you can write sqrt(float) to compute a single-precision square root, or sqrt(double) to compute a double-precision square root.

This is documented in the CUDA documentation here.

like image 158
2 revs Avatar answered Nov 14 '22 23:11

2 revs