gpt4 book ai didi

cuda - __host__ __device__ 函数可以知道它在哪里执行吗?

转载 作者:行者123 更新时间:2023-12-05 01:35:21 28 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Check whether the code is running on the GPU or CPU

(2 个回答)


7年前关闭。




我想写一些主机和设备兼容的函数,并根据代码是在设备上还是在主机上执行来更改执行。我正在尝试编写一个将在设备和主机上使用的类,并向用户隐藏区别。

如果没有办法做到这一点,是否有人对如何实现这一目标有任何建议?

IE。

__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
}

最佳答案

使用 __CUDA_ARCH__ 可能会更好宏如下:

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

例如,参见 http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#host . __CUDA_ARCH__宏还可用于有条件地保护不同 GPU 架构的代码。

关于cuda - __host__ __device__ 函数可以知道它在哪里执行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23899190/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com