Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

c++ - Check whether the code is running on the GPU or CPU

Does anybody know how to check whether the code is running on the GPU or CPU using Cuda?

__device__ __host__  double count_something(double variable) {
  if (RUN_ON_GPU) {
    use_cuda_variables();
  } else {
    use_cpu_variables();
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...