OpenCL global memory visibility limits across work-groups without fences
0 reputation · 14 Jun 2022, 01:52 UTC
0 reputation · 14 Jun 2022, 01:52 UTC
I want a producer work-group to write a result buffer in global memory and have a consumer work-group read that data reliably within the same kernel launch, without stale reads.
My understanding of the OpenCL C memory model is that it is relaxed: writes to global memory by one work-item are not guaranteed visible to others without explicit synchronization. Barriers (work_group_barrier / the older barrier() with CLK_GLOBAL_MEM_FENCE) only order execution within a single work-group and do not synchronize across work-groups, which may run on different compute units. The volatile qualifier appears to prevent the compiler from caching a value in a register but does not guarantee hardware cache coherence. This leaves me unsure which mechanism, if any, is sufficient for cross-work-group visibility inside one kernel.
In a single kernel launch, the only portable way to guarantee that a write performed by one work‑group is seen by another is to use the OpenCL 2.x atomic store/load pair with memory_order_release and memory_order_acquire and the memory_scope_device (or memory_scope_all_svm_devices if you are using SVM). This establishes a *release/acquire* ordering that, in the formal memory model, makes all preceding non‑atomic writes in the producer work‑group visible to the consumer work‑group after it observes the flag. If the device does not support scoped atomics, or if you are running on OpenCL 1.2 or earlier, the only reliable boundary is kernel termination (or a host‑side event ordering between two kernels).
barrier(CLK_GLOBAL_MEM_FENCE)) only order work‑items within the same work‑group; they provide no cross‑work‑group visibility guarantees.volatile qualifier prevents compiler caching but does not insert hardware fences or guarantee cache coherence across compute units.memory_order_release/memory_order_acquire and memory_scope_device that can be used to order non‑atomic global memory accesses.CL_DEVICE_VERSION and, for OpenCL 3.x, CL_DEVICE_ATOMIC_MEMORY_CAPABILITIES to confirm that memory_scope_device and release/acquire semantics are available.1 to an atomic flag with memory_order_release.
atomic_load_explicit(flag, memory_order_acquire).
1.
If you are unsure whether your device supports scoped atomics, run:
clGetDeviceInfo(device, CL_DEVICE_VERSION, ...);
// For OpenCL 3.x, also query CL_DEVICE_ATOMIC_MEMORY_CAPABILITIES
Share the returned values; they will determine whether the atomic‑store/load pattern is viable or if you must rely on kernel boundaries.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.