title: CUDA - what is this loop doing
body: <p>Hey
  I've seen on a website this example kernel</p>
  
  <pre><code> __global__ void loop1( int N, float alpha, float* x, float* y ) {
     int i;
     int i0 = blockIdx.x*blockDim.x + threadIdx.x;
  
     for(i=i0;i&lt;N;i+=blockDim.x*gridDim.x) {
        y[i] = alpha*x[i] + y[i];
      }
  }   
  </code></pre>
  
  <p>To compute this function in C</p>
  
  <pre><code>   for(i=0;i&lt;N;i++) {
        y[i] = alpha*x[i] + y[i];
     }
  </code></pre>
  
  <p>Surely the for loop inside the kernel isn't necessary? and you can just do <code>y[i0] = alpha*x[i0] + y[i0]</code> and remove the for loop altogether.</p>
  
  <p>I'm just curious as to why it's there and what it's purpose is. This is assuming a kernel call such as <code>loop1&lt;&lt;&lt;64,256&gt;&gt;&gt;&gt;</code> so presumably <code>gridDim.x = 1</code></p>