gpt4 book ai didi

c++ - 常量内存符号作为类的静态属性

转载 作者:行者123 更新时间:2023-11-30 02:48:14 25 4
gpt4 key购买 nike

我很抱歉这个糟糕的标题......我想要一个具有静态属性值的类,我可以在设备代码中使用它。我尝试了以下内容:

struct MyConstValue
{
static __constant__ int value;
};

理论上,现在,我应该定义MyConstValue::value,初始化它,大概通过cudaMemcpyToSymbol ,然后我可以编写一个内核,通过 MyGlobalValue::value 访问这个值.

如果我添加

int __constant__ MyConstValue::value;

为了定义符号(有和没有 __constant__ ),nvcc 输出

error: ‘static’ may not be used when defining (as opposed to declaring) a static data member [-fpermissive]

有没有办法实现我的想法?

我正在使用 CUDA 5.5,我的目标计算能力 > 2.0。

提前致谢。

最佳答案

CUDA 不支持静态类成员。

原因可能是没有定义初始化点,如果所有线程都这样做,或者如果只有一个,如果是,哪个线程。所以静态数据在这种情况下没有意义。

来自NVIDIA forum :

But what would a "static class member" idiom even mean on a GPU? It can't be the same as the GPU since there's so many new questions about its definition. Perhaps every thread has its own static member, even if that thread accesses multiple copies of the class? Every block has a single static member? Every kernel? Every DEVICE, since classes can live in memory beyond kernel invocations?

来自 CUDA 编程指南的 B 2.2:

The constant qualifier, optionally used together with device, declares a variable that:

Resides in constant memory space, Has the lifetime of an application, Is accessible from all the threads within the grid and from the host through the runtime library (cudaGetSymbolAddress() / cudaGetSymbolSize() / cudaMemcpyToSymbol() / cudaMemcpyFromSymbol() for the runtime API and cuModuleGetGlobal() for the driver API).

你可以看看this thread .

关于c++ - 常量内存符号作为类的静态属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22171025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com