What is CUDA?

NVIDIA's platform for general-purpose computing on its GPUs. Kernels are written in an extended C++, with parallelism expressed as a grid, blocks and threads.

Field: GPU Programming

Japanese name: CUDA

GPU thread hierarchy and assignment to SMs Grid B0 B1 B2 B3 B4 B5 Block Warp 0 Warp 1 Warp 2 Warp (32 threads) Same instruction in lockstep Threads grouped by 32 Each block goes to an SM SM0 B0 B3 Shared memory SM1 B1 B4 Shared memory SM2 B2 B5 Shared memory Global memory (read/write from all blocks) A block runs on one SM and cooperates via shared memory
GPU thread hierarchy and assignment to SMs · View in diagrams

Kernels and the thread hierarchy

In CUDA, the CPU (host) launches a kernel, a function that many GPU threads execute at once. One launch creates a grid of blocks, and each block contains many threads. Each thread computes a unique global index, typically i = blockIdx.x × blockDim.x + threadIdx.x, to choose its data element: thread 5 of block 2 with 256 threads per block handles element 517. Because threads are launched in whole blocks, the last block usually has surplus threads, so kernels guard memory accesses with if (i < n) to avoid writing past the array.

How blocks map onto the GPU

Each block is assigned to one streaming multiprocessor (SM) and runs there to completion, and an SM can hold several blocks at once. Inside, threads execute in warps of 32. Occupancy, the ratio of resident warps to the SM's maximum, is limited by registers per thread, shared memory per block and block size; low occupancy leaves too few warps to hide memory latency. Blocks run independently and in any order, so waiting inside a kernel for all other blocks can deadlock; global synchronization is normally obtained by splitting the work into two kernel launches.

Memory spaces

Global memory is the GPU's DRAM: high bandwidth, high latency, and fastest when adjacent threads access adjacent addresses. Shared memory is a small on-chip memory per block, managed explicitly by the program. It is divided into 32 banks, and threads of a warp hitting different addresses in the same bank are serialized. __syncthreads() makes every thread of a block wait until all arrive; placing it inside a branch that some threads skip causes hangs or undefined results. Unified memory lets host and device share one pointer, with pages migrated on demand.

Keeping the GPU busy

Host-to-device copies cross PCI Express and are often the bottleneck. Streams, ordered queues of transfers and kernels, let work in different streams overlap, so the next chunk of data can be copied while the current one is computed. Each kernel launch also carries a fixed overhead, so programs that launch thousands of tiny kernels benefit from fusing them, which also avoids writing intermediate results to memory, or from submitting recorded launch sequences at once with CUDA Graphs. CUDA itself runs only on NVIDIA GPUs; cross-vendor alternatives such as OpenCL follow a similar kernel-and-work-group model.

Try it: questions about CUDA

In GPU programming such as CUDA, what is a "kernel"?

  1. AThe core of the GPU's operating system
  2. BA function that many threads execute simultaneously on the GPU
  3. CA component that cools the GPU chip
Show answer and explanation

Answer: B. A function that many threads execute simultaneously on the GPU

In GPU programming a kernel is a function launched from the CPU-side program in which many GPU threads execute the same code. Each thread uses its own index to choose which data element to handle. For array addition, for example, the kernel is written so that each thread adds one element, and as many threads are launched as there are elements. The word is the same as for an OS kernel, but the meaning differs.

Which correctly orders the CUDA thread hierarchy from largest to smallest?

  1. AGrid → block → thread
  2. BThread → block → grid
  3. CBlock → thread → grid
Show answer and explanation

Answer: A. Grid → block → thread

One kernel launch forms a grid, which is divided into many blocks, each containing many threads. A block's threads run on the same SM (a cluster of execution units) and can cooperate through shared memory and synchronize. Different blocks run independently, so the number running at once adapts to the GPU's size, and the same program runs on small and large GPUs.

FAQ

What is the difference between a CUDA block and a warp?

A block is a programmer-chosen group of threads that runs on one SM and can share memory and synchronize. A warp is the hardware's group of 32 threads within a block that execute instructions together in lockstep.

How do I calculate the global thread index in CUDA?

For a one-dimensional launch, use i = blockIdx.x * blockDim.x + threadIdx.x. Check i < n before accessing arrays, because the last block often contains more threads than remaining elements.

What is CUDA shared memory used for?

It is fast on-chip memory shared by the threads of one block, used to stage data from global memory for reuse and to exchange intermediate results. Unlike a cache, the program decides what goes into it, and __syncthreads() orders accesses.

Questions that mention CUDA (16)

CUDA appears in 18 practice questions across 1 topic, including GPU Programming.

  1. A CUDA kernel computes each thread's index i with the expression above. What is i for the thread with blockIdx.x = 2, blockDim.x = 256 and threadIdx.x = 5?
  2. Why does the CUDA kernel above include "if (i < n)"?
  3. What does CUDA's __syncthreads() do?
  4. In CUDA, __syncthreads() is placed inside "if (threadIdx.x < 16)" so that only some threads reach it. What can happen?
  5. In CUDA, what is the relationship between blocks and SMs (streaming multiprocessors)?
  6. In CUDA, you want to wait mid-kernel for every other block to finish before moving to the next phase. What is the usual approach?
  7. What is the main purpose of using multiple CUDA streams?
  8. Which describes CUDA unified memory?
  9. What is a common guideline for choosing the number of threads per block in CUDA?
  10. For what kind of access does CUDA constant memory perform especially well?
  11. In CUDA, what generally happens right after the CPU-side program launches a kernel?
  12. In CUDA, how does the CPU-side program receive the results computed by a GPU kernel (a function marked __global__)?
  13. On a GPU whose limit is 1024 threads per thread block, a CUDA kernel is launched with blockDim.x set to 2048. What generally happens?
  14. Which describes occupancy on a GPU?
  15. A program that launches a very small kernel thousands of times in a row is slow. What is the most suitable improvement?

Books on GPU Programming Ad

As an Amazon Associate I earn from qualifying purchases.

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary