CUDA Memory Barriers: Distinguish Fences from Thread Synchronization
In CUDA, a memory fence orders one thread’s memory operations, while an execution barrier waits for its participants. Learn how thread scope, block barriers, and release/acquire atomics fit together, and why a fence or volatile flag alone does not publish data safely.
CUDA Memory Barriers: Distinguish Fences from Thread Synchronization
In CUDA, “memory barrier” is often used for two different operations. A memory fence orders a thread’s memory operations; an execution barrier makes specified threads wait until they arrive. To read data written by another thread safely, you must also match the participating thread scope and the way data is published. This article follows the CUDA C++ contract in NVIDIA’s CUDA Programming Guide 13.2, checked on November 22, 2026. The same rules should not be assumed for other GPU APIs.
How a fence differs from an execution barrier
You cannot assume that another GPU thread observes memory reads and writes in the same order as the source code. Conflicting concurrent accesses to one location without appropriate synchronization constitute a data race, whose result is undefined. Saying only “add a barrier” does not identify what must be ordered or which threads must wait.
| Operation | Does it wait for other participants? | Main guarantee | Typical use |
|---|---|---|---|
__threadfence_block() or a block-scope fence | No | Orders the calling thread’s memory operations within block scope | Ordering publication between threads in one block |
__threadfence() or a device-scope fence | No | Orders the calling thread’s memory operations at device scope | Wider ordering among threads on one CUDA device |
__threadfence_system() or a system-scope fence | No | Orders memory operations at supported system scope | Supported communication involving CPU, GPU, or peer devices |
__syncthreads() | Yes, participating threads in the block | Waits for block participants and orders their memory accesses | Before threads in one block read one another’s data |
__syncwarp(mask) | Yes, warp threads named by the mask | Waits for those warp participants and orders their memory accesses | Communication among specified lanes in one warp |
| Atomic release/acquire operations | Usually do not wait | A release and an acquire that observes it can connect prior writes to later reads | Passing data between a producer and consumer through a flag |
The key distinction is that a fence orders memory operations before and after the calling thread’s fence. It does not stop or wake another thread. __syncthreads(), by contrast, waits for participating threads in the same block and also provides memory ordering across that point. NVIDIA documents both guarantees separately in its CUDA synchronization primitives.
Choosing a thread scope
CUDA C++ thread scope describes the set of participants that can synchronize. It does not name the memory space where data lives; the data can be in shared or global memory. First identify whether the writer and reader are in one block, different blocks on one device, or include the CPU or another GPU.
| Scope | Threads it can include | Selection rule |
|---|---|---|
cuda::thread_scope_block | GPU threads in the same thread block | Use when the consumer is in that block |
cuda::thread_scope_device | GPU threads on the same CUDA device and in the same memory synchronization domain | Use for a handoff that crosses blocks but stays on one device |
cuda::thread_scope_system | Threads in the system | Use when the CPU or a peer GPU must participate and the memory and device support that access |
If the scope is narrower than the communicating threads, even an atomic flag cannot provide the intended handoff. NVIDIA’s memory-model example uses device scope for a flag shared between different blocks; narrowing the producer’s flag operation to block scope creates a data race. System scope does not mean that every address is automatically atomic between CPU and GPU. Check the allocation type, device properties, and peer-atomic support in the CUDA C++ memory model and the cuda::atomic reference.
Sharing data within a block
Suppose one block copies 128 input values into shared memory and thread 0 adds them. The block needs both the participants to arrive and their writes to be ordered before the read. Each thread writes its value, then all threads reach __syncthreads(), after which thread 0 reads the tile.
__global__ void block_sum(const int* input, int* output, int n) {
__shared__ int tile[128];
int tid = threadIdx.x;
int i = blockIdx.x * blockDim.x + tid;
tile[tid] = (i < n) ? input[i] : 0;
__syncthreads();
if (tid == 0) {
int sum = 0;
for (int j = 0; j < blockDim.x; ++j) sum += tile[j];
output[blockIdx.x] = sum;
}
}This illustrative code assumes a block size of 128, matching the tile. Threads outside the input range in the last block write zero, so every participant reaches the same barrier. Putting the barrier inside if (i < n) would let some threads skip it and make synchronization invalid. A reduction with several stages also needs a barrier wherever a later stage reads values written by other threads.
__syncthreads() cannot be used as a grid-wide barrier for all blocks. If the only need is a phase boundary between blocks, two kernels enqueued in order in the same stream are often simpler. The stream queue executes operations in enqueue order, although the host launch call remains asynchronous; see CUDA asynchronous execution and streams. If cross-block synchronization must happen inside one kernel, use an API that explicitly supports the intended group and meet its launch conditions, such as those documented for Cooperative Groups.
Publishing data from one thread to another
A producer writing a payload for a consumer is a handoff protocol, not merely a reason to insert a fence. A common pattern is for the producer to write the payload and then perform a release store to an atomic flag. The consumer performs an acquire load on that same flag and reads the payload after it observes the release.
producer: write payload → ready.store(1, release)
│ observes the store on the same atomic flag
consumer: ready.load(acquire) → read payloadThe release and acquire must connect through the same atomic object, and both operations’ scope must include both threads. In CUDA C++, <cuda/atomic> types such as cuda::atomic and cuda::atomic_ref express the memory order and scope. For different blocks on one device, cuda::thread_scope_device may fit the relationship. A consumer that waits in a loop also needs a design that guarantees the producer can make progress; this diagram explains memory ordering and is not an instruction to paste an arbitrary block spin loop into a kernel.
Older code may use __threadfence() to order a producer’s earlier global-memory write before it increments a completion counter. NVIDIA’s last-block reduction example combines a partial-sum store, a device-scope fence, and an atomic counter. The fence alone does not announce completion: the counter carries that signal, and the example handles visibility of the partial sums separately. For new designs, consider the documented cuda::atomic_thread_fence when a fence is truly needed, and prefer explicit release/acquire semantics for ordinary data handoffs because the protocol is easier to review.
Common mistakes and how to correct them
| Symptom or assumption | Cause | Correction |
|---|---|---|
Expecting another block to read immediately after __threadfence() | Treating a fence as a barrier or completion signal | Add a synchronization method that the consumer waits on, or use a stream-ordered kernel phase boundary |
| The flag is 1 but the payload appears stale | A plain flag, different atomic objects, too-narrow scope, or a broken release/acquire link | Use the same atomic flag, verify that acquire observes the release, and choose a scope including the consumer |
Assuming volatile removes a data race | Volatile accesses do not replace atomic handoff or a memory-ordering protocol | Establish atomicity, ordering, and scope under the CUDA C++ memory model |
| Some block threads stop at a barrier | Some threads skipped a conditional barrier, or the warp mask does not match participants | Check that every non-exited block thread reaches the same barrier; for warp barriers, match the mask to active participants |
| It works on one GPU but fails with CPU or another GPU | A device-scope operation is used as if it were system-scope, or memory/device support is missing | Check system-scope rules, memory type, device capabilities, and peer-atomic requirements |
PTX fence and membar instructions describe low-level memory ordering and scope. Ordinary CUDA C++ code should use its documented C++ synchronization primitives; use inline PTX only when a justified low-level requirement has been validated. See the membar/fence section of NVIDIA PTX ISA 9.2.
A checklist for choosing correct synchronization
Identify who writes and reads each location, and whether the accesses can overlap.
Determine whether the threads are in one block, different blocks, or include the CPU or a peer GPU.
Ask whether participants must actually wait. If threads exchange data within a block, use a suitable barrier; if only a handoff is needed, consider a correctly scoped atomic release/acquire pair.
Verify that the scope of each atomic, fence, or barrier includes every participant. If the issue is completion of an asynchronous copy, use that copy’s completion primitive rather than substituting an ordinary thread barrier. See [CUDA asynchronous copies](https://knowledge.linkspot.net/en/articles/58b845ac-6287-450d-bfb6-54c03dadd77a) for that separate case.
Check branches and loops around each barrier. Confirm that all required participants reach the same phase. Test small inputs, partial tiles, and repeated runs, but do not treat passing tests as proof that synchronization is correct.
The practical rule is simple: choose an execution barrier when participants must wait together; choose a fence when the goal is to order one thread’s memory operations; and design an atomic handoff with the right scope when one thread publishes data for another. Recheck the official documentation for the toolkit and device you target.
References
[NVIDIA CUDA Programming Guide — C/C++ Language Extensions](https://docs.nvidia.com/cuda/cuda-programming-guide/05-appendices/cpp-language-extensions.html)
[NVIDIA CUDA Programming Guide — CUDA C++ Memory Model](https://docs.nvidia.com/cuda/cuda-programming-guide/05-appendices/cuda-cpp-memory-model.html)
[NVIDIA CUDA Programming Guide — Asynchronous Execution and Streams](https://docs.nvidia.com/cuda/cuda-programming-guide/02-basics/asynchronous-execution.html)
[NVIDIA CUDA Programming Guide — Cooperative Groups](https://docs.nvidia.com/cuda/cuda-programming-guide/04-special-topics/cooperative-groups.html)
[NVIDIA Parallel Thread Execution ISA 9.2](https://docs.nvidia.com/cuda/archive/13.2.2/parallel-thread-execution/index.html)
[NVIDIA CUDA Core Compute Libraries — `cuda::atomic`](https://nvidia.github.io/cccl/unstable/libcudacxx/extended_api/synchronization_primitives/atomic.html)
References
- NVIDIA CUDA Programming Guide — C/C++ Language Extensions
- NVIDIA CUDA Programming Guide — CUDA C++ Memory Model
- NVIDIA CUDA Programming Guide — Asynchronous Execution and Streams
- NVIDIA CUDA Programming Guide — Cooperative Groups
- NVIDIA Parallel Thread Execution ISA 9.2
- NVIDIA CUDA Core Compute Libraries — cuda::atomic