我想编写一些兼容主机和设备的函数,并根据代码是在设备上执行还是在主机上执行来更改执行.我正在尝试编写一个将在设备和主机上使用的类,并隐藏与用户的区别.
如果没有办法做到这一点,是否有人就如何实现这一目标提出建议?
即
__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
}
Run Code Online (Sandbox Code Playgroud)
小智 7
使用__CUDA_ARCH__宏可能更好如下:
__host__ __device__ int myFunc(void) {
#if defined(__CUDA_ARCH__)
// Device code here
#else
// Host code here
#endif
Run Code Online (Sandbox Code Playgroud)
有关示例,请参阅http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#host.该__CUDA_ARCH__宏还可以用于有条件后卫不同的GPU架构的代码.