检查代码是在GPU还是CPU上运行

Check whether the code is running on the GPU or CPU(检查代码是在GPU还是CPU上运行)

本文介绍了检查代码是在GPU还是CPU上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何使用 Cuda 检查代码是在 GPU 还是 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();
  }
}

推荐答案

没有办法runtime检查一段代码在哪个架构上运行,但也不需要知道,因为它可以在编译时确定并相应地处理.nvcc 定义了几个预处理器符号,可用于在编译代码时解析编译轨迹.关键符号是__CUDA_ARCH__,它在编译主机代码时从不定义,在编译设备代码时总是定义.

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.

所以可以这样写函数:

__device__ __host__ float function(float x)
{
#ifdef __CUDA_ARCH__
    return 10.0f * __sinf(x);
#else
    return 10.0f * sin(x);
#endif
}

这将根据是为 GPU 还是主机编译而发出不同的代码.您可以在 Stack Overflow question 或 C 语言扩展 CUDA 编程指南部分.

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.

这篇关于检查代码是在GPU还是CPU上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:检查代码是在GPU还是CPU上运行

基础教程推荐