4
votes

Considering the following scenario:

Function A creates a layer of OMP parallel region, and each OMP thread make a call to a function B, which itself contain another layer of OMP parallel region.

Then if within the parallel region of function B, there is a OMP critcal region, then, does that region is critical "globally" with respect to all threads created by function A and B, or it is merely locally to function B?

And what if B is a pre-bulit function (e.g. static or dynamic linked libraries)?

1

1 Answers

5
votes

Critical regions in OpenMP have global binding and their scope extends to all occurrences of the critical construct that have the same name (in that respect all unnamed constructs share the same special internal name), no matter where they occur in the code. You can read about the binding of each construct in the corresponding Binding section of the OpenMP specification. For the critical construct you have:

The binding thread set for a critical region is all threads. Region execution is restricted to a single thread at a time among all the threads in the program, without regard to the team(s) to which the threads belong.

(HI: emphasis mine)

That's why it is strongly recommended that named critical regions should be used, especially if the sets of protected resources are disjoint, e.g.:

// This one located inside a parallel region in fun1
#pragma omp critical(fun1)
{
   // Modify shared variables a and b
}

...

// This one located inside a parallel region in fun2
#pragma omp critical(fun2)
{
   // Modify shared variables c and d
}

Naming the regions eliminates the chance that two unrelated critical construct could block each other.

As to the second part of your question, to support the dynamic scoping requirements of the OpenMP specification, critical regions are usually implemented with named mutexes that are resolved at run-time. Therefore it is possible to have homonymous critical regions in a prebuilt library function and in your code and it will work as expected as long as both codes are using the same OpenMP runtime, e.g. both were built using the same compiler suite. Cross-suite OpenMP compatibility is usually not guaranteed. Also if in B() there is an unnamed critical region, it will interfere with all unnamed critical regions in the rest of the code, no matter if they are part the same library code of belong to the user code.