12

I'm currently trying to learn ow to use OpenMP and I have a question. Is it safe to do something like that :

  std::atomic<double> result;
  #pragma omp parallel for
  for(...)
  {
  result+= //some stuff;
  }

Or shall I use :

  double result;
  #pragma omp parallel for
  for(...)
  {
    double tmp=0;
    //some stuff;
    #pragma omp atomic
    result+=tmp;
  }

Thanks !

Edit : I know the most simple way to handle that is using an array, but Im asking because I'm curious

Davidbrcz
  • 2,335
  • 18
  • 27
  • 8
    `std::atomic` is a facility provided by the C++11 standard's multithreading framework. If you use `std::atomic` in combination with external libraries like OpenMP, which provide their own implementations of such features, you will likely run into problems. – Marc Claesen Feb 04 '14 at 13:43
  • 6
    Actually, the atomic library is specified in a different chapter in the standard than the thread library. Also, I haven't found any remark in the atomic chapter saying that this library is only intended for use with std::thread. – gTcV Feb 04 '14 at 14:05
  • i agree with @gTcV - atomic types operate with c++ memory model, not threading implementation. For example,this would make use of smart pointers inside omp sections cause race condition for reference counter – Victor Proon Jul 06 '16 at 07:44
  • 1
    GCC used to generate an error for the case where you want to use C11 or C++11 atomics inside OpenMP parallel regions. If OpenMP atomics don't introduce any additional complexity than using C++11's, I would stick with the former, since you will only depend on OpenMP standard (you could use compilers without C++11 support). – Jorge Bellon Nov 10 '16 at 21:44
  • 3
    Possible duplicate of [Mixing C++11 atomics and OpenMP](https://stackoverflow.com/questions/41309299/mixing-c11-atomics-and-openmp) – Zulan Dec 06 '18 at 18:55

2 Answers2

3

Officially, no. In practice, probably.

Page Section 1.7 page 32 of the OpenMP 5.0 Specification says:

While future versions of the OpenMP specification are expected to address the following features, currently their use may result in unspecified behavior.

  • Concurrency

  • Additions to the standard library

  • C++11 Library

However, depending on the implementation of the OpenMP runtime you use, it might be alright. In fact, the LLVM OpenMP runtime even uses std::atomic to implement some of the OpenMP specification.

The safest option though is to stick with using only what OpenMP provides. Anything you can do using std::atomic you should also be able to achieve using only OpenMP.

Community
  • 1
  • 1
Increasingly Idiotic
  • 5,700
  • 5
  • 35
  • 73
  • 2
    This has been fixed in OpenMP 5.1: https://www.openmp.org/wp-content/uploads/OpenMP-API-Specification-5-1.pdf – kc9jud May 08 '21 at 04:54
-2

Since atomics slow down parallel execution and don't scale well, better do

pragma omp parallel for reduction(+:result)
for(...)
{
  double tmp=0;
  //some stuff;
  result+=tmp;
}
TNA
  • 2,595
  • 1
  • 14
  • 19