Are there templated math functions in cuda? [duplicate]

末鹿安然 提交于 2021-02-08 15:02:37

问题


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.


回答1:


[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.



来源:https://stackoverflow.com/questions/36191047/are-there-templated-math-functions-in-cuda

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!