13

I am new to Cuda, I have the following function:

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

I want to use the cuda math library, I tried to #include "math.h" but I still get the error

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

Any idea what library should I include to use the sqrt?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
Avraam Mavridis
  • 8,698
  • 19
  • 79
  • 133

2 Answers2

29

threadIdx.x is of type int. CUDA math library is overloaded only for single precision (float) and double precision (double). You need to supply either a 'float' or 'double' type parameter to sqrt() for the CUDA version of sqrt() to be called.

Change

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

into

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

For more detailed information, take a look at the CUDA sqrt() prototype documentation.

OGHaza
  • 4,795
  • 7
  • 23
  • 29
Harshil Sharma
  • 2,016
  • 1
  • 29
  • 54
9

sqrt expects a floating type variable. Try sqrt((float)(threadIdx.x))

Avi Ginsburg
  • 10,323
  • 3
  • 29
  • 56