Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CUDA function call-able by either the device or host

I have a re-useable function in some CUDA code that needs to be called from both the device and the host. Is there an appropriate qualifier for this?

e.g. what's the correct definition for func1 in this case:

int func1 (int a, int b) {
    return a+b;
}

__global__ devicecode (float *A) {
    int i = blockDim.x * blockIdx.x + threadIdx.x;
    A[i] = func1(i,i);
}

void main() {
    // Normal cuda memory set-up

    // Call func1 from inside main:
    int j = func1(2,4)

    // Normal cuda memory copy / program run / retrieve data
}

So far I can only get this to work by having the function twice: once explicitly for the device and once for the host. Is there a better way?

like image 798
Phil Avatar asked Jan 18 '11 15:01

Phil


People also ask

Which of the following function that is executed on device and callable from device only in GPU?

__global__ - Runs on the GPU, called from the CPU or the GPU*.

What is function of __ global __ qualifier in CUDA program?

__global__ : 1. A qualifier added to standard C. This alerts the compiler that a function should be compiled to run on a device (GPU) instead of host (CPU).

What are the three general section of CUDA program?

To execute any CUDA program, there are three main steps: Copy the input data from host memory to device memory, also known as host-to-device transfer. Load the GPU program and execute, caching data on-chip for performance. Copy the results from device memory to host memory, also called device-to-host transfer.

What is kernel function in CUDA?

Kernels (in software) A function that is meant to be executed in parallel on an attached GPU is called a kernel. In CUDA, a kernel is usually identified by the presence of the __global__ specifier in front of an otherwise normal-looking C++ function declaration.


1 Answers

From the CUDA Programming Guide:

The __device__ and __host__ qualifiers can be used together however, in which case the function is compiled for both the host and the device.

like image 157
tkerwin Avatar answered Nov 07 '22 06:11

tkerwin