How to Use CUDA Thread Block Clusters: Configure DSM Launches and Synchronization
A CUDA thread block cluster lets neighboring blocks cooperate and access each block’s shared memory as distributed shared memory (DSM). Clusters are optional and require compute capability 9.0 or later. The practical work is to choose a supported cluster shape, launch it with the right per-block memory allocation, and use cluster barriers to protect DSM’s lifetime. Clusters do not automatically make a kernel faster, so use them only when the data flow needs block-to-block cooperation and compare both correctness and total runtime.
How to Use CUDA Thread Block Clusters: Configure DSM Launches and Synchronization
A CUDA thread block cluster lets neighboring blocks cooperate and access each block’s shared memory as distributed shared memory (DSM). Clusters are optional and require compute capability 9.0 or later. The practical work is to choose a supported cluster shape, launch it with the right per-block memory allocation, and use cluster barriers to protect DSM’s lifetime. Clusters do not automatically make a kernel faster, so use them only when the data flow needs block-to-block cooperation and compare both correctness and total runtime.
What cooperation does a cluster add?
Threads in an ordinary CUDA block run on one SM, share that block’s shared memory, and can synchronize at a block barrier. CUDA does not guarantee the execution order of unrelated blocks, so a block in an ordinary grid cannot assume another block has started or wait for its result. A cluster extends this boundary in a limited way: CUDA schedules its neighboring blocks together within one GPC, and the CUDA C++ Cooperative Groups API lets those blocks synchronize and access participating blocks’ shared memory.
DSM is an addressable view across the separate shared-memory regions allocated to blocks in one cluster. The allocation remains per block; the conceptual total available across the cluster is number of blocks × shared memory per block. DSM is not one physically contiguous allocation, and a cluster is not a grid-wide synchronization mechanism. The CUDA Programming Guide’s programming model describes this scheduling boundary. The Cooperative Groups functionality discussed here is the C++ API, so the examples use CUDA C++.
Adjacent grid blocks → one thread block cluster
[block 0: shared memory] ⇄ [block 1: shared memory] ⇄ [block 2: shared memory]
select another block's region with cluster.map_shared_rank()
initialize each region → cluster.sync() → remote read/write/atomic
→ cluster.sync() → blocks may exitIf one block can process the data and there is no cross-block communication, an ordinary block is simpler. Consider DSM when multiple blocks must exchange data within a bounded group or need a larger shared working set than one block can provide. If the algorithm needs a barrier across the whole grid, a cluster is not a substitute for that global coordination.
How do you choose a cluster size and launch it?
The NVIDIA CUDA Programming Guide 13.2 and CUDA Runtime API 13.5 documentation were checked on 2026-11-22. Recheck support against the installed toolkit, driver, and device. CUDA C++ offers a fixed compile-time cluster shape and a runtime-configurable launch:
| Method | Configuration | When it fits and its constraint |
|---|---|---|
Compile-time __cluster_dims__(x, y, z) | Set the cluster shape on the kernel and launch with ordinary <<<grid, block>>> syntax | Simple when the shape never changes. The launch cannot override the compile-time size. |
Runtime cudaLaunchKernelEx() | Add cudaLaunchAttributeClusterDimension to the launch configuration | Useful when the input or algorithm changes the shape. Check device support and the launch configuration. |
Do not infer support from compute capability alone. Check the current device’s cluster_supported field through CUDA C++ cuda::arch_traits. The guide describes 8 blocks as the default portable maximum, but smaller GPUs or MIG configurations may support less; some architectures can support larger, non-portable sizes. cudaOccupancyMaxPotentialClusterSize() provides a kernel-specific candidate ceiling. By default, the API returns a portable size; if non-portable sizes are enabled, it may return a larger device-specific value. This is an allowed candidate, not a performance optimum. cudaOccupancyMaxActiveClusters() estimates how many clusters can coexist for a launch configuration, but the runtime may schedule them differently, so the estimate is not guaranteed to be achieved. See the CUDA Runtime API occupancy reference.
The central fields of a runtime-configured launch look like this. This excerpt shows the API fields; fill in the actual kernel arguments and error handling for your application.
cudaLaunchConfig_t config = {};
config.gridDim = grid; // total thread blocks
config.blockDim = block;
config.dynamicSmemBytes = bytesPerBlock; // allocation per block
cudaLaunchAttribute clusterAttr = {};
clusterAttr.id = cudaLaunchAttributeClusterDimension;
clusterAttr.val.clusterDim.x = clusterBlocks;
clusterAttr.val.clusterDim.y = 1;
clusterAttr.val.clusterDim.z = 1;
config.numAttrs = 1;
config.attrs = &clusterAttr;
cudaLaunchKernelEx(&config, kernel, args...);gridDim is the total number of blocks, not the number of clusters. Make the grid shape consistent with the cluster shape, and check that dynamic shared memory per block stays within the device’s opt-in limit. Allocations above 48 KB per block generally require an explicit opt-in through cudaFuncSetAttribute(). The launch field is still the per-block allocation; calculate the cluster’s DSM capacity by multiplying it by the number of blocks.
How do you protect DSM access and data lifetime?
Inside the kernel, obtain the group with Cooperative Groups cg::this_cluster(). To address a remote block’s shared memory, pass the local shared-memory pointer and the destination block’s cluster rank to cluster.map_shared_rank(). The resulting pointer can be used for remote reads, writes, or atomic operations.
Two synchronization points matter. The first cluster.sync() makes sure every participating block has started and initialized its shared memory before remote access begins. A second barrier after the DSM operations prevents one block from exiting while another still accesses its memory. All participating blocks must reach the same cluster barrier; do not let a conditional or early return exclude some of them. The CUDA guide’s DSM section explains these lifetime requirements.
namespace cg = cooperative_groups;
cg::cluster_group cluster = cg::this_cluster();
int rank = cluster.block_rank();
initialize_local_shared_memory();
cluster.sync(); // all cluster blocks are ready and exist concurrently
int* remote = cluster.map_shared_rank(localShared, destinationRank);
atomicAdd(remote + offset, value);
cluster.sync(); // finish remote accesses before any block exitsThis is a synchronization excerpt, not a complete kernel. Check that destinationRank is a valid rank in the cluster and offset stays inside that block’s allocation. Use atomics or a partitioning rule appropriate to the data when multiple threads may update the same location. __syncthreads() synchronizes only one block and cannot replace a cluster barrier.
When is the documented histogram example useful?
The NVIDIA CUDA Programming Guide uses a histogram whose bins no longer fit in one block’s shared memory as a DSM example. Each block in a cluster holds a portion of the bins in its own shared-memory region. Threads map an input value to a destination block and offset, update that remote region atomically, synchronize, and then combine the partial results in global memory. A small histogram that fits in one block can use cluster size 1; DSM is an intermediate option for larger bin sets. This is a documented design example, not a measured speedup.
When adapting the pattern, ensure the bin partition covers every bin, every computed destination rank stays inside the cluster, and remainder bins and input boundaries are handled. A larger cluster can reduce the shared-memory allocation per block, but it can also change atomic contention, synchronization cost, and the number of resident clusters. Do not assume it beats global atomics or multiple kernel stages.
Preflight, verify results, and recover from failures
**Decide whether cluster cooperation is needed.** Keep an ordinary grid for independent block work. Identify the actual data that must cross block boundaries before introducing DSM.
**Check device and kernel support.** Verify `cluster_supported`, the opt-in shared-memory limit, and whether the kernel has a fixed compile-time cluster shape. If unsupported, use a tested fallback based on ordinary shared memory, global memory, or another kernel stage.
**Calculate size and memory budgets.** Use `cudaOccupancyMaxPotentialClusterSize()` as a candidate ceiling and consider `cudaOccupancyMaxActiveClusters()` alongside actual per-block resource use. Match the dynamic shared-memory allocation to the data partition, then set the cluster launch attribute.
**Check synchronization and address bounds.** Confirm the first cluster barrier after initialization, the remote DSM operations, and the second barrier before exit. Ensure every participating block reaches each barrier and ranks, offsets, and tail inputs remain in range.
**Compare correctness and total performance.** Compare with the existing path on small and boundary inputs. Measure atomics, memory use, kernel time, and total application time. Feature support alone does not imply a speedup.
| Symptom | Check first | Safe response |
|---|---|---|
| Device reports cluster unsupported | cluster_supported, compute capability, installed CUDA runtime/driver | Skip the cluster launch and use the verified ordinary path |
| Cluster size or launch configuration is rejected | Kernel-specific size limit, fixed cluster attribute, grid and shared-memory settings | Reduce to a supported size and recheck the per-block opt-in limit |
| Wrong result or invalid remote address | Destination rank/offset, remainder-bin partition, initialization and atomic use | Initialize every region, fix bounds handling, and recheck with a small input |
| Kernel hangs or fails intermittently | Early exits, missing cluster barriers, memory lifetime during remote access | Make every block reach the barriers and wait for remote work before exit |
A cluster expands the scope of block cooperation; it does not promise better performance. Check support and resource limits on the target device, preserve DSM’s initialize–access–finish order, and adopt the cluster only after comparing correct results and total runtime with the existing implementation.
Sources
[NVIDIA CUDA Programming Guide — Programming Model: Thread Block Clusters](https://docs.nvidia.com/cuda/cuda-programming-guide/01-introduction/programming-model.html)
[NVIDIA CUDA Programming Guide — Writing SIMT Kernels: Distributed Shared Memory](https://docs.nvidia.com/cuda/cuda-programming-guide/02-basics/writing-cuda-kernels.html)
[NVIDIA CUDA Runtime API Reference — Occupancy and cluster-size queries](https://docs.nvidia.com/cuda/cuda-runtime-api/cuda_runtime_api/group__CUDART__OCCUPANCY.html)