OpenCL C specification provides for runtime barriers in a work item and across a single work group. Barriers may only synchronize threads in the same workgroup. There is no way to synchronize between different work groups. For synchronizing outside of the work group the kernel should complete its execution. There are two types of memory fences:
CLK_LOCAL_MEM_FENCE
: This ensures correct ordering of operations on local memory. It is used as follows:barrier(CLK_LOCAL_MEM_FENCE);
The barrier function will either flush any variables stored in local memory or queue a memory fence to ensure correct ordering of memory operations to local memory.
CLK_GLOBAL_MEM_FENCE
: This ensures correct ordering of operations on global memory. It is used as follows:barrier(CLK_GLOBAL_MEM_FENCE);
To help you understand, in short you should use CLK_LOCAL_MEM_FENCE
when reading and writing to the __local
memory space, and CLK_GLOBAL_MEM_FENCE
when reading and writing to the __global
memory space.
Sometimes...