title: Optimal way to record ray intersection data for ray tracing using OpenCL
body: <p>I am developing a Monte Carlo ray tracer in OpenCl for calculating 'view factors' for radiative heat transfer analysis and wish to know the optimal way to collate the number of times object x is intersected by rays fired from object i.</p>
  
  <p>Now the basic algorithm is as follows :</p>
  
  <ol>
  <li>Fire random ray, R, off from the surface of object i</li>
  <li>Test intersection of ray R with objects 0 - N</li>
  <li>Determine the first object intersected by R, let this be object x</li>
  <li>Record the first intersection by incrementing an array of int's such that array[i][x] +=1</li>
  <li>Repeat for total number of rays</li>
  <li>Divide each value in the ith row of the array by the total number of arrays fired from object i.</li>
  </ol>
  
  <p>Now typically in a parallel implementation on the CPU each thread would just maintain its own copy of array[N] and then when all rays have been fired from object i, the master thread will sum the individual arrays to get the results.</p>
  
  <p>In OpenCL on the GPU this is not a practical solution as when N increases there quickly becomes a shortage of local memory and using a single array with barriers will cripple performance.</p>
  
  <p>What is the best practical for preforming reduction of the results array or is a a memory barrier the only practical solution?</p>