orochi
orochi

Reputation: 1258

__local atomic in opencl

About atomic access of __local variables:

I know it's slow to do global operations compared with local ones. In this sense I'd like to make atomic access of some variables.

I know I can do atomic operations in OpenCL:

// Program A:
#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
#pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable
__kernel void test(global int * num)
{
    atom_inc(&num[0]);
}

How do I share atomic data between work-itens within a given work-group?

for ex: I'd like to do something like that:

// Program B: (it doesn't work, just to show how I'd like it to be)
#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
#pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable
__kernel void test(global int * num, const int numOperations)
{
    __local int num;
    if (get_global_id(0) < numOperations) {
        atom_inc(&num);
    }
}

In the end the num value should return: numOperations - 1;

Isn't this possible? If not how could I do it?

Upvotes: 6

Views: 6897

Answers (1)

Anteru
Anteru

Reputation: 19404

Typically, you have one thread which intializes the shared (local) atomic followed by some barrier. I.e. your kernel starts like this:

__local int sharedNum;
if (get_local_id (0) == 0) {
    sharedNum = 0;
}
barrier (CLK_LOCAL_MEM_FENCE);

// Now, you can use sharedNum
while (is_work_left ()) {
    atomic_inc (&sharedNum);
}

There's not much magic to it -- all items in a work-group can see the same local variables, so you can just access it as usual.

Upvotes: 7

Related Questions