F.4 Program scope variables

In OpenCL 2.0, you can use global variables, known as program scope variables, to store data shared between different kernels and multiple invocations of the same kernel.

Program scope variables are analogous to global variables in C or C++ programming, with similar benefits and drawbacks. The lifetime of the global variables corresponds to the lifetime of the program that defines the variables. The use of global variables in OpenCL is transparent to the host-side code.

The host code must enable CL2.0 when compiling with -cl-std=CL2.0, when building the OpenCL program. For example:

cl_program program = clCreateProgramWithSource(context, 1, &source, NULL, &err);
err = clBuildProgram(program, 0, NULL, "-cl-std=CL2.0", NULL, NULL);

The following code sample shows how to use a global variable:

__global int my_global;

__kernel void kernel_one(int x)
	my_global = x;

__kernel void kernel_two(__global int *the_answer)
	*the_answer = my_global;

For more information, see the OpenCL C specification V2.0, section 6.5 Address space qualifiers.

Non-ConfidentialPDF file icon PDF version101574_0302_00_en
Copyright © 2019 Arm Limited or its affiliates. All rights reserved.