gpt4 book ai didi

cuda - __CUDA_ARCH__ 宏的行为

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

在主机代码中,__CUDA_ARCH__ 宏似乎不会生成不同的代码路径,相反,它会为当前设备的确切代码路径生成代码。

但是,如果 __CUDA_ARCH__ 在设备代码中,它将为编译选项 (/arch) 中指定的不同设备生成不同的代码路径。

任何人都可以确认这是正确的吗?

最佳答案

__CUDA_ARCH__ 在设备代码中使用时将携带一个定义给它的数字,以反射(reflect)当前正在编译的代码架构。

它不打算在主机代码中使用。来自 nvcc manual :

This macro can be used in the implementation of GPU functions for determining the virtual architecture for which it is currently being compiled. The host code (the non-GPU code) must not depend on it.



因此,在主机代码中使用 __CUDA_ARCH__ 是未定义的(至少由 CUDA 定义)。正如@tera 在评论中指出的那样,由于宏在主机代码中未定义,因此它可用于区分主机/设备路径,例如,在 __host__ __device__ 函数定义中。
#ifndef __CUDA_ARCH__
//host code here
#else
//device code here
#endif

关于cuda - __CUDA_ARCH__ 宏的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16072740/

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