简体   繁体   English

OpenCL内核_本地内存行为不正常

[英]OpenCL Kernel _local memory not behaving correctly

I have a test kernel here that I'm calling from the Julia OpenCL API. 我在这里有一个测试内核,我从Julia OpenCL API调用。 The fact that I'm calling it from Julia isn't important, it's just what I'm using to run OpenCL, here is the code: 我从Julia调用它的事实并不重要,它正是我用来运行OpenCL的,这里是代码:

using OpenCL
const cl = OpenCL

device, ctx, queue = cl.create_compute_context()

C_buff = cl.Buffer(Float32, ctx, :w, 2)

const testkernel = """
kernel void test(global float *C)
{
    int gid = get_global_id(0);
    int lid = get_local_id(0);

    local float x;

    if (lid == 0)
    {
        x = 0.0f;
    }
    barrier(CLK_LOCAL_MEM_FENCE);

    x += 1.0f;

    barrier(CLK_LOCAL_MEM_FENCE);

    if (lid == 0)
    {
        C[gid / 2] = x;
    }
}
"""

program = cl.Program(ctx, source=testkernel) |> cl.build!
kernel = cl.Kernel(program, "test")
cl.call(queue, kernel, 4, 2, C_buff)
cl.read(queue, C_buff)

What I can't figure out is this returns a vector [1.0,1.0] , when it seems like it should return the vector [2.0,2.0] . 我无法弄清楚的是,它返回一个向量[1.0,1.0] ,当它看起来应该返回向量[2.0,2.0] Since basically I have 4 work-items broken up into two work-groups (each containing 2 work-items). 基本上我有4个工作项分为两个工作组(每个包含2个工作项)。

One local float x is instantiated for each work-group and the first work-item in each work-group sets it to 0. Then each work-item in the work-group adds 1 to it, and since there's two work-items in each work-group it should be 2, but when I return C , I get a vector of ones instead. 为每个工作组实例化一个本地浮点数x并且每个工作组中的第一个工作项将其设置为0.然后工作组中的每个工作项将1添加到它,因为有两个工作项每个工作组应该是2,但是当我返回C ,我会得到一个向量。

 barrier(CLK_LOCAL_MEM_FENCE); x += 1.0f; barrier(CLK_LOCAL_MEM_FENCE); 

Barriers aren't mutexes. 障碍不是互斥体。 You have a data race where both of the work items try to write to the same variable at the same time. 您有一个数据竞争,其中两个工作项尝试同时写入同一个变量。

You'll have to use atomics or redesign your code. 你必须使用原子或重新设计你的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM