liveBook · Manning

__constant__

As explained in Grokking GPU Programming

__constant__ is a CUDA variable qualifier that places a variable in the device's constant memory address space.

__constant__ places a named variable in the GPU’s 64 KB read-only constant-memory region. Unlike const, which only promises that a value will not change, __constant__ selects hardware storage with a dedicated cache. When warp threads read the same address, one fetch can serve them all; reads from different addresses serialize. section 13.16 section 13.4

Use the three-step pattern:

  • Declare the variable at file scope with __constant__. section 13.4
  • Copy its value from host memory with cudaMemcpyToSymbol() before launching the kernel—not cudaMemcpy(). The value persists across launches until overwritten. section 13.16 section 13.12
  • Read the symbol directly in the kernel. Prefer it for small arrays under 64 KB with uniform reads; pass a single scalar as a kernel argument, and use const __restrict__ pointers for larger or per-thread-indexed data. section 13.16 section 13.11
__constant__ float SCALE;               // In GPU constant memory (64 KB region)
 
// In host code: copy to constant memory before launching kernel
float h_scale = 2.5f;
cudaMemcpyToSymbol(SCALE, &h_scale, sizeof(float)); // not cudaMemcpy
 
// In kernel: read SCALE directly - hardware broadcasts to all 32 threads
__global__ void kernel(float* data, int n) {
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    if (idx < n) data[idx] *= SCALE;  // One broadcast fetch serves all 32 threads
}

See the full pattern with a scale value: declare the symbol, copy it from the host, then read it in a kernel where threads share the broadcast value. section 13.15

How Grokking GPU Programming covers it

How deep it goes
Explains declaration, copying, broadcast behavior, capacity, and common mistakes without detailing hardware implementation.
Examples
Uses scale-factor array processing and small lookup tables to demonstrate uniform and indexed reads.
What it assumes
Assumes familiarity with CUDA host/device code and kernels; distinguishes the qualifier from ordinary const.
What you can do after
Operate constant memory by declaring a symbol, copying with cudaMemcpyToSymbol(), and reading it in a kernel.
How it connects
Contrasts const qualifiers and kernel arguments, and relates constant memory to global memory, caches, and warp broadcasts.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking GPU Programming. The section links point to the parts of the book it draws on.