Check if the code works on the GPU or CPU

Does anyone know how to check if code is running on a GPU or processor using Cuda?

__device__ __host__ double count_something(double variable) { if (RUN_ON_GPU) { use_cuda_variables(); } else { use_cpu_variables(); } } 
+6
source share
2 answers

It is not possible to verify which architecture is working on a piece of code, but there is no need to know, because it can be determined at compile time and processed accordingly. nvcc defines several preprocessor characters that can be used to parse the compilation path when compiling code. The symbol character __CUDA_ARCH__ , which is never determined when compiling the host code and is always determined when compiling the device code.

Thus, you can 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 produces a different code depending on whether it is compiled for the GPU or host. You can read a more detailed discussion of compilation control in this stack overflow question or in the C language extension in the CUDA programming guide.

+11
source

I can’t add the correct code markdown in the comments - I decided to add a complete answer. Using only __CUDA_ARCH__ determines that the check is not entirely correct. In some cases, this code does not work - I spent a lot of time debugging before I found a solution (CUDA documentation does not mention this now).
__CUDA_ARCH__ can be defined even in the main code, but in this case it is defined as 0. Thus, the correct check looks something like this:

 __device__ __host__ float function(float x) { #if (defined(__CUDA_ARCH__) && (__CUDA_ARCH__ > 0)) // device code here return 10.0f * __sinf(x); #else // host code here return 10.0f * sin(x); #endif } 
+2
source

Source: https://habr.com/ru/post/944020/


All Articles