title: How slow is comparison and branching on GPU
body: <p>I have read that comparisons and branching is slow on GPU. I would like to know how much. (I'm familier with OpenCL, but the question is general also for CUDA, AMP ... )</p>
  
  <p>I would like to know it, before I start to port my code to GPU. In particular I'm interested in finding <strong>lowest value in neighborhood</strong> ( 4 or 9 nearest neighbors) of each point in 2D array. i.e. something like convolution, but instead of summing and multiplying I need comparisons and branching.</p>
  
  <p>for example code like this ( <strong>NOTE: this example code is not yet optimized for GPU</strong> to be more readeable ... so partitioning to workgroups, prefeaching of local memory ... is missing )</p>
  
  <pre><code>for(int i=1;i&lt;n-1;i++){ for(int j=1;j&lt;n-1;j++){ // iterate over 2D array
       float hij  = h[i][j];
       int imin = 0,jmin = 0;
       float dh,dhmin=0;
       // find lowest neighboring element h[i+imin][j+jmin] of h[i][j]
       dh = h[i-1][j  ]-hij;  if( dh&lt;dhmin ){  imin = -1; jmin =  0; dhmin = dh; }
       dh = h[i+1][j  ]-hij;  if( dh&lt;dhmin ){  imin = +1; jmin =  0; dhmin = dh; }
       dh = h[i  ][j-1]-hij;  if( dh&lt;dhmin ){  imin =  0; jmin = -1; dhmin = dh; }
       dh = h[i  ][j+1]-hij;  if( dh&lt;dhmin ){  imin =  0; jmin = +1; dhmin = dh; }
       if( dhmin&lt;-0.00001 ){ // if lower
         // ... Do something with hij, dhmin and save to h[i+imin][j+jmin] ...
       }
  } }
  </code></pre>
  
  <ul>
  <li>Would it be worth to port to GPU despite a lot of <code>if</code> branching and
  comparison? ( i.e. if this 4-5 comparisons per elemet would be 10x slower than the same 4-5 comparisons on CPU it would be a bottleneck )</li>
  <li>is there any optimization trick how to minizmize  <code>if</code>
  branching and comparison slow down?</li>
  </ul>
  
  <p>Which I used in this hydraulic errosion code:
  <a href="http://www.openprocessing.org/sketch/146982" rel="nofollow">http://www.openprocessing.org/sketch/146982</a></p>