OSDN Git Service

[Support] Move Parallel algorithms from LLD to LLVM.
[android-x86/external-llvm.git] / include / llvm / Support / Parallel.h
1 //===- llvm/Support/Parallel.h - Parallel algorithms ----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_SUPPORT_PARALLEL_H
11 #define LLVM_SUPPORT_PARALLEL_H
12
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/Config/llvm-config.h"
15 #include "llvm/Support/MathExtras.h"
16
17 #include <algorithm>
18 #include <condition_variable>
19 #include <functional>
20 #include <mutex>
21
22 #if defined(_MSC_VER) && LLVM_ENABLE_THREADS
23 #pragma warning(push)
24 #pragma warning(disable : 4530)
25 #include <concrt.h>
26 #include <ppl.h>
27 #pragma warning(pop)
28 #endif
29
30 namespace llvm {
31
32 namespace detail {
33 class Latch {
34   uint32_t Count;
35   mutable std::mutex Mutex;
36   mutable std::condition_variable Cond;
37
38 public:
39   explicit Latch(uint32_t count = 0) : Count(Count) {}
40   ~Latch() { sync(); }
41
42   void inc() {
43     std::unique_lock<std::mutex> lock(Mutex);
44     ++Count;
45   }
46
47   void dec() {
48     std::unique_lock<std::mutex> lock(Mutex);
49     if (--Count == 0)
50       Cond.notify_all();
51   }
52
53   void sync() const {
54     std::unique_lock<std::mutex> lock(Mutex);
55     Cond.wait(lock, [&] { return Count == 0; });
56   }
57 };
58
59 class TaskGroup {
60   Latch L;
61
62 public:
63   void spawn(std::function<void()> f);
64
65   void sync() const { L.sync(); }
66 };
67 }
68
69 namespace parallel {
70 struct sequential_execution_policy {};
71 struct parallel_execution_policy {};
72
73 template <typename T>
74 struct is_execution_policy
75     : public std::integral_constant<
76           bool, llvm::is_one_of<T, sequential_execution_policy,
77                                 parallel_execution_policy>::value> {};
78
79 constexpr sequential_execution_policy seq{};
80 constexpr parallel_execution_policy par{};
81
82 namespace detail {
83
84 #if LLVM_ENABLE_THREADS
85
86 #if defined(_MSC_VER)
87 template <class RandomAccessIterator, class Comparator>
88 void parallel_sort(RandomAccessIterator Start, RandomAccessIterator End,
89                    const Comparator &Comp) {
90   concurrency::parallel_sort(Start, End, Comp);
91 }
92 template <class IterTy, class FuncTy>
93 void parallel_for_each(IterTy Begin, IterTy End, FuncTy Fn) {
94   concurrency::parallel_for_each(Begin, End, Fn);
95 }
96
97 template <class IndexTy, class FuncTy>
98 void parallel_for_each_n(IndexTy Begin, IndexTy End, FuncTy Fn) {
99   concurrency::parallel_for(Begin, End, Fn);
100 }
101
102 #else
103 const ptrdiff_t MinParallelSize = 1024;
104
105 /// \brief Inclusive median.
106 template <class RandomAccessIterator, class Comparator>
107 RandomAccessIterator medianOf3(RandomAccessIterator Start,
108                                RandomAccessIterator End,
109                                const Comparator &Comp) {
110   RandomAccessIterator Mid = Start + (std::distance(Start, End) / 2);
111   return Comp(*Start, *(End - 1))
112              ? (Comp(*Mid, *(End - 1)) ? (Comp(*Start, *Mid) ? Mid : Start)
113                                        : End - 1)
114              : (Comp(*Mid, *Start) ? (Comp(*(End - 1), *Mid) ? Mid : End - 1)
115                                    : Start);
116 }
117
118 template <class RandomAccessIterator, class Comparator>
119 void parallel_quick_sort(RandomAccessIterator Start, RandomAccessIterator End,
120                          const Comparator &Comp, TaskGroup &TG, size_t Depth) {
121   // Do a sequential sort for small inputs.
122   if (std::distance(Start, End) < detail::MinParallelSize || Depth == 0) {
123     std::sort(Start, End, Comp);
124     return;
125   }
126
127   // Partition.
128   auto Pivot = medianOf3(Start, End, Comp);
129   // Move Pivot to End.
130   std::swap(*(End - 1), *Pivot);
131   Pivot = std::partition(Start, End - 1, [&Comp, End](decltype(*Start) V) {
132     return Comp(V, *(End - 1));
133   });
134   // Move Pivot to middle of partition.
135   std::swap(*Pivot, *(End - 1));
136
137   // Recurse.
138   TG.spawn([=, &Comp, &TG] {
139     parallel_quick_sort(Start, Pivot, Comp, TG, Depth - 1);
140   });
141   parallel_quick_sort(Pivot + 1, End, Comp, TG, Depth - 1);
142 }
143
144 template <class RandomAccessIterator, class Comparator>
145 void parallel_sort(RandomAccessIterator Start, RandomAccessIterator End,
146                    const Comparator &Comp) {
147   TaskGroup TG;
148   parallel_quick_sort(Start, End, Comp, TG,
149                       llvm::Log2_64(std::distance(Start, End)) + 1);
150 }
151
152 template <class IterTy, class FuncTy>
153 void parallel_for_each(IterTy Begin, IterTy End, FuncTy Fn) {
154   // TaskGroup has a relatively high overhead, so we want to reduce
155   // the number of spawn() calls. We'll create up to 1024 tasks here.
156   // (Note that 1024 is an arbitrary number. This code probably needs
157   // improving to take the number of available cores into account.)
158   ptrdiff_t TaskSize = std::distance(Begin, End) / 1024;
159   if (TaskSize == 0)
160     TaskSize = 1;
161
162   TaskGroup TG;
163   while (TaskSize <= std::distance(Begin, End)) {
164     TG.spawn([=, &Fn] { std::for_each(Begin, Begin + TaskSize, Fn); });
165     Begin += TaskSize;
166   }
167   TG.spawn([=, &Fn] { std::for_each(Begin, End, Fn); });
168 }
169
170 template <class IndexTy, class FuncTy>
171 void parallel_for_each_n(IndexTy Begin, IndexTy End, FuncTy Fn) {
172   ptrdiff_t TaskSize = (End - Begin) / 1024;
173   if (TaskSize == 0)
174     TaskSize = 1;
175
176   TaskGroup TG;
177   IndexTy I = Begin;
178   for (; I + TaskSize < End; I += TaskSize) {
179     TG.spawn([=, &Fn] {
180       for (IndexTy J = I, E = I + TaskSize; J != E; ++J)
181         Fn(J);
182     });
183   }
184   TG.spawn([=, &Fn] {
185     for (IndexTy J = I; J < End; ++J)
186       Fn(J);
187   });
188 }
189
190 #endif
191
192 #endif
193
194 template <typename Iter>
195 using DefComparator =
196     std::less<typename std::iterator_traits<Iter>::value_type>;
197
198 } // namespace detail
199
200 // sequential algorithm implementations.
201 template <class Policy, class RandomAccessIterator,
202           class Comparator = detail::DefComparator<RandomAccessIterator>>
203 void sort(Policy policy, RandomAccessIterator Start, RandomAccessIterator End,
204           const Comparator &Comp = Comparator()) {
205   static_assert(is_execution_policy<Policy>::value,
206                 "Invalid execution policy!");
207   std::sort(Start, End, Comp);
208 }
209
210 template <class Policy, class IterTy, class FuncTy>
211 void for_each(Policy policy, IterTy Begin, IterTy End, FuncTy Fn) {
212   static_assert(is_execution_policy<Policy>::value,
213                 "Invalid execution policy!");
214   std::for_each(Begin, End, Fn);
215 }
216
217 template <class Policy, class IndexTy, class FuncTy>
218 void for_each_n(Policy policy, IndexTy Begin, IndexTy End, FuncTy Fn) {
219   static_assert(is_execution_policy<Policy>::value,
220                 "Invalid execution policy!");
221   for (IndexTy I = Begin; I != End; ++I)
222     Fn(I);
223 }
224
225 // Parallel algorithm implementations, only available when LLVM_ENABLE_THREADS
226 // is true.
227 #if LLVM_ENABLE_THREADS
228 template <class RandomAccessIterator,
229           class Comparator = detail::DefComparator<RandomAccessIterator>>
230 void sort(parallel_execution_policy policy, RandomAccessIterator Start,
231           RandomAccessIterator End, const Comparator &Comp = Comparator()) {
232   detail::parallel_sort(Start, End, Comp);
233 }
234
235 template <class IterTy, class FuncTy>
236 void for_each(parallel_execution_policy policy, IterTy Begin, IterTy End,
237               FuncTy Fn) {
238   detail::parallel_for_each(Begin, End, Fn);
239 }
240
241 template <class IndexTy, class FuncTy>
242 void for_each_n(parallel_execution_policy policy, IndexTy Begin, IndexTy End,
243                 FuncTy Fn) {
244   detail::parallel_for_each_n(Begin, End, Fn);
245 }
246 #endif
247
248 } // namespace parallel
249 } // namespace llvm
250
251 #endif // LLVM_SUPPORT_PARALLEL_H