There is no way to runtime check which architecture a piece of code is running on, but there is also no need to know, because it can be determined at compile time and handled accordingly. nvcc
defines several preprocessor symbols which can be used to parse the compilation trajectory while code is being compiled. The key symbol is __CUDA_ARCH__
which is never defined when compiling host code and always defined when compiling device code.
So it is possible to write a function like this:
__device__ __host__ float function(float x)
{
#ifdef __CUDA_ARCH__
return 10.0f * __sinf(x);
#else
return 10.0f * sin(x);
#endif
}
which will emit different code depending on whether it is compiled for the GPU or host. You can read a more thorough discussion about compilation steering in this Stack Overflow question or in the C language extensions section of the CUDA programming guide.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…