【问题标题】:Why is my call of the CUDA math library sqrt() function failing?为什么我调用 CUDA 数学库 sqrt() 函数失败?
【发布时间】:2013-12-01 12:13:45
【问题描述】:

我是 Cuda 新手,我有以下功能:

__global__ void square(float *myArrayGPU)
{
   myArrayGPU[threadIdx.x] = sqrt(threadIdx.x);
}

我想使用 cuda 数学库,我尝试#include "math.h" 但我仍然收到错误

error: calling a __host__ function("__sqrt") from a __global__ function("square") is not allowed

知道我应该包含什么库来使用sqrt吗?

【问题讨论】:

    标签: c++ c math cuda square-root


    【解决方案1】:

    threadIdx.x 是 int 类型。 CUDA 数学库仅重载单精度 (float) 和双精度 (double)。您需要为 sqrt() 提供“浮点”或“双精度”类型参数,以便调用 sqrt() 的 CUDA 版本。

    改变

    myArrayGPU[threadIdx.x] = sqrt(threadIdx.x);
    

    进入

    myArrayGPU[threadIdx.x] = sqrt( (float) threadIdx.x);
    

    更多详细信息,请查看CUDA sqrt() prototype documentation

    【讨论】:

      【解决方案2】:

      sqrt 需要一个浮点类型变量。试试sqrt((float)(threadIdx.x))

      【讨论】:

      • 不错的答案,但不幸的是,Harshil Sharma 击败了你,获得了“教科书版”...... :-)
      猜你喜欢
      • 2023-03-21
      • 1970-01-01
      • 2020-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-14
      • 1970-01-01
      • 2021-12-22
      相关资源
      最近更新 更多