title: PyCUDA kernel function
body: <p>I am new to PyCUDA and was going through some of the examples on the PyCUDA website. I am trying to figure out the logic behind certain lines of code and would really appreciate if someone explained the idea behind it.</p>
  
  <p>The below code snippet is from the PyCUDA website. Inside the function definition, I don't understand </p>
  
  <pre><code>int idx = threadIdx.x + threadIdx.y*4;
  </code></pre>
  
  <p>how the above line is being used to calculate the index of the array. Why are threadIdx.x and threadIdx.y added together and why is threadIdx.y multiplied by 4.</p>
  
  <p>For the function call to the GPU why is the block defined as 5,5,1. Since it is an array of 5x5 elements so in my understanding the block size should be 5,5 instead of 5,5,1.</p>
  
  <pre><code>import pycuda.driver as cuda
  import pycuda.autoinit
  from pycuda.compiler import SourceModule
  import numpy
  a = numpy.random.randn(5,5)
  a = a.astype(numpy.float32)
  a_gpu = cuda.mem_alloc(a.nbytes)
  cuda.memcpy_htod(a_gpu, a)
  mod = SourceModule("""
  __global__ void doubleMatrix(float *a)
  {
  int idx = threadIdx.x + threadIdx.y*4;
  a[idx] *= 2;
  }
  """)
  func = mod.get_function("doubleMatrix")
  func(a_gpu, block=(5,5,1))
  a_doubled = numpy.empty_like(a)
  cuda.memcpy_dtoh(a_doubled, a_gpu)
  print ("ORIGINAL MATRIX")
  print a
  print ("DOUBLED MATRIX AFTER PyCUDA EXECUTION")
  print a_doubled
  </code></pre>