使用CUDA常量 [英] Using constants with CUDA

查看:330
本文介绍了使用CUDA常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在CUDA中使用常量的最佳方式?

Which is the best way of using constants in CUDA?

一种方法是在常量内存中定义常量,如:

One way is to define constants in constant memory, like:

// CUDA global constants
__constant__ int M;

int main(void)
{
    ...
    cudaMemcpyToSymbol("M", &M, sizeof(M));
    ...
}

另一种方法是使用C预处理程序:

An alterative way would be to use the C preprocessor:

#define M = ... 

我认为用C预处理器定义常量要快得多。那么,在CUDA设备上使用常量内存的好处是什么?

I would think defining constants with the C preprocessor is much faster. Which are then the benefits of using the constant memory on a CUDA device?

推荐答案


  1. 已知在编译时应使用
    预处理器宏定义(例如 #define

  2. __ constant __ 内存
  3. 如果程序选择的数量相对较少,这些选择会影响内核执行时,额外的编译时优化的一种可能方法是使用模板代码/内核

  1. constants that are known at compile time should be defined using preprocessor macros (e.g. #define)
  2. Usage of __constant__ memory may be beneficial for programs who use certain values that don't change for the duration of the kernel and for which certain access patterns are present (e.g. all threads access the same value at the same time). This is not better or faster than constants that satisfy the requirements of item 1 above.
  3. If the number of choices to be made by a program are relatively small in number, and these choices affect kernel execution, one possible approach for additional compile-time optimization would be to use templated code/kernels

这篇关于使用CUDA常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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