title: OpenCV: parallel for loop. What's wrong with my code?
body: <p>For the purpose of testing the parallel for loop in OpenCV, I created the following code, which just takes a matrix and does some manipulations and outputs another matrix. The parallel code is supposed to give the same results as given by the serial code. However, it is not the case.</p>
  
  <p>Before compiling the code, <strong>make sure that you have already had TBB properly enabled and installed in OpenCV</strong>. (Otherwise the parallelization will not be taken into account and will be treated as serial code, thus you'll obviously obtain the same results.)</p>
  
  <pre><code>#include &lt;opencv2/core/core.hpp&gt;
  #include "opencv2/highgui.hpp"
  #include "opencv2/imgproc.hpp"
  #include &lt;iostream&gt;    
  
  using namespace cv;
  using namespace std;    
  
  class Parallel_process : public cv::ParallelLoopBody
  {
  
  private:
      const Mat&amp; im;
      Mat&amp; out;
      int r;
  
  public:
      Parallel_process(const Mat&amp; inputIm, int radius, Mat&amp; outputIm)
          : im(inputIm), out(outputIm), r(radius){}
  
      virtual void operator()(const cv::Range&amp; range) const
      {
          //cout &lt;&lt; "start=" &lt;&lt; range.start&lt;&lt; " end=" &lt;&lt; range.end&lt;&lt;endl;
          //cout &lt;&lt; endl;
          for(int y = range.start; y &lt; range.end; y++)
          {
              //cout &lt;&lt; " " &lt;&lt; y;
              for(int x=0; x&lt; im.cols - r; x++)
              {
                  out(Rect(x,y,r,r)) = im(Rect(x,y,r,r))*im(Rect(x,y,r,r));
                  //cout&lt;&lt;im(Rect(x,y,r,r))&lt;&lt;endl;
              } 
          }
      }
  };
  
  
  int main(int , char** )
  {
  
      double start, timeSec;
  
      int n = 5, r = 2;
  
      /// Define a matrix
      Mat M = Mat(n, n, CV_32F);
      //randu(M, 0, 1);
      for(int y=0; y&lt; M.rows; y++)
      {
          for(int x=0; x&lt; M.cols; x++)
          {
              M.at&lt;float&gt;(x,y) = abs(x-y);    
          } 
      } 
  
      //cout&lt;&lt;M&lt;&lt;endl;
  
      Mat M1 = M.clone();
      Mat M2 = M.clone();    
  
  
      /// Serial loop
      start = (double)getTickCount();
      for(int y=0; y&lt; M1.rows - r; y++)
      {
          for(int x=0; x&lt; M1.cols - r; x++)
          {
              M1(Rect(x,y,r,r)) = M(Rect(x,y,r,r))*M(Rect(x,y,r,r));  
          } 
      } 
      timeSec = (getTickCount() - start) / getTickFrequency();
      cout &lt;&lt; "Non parallel time: " &lt;&lt; timeSec &lt;&lt; "s" &lt;&lt; endl;
      //cout&lt;&lt;M1&lt;&lt;endl;        
  
  
      /// Parallel loop
      start = (double)getTickCount();
      parallel_for_(Range(0,(int)M2.rows-r), Parallel_process(M,r,M2));
      timeSec = (getTickCount() - start) / getTickFrequency();
      cout &lt;&lt; "Parallel time: " &lt;&lt; timeSec &lt;&lt; "s" &lt;&lt; endl;    
      //cout&lt;&lt;M2&lt;&lt;endl;    
  
      /// Check the results
      cout &lt;&lt; "Check: norm(M1-M2)=" &lt;&lt; norm(M1-M2) &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>By executing the obtained binary, I got very random results (even with the same binary, i.e. compiled once but executed several times).</p>
  
  <p>Hope somebody can help to figure it out. Thanks in advance.</p>
  
  <p><img src="https://i.stack.imgur.com/E9sbI.png" alt="enter image description here"></p>