Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a kernel function which callable from both the host and device?

Tags:

cuda

The following trial presents my intention, which failed to compile:

__host__ __device__ void f(){}

int main()
{
    f<<<1,1>>>();
}

The compiler complaints:

a.cu(5): error: a __device__ function call cannot be configured

1 error detected in the compilation of "/tmp/tmpxft_00001537_00000000-6_a.cpp1.ii".

Hope my statement is clear, and thanks for advices.

like image 721
Hailiang Zhang Avatar asked Dec 27 '22 02:12

Hailiang Zhang


1 Answers

You need to create a CUDA kernel entry point, e.g. __global__ function. Something like:

#include <stdio.h>

__host__ __device__ void f() {
#ifdef __CUDA_ARCH__
    printf ("Device Thread %d\n", threadIdx.x);
#else
    printf ("Host code!\n");
#endif
}

__global__ void kernel() {
   f();
}

int main() {
   kernel<<<1,1>>>();
   if (cudaDeviceSynchronize() != cudaSuccess) {
       fprintf (stderr, "Cuda call failed\n");
   }
   f();
   return 0;
}
like image 135
Eugene Avatar answered Jan 05 '23 17:01

Eugene