79262092

Date: 2024-12-08 08:35:07
Score: 0.5
Natty:
Report link

OpenMP Synchronization

Atomic Operations

If, as in the example above, our critical section is a single assignment, OpenMP provides a potentially more efficient way of protecting this.

OpenMP provides an atomic directive which, like critical, specifies the next statement must be done by one thread at a time:

#pragma omp atomic global_data++;

Unlike a critical directive:

The statement under the directive can only be a single C assignment statement. It can be of the form: x++, ++x, x-- or --x. It can also be of the form x OP= expression where OP is some binary operator. No other statement is allowed. The motivation for the atomic directive is that some processors provide single instructions for operations such as x++. These are called Fetch-and-add instructions.

As a rule, if your critical section can be done in an atomic directive, it should. It will not be slower, and might be faster.

Reasons:
  • Long answer (-0.5):
  • No code block (0.5):
  • Low reputation (0.5):
Posted by: goldenhand76