检查代码是否在GPU或CPU上运行 [英] Check whether the code is running on the GPU or CPU

查看:1821
本文介绍了检查代码是否在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();
  }
}


推荐答案

是无法运行 检查一段代码正在运行的架构,但也没有必要知道,因为它可以在编译时确定并相应地处理。 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问题 C语言扩展部分。

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上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆