title: No speedup with OpenMP
body: <p>I am working with OpenMP in order to obtain an algorithm with a near-linear speedup.
  Unfortunately I noticed that I could not get the desired speedup.</p>
  <p>So, in order to understand the error in my code, I wrote another code, an easy one, just to double-check that the speedup was in principle obtainable on my hardware.</p>
  <p>This is the toy example i wrote:</p>
  <pre><code>#include &lt;omp.h&gt;
  #include &lt;cmath&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  #include &lt;string.h&gt;
  #include &lt;cstdlib&gt;
  #include &lt;fstream&gt;
  #include &lt;sstream&gt;
  #include &lt;iomanip&gt;
  #include &lt;iostream&gt;
  #include &lt;stdexcept&gt;
  #include &lt;algorithm&gt;
  #include &quot;mkl.h&quot;
  
  int main () {
        int number_of_threads = 1;
        int n = 600;
        int m = 50;
        int N = n/number_of_threads;
        int time_limit = 600;
        double total_clock = omp_get_wtime();
        int time_flag = 0;
  
        #pragma omp parallel num_threads(number_of_threads)
         {
            int thread_id = omp_get_thread_num();
            int iteration_number_local = 0;
            double *C = new double[n]; std::fill(C, C+n, 3.0);
            double *D = new double[n]; std::fill(D, D+n, 3.0);
            double *CD = new double[n]; std::fill(CD, CD+n, 0.0);
  
            while (time_flag == 0){
                  for (int i = 0; i &lt; N; i++)                     
                      for(int z = 0; z &lt; m; z++)
                          for(int x = 0; x &lt; n; x++)
                              for(int c = 0; c &lt; n; c++){
                                  CD[c] = C[z]*D[x];
                                  C[z] = CD[c] + D[x];
                              }
                  iteration_number_local++;
                  if ((omp_get_wtime() - total_clock) &gt;= time_limit) 
                      time_flag = 1; 
             }
         #pragma omp critical
         std::cout&lt;&lt;&quot;I am &quot;&lt;&lt;thread_id&lt;&lt;&quot; and I got&quot; &lt;&lt;iteration_number_local&lt;&lt;&quot;iterations.&quot;&lt;&lt;std::endl;
         }
      }
  </code></pre>
  <p>I want to highlight again that this code is only a toy-example to try to see the speedup: the first for-cycle becomes shorter when the number of parallel threads increases (since N decreases).</p>
  <p>However, when I go from 1 to 2-4 threads the number of iterations double up as expected; but this is not the case when I use 8-10-20 threads: the number of iterations does not increase linearly with the number of threads.</p>
  <p>Could you please help me with this? Is the code correct? Should I expect a near-linear speedup?</p>
  <blockquote>
  <p><strong>Results</strong></p>
  <p>Running the code above I got the following results.</p>
  <p>1 thread: 23 iterations.</p>
  <p>20 threads: 397-401 iterations per thread (instead of 420-460).</p>
  </blockquote>