我正在一个项目中,对于各种对象,基本数学函数(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/