title: OpenMP with "collapse()" for nested for-loops performs worse when without
body: <p>This is my code:</p>
  
  <pre><code>double res1[NNN];  
  #pragma omp parallel for collapse(3) schedule(dynamic) 
  for (int i=0; i&lt;NNN; i++)
  {
      for (int j=0;j&lt;NNN;j++)
      {
          for (int k=0;k&lt;NNN;k++)
          {
              res1[i] = log(fabs(i*j*k));
          }
      }
  }
  std::cout&lt;&lt; res1[10] &lt;&lt; std::endl;
  </code></pre>
  
  <p>When I use <code>collapse(3)</code> it takes ~50 sec; without <code>collapse(3)</code> only ~6-7 seconds. I am very puzzled by this behavior, since I would have expected a better performance with "collapse" than without.</p>
  
  <p>Am I missing something?</p>
  
  <hr>
  
  <p>I did some experiments and played with different configs:</p>
  
  <p>(NNN = 2500 and 24 cores)</p>
  
  <ol>
  <li><code>schedule(STATIC)</code> &amp;&amp; <code>collapse(3)</code> -> ~54 sec</li>
  <li><code>schedule(STATIC)</code> &amp;&amp; <code>collapse(2)</code> ->  ~8 sec</li>
  <li><code>schedule(STATIC)</code> &amp;&amp; <code>collapse(1)</code> ->  ~8 sec</li>
  </ol>
  
  <p>I also tried with <code>DYNAMIC</code> schedule, but it takes enormous time (several minutes).</p>
  
  <hr>
  
  <p>In my original problem, I have 4 DIM "for-loops" (4D array): 51x23x51x23.</p>
  
  <p>What is the best way to use OpenMP/MPI to minimize running time? I have in total ~300 CPU cores. What is the best way to spread my array over these cores? The length of array is flexible (I can fit it to the number of CPUs in some way).</p>
  
  <p>Any suggestions?</p>