title: Create local array dynamic inside OpenCL kernel
body: <p>I have a OpenCL kernel that needs to process a array as multiple arrays where each sub-array sum is saved in a local cache array.</p>
  
  <p>For example, imagine the fowling array:</p>
  
  <pre><code>[[1, 2, 3, 4], [10, 30, 1, 23]]
  </code></pre>
  
  <ul>
  <li>Each work-group gets a array (in the exemple we have 2 work-groups);</li>
  <li><p>Each work-item process two array indexes (for example multiply the value index the local_id), where the work-item result is saved in a work-group shared array.</p>
  
  <pre><code>__kernel void test(__global int **values, __global int *result, const int array_size){
      __local int cache[array_size];
  
      // initialise
      if (get_local_id(0) == 0){
          for (int i = 0; i &lt; array_size; i++)
              cache[i] = 0;
      }
  
      barrier (CLK_LOCAL_MEM_FENCE);
  
      if(get_global_id(0) &lt; 4){
          for (int i = 0; i&lt;2; i++)
              cache[get_local_id(0)] += values[get_group_id(0)][i] * 
                                                           get_local_id(0);
      }
  
      barrier (CLK_LOCAL_MEM_FENCE);
  
      if(get_local_id(0) == 0){
          for (int i = 0; i&lt;array_size; i++)
              result[get_group_id(0)] += cache[i];
      }
  }
  </code></pre></li>
  </ul>
  
  <p>The problem is that I can not define the cache array size by using a kernel parameter, but i need to in order to have a dynamic kernel.</p>
  
  <p>How can I create it dynamically? like malloc function in c...</p>
  
  <p>Or the only solution available is to send a temp array to my kernel function?</p>