cuda中有模板化的数学函数吗? [英] Are there templated math functions in cuda?

查看:41
本文介绍了cuda中有模板化的数学函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找cuda中的模板化数学函数,但似乎找不到.在普通的c ++中,如果我调用 std :: sqrt ,它是模板化的,并且会根据参数是float还是double来执行不同的版本.

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.

我想要类似这样的CUDA设备代码.我的内核具有作为模板参数传递的实型,现在我必须在使用 sqrtf 进行浮点型操作和使用 sqrt 进行双精度处理之间进行选择.我以为推力可能具有此功能,但仅适用于复数.

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.

推荐答案

[将注释,已删除的答案和一些其他历史记录转换为答案,以使此内容摆脱CUDA标签的未答复队列,请在进行编辑和修改认为合适]

[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;是

最初的基于Open64的工具链在2008-2009年期间添加了不错的模板支持(使诸如Komrade和后来的Thrust之类的事情成为可能),而现代的前端确实是C ++的适当子集.由于自CUDA 2开发周期以来就已经将模板支持和主机C ++编译纳入了工具链,因此支持代码和数学库已在完全基于模板的重载系统中发展.

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.

因此,CUDA中的标准数学函数会根据参数类型进行重载,因此您可以编写 sqrt(float)来计算单精度平方根或 sqrt(double)来计算双精度平方根.

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.

此内容记录在CUDA文档中此处.

This is documented in the CUDA documentation here.

这篇关于cuda中有模板化的数学函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆