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.
Function arguments of type
image2d_t,
image3d_t,
image2d_array_t,
image1d_t,
image1d_buffer_t, and
image1d_array_t
refer to image memory objects allocated in the __global
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 readonly variables can be accessed
by all (global) work-items of the kernel during its execution. Pointers to the
__constant
address space are allowed as arguments to functions
(including kernel functions) and for variables declared inside functions.
All string literal storage shall be in the __constant
address space.
NOTE: Each argument to a kernel that is a pointer to the __constant
address space is counted separately towards the maximum number of such arguments,
defined as CL_DEVICE_MAX_CONSTANT_ARGS
are described in the
table for clGetDeviceInfo.
Variables in the program scope or the outermost scope of kernel functions can be
declared in the __constant
address space. These variables are
required to be initialized and the values used to initialize these variables must be
a compile time constant. Writing to such a variable results in a compile-time error.
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 allocated in the __constant
address space counts as a separate constant argument.
There is no generic address space name for program scope variables. All program scope
variables must be declared in the __constant
address space. For example:
// 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]; |
There is no address space for function return values. Using an address space qualifier in a function return type declaration will generate a compilation error, unless the return type is declared as a pointer type and the qualifier is used on the points-to address space.
For example:
__private int f() { ... } // should generate an error __local int *f() { ... } // allowed __local int * __private f() { ... }; // should generate an error. |