title: Grid-Stride Loop in cuda and matrix operations, why do we need it?
body: <pre><code>__global__ void substract(float *A, float *B, float *res, int *n)
  {
      int size = *n;
      int tid = threadIdx.x + blockIdx.x*blockDim.x;
      while (tid &lt; size) 
      {
          res[tid] = A[tid] - B[tid];
          tid += blockDim.x * gridDim.x;
      }
  }
  
  
  int function(...) {
      int threadsPerBlock = 256;
      int blocks = (n+threadsPerBlock-1)/threadsPerBlock;
      int blocksPerGrid = 32&lt;blocks ? 32 : blocks;
  .
  .
  .
      substract&lt;&lt;&lt;blocksPerGrid, threadsPerBlock&gt;&gt;&gt;(A, B, res, n);
  .
  .
  .
  }
  </code></pre>
  
  <p>So I wrote this code that takes an array <code>A</code> that represents a matrix of size <code>n</code>x<code>n</code> and a second array <code>B</code> that represents the vector of size <code>n</code> and I  subtract one from the other. Let's say the size of this array is 1000x1000. I wrote it kind of by following the examples on multiple cuda guides, but I can't understand why we need this part: <code>tid += blockDim.x * gridDim.x;</code></p>
  
  <p>Since it will never fit as array id element, it will always be larger than or equal to 1024 and my array only has 0-999 id's, it seems useless to me, but without it my program crashes, the screen turns black and after a few seconds it returns and I get the pop up that drivers have recovered. So I tried to understand why I can't just go through the whole array with the <code>tid = threadIdx.x + blockIdx.x*blockDim.x;</code>. I printed all the <code>tid</code>s before the <code>while</code> loop and it seems it just goes all the way from 0 to 1024 in random order since it can't count on <code>tid += blockDim.x * gridDim.x;</code> to  calculate anything inside my array boundaries I guess.</p>