OpenCL implements the following disjoint address spaces: __global,
__local
,
__constant and __private.
The address space qualifier may be used in variable declarations to specify the region of memory that is used to allocate the object. The C syntax for type qualifiers is extended in OpenCL to include an address space name as a valid type qualifier. If the type of an object is qualified by an address space name, the object is allocated in the specified address name; otherwise, the object is allocated in the generic address space.
The address space names without the __ prefix i.e. global
, local
, constant
and private
may be substituted for the corresponding address space names with the __ prefix.
The generic address space name for arguments to a function in a program, or local variables of a function is __private
. All function arguments shall be in the __private
address space.
__kernel function arguments declared to be a pointer of a type can point to one of the following address spaces only: __global
, __local
or __constant
. A pointer to address space A can only be assigned to a pointer to the same address space A. Casting a pointer to address space A to a pointer to address space B is illegal.
The __kernel
function arguments declared to be of type image2d_t or image3d_t
always point to the __global
address space.
There is no generic address space name for program scope variables. All program scope variables must be declared in the __constant
address space.
The __constant
or constant
address space name is used to describe variables allocated in
global memory and which are accessed inside a kernel(s) as read-only variables. These read-
only variables can be accessed by all (global) work-items of the kernel during its execution. Pointers to the __constant
address space are allowed arguments to functions (including __kernel
functions) and for variables declared inside functions. Variables allocated in the __constant
address space can only be defined as program scope variables and are required to be initialized.
Writes to variables declared with the __constant
address space qualifier in the OpenCL program source should be a compile-time error.
All string literal storage shall be in the __constant
address space.
NOTE: Any argument to a kernel that is declared with the __constant
address space qualifier counts as a separate constant argument. The relevant device capabilities CL_DEVICE_MAX_CONSTANT_ARGS and CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE are described in the table for clGetDeviceInfo.
Variables inside a function or in program scope can also be declared with the __constant
address qualifier. Implementations are not required to aggregate these declarations into the fewest number of constant arguments. This behavior is implementation defined.
Thus portable code must conservatively assume that each variable declared inside a function or in program scope with the __constant
qualifier counts as a separate constant argument.
The __local
or local
address space name is
used to describe variables that need to be allocated in local memory and are shared by
all work-items of a work-group. Pointers to the __local
address
space are allowed as arguments to functions (including __kernel
functions). Variables allocated in the __local
address space can
also be defined inside a __kernel
function only.
// Declares a pointer p in the __private address space that // points to an int object in address space __global __global int *p; // Declares an array of 4 floats in the __private address space. float x[4]; // Examples of variables allocated in the __local address space // inside a __kernel function __kernel void my_func(...) { __local float a; // A single float allocated // in local address space __local float b[10]; // An array of 10 floats // allocated in local address space. } |
Variables allocated in the __local
address space inside a __kernel
function cannot be initialized.
__kernel void my_func(...)
{
local float a = 1; // not allowed
__local float b;
b = 1; // allowed
}
NOTE: Variables allocated in the __local
address space inside a __kernel
function are allocated for each work-group executing the kernel.