Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

omp pragmas outside omp parallel block

Tags:

c++

c

openmp

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.

like image 421
angainor Avatar asked Sep 07 '12 11:09

angainor


1 Answers

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.

like image 56
angainor Avatar answered Nov 03 '22 08:11

angainor