title: CUDA Array Reduction
body: <p>I'm aware that there are multiple questions similar to this one already answered but I've been unable to piece together anything very helpful from them other than that I'm probably incorrectly indexing something.</p>
  
  <p>I'm trying to preform a sequential addressing reduction on input vector A into output vector B.</p>
  
  <p>The full  code is available here <a href="http://pastebin.com/7UGadgjX" rel="nofollow">http://pastebin.com/7UGadgjX</a>, but this is the kernel:</p>
  
  <pre><code>__global__ void vectorSum(int *A, int *B, int numElements) {
    extern __shared__ int S[];
    // Each thread loads one element from global to shared memory
    int tid = threadIdx.x;
    int i = blockDim.x * blockIdx.x + threadIdx.x;
    if (i &lt; numElements) {
      S[tid] = A[i];
      __syncthreads();
      // Reduce in shared memory
      for (int t = blockDim.x/2; t &gt; 0; t&gt;&gt;=1) {
        if (tid &lt; t) {
          S[tid] += S[tid + t];
        }
        __syncthreads();
      }
      if (tid == 0) B[blockIdx.x] = S[0];
    }
  }
  </code></pre>
  
  <p>and these are the kernel launch statements:</p>
  
  <pre><code>// Launch the Vector Summation CUDA Kernel
    int threadsPerBlock = 256;
    int blocksPerGrid =(numElements + threadsPerBlock - 1) / threadsPerBlock;
    vectorSum&lt;&lt;&lt;blocksPerGrid, threadsPerBlock&gt;&gt;&gt;(d_A, d_B, numElements);
  </code></pre>
  
  <p>I'm getting a unspecified launch error which I've read is similar to a segfault. I've been following the nvidia reduction documentation closely and tried to keep my kernel within the bounds of numElements but I seem to be missing something key considering how simple the code is.</p>