title: Perform both multiplication and addition in parallel using cuda
body: <p>I have two vectors and I want to calculate dot product of those two vectors in parallel. I was able to do multiplication of each element in parallel and after that addition in parallel. following is the code which I have tried.</p>
  
  <p>But I want to do <strong>both multiplication and addition in parallel</strong>. That means elements which have performed multiplication should be added even if other elements haven't done multiplication yet. Hope you understood what I have said.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;cuda.h&gt;
  __global__ void dotproduct(int *a,int *b,int *c,int N)
  {
      int k=N;
      int i=threadIdx.x;                      
      c[i]=a[i]*b[i];                         
      if(N%2==1)                          
          N=N+1;                          
      __syncthreads();                        
      while(i&lt;(N/2))
      {
          if((i+1)*2&lt;=k)
          {
              c[i]=c[i*2]+c[i*2+1];               
          }
          else
          {
              c[i]=c[k-1];
          }
          k=N/2;
          N=N/2;
          if(N%2==1&amp;&amp;N!=1)                            
              N=N+1;
          __syncthreads();                    //wait for all the threads to synchronize               
      }
  }
  
  int main()
  {
      int N=10;                           //vector size
      int a[N],b[N],c[N];                     
      int *dev_a,*dev_b,*dev_c;                   
      cudaMalloc((void**)&amp;dev_a,N*sizeof(int));           
      cudaMalloc((void**)&amp;dev_b,N*sizeof(int));           
      cudaMalloc((void**)&amp;dev_c,N*sizeof(int));           
      for(int i=0;i&lt;N;i++)
      {
          a[i]=rand()%10;                     
          b[i]=rand()%10;                     
      }
      cudaMemcpy(dev_a,a,N*sizeof(int),cudaMemcpyHostToDevice);   
      cudaMemcpy(dev_b,b,N*sizeof(int),cudaMemcpyHostToDevice);   
      dotproduct&lt;&lt;&lt;1,N&gt;&gt;&gt;(dev_a,dev_b,dev_c,N);           
      cudaMemcpy(c,dev_c,N*sizeof(int),cudaMemcpyDeviceToHost);   
      for(int i=0;i&lt;N;i++)
      {
          printf("%d,%d\n",a[i],b[i]);                
  
      }
      printf("the answer is : %d in GPU\n",c[0]);         
  
      cudaFree(dev_a);                        
      cudaFree(dev_b);                        
      cudaFree(dev_c);                        
      cudaThreadExit();                       
      return 0;
  
  }
  </code></pre>