用另一个常量声明设备常量 [英] declaring device constant in terms of another constant

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

问题描述

当我尝试用另一个常量来定义一个常量时,它们都存储在设备常量内存中,例如:

When I try to define a constant in terms of another constant, both stored in device constant memory, as in:

__device__ __constant__ float x=0.1;
__device__ __constant__ float y=2*x;

我收到错误:


错误:无法为设备上的非空构造函数或析构函数生成代码

error: can't generate code for non empty constructors or destructors on device

任何提示吗?

推荐答案

__ constant __ const不同。特别是,可以从主机修改 __ constant __ 对象。因此,编译器无法应用编译时间评估。在运行时无法从设备代码中写入 __ constant __ 对象,因此也无法进行运行时初始化。另外,在实际的内核代码开始执行之前,没有设备可以执行这种初始化的初始化例程。编译器产生的错误消息似乎暗示了最后一个事实。

__constant__ is not the same as const. In particular, a __constant__ object can be modified from the host. So the compiler cannot apply compile time evaluation. A __constant__ object cannot be written from within the device code at runtime, so runtime initialization is also not possible. In addition, there is no init routine for the device that could perform such an initialization prior to the actual kernel code commencing execution. The error message produced by the compiler seems to allude to that last fact.

您可以使用已定义的常量,例如:

You could use defined constants, for example:

#define MAGIC_NUMBER_1  (0.1f)
#define MAGIC_NUMBER_2  (2.0f * MAGIC_NUMBER_1)

__constant__ float x = MAGIC_NUMBER_1;
__constant__ float y = MAGIC_NUMBER_2;

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

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