title: Why OpenMP-based Jacobi Stencil program is slower than sequential version
body: <p>Can someone tell me why the OpenMP-based Jacobi iteration is so slower than the sequential version?</p>
  
  <pre><code>ITERATION = 100;
  DIM = 4000;
  
  // sequential version takes only ~5 seconds
  for(size_t i=0; i&lt;ITERATION; ++i) {
    for(size_t y=1; y&lt;(DIM-1); ++y) {
      for(int x=(y*DIM)+1; x&lt;(y*DIM) + DIM-1; ++x) {                                                                                              
        dest[x]=(src[x-DIM]+src[x+DIM]+src[x]+src[x-1]+src[x+1])*0.2;
      }   
    }   
    std::swap(dest, src);
  }
  
  // openmp version takes 7s on 4 cores
  for(size_t i=0; i&lt;ITERATION; ++i) {
    #pragma omp parallel for
    for(size_t y=1; y&lt;(DIM-1); ++y) {
      for(int x=(y*DIM)+1; x&lt;(y*DIM) + DIM-1; ++x) {
        dest[x]=(src[x-DIM]+src[x+DIM]+src[x]+src[x-1]+src[x+1])*0.2;
      }
    }
    std::swap(dest, src);
  }
  </code></pre>
  
  <p>I use four threads and compile the program with O2 flag.</p>