Is it ok to use omp pragmas like critical, single, master, or barrier outside of an omp parallel block? I have a function that can be called either from an OMP parallel block, or not. If yes, I need to enclose part of the code in a critical section. In other words, is this code fine?
void myfunc(){
#pragma omp critical
{ /* code */ }
}
// not inside an omp parallel region
myfunc();
#pragma omp parallel
{
// inside an omp parallel region
myfunc();
}
I have found no mention of this in the OpenMP documentation. I guess the code should behave exactly like with 1 thread execution - and this is how it works with gcc. I would like to know if this behavior is portable, or is it something that the specification does not define and anything can be expected.
According to this document:
The DO/for, SECTIONS, SINGLE, MASTER and BARRIER directives bind to the dynamically enclosing PARALLEL, if one exists. If no parallel region is currently being executed, the directives have no effect.
So the answer is those pragmas can be used outside a parallel region. Although I still do not find it explicitly written in the documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With