Can a __host__ __device__ function know where it is executing?

cuda

Solution

It may be better to use the `__CUDA_ARCH__` macro as follows :

__host__ __device__ int myFunc(void) {
#if defined(__CUDA_ARCH__)
 // Device code here
#else
 // Host code here
#endif

For an example, see http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#host . The `__CUDA_ARCH__` macro can also be used to conditionally guard code for different GPU architectures.

Problem

I would like to write some functions that are both host and device compatible, and change the execution depending on whether the code is being executed on the device or the host. I am trying to write a class that will be used both on the device and host, and hide the distinction from the user. If there is no way to do this, does anybody have suggestions on how to accomplish this goal? i.e. ``` __host__ __device__ void foo(){ bool executingOnHost = // how do I figure this out? if( executingOnHost) // do stuff using host pointers else // do stuff with device pointers } ```

Original source

Related problems