title: Dynamic Shared Memory in CUDA
body: <p>There are similar questions to what I'm about to ask, but I feel like none of them get at the heart of what I'm really looking for. What I have now is a CUDA method that requires defining two arrays into shared memory. Now, the size of the arrays is given by a variable that is read into the program after the start of execution. Because of this, I cannot use that variable to define the size of the arrays, due to the fact that defining the size of shared arrays requires knowing the value at compile time. I do not want to do something like <code>__shared__ double arr1[1000]</code> because typing in the size by hand is useless to me as that will change depending on the input. In the same vein, I cannot use <code>#define</code> to create a constant for the size.</p>
  
  <p>Now I can follow an example similar to what is in the manual (<a href="http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#shared" rel="noreferrer">http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#shared</a>) such as</p>
  
  <pre><code>extern __shared__ float array[];
  __device__ void func()      // __device__ or __global__ function
  {
      short* array0 = (short*)array; 
      float* array1 = (float*)&amp;array0[128];
      int*   array2 =   (int*)&amp;array1[64];
  }
  </code></pre>
  
  <p>But this still runs into an issue. From what I've read, defining a shared array always makes the memory address the first element. That means I need to make my second array shifted over by the size of the first array, as they appear to do in this example. But the size of the first array is dependent on user input.</p>
  
  <p>Another question (<a href="https://stackoverflow.com/questions/9187899/cuda-shared-memory-array-variable">Cuda Shared Memory array variable</a>) has a similar issue, and they were told to create a single array that would act as the array for both arrays and simply adjust the indices to properly match the arrays. While this does seem to do what I want, it looks very messy. Is there any way around this so that I can still  maintain two independent arrays, each with sizes that are defined as input by the user?</p>