title: multi-GPU basic usage
body: <p>How can I use two devices in order to improve for example 
  the performance of the following code (sum of vectors)?
  Is it possible to use more devices "at the same time"?
  If yes, how can I manage the allocations of the vectors on the global memory of the different devices?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;math.h&gt;
  #include &lt;time.h&gt;
  #include &lt;cuda.h&gt;
  
  #define NB 32
  #define NT 500
  #define N NB*NT
  
  __global__ void add( double *a, double *b, double *c);
  
  //===========================================
  __global__ void add( double *a, double *b, double *c){
  
      int tid = threadIdx.x + blockIdx.x * blockDim.x; 
  
      while(tid &lt; N){
          c[tid] = a[tid] + b[tid];
          tid += blockDim.x * gridDim.x;
      }
  
  }
  
  //============================================
  //BEGIN
  //===========================================
  int main( void ) {
  
      double *a, *b, *c;
      double *dev_a, *dev_b, *dev_c;
  
      // allocate the memory on the CPU
      a=(double *)malloc(N*sizeof(double));
      b=(double *)malloc(N*sizeof(double));
      c=(double *)malloc(N*sizeof(double));
  
      // allocate the memory on the GPU
      cudaMalloc( (void**)&amp;dev_a, N * sizeof(double) );
      cudaMalloc( (void**)&amp;dev_b, N * sizeof(double) );
      cudaMalloc( (void**)&amp;dev_c, N * sizeof(double) );
  
      // fill the arrays 'a' and 'b' on the CPU
      for (int i=0; i&lt;N; i++) {
          a[i] = (double)i;
          b[i] = (double)i*2;
      }
  
      // copy the arrays 'a' and 'b' to the GPU
      cudaMemcpy( dev_a, a, N * sizeof(double), cudaMemcpyHostToDevice);
      cudaMemcpy( dev_b, b, N * sizeof(double), cudaMemcpyHostToDevice);
  
      for(int i=0;i&lt;10000;++i)
          add&lt;&lt;&lt;NB,NT&gt;&gt;&gt;( dev_a, dev_b, dev_c );
  
      // copy the array 'c' back from the GPU to the CPU
      cudaMemcpy( c, dev_c, N * sizeof(double), cudaMemcpyDeviceToHost);
  
      // display the results
      // for (int i=0; i&lt;N; i++) {
      //      printf( "%g + %g = %g\n", a[i], b[i], c[i] );
      //  }
      printf("\nGPU done\n");
  
      // free the memory allocated on the GPU
      cudaFree( dev_a );
      cudaFree( dev_b );
      cudaFree( dev_c );
      // free the memory allocated on the CPU
      free( a );
      free( b );
      free( c );
  
      return 0;
  }
  </code></pre>
  
  <p>Thank you in advance.
  Michele</p>