如何为初学者使用常量内存(Cuda C) [英] How to use constant memory for beginners (Cuda C)

查看:16
本文介绍了如何为初学者使用常量内存(Cuda C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个常量值(ABC)我想保存在常量内存中;我通过输入以下代码行找到了一种方法:

I have 3 constant values (A,B,C) which I would like to save in the constant memory; I figured out one way to do it by typing this lines of code:

// CUDA global constants
__constant__ int A;
__constant__ int B;
__constant__ int C;

int main(void)
{

    float pA=1;
    float pB=2;
    float pC=3;
    ...
    cudaMemcpyToSymbol(A, &pA, sizeof(A));
    cudaMemcpyToSymbol(B, &pB, sizeof(B));
    cudaMemcpyToSymbol(C, &pC, sizeof(C));
    ...
}

但是我认为这不是最好的方法,因为如果我有更多的常量会变得非常不方便.

However I believe this is not the best way to proceed since it would become very inconvenient if I had a larger number of constants.

这是我的问题:如何复制我上面编写的代码行以获得更紧凑的形式?

Here is my question: how can I replicate the lines of code I wrote above in order to have a more compact form?

推荐答案

CUDA中的常量内存是65536字节的专用内存空间.它是专用的,因为它具有一些特殊功能,例如缓存和广播.

The constant memory in CUDA is a dedicated memory space of 65536 bytes. It is dedicated because it has some special features like cache and broadcasting.

常量内存空间驻留在设备内存中,缓存在Compute Capability 1.x和Compute Capability 2.x中提到的常量缓存中.

The constant memory space resides in device memory and is cached in the constant cache mentioned in Compute Capability 1.x and Compute Capability 2.x.

见教派.5.3.2.设备内存访问和部门.G.4.4.更多详细信息,请参阅 CUDA C 编程指南中的常量内存.

See Sect. 5.3.2. Device Memory Access and Sect. G.4.4. Constant Memory in the CUDA C Programming Guide for more details.

因此,您可以像以前那样为一个元素分配常量内存,也可以为一组元素分配内存.

So, you can allocate constant memory for one element as you already did, and you can also allocate memory for an array of element.

__constant__ float c_ABC[3]; // 3 elements of type float (12 bytes)

但是,在 CUDA 中不允许动态分配常量内存.因此,您必须像使用一个元素一样将数据从 CPU 复制到 GPU.

However, dynamically allocation of constant memory is not allowed in CUDA. Therefore, you must copy the data from the CPU to the GPU as you did with one element.

float pABC[] = {1, 2, 3};
...
cudaMemcpyToSymbol(c_ABC, &pABC, 3 * sizeof(float));

您可以在 CPU 中初始化 pABC,例如在循环中或从文件中加载数据,然后将数据复制到 GPU 的常量内存中.

You can initialize pABC in the CPU for example in a loop or loading data from a file and then copy the data in the constant memory of the GPU.

请注意,我已将您的示例调整为始终使用浮动.

NOTE that I have adjusted your example to use always floats.

这篇关于如何为初学者使用常量内存(Cuda C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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