What is GPU kernel?

A function that many threads execute simultaneously on a GPU. It is launched from the CPU-side program, and each thread uses its index to choose its data. Distinct from an OS kernel.

Field: GPU Programming

Japanese name: GPUカーネル

How it differs from an ordinary function

An ordinary function runs once each time it is called. A GPU kernel is run by many threads at the same time from a single launch, all executing the same code. The programmer writes the kernel as the work of one thread. In CUDA, a function is marked as a kernel with the __global__ specifier. OpenCL also uses the word kernel for a function that executes on a device.

How each thread picks its work

Each thread is given a unique index that it can read inside the kernel. Because every thread runs the same code, it uses this index to choose its data. For an array addition, thread i adds element i. In CUDA, threads are launched in whole blocks. To process 1000 elements with blocks of 256 threads, 1000 ÷ 256 is rounded up to 4 blocks. That gives 4 × 256 = 1024 threads, so the kernel checks the index to make the 24 surplus threads do nothing.

Relationship with the host

A kernel is launched by the program on the CPU side, called the host. In CUDA the launch is asynchronous: the host moves on without waiting for the kernel to finish. Before using the results, the host must synchronize and wait for completion. A CUDA kernel cannot return a value. It writes its results to arrays in GPU memory, and the host obtains them later, for example by copying them back.

Try it: questions about GPU kernel

What can happen if a GPU kernel uses too many registers per thread?

  1. ARegisters grow automatically and performance always improves
  2. BResults are always wrong
  3. CEach SM has a fixed register file, so fewer threads fit at once and memory latency becomes harder to hide
Show answer and explanation

Answer: C. Each SM has a fixed register file, so fewer threads fit at once and memory latency becomes harder to hide

GPUs keep the registers of every active thread on chip so that switching warps is instantaneous. So if each thread uses many registers, fewer threads fit on an SM at once. Registers that do not fit are spilled to memory, which can also slow things down. Compiler options can cap register usage to balance it against thread count.

In CUDA, how does the CPU-side program receive the results computed by a GPU kernel (a function marked __global__)?

  1. AThrough the kernel's return value, which sends the results of all threads back to the CPU together
  2. BThe kernel writes its results to an array in GPU memory, and after the kernel finishes the CPU side copies that array to CPU memory (or reads unified memory)
  3. CThe CPU-side program reads the values each thread printed with printf as variables
Show answer and explanation

Answer: B. The kernel writes its results to an array in GPU memory, and after the kernel finishes the CPU side copies that array to CPU memory (or reads unified memory)

A GPU kernel is executed by many threads at once, so it cannot return its results as a single value, and in CUDA a __global__ function must have a void return type. The CPU side allocates an array in GPU memory and passes its pointer as an argument, and each thread writes its result to the element it handles. Because kernel launches are asynchronous, the CPU side copies the array back with cudaMemcpy or similar after the kernel has finished (in the same stream, the copy waits for the preceding kernel to finish). printf inside a kernel is a debugging aid for displaying output, and the program cannot read those values back.

FAQ

Is it related to an operating system kernel?

No. An OS kernel is the central program of an operating system. A GPU kernel is a function executed in parallel on a GPU; the same word is used with a different meaning.

Where is the number of threads that run a kernel decided?

The host program that launches the kernel decides it. In CUDA, the launch specifies the number of blocks and the number of threads per block. In OpenCL, it is given as the size of an index space.

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