Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In a situation like the following:</p> <pre><code>#pragma omp parallel { #pragma omp for for(int ii = 0; ii &lt; n; ii++) { /* ... */ #pragma omp for for(int jj = 0; jj &lt; m; jj++) { /* ... */ } } } </code></pre> <p>what happens is that you trigger an undefined behavior as you violate the OpenMP standard. More precisely you violate the restrictions appearing in section 2.5 (worksharing constructs):</p> <blockquote> <p>The following restrictions apply to worksharing constructs:</p> <ul> <li>Each worksharing region must be encountered by all threads in a team or by none at all.</li> <li>The sequence of worksharing regions and barrier regions encountered must be the same for every thread in a team.</li> </ul> </blockquote> <p>This is clearly shown in the examples <a href="http://www.openmp.org/mp-documents/OpenMP3.1.pdf" rel="nofollow">A.39.1c and A.40.1c</a>:</p> <p><strong>Example A.39.1c</strong>: The following example of loop construct nesting <em>is conforming</em> because the inner and outer loop regions bind to different parallel regions:</p> <pre><code>void work(int i, int j) {} void good_nesting(int n) { int i, j; #pragma omp parallel default(shared) { #pragma omp for for (i=0; i&lt;n; i++) { #pragma omp parallel shared(i, n) { #pragma omp for for (j=0; j &lt; n; j++) work(i, j); } } } } </code></pre> <p><strong>Example A.40.1c</strong>: The following example is <em>non-conforming</em> because the inner and outer loop regions are closely nested</p> <pre><code>void work(int i, int j) {} void wrong1(int n) { #pragma omp parallel default(shared) { int i, j; #pragma omp for for (i=0; i&lt;n; i++) { /* incorrect nesting of loop regions */ #pragma omp for for (j=0; j&lt;n; j++) work(i, j); } } } </code></pre> <p>Notice that this is different from:</p> <pre><code>#pragma omp parallel for for(int ii = 0; ii &lt; n; ii++) { /* ... */ #pragma omp parallel for for(int jj = 0; jj &lt; m; jj++) { /* ... */ } } </code></pre> <p>in which you try to spawn a nested parallel region. Only in this case the discussion of Mysticial answer holds.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload