title: How to use arrays in program (global) scope in OpenCL
body: <p>AMD OpenCL Programming Guide, Section 6.3 Constant Memory Optimization:</p>
  
  <blockquote>
    <p>Globally scoped constant arrays. These arrays are initialized,
    globally scoped, and in the constant address space (as specified in
    section 6.5.3 of the OpenCL specification). If the size of an array is
    below 64 kB, it is placed in hardware constant buffers; otherwise, it
    uses global memory. An example of this is a lookup table for math
    functions.</p>
  </blockquote>
  
  <p>I want to use this "globally scoped constant array". I have such code in pure C</p>
  
  <pre><code>#define SIZE 101
  int *reciprocal_table;
  
  int reciprocal(int number){
    return reciprocal_table[number];
  }
  
  void kernel(int *output)
  {
    for(int i=0; i &lt; SIZE; i+)
      output[i] = reciprocal(i);
  }
  </code></pre>
  
  <p>I want to port it into OpenCL</p>
  
  <pre><code>__kernel void kernel(__global int *output){
    int gid = get_global_id(0);
  
    output[gid] = reciprocal(gid);
  }
  
  int reciprocal(int number){
    return reciprocal_table[number];
  }
  </code></pre>
  
  <p>What should I do with global variable <code>reciprocal_table</code>? If I try to add <code>__global</code> or <code>__constant</code> to it I get an error:</p>
  
  <pre><code>global variable must be declared in addrSpace constant
  </code></pre>
  
  <p>I don't want to pass <code>__constant int *reciprocal_table</code> from <code>kernel</code> to <code>reciprocal</code>. Is it possible to initialize global variable somehow? I know that I can write it down into code, but does other way exist?</p>
  
  <p>P.S. I'm using AMD OpenCL</p>
  
  <p><strong>UPD</strong> Above code is just an example. I have real much more complex code with a lot of functions. So I want to make array in program scope to use it in all functions.</p>
  
  <p><strong>UPD2</strong> Changed example code and added citation from Programming Guide</p>