title: Parallel for loop in openmp
body: <p>I'm trying to parallelize a very simple for-loop, but this is my first attempt at using openMP in a long time. I'm getting baffled by the run times. Here is my code:</p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;algorithm&gt;
  
  using namespace std;
  
  int main () 
  {
      int n=400000,  m=1000;  
      double x=0,y=0;
      double s=0;
      vector&lt; double &gt; shifts(n,0);
  
  
      #pragma omp parallel for 
      for (int j=0; j&lt;n; j++) {
  
          double r=0.0;
          for (int i=0; i &lt; m; i++){
  
              double rand_g1 = cos(i/double(m));
              double rand_g2 = sin(i/double(m));     
  
              x += rand_g1;
              y += rand_g2;
              r += sqrt(rand_g1*rand_g1 + rand_g2*rand_g2);
          }
          shifts[j] = r / m;
      }
  
      cout &lt;&lt; *std::max_element( shifts.begin(), shifts.end() ) &lt;&lt; endl;
  }
  </code></pre>
  
  <p>I compile it with </p>
  
  <pre><code>g++ -O3 testMP.cc -o testMP  -I /opt/boost_1_48_0/include
  </code></pre>
  
  <p>that is, no "-fopenmp", and I get these timings:</p>
  
  <pre><code>real    0m18.417s
  user    0m18.357s
  sys     0m0.004s
  </code></pre>
  
  <p>when I do use "-fopenmp", </p>
  
  <pre><code>g++ -O3 -fopenmp testMP.cc -o testMP  -I /opt/boost_1_48_0/include
  </code></pre>
  
  <p>I get these numbers for the times:</p>
  
  <pre><code>real    0m6.853s
  user    0m52.007s
  sys     0m0.008s
  </code></pre>
  
  <p>which doesn't make sense to me. How using eight cores can only result in just 3-fold 
   increase of performance? Am I coding the loop correctly?</p>