title: Nested loop summation in 2D using OpenCL
body: <p>I have recently started working with OpenCL in C++ and I'm trying to fully understand how to use 2D and 3D NDRange. I'm currently implementing Inverse Distance Weighting in OpenCL, but my problem is general. </p>
  
  <p>Below is the serial function to compute the weights and it consists of a nested loop.</p>
  
  <pre><code>void computeWeights(int nGrids, int nPoints, double *distances, double *weightSum, const double p) {
  
      for (int i = 0; i &lt; nGrids; ++i) {
          double sum = 0;
          for (int j = 0; j &lt; nPoints; ++j) {
              double weight = 1 / pow(distances[i * nPoints + j], p);
              distances[i * nPoints + j] = weight;
              sum += weight;
          }
          weightSum[i] = sum;
      }
  }
  </code></pre>
  
  <p>What I would want is to implement the above function using a 2D NDRange, the first being over nGrids and the second over nPoints. What I don't understand, though, is how to handle the summation of the weights into weightSum[i]. I understand that I may have to use parallel sum reduction, somehow.</p>