title: Understanding the collapse clause in openmp
body: <p>I came across an OpenMP code that had the collapse clause, which was new to me. I'm trying to understand what it means, but I don't think I have fully grasped it's implications; One definition that I found is:</p>
  
  <blockquote>
    <p><a href="https://computing.llnl.gov/tutorials/openMP/">COLLAPSE</a>: Specifies how many loops in a nested loop should be collapsed into one large iteration space and divided according to the schedule clause. The sequential execution of the iterations in all associated loops determines the order of the iterations in the collapsed iteration space.</p>
  </blockquote>
  
  <p>I thought I understood what that meant, so I tried the follwoing simple program:</p>
  
  <pre><code>int i, j;
  #pragma omp parallel for num_threads(2) private(j)
  for (i = 0; i &lt; 4; i++)
      for (j = 0; j &lt;= i; j++)
          printf("%d %d %d\n", i, j, omp_get_thread_num());
  </code></pre>
  
  <p>Which produced</p>
  
  <pre><code>0 0 0
  1 0 0
  1 1 0
  2 0 0
  2 1 0
  2 2 1
  3 0 1
  3 1 1
  3 2 1
  3 3 1
  </code></pre>
  
  <p>I then added the <code>collapse(2)</code> clause. I expected to have the same result in the first two columns but now have an equal number of <code>0</code>'s and <code>1</code>'s in the last column.
  But I got</p>
  
  <pre><code>0 0 0
  1 0 0
  2 0 1
  3 0 1
  </code></pre>
  
  <p>So my questions are:</p>
  
  <ol>
  <li>What is happening in my code?</li>
  <li>Under what circumstances should I use <code>collapse</code>?</li>
  <li>Can you provide an example that shows the difference between using <code>collapse</code> and not using it?</li>
  </ol>