title: scan-array CUDA
body: <p>I'm trying to scan a simple array using CUDA but it seems there is something wrong with the code below..I am trying to find what i am doing wrong but i can't.Can anyone please help me?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  
  __global__ void prescan(int *g_odata, int *g_idata, int n){
  
      extern __shared__ int temp[];// allocated on invocation
      int thid = threadIdx.x;
      int offset = 1;
      temp[2*thid] = g_idata[2*thid]; // load input into shared memory
      temp[2*thid+1] = g_idata[2*thid+1];
  
  
      for (int d = n&gt;&gt;1; d &gt; 0; d &gt;&gt;= 1){ // build sum in place up the tree
          __syncthreads();
          if (thid &lt; d){
  
              int ai = offset*(2*thid+1)-1;
              int bi = offset*(2*thid+2)-1;
              temp[bi] += temp[ai];
          }
          offset *= 2;
      }
  
      if (thid == 0) { temp[n - 1] = 0; } // clear the last element
  
      for (int d = 1; d &lt; n; d *= 2){ // traverse down tree &amp; build scan
          offset &gt;&gt;= 1;
          __syncthreads();
          if (thid &lt; d){
  
              int ai = offset*(2*thid+1)-1;
              int bi = offset*(2*thid+2)-1;
              int t = temp[ai];
              temp[ai] = temp[bi];
              temp[bi] += t;
          }
      }
  
      __syncthreads();
  
      g_odata[2*thid] = temp[2*thid]; // write results to device memory
      g_odata[2*thid+1] = temp[2*thid+1];
  }
  
  int main(int argc, char *argv[]){
  
      int i;
      int *input = 0;
      int *output = 0;
      int *g_idata = 0;
      int *g_odata = 0;
  
      int numblocks = 1;
      int radix = 16;
  
      input = (int*)malloc(numblocks*radix*sizeof(int));
      output = (int*)malloc(numblocks*radix*sizeof(int));
  
      cudaMalloc((void**)&amp;g_idata, numblocks*radix*sizeof(int));
      cudaMalloc((void**)&amp;g_odata, numblocks*radix*sizeof(int));
  
      for(i=0; i&lt;numblocks*radix; i++){
              input[i] = 1 + 2*i;
      }
  
      for(i=0; i&lt;numblocks*radix; i++){
          printf("%d   ", input[i]);
      }
  
      cudaMemcpy(g_idata, input, numblocks*radix*sizeof(int), cudaMemcpyHostToDevice);
  
      prescan&lt;&lt;&lt;1,8&gt;&gt;&gt;(g_odata, g_idata, numblocks*radix);
  
      cudaThreadSynchronize();
  
      cudaMemcpy(output, g_odata, numblocks*radix*sizeof(int), cudaMemcpyDeviceToHost);
  
      for(i=0; i&lt;numblocks*radix; i++){
          printf("%d   ", output[i]);
      }
  
      free(input);
      free(output);
      cudaFree(g_idata);
      cudaFree(g_odata);
  
      return 0;
  }
  </code></pre>
  
  <p>The output is this: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 0 0 0 0 0 0 0 0 0 0 0 0 0   0 0 0.I want to have this output: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 0 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225</p>