What Is a CUDA Thread? Turn Execution Indices into Data Work
A CUDA thread is a logical execution unit that runs part of a GPU kernel. Its coordinates identify the data it should process, so many threads can run the same kernel code on different items. A CUDA thread is not permanently paired with one CUDA core. This guide explains how threads map to array or image data and when one thread can handle several items. [NVIDIA CUDA Programming Guide — Writing SIMT Kernels](https://docs.nvidia.com/cuda/cuda-programming-guide/02-basics/writing-cuda-kernels.html)
What Is a CUDA Thread? Turn Execution Indices into Data Work
A CUDA thread is a logical execution unit that runs part of a GPU kernel. Its coordinates identify the data it should process, so many threads can run the same kernel code on different items. A CUDA thread is not permanently paired with one CUDA core. This guide explains how threads map to array or image data and when one thread can handle several items. NVIDIA CUDA Programming Guide — Writing SIMT Kernels
What does a CUDA thread represent?
A kernel is a function that runs on the GPU, and a launch starts a set of threads to execute its code. In the CUDA programming model, a thread is the basic unit of parallel work. Each thread has its own identity, state, and control flow. The same kernel can therefore act on different data selected by each thread. NVIDIA CUDA Programming Guide — Programming Model
A thread is a software execution model. NVIDIA documents blocks being assigned to an SM and notes that physical hardware layout and execution can vary by GPU. Do not assume that one CUDA thread continuously occupies one CUDA core, or estimate performance by dividing thread count by core count. The compiler may keep a thread's private values in registers. If a kernel needs too many registers or uses large temporary values, some thread-local values may be placed in local memory, which can physically reside in device memory. “Local” describes its scope; it does not promise on-chip storage. NVIDIA CUDA Programming Guide — Writing SIMT Kernels
| View | Meaning for a CUDA thread | Coding implication |
|---|---|---|
| Execution unit | Runs kernel instructions with its own state and coordinates | Same code can process different data by coordinate |
| Hardware placement | A block runs on one SM; the GPU manages thread execution | Do not assume one thread is fixed to one core |
| Thread-private values | Registers and thread-local variables may be used | Check register cost and memory placement for large temporary values |
How do coordinates become data positions?
CUDA provides threadIdx for a thread's position inside its block and blockIdx for the block's position in the grid. blockDim is the block size and gridDim is the grid size. In one-dimensional work, these values form a global thread number that can serve as an array index.
i = blockIdx.x × blockDim.x + threadIdx.x
x = blockIdx.x × blockDim.x + threadIdx.x
y = blockIdx.y × blockDim.y + threadIdx.y
index = y × width + x#include <cuda_runtime.h>
#include <cstddef>
__global__ void double_pixels(
const float* input, float* output, int width, int height) {
const int x = blockIdx.x * blockDim.x + threadIdx.x;
const int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
const std::size_t index =
static_cast<std::size_t>(y) * width + x;
output[index] = 2.0f * input[index];
}
}
const int width = 5;
const int height = 3;
const dim3 block(4, 2);
const dim3 grid(
(width + block.x - 1) / block.x,
(height + block.y - 1) / block.y);
double_pixels<<<grid, block>>>(input, output, width, height);For example, if an image is 5 pixels wide and 3 pixels high, blocks of 4 by 2 threads require a grid of 2 by 2 blocks. The launch starts 32 threads, but the image has only 15 pixels. Some coordinates in the last blocks fall outside the image, so check x < width and y < height before every memory access. CUDA supports one-, two-, and three-dimensional coordinates, which can make images and matrices easier to describe. NVIDIA CUDA Programming Guide — Writing SIMT Kernels
The example doubles each valid pixel. Assume input and output are buffers already accessible to the GPU. The launch setup is for a 5-by-3 image; this snippet omits allocation and the surrounding host program, so it is not a standalone executable.
An x coordinate that increases fastest often gives neighboring threads neighboring pixels in a row-major array. This can help group global-memory requests, depending on the layout and access pattern. Correct coordinates do not guarantee a fast kernel; measure performance on the target GPU and input. NVIDIA CUDA Programming Guide — Writing SIMT Kernels
Must one thread handle only one data item?
No. One thread per item is a clear starting point when each item is independent. If the data is larger or you want to control the number of blocks, one thread can process several items in a grid-stride loop. Its first position is its unique global thread index; each next position advances by the total number of threads in the grid. With eight total threads, thread 0 handles items 0, 8, and 16, while thread 7 handles 7 and 15. NVIDIA Technical Blog — An Even Easier Introduction to CUDA (Updated)
#include <cstddef>
__global__ void double_array(float* values, std::size_t n) {
const std::size_t first =
static_cast<std::size_t>(blockIdx.x) * blockDim.x + threadIdx.x;
const std::size_t stride =
static_cast<std::size_t>(blockDim.x) * gridDim.x;
for (std::size_t i = first; i < n; i += stride) {
values[i] *= 2.0f;
}
}A grid-stride loop separates the data length from the launch thread count and lets each thread revisit a regular sequence of positions. It is not automatically faster than one thread per item. Large differences in loop counts or item processing time can leave some threads waiting. Use the following trade-offs to choose a first version.
| Mapping | Useful when | Cost and check |
|---|---|---|
| One item per thread | Items are independent and a direct partition is easiest | Check the final threads with a bounds condition |
| Several items per thread | You want grid size independent of data length or each thread to loop over work | Compare loop cost and work imbalance on the actual input |
NVIDIA's grid-stride example shows how to process larger arrays with a controlled grid. Choosing a block count as a multiple of the SM count is an example-specific option, not a rule for every GPU and kernel. Do not assume a smaller grid is faster; consider total grid size, work per thread, and memory movement. NVIDIA Technical Blog — An Even Easier Introduction to CUDA (Updated)
How can you check a thread-to-data mapping?
Use this sequence to catch index mistakes and out-of-range access in a new kernel.
**Choose the unit of work.** In this example, one thread calculates one image pixel. If one item writes several outputs, decide which thread owns each output.
**Write down each coordinate.** For a row-major image, x is the column and y is the row. Calculate a small grid and check that two threads do not point to the same pixel.
**Guard memory access.** A launch can include coordinates outside the image when its width or height is not a multiple of the block size. Read and write only inside the bounds condition.
**Choose work per thread.** Decide between one item and a grid-stride loop, and use an integer type wide enough for the array length and stride. For large arrays, check that dimensions and the linear index cannot overflow.
**Check a small result, then measure performance separately.** For the 5-by-3 input, all 15 valid outputs should equal twice their inputs; outside coordinates must not access memory. After correctness, measure both kernel time and total application time on representative inputs.
These examples explain CUDA C++ thread coordinates and data mapping. They were not compiled or run on a GPU in this writing session. No block size is claimed to be optimal and no speedup is claimed. Thread, block, and grid are programming abstractions; actual efficiency depends on GPU architecture, memory access, and work per thread. 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 Technical Blog — An Even Easier Introduction to CUDA (Updated)](https://developer.nvidia.com/blog/even-easier-introduction-cuda)