I am new to OpenCL and really confused by this restriction. For example, if I want to write a LCG, I have to make the state word be modifiable to both rand()
and srand()
. In ANSI C, I will do that with something like:
/* ANSI C */
static unsigned long _holdrand = 1; /* Global! */
unsigned long rand(){
_holdrand = _holdrand * 214013L + 2531011L;
return (_holdrand >> 16) & 0x7FFF;
}
void srand( unsigned long seed ){
_holdrand = seed;
}
But OpenCL restrict all global scope variables being __constant
. I could move _holdrand
into function scope, and return it's pointer out of that function.
/* OpenCL C */
uint* holdrand(){
__private static uint _holdrand = 1;
return &_holdrand;
}
uint rand(){
*holdrand() = *holdrand() * 214013L + 2531011L;
return (*holdrand() >> 16) & 0x7FFF;
}
void srand( uint seed ){
*holdrand() = seed;
}
It works fine and I don't know if this is a good solution. The restriction made nonsense, I just avoided it by adding more weird code.
__private uint _holdrand = 1;
/* It should be the same thing... Why this is not allowed? */
Since the return-a-pointer-of-static manner will behave exactly the same as the global scope variable approach in ANSI C, I couldn't understand what the restriction meaning for. Could someone explain why? Did I missed something? What should I do to make _holdrand
modifiable in two different functions in this example?