title: Unable to find simple sum of 1 to 100 numbers in CUDA?
body: <p>I am working on image processing algorithm using CUDA. In my algorithm i want to find sum of all pixels of image using CUDA kernel. so i made kernel method in cuda for measure sum of all pixels of 16 bit gray scale image, but i got wrong answer.
  So i make simple program in cuda for find sum of 1 to  100 numbers and my code is below.
  In my code i got not exact sum of that 1 to 100 numbers using GPU, but i got exact sum of that 1 to 100 numbers using CPU. So what i had done in that code ? </p>
  
  <pre><code>#include "cuda_runtime.h"
  #include "device_launch_parameters.h"
  #include &lt;stdio.h&gt;
  #include &lt;conio.h&gt;
  #include &lt;malloc.h&gt;
  #include &lt;limits&gt;
  #include &lt;math.h&gt;
  
  using namespace std;
  
  __global__ void computeMeanValue1(double *pixels,double *sum){
  
          int x = threadIdx.x;
  
          sum[0] = sum[0] + (pixels[(x)]);
          __syncthreads();
  }
  
  int main(int argc, char **argv)
  {
      double *data;
      double *dev_data;
      double *dev_total;
      double *total;
  
      data=new double[(100) * sizeof(double)];
      total=new double[(1) * sizeof(double)];
  
      double cpuSum=0.0;
  
      for(int i=0;i&lt;100;i++){
          data[i]=i+1;
          cpuSum=cpuSum+data[i];
      }
      cout&lt;&lt;"CPU total = "&lt;&lt;cpuSum&lt;&lt;std::endl;
  
      cudaMalloc( (void**)&amp;dev_data, 100 * sizeof(double));
      cudaMalloc( (void**)&amp;dev_total, 1 * sizeof(double));
  
      cudaMemcpy(dev_data, data, 100 * sizeof(double), cudaMemcpyHostToDevice);
  
      computeMeanValue1&lt;&lt;&lt;1,100&gt;&gt;&gt;(dev_data,dev_total);
      cudaDeviceSynchronize();
  
      cudaMemcpy(total, dev_total, 1* sizeof(double), cudaMemcpyDeviceToHost);
      cout&lt;&lt;"GPU total = "&lt;&lt;total[0]&lt;&lt;std::endl;
  
      cudaFree(dev_data);
      cudaFree(dev_total);
  
      free(data);
      free(total);
  
      getch();
      return 0;
  }
  </code></pre>