c++ - CUDA内核和数学函数的显式命名空间

标签 c++ cuda

我正在一个项目中,对于各种对象,基本数学函数(cos,sin,exp,log,sqrt等)的多个定义都已重载。我包括具有此类定义的多个 header (例如<cmath><cuda.h>和我自己的 header )并不少见。为了完全确保使用正确的实现,我想显式地写出 namespace ,例如std::sqrt()。这让我想知道两个相关的事情:

问:如何确保使用特定功能的<cuda.h>实现,即是否可以明确写出 namespace ?

问:我是否会使用例如CUDA内核中的std::sqrt()(说性能问题,因为它通常是宿主函数)?

最佳答案

在cuda内核中,您只能使用具有__device__属性的函数。这保证了不会使用std::cmath中的任何内容。

error: calling a constexpr __host__ function("sqrt") from a __global__ function("kernel") is not allowed. The experimental flag '--expt-relaxed-constexpr' can be used to allow this.

关于c++ - CUDA内核和数学函数的显式命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60356422/

相关文章:

c++ - 如何避免 Qt app.exec() 阻塞主线程

c++ - 为什么我不能在类外定义返回类型 'pointer to struct'的函数?

c++ - 使用 distutils 构建 Python 扩展模块

c++ - 为什么从 __global__ 函数中引用外部内存会搞砸一切?

c# - 用 C# 编写 CUDA?

cuda - 如何隐藏 NVCC 的 "function was declared but never referenced"警告?

c++使用模板制作最通用的函数

c++错误使用模板继承baseClass

python - "Unrolling"递归函数?

cudaMallocManaged 和 cudaDeviceSynchronize()