How to use Constant memory on GPU

A simple way to use constant memory on gpu by using __constant__ directive of Cuda. First, we define a static variable (for instance an array) on GPU in the following way:

__device__  __constant__ float constantArray[SIZE];

where SIZE represents the number of the constantArray’s elements. Second, we declare a standard array, for example, like this:

float myArray [SIZE] = { 2.0, ..., 10.0 };

Now, we can initialize the constant array by using the following function:

cudaError_t cudaMemcpyToSymbol(constantArray, myArray, sizeof(float)*SIZE);

where cudaError_t is the value returned by the function. So, we able to verify if the function was performed correctly by checking the value returned by the function.