__global

Address space qualifier.

__global global

Description

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.

As image objects are always allocated from the global address space, the __global or global qualifier should not be specified for image types. The elements of an image object cannot be directly accessed. Built-in functions to read from and write to an image object are provided.

Variables defined at program scope and static variables inside a function can also be declared in the global address space. They can be defined with any valid OpenCL C data type except for those in table 6.3. In particular, such program scope variables may be of any user-defined type, or a pointer to a user-defined type. In the presence of shared virtual memory, these pointers or pointer members should work as expected as long as they are shared virtual memory pointers and the referenced storage has been mapped appropriately. These variables in the global address space have the same lifetime as the program, and their values persist between calls to any of the kernels in the program. These variables are not shared across devices. They have distinct storage.

Program scope and static variables in the global address space may be initialized, but only with constant expressions.

The const qualifier can also be used with the __global qualifier to specify a read-only buffer memory object.

Notes

OpenCL implements the following disjoint named 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 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. The address space for a variable at program scope or a static variable inside a function can either be __global or __constant, but defaults to __global if not specified.

OpenCL 2.0 adds support for an unnamed generic address space. Pointers that are declared without pointing to a named address space point to the generic address space. Before referring to the region pointed to, the pointer must be associated with a named address space. Functions may be written with arguments and return values that point to the generic address space.

kernel function arguments declared to be a pointer or an array of a type must point to one of the named address spaces __global, __local or __constant.

The named address spaces are a subset of the generic address space except for the constant address space.

A pointer to address space A can only be assigned to a pointer to the same address space A or a pointer to the generic address space. Casting a pointer to address space A to a pointer to address space B is illegal if A and B are named address spaces and A is not the same as B.

The __global, __constant, __local, __private, __generic, global, constant, local, private and generic names are reserved for use as address space qualifiers and shall not be used otherwise.

The size of pointers to different address spaces may differ. It is not correct to assume that, for example, sizeof(__global int *) always equals sizeof(__local int *).

Examples

General qualifier examples follow:

// declares a pointer p in the private address space that // points to an object in address space global global int *p; void foo (...) { // declares an array of 4 floats in the private address space float x[4]; ... }

Example:

private int f() { ... } // should generate an error local int *f() { ... } // allowed local int * private f() { ... }; // should generate an error.
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 int foo; // OK. int foo; // OK. Declared in the global address space global uchar buf[512]; // OK. global int baz = 12; // OK. Initialization is allowed static global int bat; // OK. Internal linkage global uchar bigbuf[CL_DEVICE_MAX_GLOBAL_VARIABLE_SIZE]; // OK. static int foo; // OK. Declared in the global address space static global int foo; // OK. int *foo; // OK. foo is allocated in global address space. // pointer to foo in generic address space void func(...) { int *foo; // OK. foo is allocated in private address space. // foo points to a location in generic address space. ... } global int * global ptr; // OK. int * global ptr; // OK. constant int *global ptr=&baz; // error since baz is in global address // space. global int * constant ptr = &baz; // OK // Pointers work. Also, initialization to a constant known at // program load time global int *global baz_ptr = &baz; global image2d_t im; // Error. Invalid type for program scope // variables global event_t ev; // Error. Invalid type for program scope variables global int *bad_ptr; // Error. No implicit address space

Specification

OpenCL Specification

Also see

__local, __constant, __private, qualifiers

Copyright © 2007-2013 The Khronos Group Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and/or associated documentation files (the "Materials"), to deal in the Materials without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Materials, and to permit persons to whom the Materials are furnished to do so, subject to the condition that this copyright notice and permission notice shall be included in all copies or substantial portions of the Materials.