title: How to optimize Conway's game of life for CUDA?
body: <p>I've written this CUDA kernel for Conway's game of life:</p>
  
  <pre><code>__global__ void gameOfLife(float* returnBuffer, int width, int height) {  
      unsigned int x = blockIdx.x*blockDim.x + threadIdx.x;  
      unsigned int y = blockIdx.y*blockDim.y + threadIdx.y;  
      float p = tex2D(inputTex, x, y);  
      float neighbors = 0;  
      neighbors += tex2D(inputTex, x+1, y);  
      neighbors += tex2D(inputTex, x-1, y);  
      neighbors += tex2D(inputTex, x, y+1);  
      neighbors += tex2D(inputTex, x, y-1);  
      neighbors += tex2D(inputTex, x+1, y+1);  
      neighbors += tex2D(inputTex, x-1, y-1);  
      neighbors += tex2D(inputTex, x-1, y+1);  
      neighbors += tex2D(inputTex, x+1, y-1);  
      __syncthreads();  
      float final = 0;  
      if(neighbors &lt; 2) final = 0;  
      else if(neighbors &gt; 3) final = 0;  
      else if(p != 0) final = 1;  
      else if(neighbors == 3) final = 1;  
      __syncthreads();  
      returnBuffer[x + y*width] = final;  
  }
  </code></pre>
  
  <p>I am looking for errors/optimizations.
  Parallel programming is quite new to me and I am not sure if I get how to do it right.</p>
  
  <p>The rest is a memcpy from an input array to the 2D texture inputTex bound to a CUDA array. Output is memcpy-ed from global memory to host and then dealt with.</p>
  
  <p>As you can see a thread deals with a single pixel. I am unsure if that is the fastest way as some sources suggest doing a row or more per thread. If I understand correctly NVidia themselves say that the more threads, the better. I would love advice on this from someone with practical experience.</p>