cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

gopal_hc
Journeyman III

__global vs __constant qualifier in OpenCL

Jump to solution

I want an array variable to have a program scope.

One way I can do this by passing it as a function pointer throughout the program, which might be complex when we have multiple functions reading/writing this array variable.

Second way to do this, is to have a global variable having program scope. As per the OpenCL specification, Global variables are declared in the program source with the __constant qualifier and are accessed as read-only variables.

//I am writing one sample program to demonstrate my problem:

__constant uint arr[2] = {0, 0};  // an array of unsigned integer

void func1 (uint tmp)

{

         for(int i = 0; i < 2; i ++)

               arr = tmp+i;

}

void func2(uint tmp)

{

         for(int i = 0; i < 2; i++)

               tmp = arr;

}

__kernel void demoKernel(__global uint *input,

                                           __global uint *output)

{

        uint index = get_global_id(0);

        func1(input[index]);

        func2(output[index]);

}

when i compiled this i got the following error:

tmp/OCLrnlEIO.cl", line 5: error: expression must be a modifiable lvalue

                       arr = tmp+i;

I searched in google for this error, i found that it is because of type of "arr" is array of 2 length (it is not a pointer).

So my questions are:

1.  What is reason for this error and How I can fix this ?

2.  secondly, my requirement is to not only to read the array, but also write on it, so how should I use __constant qualifier for that which is read-only variable?

Thanks in Advance !!

0 Likes
1 Solution

Accepted Solutions
himanshu_gautam
Grandmaster

Re: __global vs __constant qualifier in OpenCL

Jump to solution

Hi

Constant varialbe can not be modified by external sources.

In your program arr is declared as constant uint and in the function func1() you are trying to modify it value. Its not allowed.

Please refer the OpenCL spect 1.2 - section 6.5.3. for more details.


View solution in original post

0 Likes
3 Replies
himanshu_gautam
Grandmaster

Re: __global vs __constant qualifier in OpenCL

Jump to solution

Hi

Constant varialbe can not be modified by external sources.

In your program arr is declared as constant uint and in the function func1() you are trying to modify it value. Its not allowed.

Please refer the OpenCL spect 1.2 - section 6.5.3. for more details.


View solution in original post

0 Likes
gopal_hc
Journeyman III

Re: __global vs __constant qualifier in OpenCL

Jump to solution

I agree,

so when I tried to use __global qualifier as:

__global uint arr[2] = {0, 0};  // an array of unsigned integer

I got the following error, which means that I can not use any other qualifier except __constant as a program scope.

/tmp/OCLqgA8In.cl", line 5: error: global variable must be declared in

          addrSpace constant

  __global uint arr[];

Is there any way to do this in OpenCL without passing as a parameter across all the functions in the program scope?

0 Likes
himanshu_gautam
Grandmaster

Re: __global vs __constant qualifier in OpenCL

Jump to solution

Check Section6.5 of OpenCL 1.2 spec.

"There is no generic address space name for program scope variables. All program scope

variables must be declared in the __constant address space."

0 Likes