LLVM  10.0.0svn
Parallel.h
Go to the documentation of this file.
1 //===- llvm/Support/Parallel.h - Parallel algorithms ----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_SUPPORT_PARALLEL_H
10 #define LLVM_SUPPORT_PARALLEL_H
11 
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/Config/llvm-config.h"
15 
16 #include <algorithm>
17 #include <condition_variable>
18 #include <functional>
19 #include <mutex>
20 
21 namespace llvm {
22 
23 namespace parallel {
26 
27 template <typename T>
29  : public std::integral_constant<
30  bool, llvm::is_one_of<T, sequential_execution_policy,
31  parallel_execution_policy>::value> {};
32 
35 
36 namespace detail {
37 
38 #if LLVM_ENABLE_THREADS
39 
40 class Latch {
41  uint32_t Count;
42  mutable std::mutex Mutex;
43  mutable std::condition_variable Cond;
44 
45 public:
46  explicit Latch(uint32_t Count = 0) : Count(Count) {}
47  ~Latch() { sync(); }
48 
49  void inc() {
50  std::lock_guard<std::mutex> lock(Mutex);
51  ++Count;
52  }
53 
54  void dec() {
55  std::lock_guard<std::mutex> lock(Mutex);
56  if (--Count == 0)
57  Cond.notify_all();
58  }
59 
60  void sync() const {
61  std::unique_lock<std::mutex> lock(Mutex);
62  Cond.wait(lock, [&] { return Count == 0; });
63  }
64 };
65 
66 class TaskGroup {
67  Latch L;
68  bool Parallel;
69 
70 public:
71  TaskGroup();
72  ~TaskGroup();
73 
74  void spawn(std::function<void()> f);
75 
76  void sync() const { L.sync(); }
77 };
78 
80 
81 /// Inclusive median.
82 template <class RandomAccessIterator, class Comparator>
83 RandomAccessIterator medianOf3(RandomAccessIterator Start,
84  RandomAccessIterator End,
85  const Comparator &Comp) {
86  RandomAccessIterator Mid = Start + (std::distance(Start, End) / 2);
87  return Comp(*Start, *(End - 1))
88  ? (Comp(*Mid, *(End - 1)) ? (Comp(*Start, *Mid) ? Mid : Start)
89  : End - 1)
90  : (Comp(*Mid, *Start) ? (Comp(*(End - 1), *Mid) ? Mid : End - 1)
91  : Start);
92 }
93 
94 template <class RandomAccessIterator, class Comparator>
95 void parallel_quick_sort(RandomAccessIterator Start, RandomAccessIterator End,
96  const Comparator &Comp, TaskGroup &TG, size_t Depth) {
97  // Do a sequential sort for small inputs.
98  if (std::distance(Start, End) < detail::MinParallelSize || Depth == 0) {
99  llvm::sort(Start, End, Comp);
100  return;
101  }
102 
103  // Partition.
104  auto Pivot = medianOf3(Start, End, Comp);
105  // Move Pivot to End.
106  std::swap(*(End - 1), *Pivot);
107  Pivot = std::partition(Start, End - 1, [&Comp, End](decltype(*Start) V) {
108  return Comp(V, *(End - 1));
109  });
110  // Move Pivot to middle of partition.
111  std::swap(*Pivot, *(End - 1));
112 
113  // Recurse.
114  TG.spawn([=, &Comp, &TG] {
115  parallel_quick_sort(Start, Pivot, Comp, TG, Depth - 1);
116  });
117  parallel_quick_sort(Pivot + 1, End, Comp, TG, Depth - 1);
118 }
119 
120 template <class RandomAccessIterator, class Comparator>
121 void parallel_sort(RandomAccessIterator Start, RandomAccessIterator End,
122  const Comparator &Comp) {
123  TaskGroup TG;
124  parallel_quick_sort(Start, End, Comp, TG,
125  llvm::Log2_64(std::distance(Start, End)) + 1);
126 }
127 
128 template <class IterTy, class FuncTy>
129 void parallel_for_each(IterTy Begin, IterTy End, FuncTy Fn) {
130  // TaskGroup has a relatively high overhead, so we want to reduce
131  // the number of spawn() calls. We'll create up to 1024 tasks here.
132  // (Note that 1024 is an arbitrary number. This code probably needs
133  // improving to take the number of available cores into account.)
134  ptrdiff_t TaskSize = std::distance(Begin, End) / 1024;
135  if (TaskSize == 0)
136  TaskSize = 1;
137 
138  TaskGroup TG;
139  while (TaskSize < std::distance(Begin, End)) {
140  TG.spawn([=, &Fn] { std::for_each(Begin, Begin + TaskSize, Fn); });
141  Begin += TaskSize;
142  }
143  std::for_each(Begin, End, Fn);
144 }
145 
146 template <class IndexTy, class FuncTy>
147 void parallel_for_each_n(IndexTy Begin, IndexTy End, FuncTy Fn) {
148  ptrdiff_t TaskSize = (End - Begin) / 1024;
149  if (TaskSize == 0)
150  TaskSize = 1;
151 
152  TaskGroup TG;
153  IndexTy I = Begin;
154  for (; I + TaskSize < End; I += TaskSize) {
155  TG.spawn([=, &Fn] {
156  for (IndexTy J = I, E = I + TaskSize; J != E; ++J)
157  Fn(J);
158  });
159  }
160  for (IndexTy J = I; J < End; ++J)
161  Fn(J);
162 }
163 
164 #endif
165 
166 template <typename Iter>
167 using DefComparator =
168  std::less<typename std::iterator_traits<Iter>::value_type>;
169 
170 } // namespace detail
171 
172 // sequential algorithm implementations.
173 template <class Policy, class RandomAccessIterator,
175 void sort(Policy policy, RandomAccessIterator Start, RandomAccessIterator End,
176  const Comparator &Comp = Comparator()) {
178  "Invalid execution policy!");
179  llvm::sort(Start, End, Comp);
180 }
181 
182 template <class Policy, class IterTy, class FuncTy>
183 void for_each(Policy policy, IterTy Begin, IterTy End, FuncTy Fn) {
185  "Invalid execution policy!");
186  std::for_each(Begin, End, Fn);
187 }
188 
189 template <class Policy, class IndexTy, class FuncTy>
190 void for_each_n(Policy policy, IndexTy Begin, IndexTy End, FuncTy Fn) {
192  "Invalid execution policy!");
193  for (IndexTy I = Begin; I != End; ++I)
194  Fn(I);
195 }
196 
197 // Parallel algorithm implementations, only available when LLVM_ENABLE_THREADS
198 // is true.
199 #if LLVM_ENABLE_THREADS
200 template <class RandomAccessIterator,
202 void sort(parallel_execution_policy policy, RandomAccessIterator Start,
203  RandomAccessIterator End, const Comparator &Comp = Comparator()) {
204  detail::parallel_sort(Start, End, Comp);
205 }
206 
207 template <class IterTy, class FuncTy>
208 void for_each(parallel_execution_policy policy, IterTy Begin, IterTy End,
209  FuncTy Fn) {
210  detail::parallel_for_each(Begin, End, Fn);
211 }
212 
213 template <class IndexTy, class FuncTy>
214 void for_each_n(parallel_execution_policy policy, IndexTy Begin, IndexTy End,
215  FuncTy Fn) {
216  detail::parallel_for_each_n(Begin, End, Fn);
217 }
218 #endif
219 
220 } // namespace parallel
221 } // namespace llvm
222 
223 #endif // LLVM_SUPPORT_PARALLEL_H
const ptrdiff_t MinParallelSize
Definition: Parallel.h:79
void for_each_n(Policy policy, IndexTy Begin, IndexTy End, FuncTy Fn)
Definition: Parallel.h:190
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void parallel_for_each_n(IndexTy Begin, IndexTy End, FuncTy Fn)
Definition: Parallel.h:147
constexpr sequential_execution_policy seq
Definition: Parallel.h:33
Latch(uint32_t Count=0)
Definition: Parallel.h:46
SmartMutex< false > Mutex
Mutex - A standard, always enforced mutex.
Definition: Mutex.h:68
auto partition(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::partition which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1254
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1095
constexpr parallel_execution_policy par
Definition: Parallel.h:34
void for_each(Policy policy, IterTy Begin, IterTy End, FuncTy Fn)
Definition: Parallel.h:183
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:940
std::less< typename std::iterator_traits< Iter >::value_type > DefComparator
Definition: Parallel.h:168
void parallel_for_each(IterTy Begin, IterTy End, FuncTy Fn)
Definition: Parallel.h:129
#define I(x, y, z)
Definition: MD5.cpp:58
void parallel_sort(RandomAccessIterator Start, RandomAccessIterator End, const Comparator &Comp)
Definition: Parallel.h:121
RandomAccessIterator medianOf3(RandomAccessIterator Start, RandomAccessIterator End, const Comparator &Comp)
Inclusive median.
Definition: Parallel.h:83
void for_each(parallel_execution_policy policy, IterTy Begin, IterTy End, FuncTy Fn)
Definition: Parallel.h:208
void parallel_quick_sort(RandomAccessIterator Start, RandomAccessIterator End, const Comparator &Comp, TaskGroup &TG, size_t Depth)
Definition: Parallel.h:95
void spawn(std::function< void()> f)
Definition: Parallel.cpp:103
print Print MemDeps of function
void sort(Policy policy, RandomAccessIterator Start, RandomAccessIterator End, const Comparator &Comp=Comparator())
Definition: Parallel.h:175
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:581