Check whether the code is running on the GPU or 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上运行


- 使用/clr 时出现 LNK2022 错误 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 静态初始化顺序失败 2022-01-01
- C++ 协变模板 2021-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 近似搜索的工作原理 2021-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01