问题:
我有一个.h文件,如果在c/c++或者CUDA的计算能力 >= 1.3的情况下编译,希望将real定义为double。如果是在CUDA的计算能力 < 1.3的情况下编译,则将real定义为float。
经过多个小时的尝试,我写出了以下代码,但是它不起作用:
# if defined(__CUDACC__)
# warning * making definitions for cuda
# if defined(__CUDA_ARCH__) # warning __CUDA_ARCH__ is defined # else # warning __CUDA_ARCH__ is NOT defined # endif
# if (__CUDA_ARCH__ >= 130) # define real double # warning using double in cuda # elif (__CUDA_ARCH__ >= 0) # define real float # warning using float in cuda # warning how the hell is this printed when __CUDA_ARCH__ is not defined? # else # define real # error what the hell is the value of __CUDA_ARCH__ and how can I print it # endif
# else # warning * making definitions for c/c++ # define real double # warning using double for c/c++ # endif
当我使用以下命令进行编译(注意-arch标志):
nvcc -arch compute_13 -Ilibcutil testFloatDouble.cu
我得到以下输出:
* making definitions for cuda __CUDA_ARCH__ is defined using double in cuda
* making definitions for cuda warning __CUDA_ARCH__ is NOT defined warning using float in cuda how the hell is this printed if __CUDA_ARCH__ is not defined now?
Undefined symbols for architecture i386: "myKernel(float*, int)", referenced from: ....
我知道文件会被nvcc编译两次。第一次编译是正确的(CUDACC已定义且CUDA_ARCH> = 130),但第二次发生了什么?CUDA_DEFINED已定义,但CUDA_ARCH未定义或值小于130?为什么?
谢谢你的时间。
经过多个小时的尝试,我写出了以下代码,但是它不起作用:
# if defined(__CUDACC__)
# warning * making definitions for cuda
# if defined(__CUDA_ARCH__) # warning __CUDA_ARCH__ is defined # else # warning __CUDA_ARCH__ is NOT defined # endif
# if (__CUDA_ARCH__ >= 130) # define real double # warning using double in cuda # elif (__CUDA_ARCH__ >= 0) # define real float # warning using float in cuda # warning how the hell is this printed when __CUDA_ARCH__ is not defined? # else # define real # error what the hell is the value of __CUDA_ARCH__ and how can I print it # endif
# else # warning * making definitions for c/c++ # define real double # warning using double for c/c++ # endif
当我使用以下命令进行编译(注意-arch标志):
nvcc -arch compute_13 -Ilibcutil testFloatDouble.cu
我得到以下输出:
* making definitions for cuda __CUDA_ARCH__ is defined using double in cuda
* making definitions for cuda warning __CUDA_ARCH__ is NOT defined warning using float in cuda how the hell is this printed if __CUDA_ARCH__ is not defined now?
Undefined symbols for architecture i386: "myKernel(float*, int)", referenced from: ....
我知道文件会被nvcc编译两次。第一次编译是正确的(CUDACC已定义且CUDA_ARCH> = 130),但第二次发生了什么?CUDA_DEFINED已定义,但CUDA_ARCH未定义或值小于130?为什么?
谢谢你的时间。
nvcc
解析任何代码时,包括主机代码时,__CUDA_ARCH__
都会被定义。在主机代码中似乎被定义为0
。 - Jared Hoberock__CUDA_ARCH__
未定义。当然,我们很可能是错的。 - talonmies