title: Branch divergence, CUDA and Kinetic Monte Carlo
body: <p>So, I have a code that uses Kinetic Monte Carlo on a lattice in order to simulate something. I am using CUDA to run this code on my GPU (although I believe the same question applies to OpenCl as well).</p>
  
  <p>This means that I divide my lattice into little sub-lattices and each thread operates on one of them. Since I am doing KMC, each thread has this code : </p>
  
  <pre><code>   While(condition == true){
       *Grab a sample u from U[0,1]*
        for(i = 0; i &lt; 100;i++){
           *Do some stuff here to generate A*
            if(A &gt; u){
                *Do more stuff here, which could include updates to global memory*
                 break();
             }
        }
     }
  </code></pre>
  
  <p>A is different for different threads and so is u and 100 is just a random number. In code, this could be 1000 or even 10000.</p>
  
  <p>So, won't we have branch divergence when the time comes for a thread to pass through that if? How badly can this affect performance? I know that the answer depends on the code inside the if-clause but how will this scale as I add more and more threads?</p>
  
  <p>Any reference on how I can estimate losses/gains in performance would also be welcome. </p>
  
  <p>Thanks!</p>