4

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.

Hailiang Zhang
  • 17,604
  • 23
  • 71
  • 117

2 Answers2

12

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;
}
Eugene
  • 9,242
  • 2
  • 30
  • 29
-2

The tutorial you are looking at is so old, 2008? It might not be compatible with the version of CUDA you are using.

You can use __global__ and that means __host__ __device__, this works:

__global__ void f()
{
    const int tid = threadIdx.x + blockIdx.x * blockDim.x;
}

int main()
{
    f<<<1,1>>>();
}
Adam
  • 3,872
  • 6
  • 36
  • 66
  • `__global__` specifies a kernel entry point, i.e. a function that will auto-parallelize into GPU code when called with launch parameters. `__host__` and `__device__` are not used to decorate kernel functions. The only sense in which you could say `__global__` means `__host__ __device__` with any sense is in the case of [cuda dynamic parallelism](http://docs.nvidia.com/cuda/cuda-dynamic-parallelism/index.html), which is only available on cc 3.5 devices. Even in that case, I think it's sloppy to say `__global__` means `__host__ __device__` – Robert Crovella Jun 12 '13 at 03:03
  • @RobertCrovella I agree, I only meant they are equivalent in his context, as my code cannot be called from the host anyway as it has kernel variables. – Adam Jun 12 '13 at 03:11