title: CUDA atomicAdd() is giving wrong results
body: <p>I am using atomicAdd() to add 1 to each of the elements of an array c = {0,0,0,0,0} using two different schemes </p>
  
  <ol>
  <li>c[i] = c[i] + 1; </li>
  </ol>
  
  <blockquote>
    <p>result - c = {1,1,1,1,1}</p>
  </blockquote>
  
  <ol>
  <li>c[i] = atomicAdd(&amp;(c[i]),1); </li>
  </ol>
  
  <blockquote>
    <p>result c = {0,0,0,0,0}</p>
  </blockquote>
  
  <p>I am totally clueless as to why I am getting such results, here is the small code I am using to get the results.</p>
  
  <pre><code>#include "cuda_runtime.h"
  #include "device_launch_parameters.h"
  
  #include &lt;stdio.h&gt;
  #include&lt;windows.h&gt;
  
  void addWithCuda(int *c, int size);
  
  __global__ void addKernel(int *c, int size)
  {
      int i = threadIdx.x;
      if (i &lt; size)
       c[i] = c[i] + 1;
      //c[i] = atomicAdd(&amp;(c[i]),(int)1);
  }
  
  int main()
  {
      const int arraySize = 5;
  
      int c[arraySize] = {0,0,0,0,0};
  
      // Add vectors in parallel.
      addWithCuda(c, arraySize);
  
      Sleep(3000);
      printf("result = {%d,%d,%d,%d,%d}\n",
          c[0], c[1], c[2], c[3], c[4]);
      return 0;
  }
  
  // Helper function for using CUDA to add vectors in parallel.
  void addWithCuda(int *c, int size)
  {
  
      int *dev_c = 0;
      cudaError_t cudaStatus;
  
      // Choose which GPU to run on, change this on a multi-GPU system.
      cudaStatus = cudaSetDevice(0);
      if (cudaStatus != cudaSuccess) {
          fprintf(stderr, "cudaSetDevice failed!  Do you have a CUDA-capable GPU         installed?");
  
      }
  
      // Allocate GPU buffers for three vectors (two input, one output)    .
      cudaStatus = cudaMalloc((void**)&amp;dev_c, size * sizeof(int));
      if (cudaStatus != cudaSuccess) {
          fprintf(stderr, "cudaMalloc failed!");
  
      }
  
          cudaStatus = cudaMemcpy(dev_c, c, size * sizeof(int), cudaMemcpyHostToDevice);
      if (cudaStatus != cudaSuccess) {
          fprintf(stderr, "cudaMemcpy failed!");
  
      }
  
      // Launch a kernel on the GPU with one thread for each element.
      addKernel&lt;&lt;&lt;1, size&gt;&gt;&gt;(dev_c, size);
  
  
      // cudaDeviceSynchronize waits for the kernel to finish, and returns
      // any errors encountered during the launch.
      cudaStatus = cudaDeviceSynchronize();
      if (cudaStatus != cudaSuccess) {
          fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
  
      }
  
      // Copy output vector from GPU buffer to host memory.
      cudaStatus = cudaMemcpy(c, dev_c, size * sizeof(int), cudaMemcpyDeviceToHost);
      if (cudaStatus != cudaSuccess) {
          fprintf(stderr, "cudaMemcpy failed!");
  
      }
  }
  </code></pre>