Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a host function in a CUDA kernel?

Tags:

cuda

gpgpu

As the following error implies, calling a host function ('rand') is not allowed in kernel, and I wonder whether there is a solution for it if I do need to do that.

error: calling a host function("rand") from a __device__/__global__ function("xS_v1_cuda") is not allowed
like image 560
Hailiang Zhang Avatar asked Mar 30 '12 03:03

Hailiang Zhang


People also ask

What is host in CUDA?

In CUDA, the host refers to the CPU and its memory, while the device refers to the GPU and its memory. Code run on the host can manage memory on both the host and device, and also launches kernels which are functions executed on the device.

What is host code and device code?

The Device CodeOne is called the host (your CPU), another is called device (your GPU). In our example, the add() function executes on the device (our GPU) and the rest of the C program executes on our CPU.

Which code will run on host and which code will run on GPU?

CUDA calls code that is slated to run on the CPU host code, and functions that are bound for the GPU device code. You can tell the two of them apart by looking at the function signatures; device code has the __global__ or __device__ keyword at the beginning of the function, while host code has no such qualifier.

What are kernels in CUDA?

The kernel is a function executed on the GPU. Every CUDA kernel starts with a __global__ declaration specifier. Programmers provide a unique global ID to each thread by using built-in variables. Figure 2. CUDA kernels are subdivided into blocks.


1 Answers

Unfortunately you can not call functions in device that are not specified with __device__ modifier. If you need in random numbers in device code look at cuda random generator curand http://developer.nvidia.com/curand

If you have your own host function that you want to call from a kernel use both the __host__ and __device__ modifiers on it:

__host__ __device__ int add( int a, int b )
{
    return a + b;
}

When this file is compiled by the NVCC compiler driver, two versions of the functions are compiled: one callable by host code and another callable by device code. And this is why this function can now be called both by host and device code.

like image 126
geek Avatar answered Sep 20 '22 14:09

geek