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.
[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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With