__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
