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 arguments to a __kernel
function 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. All program scope variables must be declared in the __constant
address space.
The __global
or global
address space
name is used to refer to memory objects (buffer or
image objects) allocated from the global memory pool.
A buffer memory object can be declared as a pointer to a scalar, vector or user-defined struct. This allows the kernel to read and/or write any location in the buffer.
The actual size of the array memory object is determined when the memory object is allocated via appropriate API calls in the host code.
If an image memory object is attached to an argument declared with this qualifier, the argument must be declared as type image2d_t for a 2D image memory object or as type image3d_t for a 3D image memory object. The elements of an image memory object cannot be directly accessed. Built-in functions to read from and write to an image memory object are provided.
The const qualifier can also be used with the __global
qualifier to specify a read-only buffer memory object.
// 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]; |
__global float4 *color; // An array of float4 elements typedef struct { float a[3]; int b[2]; } foo_t; __global foo_t *my_info; // An array of foo_t elements. __global image2d_t texture; // A 2D texture image |