Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a __host__ __device__ function know where it is executing? [duplicate]

Tags:

cuda

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  
}      
like image 403
Acerebral Avatar asked Nov 02 '25 11:11

Acerebral


1 Answers

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.

like image 169
Vyas Avatar answered Nov 04 '25 20:11

Vyas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!