Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my call of the CUDA math library sqrt() function failing?

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?

like image 264
Avraam Mavridis Avatar asked Dec 01 '13 12:12

Avraam Mavridis


People also ask

How use sqrt function in C Linux?

The sqrt() function is defined in math. h header file. To find the square root of int , float or long double data types, you can explicitly convert the type to double using cast operator. int x = 0; double result; result = sqrt(double(x));

How do you do Cuda in math?

Available to any CUDA C or CUDA C++ application simply by adding “#include math. h” in your source code, the CUDA Math library ensures that your application benefits from high performance math routines optimized for every NVIDIA GPU architecture.

Does Cuda support C++?

Using the CUDA Toolkit you can accelerate your C or C++ applications by updating the computationally intensive portions of your code to run on GPUs. To accelerate your applications, you can call functions from drop-in libraries as well as develop custom applications using languages including C, C++, Fortran and Python.


2 Answers

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.

like image 134
Harshil Sharma Avatar answered Oct 15 '22 10:10

Harshil Sharma


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

like image 20
Avi Ginsburg Avatar answered Oct 15 '22 09:10

Avi Ginsburg