title: Why program (global) scope variables must be __constant?
body: <p>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 <code>rand()</code> and <code>srand()</code>. In ANSI C, I will do that with something like:</p>
  
  <pre><code>/* ANSI C */
  static unsigned long _holdrand = 1; /* Global! */
  
  unsigned long rand(){
      _holdrand = _holdrand * 214013L + 2531011L;
      return (_holdrand &gt;&gt; 16) &amp; 0x7FFF; 
  }
  void srand( unsigned long seed ){
      _holdrand = seed;
  }
  </code></pre>
  
  <p>But OpenCL restrict all global scope variables being <code>__constant</code>. I could move <code>_holdrand</code> into function scope, and return it's pointer out of that function.</p>
  
  <pre><code>/* OpenCL C */
  uint* holdrand(){
      __private static uint _holdrand = 1;
      return &amp;_holdrand;
  }
  
  uint rand(){
      *holdrand() = *holdrand() * 214013L + 2531011L;
      return (*holdrand() &gt;&gt; 16) &amp; 0x7FFF; 
  }
  void srand( uint seed ){
      *holdrand() = seed;
  }
  </code></pre>
  
  <p>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. </p>
  
  <pre><code>__private uint _holdrand = 1;
  /* It should be the same thing... Why this is not allowed? */
  </code></pre>
  
  <p>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 <code>_holdrand</code> modifiable in two different functions in this example?</p>