Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between omp critical and omp single

I am trying to understand the exact difference between #pragma omp critical and #pragma omp single in OpenMP:

Microsoft definitions for these are:

  • Single: Lets you specify that a section of code should be executed on a single thread, not necessarily the master thread.
  • Critical: Specifies that code is only be executed on one thread at a time.

So it means that in both, the exact section of code afterwards would be executed by just one thread and other threads will not enter that section e.g. if we print something, we will see the result on screen once, right?

How about the difference? It looks that critical take care of time of execution, but not single! But I don't see any difference in practice! Does it mean that a kind of waiting or synchronization for other threads (which do not enter that section) is considered in critical, but there is nothing that holds other threads in single? How it can change the outcome in practice?

I appreciate if anyone can clarify this to me especially by an example. Thanks!

like image 950
Amir Avatar asked Oct 30 '15 17:10

Amir


People also ask

What is OMP critical?

Specifies a code block that is restricted to access by only one thread at a time.

What is OMP single?

Specifies a structured block that will be executed only once by a single thread in the team.

What is the difference between atomic and critical?

critical: the enclosed code block will be executed by only one thread at a time, and not simultaneously executed by multiple threads. It is often used to protect shared data fromrace conditions. atomic: the memory update (write, or read-modify-write) in the next instruction will be performed atomically.

What is Pragma critical?

Syntax. # pragma omp critical , (name) where name can optionally be used to identify the critical region. Identifiers naming a critical region have external linkage and occupy a namespace distinct from that used by ordinary identifiers.


1 Answers

single and critical are two very different things. As you mentioned:

  • single specifies that a section of code should be executed by single thread (not necessarily the master thread)
  • critical specifies that code is executed by one thread at a time

So the former will be executed only once while the later will be executed as many times as there are of threads.

For example the following code

int a=0, b=0; #pragma omp parallel num_threads(4) {     #pragma omp single     a++;     #pragma omp critical     b++; } printf("single: %d -- critical: %d\n", a, b); 

will print

single: 1 -- critical: 4 

I hope you see the difference better now.

For the sake of completeness, I can add that:

  • master is very similar to single with two differences:
    1. master will be executed by the master only while single can be executed by whichever thread reaching the region first; and
    2. single has an implicit barrier upon completion of the region, where all threads wait for synchronization, while master doesn't have any.
  • atomic is very similar to critical, but is restricted for a selection of simple operations.

I added these precisions since these two pairs of instructions are often the ones people tend to mix-up...

like image 199
Gilles Avatar answered Oct 03 '22 18:10

Gilles