Omp pragmas outside omp parallel block

Can omp pragmas be used, such as critical, single, main, or a barrier outside of a parallel omp block? I have a function that can be called either from a parallel OMP block or not. If so, I need to attach a piece of code in the critical section. In other words, is this code good?

void myfunc(){ #pragma omp critical { /* code */ } } // not inside an omp parallel region myfunc(); #pragma omp parallel { // inside an omp parallel region myfunc(); } 

I did not mention this in the OpenMP documentation. I assume that the code should behave exactly the same as when executing 1 thread - and so it works with gcc. I would like to know if this behavior is portable, or is that what the specification does not define, and something to be expected.

+8
c ++ c openmp
source share
1 answer

According to this document:

The DO / for, SECTIONS, SINGLE, MASTER, and BARRIER directives are bound to dynamically close PARALLEL, if one exists. If the parallel scope is not currently running, the directives do not apply.

Thus, the answer is that pragmas can be used outside the parallel area. Although I still have not found it explicitly written in the documentation.

+2
source share

All Articles