OpenCL global constant private修饰符

一、内核函数参数
申明为一个指针类型,指向一段内存空间,该段内存空间必须用global, local, or constant.修饰。
kernel void my_func(int *p) // illegal because generic address space name for p is private.
kernel void my_func(private int *p) // illegal because memory pointed to by p is allocated in private.

“非内核函数没有这个限制”
void my_func(int *p)     // generic address space name for p is private. legal as my_func is not a kernel function
void my_func(private int *p)  // legal as my_func is not a kernel function


二、Global Address Space
The global address qualifier should not be used for image types.
global修饰的指针可以作为函数(包括kernel函数)参数和局部变量声明。但是函数内部局部变量不能用global修饰。
void my_func(global float4 *vA, global float4 *vB)
{
    global float4 *p; // legal
    global float4 a; // illegal
}


三、Constant Address Space
constant用于修饰存放在global memory中,但是在kernel函数中作为只读访问的变量。这个内存区域允许运行同
一kernel的所有work_group中的work_item访问。
Image types cannot be allocated in the constant address space.
constant修饰的指针可以作为函数(包括kernel函数)和局部变量声明。同时函数内部局部变量也可用constant修饰
(必须同时赋初值)。

kernel函数最外层大括号中可以用constant声明变量。程序中函数外申明的变量只能用constant修饰。程序中所有
用到的字符串常量,都位于constant空间。
// legal - program scope variables can be allocated only
// in the constant address space
constant float wtsA[] = { 0, 1, 2, . . . };

// program scope illegal - program scope variables can be allocated only in the constant address // space
global float wtsB[] = { 0, 1, 2, . . . };
kernel void my_func(constant float4 *vA, constant float4 *vB)
{
    constant float4 *p = vA;     // legal
    constant float a;                 // illegal – not initialized
    constant float b = 2.0f;       // legal – initialized with a compile-time constant
    p[0] = (float4)(1.0f);            // illegal – p cannot be modified

    // the string "opencl version" is allocated in the constant address space
    char *c = "opencl version";
}

  Constant memory驻存在global memory中,但kernel在运行时将其加载到所有的work-group共享的片上cache中。例如,如果存在所有的work-group都使用的只读数据,并且数据大小适合存在constant memory,那么可以将数据分配给Constant memory。Constant cache最适合在kernel多次调用中保持恒定的高带宽表的查找。Constant cache针对高速cache命中性能做了优化。

  默认情况下,constant cache的大小是16kB,可以通过在aoc命令中包含 -const-cache-bytes=<N> 的选项指定恒定大小的constant cachre,其中N以bytes为单位。

  与具有额外硬件,能够容忍较长时间内存延迟的global memory不同,constant cache会因为高速缓存未命中而导致较大的性能损失。如果_constant参数不能很好的适应cache,那么可以使用_global const来获得更好的性能。

        如果host的应用程序写入已加载到constant cache的constant memory中,那么将从contant cache中丢弃缓存的数据,所以,host侧尽量不要写device的constant memory。

四、Local Address Space
A good analogy for local memory is a user-managed cache.
local修饰的指针可以作为函数(包括kernel函数)参数和局部变量声明。
在kernel中申明的local变量有以下限制:
(1)kernel函数最外层大括号中可以用constant声明变量。
(2)申明的变量不能被初始化
kernel void my_func(global float4 *vA, local float4 *l)
{
    local float4 *p;    // legal
    local float4 a;    // legal
    a = 1;
    local float4 b = (float4)(0);     // illegal – b cannot be
    // initialized
    if (...)
    {
        local float c; // illegal – must be allocated at
        // kernel function scope
        ...
    }
}

五、Private Address Space
kernel函数内部申明的不带任何地址空间修饰符的变量申明;
在非kernel函数中申明的所有变量;
以及所有函数参数都位于private地址空间;

private memory is first allocated to the ALU‘s private registers. If there is not enough register to hold the private data, the data will be stored in global memory. Therefore, using too much private memory may affect the performance of the program.

六、总结(这里叫法以CUDA为准)

1.片上存储空间

        register和shared memroy

2.device memory空间

        global memory/constant memory/local memory/texture memory。实际上,global memory/constant memory/local memory/texture memory都是位于GPU外挂的物理内存颗粒上的,是什么使得他们之间的不同呢?—— cache。换句话说,除了global memory与local memory共用global memory的cache之外,constant memory/texture memory都有它们自己独立的cahce,它们各自的cache会针对它们存储的数据的特点进行特殊设计,使得GPU访问数据效率最大化。

3.但是,实际上global memory/local memory还是有细微差别

        "Local memory" in CUDA is actually global memory (and should really be called "thread-local global memory") with interleaved addressing (which makes iterating over an array in parallel a bit faster than having each thread's data blocked together). If you want things to be really fast, you want to use either shared memory, or, better yet, registers (especially on the latest devices where you get up to 255 registers per thread). 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

denglin12315

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值