title: Threads and Blocks of Cuda are not working exactly
body: <p>I m making algorithem in cuda for image processing in Visual Studio 2010. In my coding i got a problem with working on threads and blocks of cuda. So my sample code of C and CUDA is below, and C code is work fine, but CUDA code is not work exactly.
  My C code</p>
  
  <pre><code>void checkGpuBlockValue(unsigned int *a,unsigned int *b,int length)
  {
      for(int i=0;i&lt;length;i++){
          b[i]=a[i]+i;
      }
  
  }
  
  int main()
  {
      const int range=1000;
      unsigned int *a=new unsigned int[range];
      unsigned int *b=new unsigned int[range];
  
      for(int i=0;i&lt;range;i++)
      {
          a[i]=i;
      }
  
  checkGpuBlockValue(a,b,range);
  
  for(int j=0;j&lt;range;j++)
      {
          cout&lt;&lt;"b["&lt;&lt;j&lt;&lt;"] = "&lt;&lt;b[j]&lt;&lt;std::endl;
      }
  }
  </code></pre>
  
  <p>OutPut =</p>
  
  <pre><code>OutPut :
  b[0] = 0
  b[1] = 2
  b[2] = 4
  b[3] = 6
  b[4] = 8
  .
  .
  .
  .
  .
  
  b[996] = 1992
  b[997] = 1994
  b[998] = 1996
  b[999] = 1998
  </code></pre>
  
  <p>this works fine.</p>
  
  <p>My CUDA code(not working well) is ;</p>
  
  <pre><code>__global__
  void checkGpuBlockValue(unsigned int *a,unsigned int *b,int length)
  {
      unsigned int i = (blockIdx.x * blockDim.x) + threadIdx.x;
  
      if(i&lt;length){
          b[i]=a[i]+i;
      }
  
  }
  
  int main()
  {
      const int range=1000;
      unsigned int *a=new unsigned int[range];
      unsigned int *b=new unsigned int[range];
  
      unsigned int *dev_a;
      unsigned int *dev_b;
  
      for(int i=0;i&lt;range;i++)
      {
          a[i]=i;
      }
  
      cudaMalloc( (void**)&amp;dev_a, range* sizeof(unsigned int));
      cudaMalloc( (void**)&amp;dev_b, range* sizeof(unsigned int));
  
      cudaMemcpy(dev_a, a, range, cudaMemcpyHostToDevice);
      cudaMemcpy(dev_b, a, range, cudaMemcpyHostToDevice);
  
      static const int BLOCK_WIDTH = 8;       
  
      //1024 is the maximum number of threads per block for modern GPUs.
  
      int x = static_cast&lt;int&gt;(ceilf(static_cast&lt;float&gt;(range) / BLOCK_WIDTH));
  
  
      const dim3 grid (x,1);                              
      const dim3 block(BLOCK_WIDTH,1);    
  
      checkGpuBlockValue&lt;&lt;&lt;grid,block&gt;&gt;&gt;(dev_a,dev_b,range);
      cudaDeviceSynchronize();
  
      cudaMemcpy(b, dev_b, range, cudaMemcpyDeviceToHost);
  
  
      for(int j=0;j&lt;range;j++)
      {
          cout&lt;&lt;"b["&lt;&lt;j&lt;&lt;"] = "&lt;&lt;b[j]&lt;&lt;std::endl;
      }
  
      cudaFree(dev_a);
      cudaFree(dev_b);
  }
  </code></pre>
  
  <p>OUT PUT is :</p>
  
  <pre><code>Out Put =
  b[0] = 0
  b[1] = 2
  b[2] = 4
  b[3] = 6
  .
  .
  .
  .
  .
  b[242] = 484
  b[243] = 486
  b[244] = 488
  b[245] = 490
  b[246] = 492
  b[247] = 494
  b[248] = 496
  b[249] = 498
  b[250] = 3452816845
  b[251] = 3452816845
  b[252] = 3452816845
  b[253] = 3452816845
  b[254] = 3452816845
  b[255] = 3452816845
  b[256] = 3452816845
  .
  .
  .
  .
  .
  .
  b[996] = 3452816845
  b[997] = 3452816845
  b[998] = 3452816845
  b[999] = 3452816845
  </code></pre>
  
  <p>In my code i m puting value of  0 to 1000 in int *a and than add that *a with value from 0 to 1000 and result is storing in int *b. 
  So my code is work well for 0 to 249(upto 250) loop, but after 250 it gives wrong value.
  So what is wrong i m doing here ? please suggest to me.</p>