All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Parallel.h
Go to the documentation of this file.
1 /*
2  * Header which provides parallel STL implementations when available.
3  */
4 #ifndef PARALLEL_H
5 #define PARALLEL_H
6 
7 #include <atomic>
8 #include <functional>
9 
10 #if ((defined(__GNUC__) || defined(__GNUG__)) && !(defined(__clang__) || defined(__INTEL_COMPILER))) && defined _OPENMP
11 #include <parallel/algorithm>
12 #else
13 #define NETWORKIT_NO_PARALLEL_STL
14 #include <algorithm>
15 #endif
16 
17 namespace Aux {
18  namespace Parallel {
19  #ifdef NETWORKIT_NO_PARALLEL_STL
20  using std::sort;
21  using std::max_element;
22  #else
23  using __gnu_parallel::sort;
24  using __gnu_parallel::max_element;
25  #endif
26 
27  template <typename ValueType, typename Comp>
28  void atomic_set(std::atomic<ValueType> &target, const ValueType &input, Comp shallSet) {
29  // load current value
30  auto curValue = target.load(std::memory_order_relaxed);
31 
32  do {
33  if (!shallSet(input, curValue)) break; // skip if shall not be set (anymore)
34  // set new value unless current value has been changed in the meantime, if current value has changed load it (so we can compare again)
35  } while (!target.compare_exchange_weak(curValue, input, std::memory_order_release, std::memory_order_relaxed));
36  }
37 
38  template <typename ValueType>
39  void atomic_max(std::atomic<ValueType> &target, const ValueType &input) {
40  atomic_set(target, input, std::greater<ValueType>());
41  }
42 
43  template <typename ValueType>
44  void atomic_min(std::atomic<ValueType> &target, const ValueType &input) {
45  atomic_set(target, input, std::less<ValueType>());
46  }
47 
48  }
49 }
50 
51 #endif
void atomic_max(std::atomic< ValueType > &target, const ValueType &input)
Definition: Parallel.h:39
void atomic_set(std::atomic< ValueType > &target, const ValueType &input, Comp shallSet)
Definition: Parallel.h:28
void atomic_min(std::atomic< ValueType > &target, const ValueType &input)
Definition: Parallel.h:44