omp atomic

Ensures that a specific memory location is updated atomically, which prevents the possibility of multiple, simultaneous reading and writing of threads.

Syntax

#pragma omp atomic [ read | write | update | capture ]

expression-stmt

OR

#pragma omp atomic capture

structured-block

Arguments

If the clause is

read

expression-stmt is of the form v = x;

write

expression-stmt is of the form x = expr;

update (or not present)

expression-stmt is one of the forms:

x++;

x--;

++x;

--x;

x binop= expr;

capture

expression-stmt is one of the forms:

v = x++;

v = x--;

v = ++x;

v = --x;

v = x binop= expr;

OR if a structured-block is used, it is in one of the following forms:

{v = x; x binop= expr}

{x binop= expr; v = x}

In the above expressions:

  • x and v (as applicable) are both l-value expressions with scalar type.

  • During the execution of an atomic region, multiple syntactic occurrences of x must designate the same storage location.

  • Neither of v and expr (as applicable) may access the storage location designated by x.

  • expr is an expression with scalar type.

  • binop is one of +, *, -, /, &, ^, |, <<, or >>.

  • binop, binop=, ++, and -- are not overloaded operators.

Description

The binding thread set for an atomic region is all threads. Atomic regions enforce exclusive access with respect to other atomic regions that access the same storage location x among all the threads in the program without regard to the teams to which the threads belong

Note that the following restriction applies to the #pragma omp atomic: all atomic accesses to the storage locations designated by x throughout the program must have the same type and type parameters.

The following table describes what happens when you specify one of the clauses in an atomic construct.

Clause

Result

read

Causes an atomic read of the location designated by x regardless of the native machine word size.

write

Causes an atomic write of the location designated by x regardless of the native machine word size.

[update]

Causes an atomic update of the location designated by x using the designated operator or intrinsic. The following rules also apply:

  • The evaluation of expr or expr-list need not be atomic with respect to the read or write of the location designated by x.
  • No task scheduling points occur between the read and the write of the location designated by x.

capture

Causes an atomic update of the location designated by x using the designated operator or intrinsic while also capturing the original or final value of the location designated by x with respect to the atomic update. The following rules also apply:

  • The original or final value of the location designated by x is written in the location designated by v, depending on the form of the atomic construct, structured block, or statements, following the usual language semantics.
  • Only the read and write of the location designated by x are performed mutually atomically.
  • The evaluation of expr or expr-list, and the write to the location designated by v do not need to be atomic with respect to the read or write of the location designated by x.

No task scheduling points occur between the read and the write of the location designated by x.

Any combination of two or more of these atomic constructs enforces mutually exclusive access to the locations designated by x.

A race condition exists when two unsynchronized threads access the same shared variable with at least one thread modifying the variable; this can cause unpredictable results. To avoid race conditions, all accesses of the locations designated by x that could potentially occur in parallel must be protected with an atomic construct.

Atomic regions do not guarantee exclusive access with respect to any accesses outside of atomic regions to the same storage location x even if those accesses occur during a critical or ordered region, while an OpenMP* lock is owned by the executing task, or during the execution of a reduction clause.

However, other OpenMP* synchronization can ensure the desired exclusive access. For example, a barrier pragma following a series of atomic updates to x guarantees that subsequent accesses do not form a race condition with the atomic accesses.

Examples:

#pragma omp atomic update
     k += n*mass;		 // k is updated atomically

#pragma omp atomic read
     tmp = c;		//  c is read atomically 

#pragma omp atomic write
     count = n*m;		// count is written atomically

#pragma omp atomic capture
     { d = v; v += n; } // atomically update v, but capture original value in d

#pragma omp atomic capture
     o = ++c;	// atomically update c, then capture that value


Submit feedback on this help topic

Copyright © 1996-2011, Intel Corporation. All rights reserved.