Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenMP atomic on a reference type?

The OpenMP standard (<= 4.0) says about atomic:

#pragma omp atomic [read | write | update | capture ] new-line
expression-stmt

where expression-stmt is an expression statement with one of the following forms:
...
If clause is update or not present:
x++;
...
In the preceding expressions:
x and v (as applicable) are both l-value expressions with scalar type.
...

So, when I interpret this correctly, the following short code snippet is illegal:

int main()
{
  int myCounter = 0;
  int& reference = myCounter;

  #pragma omp parallel for
  for (int i = 0; i < 100; ++i)
  {
    #pragma omp atomic
    reference++; // Increment through reference.
  }
  return 0;
}

Reason: According to this post, a reference (here int& reference) is not a scalar type. But the standard explicitly states that it must be one, in order to use atomic.

The code compiles with g++, without any warning (-Wall -Wextra).

My question is: Have I misunderstood the standard, or the concept of C++'s "reference type"? Or do most compilers compile this code, because otherwise the use of atomic is severely limited (basically no data on the heap could be the target of atomic, because you always need a reference or a dereferenced pointer)?

like image 588
Sedenion Avatar asked Jun 16 '14 16:06

Sedenion


People also ask

What are OpenMP atomic operations?

Basic OpenMP Atomic Operations Use OpenMP atomic operations to allow multiple threads to safely update a shared numeric variable, such as on hardware platforms that support atomic operation use.

What is the difference between OpenMP lock and atomic region?

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.

How can I ensure exclusive access to an OpenMP database?

However, other OpenMP synchronization can ensure the desired exclusive access. For example, a barrier that follows a series of atomic updates to x guarantees that subsequent accesses do not form a race with the atomic accesses. A compliant implementation may enforce exclusive access between atomic regions that update different storage locations.

How many pages are in the OpenMP specification?

These 8- and 12-page documents provide a quick reference to OpenMP with section numbers that refer you to where you can find greater detail in the full specification.


1 Answers

A reference type is not a scalar type. However, this fact has no bearing on your question. The important fact is that an expression that evaluates a reference to a scalar type is an lvalue with scalar type. To be specific, the variable reference has type int& but the expression reference has type int and value category lvalue. So yes, your program is conforming.

like image 169
Casey Avatar answered Oct 03 '22 06:10

Casey