title: CUDA array elements shifting operation
body: <p>I'm currently performing an array shifting operation on CUDA but I'm stuck at the part where I would need to parallelize the operation on the GPU (I've already done it for CPU).  So, the operation is basically shifting elements in the array.</p>
  
  <p>So for example if I have a M by N matrix, for each row if I see a -1, I would replace
  the -1 with the element next to it, and so on until I hit the end of the row, and I 
  need to do that for all columns in parallel.</p>
  
  <p>So for a quick example:</p>
  
  <pre><code> 3  4  1 -1  5  6  7  8
  -1  4  5  2  1  2  5  2
   2  4  5  1  2  3  4 -1
  </code></pre>
  
  <p>for that matrix, the resulting matrix would be:</p>
  
  <pre><code> 3  4  1  5  6  7  8  8
   4  5  2  1  2  5  2  2
   2  4  5  1  2  3  4 -1
  </code></pre>
  
  <p>PS. The last element remains the same because it hits the boundary where it has nothing to
     replace with.  Also, only one -1 will appear in each row</p>
  
  <p>So, that's basically the operation, but my question is how do I assign a thread to each row
  or .. parallelize all the rows and do this shifting at the same time in cuda?  Also, my 
  array is converted from a 2d array to a 1d array using the equation </p>
  
  <pre><code>array1d[i+width*j]  =  array2d[i][j];
  </code></pre>
  
  <p>So far I've tried this:</p>
  
  <pre><code>__global__ void gpu_shiftArray(int *Arr, int *location, int width, int height)
  {
  int i = blockIdx.x * blockDim.x + threadIdx.x;
  int j = blockIdx.y * blockDim.y + threadIdx.y;
  
  int index = i+width*j;
  
  //shift when I see -1
  if(Arr[index] == -1)
  {
      Arr[index] = (index % height) ? Arr[index+1] : 
      }
      //location stores the index of -1, so anything after the -1 will be shifted too
  if((location[i]+width*j) &lt;= index)
  {
      Arr[index] = (index % height) ? Arr[index+1] : 
  }
  }
  </code></pre>
  
  <p>The output of it is not exactly correct (off by like 5-10 values) but I'm not exactly sure 
  why nor I don't know what I am doing wrong.</p>