What Is a CUDA Thread Block? Cooperation and Safe Synchronization
A CUDA thread block is a group of threads arranged to work together in one kernel. Threads in the same block run on one SM, can exchange values through shared memory, and can wait at a block barrier. That cooperation stops at the block boundary. Blocks in the same grid have no guaranteed execution order, so a block barrier cannot synchronize the whole grid. [NVIDIA CUDA Programming Guide — Programming Model](https://docs.nvidia.com/cuda/cuda-programming-guide/01-introduction/programming-model.html)
What Is a CUDA Thread Block? Cooperation and Safe Synchronization
A CUDA thread block is a group of threads arranged to work together in one kernel. Threads in the same block run on one SM, can exchange values through shared memory, and can wait at a block barrier. That cooperation stops at the block boundary. Blocks in the same grid have no guaranteed execution order, so a block barrier cannot synchronize the whole grid. NVIDIA CUDA Programming Guide — Programming Model
What does a thread block group, and what does it guarantee?
When launching a kernel, you choose the block size along with the thread coordinates. The threads run the same kernel code but use their positions within the block to perform different work. Blocks can be one-, two-, or three-dimensional. The key distinction is not their coordinate shape; it is the scope in which threads can exchange data and synchronize. NVIDIA CUDA Programming Guide — Writing SIMT Kernels
| Unit | Meaning in CUDA | What code can rely on |
|---|---|---|
| Thread | One execution state and work position | Its own index distinguishes its work item |
| Thread block | A group of threads in one kernel launch | Threads share the block’s shared memory and can order work with a block barrier |
| Grid | All blocks started by one kernel launch | The basic CUDA model has no ordinary barrier that waits for every block |
| SM | A GPU hardware execution unit | A block runs on one SM, but the program does not choose where or when |
In NVIDIA’s CUDA model, threads in one block run on the same SM and can access that block’s shared memory. Blocks in a grid have no guaranteed order and may not run simultaneously. It is therefore safer to make each block compute an independent partial result. If blocks depend on one another, design a separate phase or use an explicitly supported synchronization mechanism. NVIDIA CUDA Programming Guide — Programming Model
When can a block read shared values?
Suppose six values, [1, 2, 3, 4, 5, 6], are divided among blocks of four threads to compute partial sums. The first block receives four valid values and sums to 10. In the second block, only 5 and 6 are valid; the other two threads write 0, so its sum is 11. Inside each block, after all threads write to shared memory and reach __syncthreads(), a thread can read the block’s values and compute the partial sum. NVIDIA CUDA Programming Guide — Writing SIMT Kernels
n = 6, blockDim.x = 4, gridDim.x = 2
block 0: [1, 2, 3, 4] → __syncthreads() → partial[0] = 10
block 1: [5, 6, 0, 0] → __syncthreads() → partial[1] = 11
10 + 11 = 21The following code is an explanatory first stage for blocks with exactly four threads each. Assume values and partial are arrays accessible to the GPU. Each block writes only its own four shared-memory slots. After the barrier, the first thread adds those values and stores one partial result. A separate stage must add the two partial results to obtain 21.
#include <cuda_runtime.h>
__global__ void sum_tile(const float* values, float* partial, int n) {
__shared__ float tile[4];
const int lane = threadIdx.x;
const int i = blockIdx.x * blockDim.x + lane;
tile[lane] = (i < n) ? values[i] : 0.0f;
__syncthreads();
if (lane == 0) {
float sum = 0.0f;
for (int k = 0; k < 4; ++k) {
sum += tile[k];
}
partial[blockIdx.x] = sum;
}
}
const int n = 6;
const dim3 block(4);
const dim3 grid((n + block.x - 1) / block.x);
sum_tile<<<grid, block>>>(values, partial, n);When n=6, partial contains 10 and 11. Combining them into 21 is a separate operation. __syncthreads() in one block does not wait for the other block to finish. This example explains the block’s coordination boundary; it is not a shared-memory optimization tutorial.
Where should the barrier go, and what mistakes should you avoid?
__syncthreads() does not pause only the thread that calls it. It waits until every thread in that block reaches the same barrier. It also orders shared-memory writes before later reads. Therefore, even if a tail thread has no valid input, it can write a neutral value such as zero and still participate in the barrier. NVIDIA CUDA Programming Guide — Writing SIMT Kernels
| Placement | What happens | Assessment |
|---|---|---|
| Every thread writes a valid value or zero, then all reach the barrier | Reads happen after all shared-memory slots are ready | Suitable for a block-local tile |
| Put the write and barrier inside an i < n condition | Some tail threads skip the barrier | Cannot guarantee every thread reaches the same barrier |
| Only thread 0 sums after the barrier | Thread 0 reads the prepared tile | Can produce one partial result per block |
| Wait for another block at this barrier | An ordinary block barrier does not observe another block | Use a separate kernel phase or another supported design |
In particular, do not put the barrier inside a condition like this:
if (i < n) {
tile[lane] = values[i];
__syncthreads();
}If only some threads pass the condition in the tail block, the others never reach the barrier. If the kernel appears stuck or progress is not guaranteed, first check whether the barrier is inside a conditional and whether every thread reaches the same call. Write a neutral value such as zero for invalid input positions so all block threads can pass the barrier.
How should you check block size and cooperation?
Adding more threads to a block does not always make it faster. More threads can increase the block’s register and shared-memory requirements, which may reduce how many blocks can reside on an SM at once. Higher occupancy also does not guarantee a proportional performance gain. CUDA C++ Best Practices Guide 13.5 gives 128–256 threads as an example starting range for experimentation; treat it as a heuristic and measure for the kernel’s resources and device. NVIDIA CUDA C++ Best Practices Guide 13.5 — Thread and Block Heuristics
Review new block cooperation code in this order:
**Choose data that requires cooperation.** Share block state only when several threads must read a common intermediate value or combine values they produced.
**Give each block an independent result.** In this example, block 0 writes only partial[0] and block 1 writes only partial[1]. Do not rely on another block’s order or result.
**Initialize every shared-memory slot.** Threads beyond the input range should write zero instead of reading memory. Allocate enough partial-output space for the grid.
**Check barrier participation.** Every thread in a block must reach the same __syncthreads() before shared values are read. If a barrier stalls, inspect data-dependent branches and early returns first.
**Compare a small result by hand.** For [1, 2, 3, 4, 5, 6], the block partial sums are 10 and 11, and the next-stage sum is 21. If values differ, check tail initialization, array bounds, and block output indices first.
The code is an illustrative CUDA C++ fragment for block scope and barriers; it was not compiled, run, or performance-tested on a GPU. The fixed block size of four is only to make the arithmetic easy to follow. For larger inputs, revisit tile size, output space, integer range, and device resource limits. NVIDIA CUDA Programming Guide — Programming Model
Sources
[NVIDIA CUDA Programming Guide — Programming Model](https://docs.nvidia.com/cuda/cuda-programming-guide/01-introduction/programming-model.html)
[NVIDIA CUDA Programming Guide — Writing SIMT Kernels](https://docs.nvidia.com/cuda/cuda-programming-guide/02-basics/writing-cuda-kernels.html)
[NVIDIA CUDA C++ Best Practices Guide 13.5 — Thread and Block Heuristics](https://docs.nvidia.com/cuda/cuda-c-best-practices-guide/)