liveBook · Manning

Const __restrict__ qualifiers

As explained in Grokking GPU Programming

A const __restrict__ pointer is a pointer declaration that marks its data as read-only and promises the referenced data is not aliased through another pointer.

A CUDA kernel can use `const __restrict__` on a pointer parameter to promise that its data is read-only and is not aliased through another pointer. This gives the compiler information it can use to choose a suitable load path. section 13.3

Marking a large input array

In `__global__ void kernel(const float* __restrict__ input, float* output)`, `input` is read-only and unaliased. This is a fit for a large read-only array or per-thread indexing; the compiler chooses the load path, which varies by GPU generation. section 13.3

Choose the read-only data approach to match how the array is used:

  • Use `const __restrict__` pointers for large arrays or per-thread indexing. section 13.16
  • Use `__constant__` for a small uniform array under 64 KB when every thread reads the same index. Use `__ldg(ptr)` explicitly only when the compiler cannot infer read-only status. section 13.3

How Grokking GPU Programming covers it

How deep it goes
Defines the read-only and non-aliasing promises, while leaving the exact load path to the compiler and GPU generation.
Examples
Shows a qualified `float` input pointer and recommends it for large read-only arrays or per-thread indexing.
What you can do after
Apply it via CUDA kernel pointer parameters to mark large read-only arrays and let the compiler choose their load path.
How it connects
Connects CUDA kernel arguments and constant memory with read-only loads and the explicit `__ldg` intrinsic.

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.