1 //===- llvm/Support/Parallel.h - Parallel algorithms ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_SUPPORT_PARALLEL_H
11 #define LLVM_SUPPORT_PARALLEL_H
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/Config/llvm-config.h"
15 #include "llvm/Support/MathExtras.h"
18 #include <condition_variable>
22 #if defined(_MSC_VER) && LLVM_ENABLE_THREADS
24 #pragma warning(disable : 4530)
35 mutable std::mutex Mutex;
36 mutable std::condition_variable Cond;
39 explicit Latch(uint32_t count = 0) : Count(Count) {}
43 std::unique_lock<std::mutex> lock(Mutex);
48 std::unique_lock<std::mutex> lock(Mutex);
54 std::unique_lock<std::mutex> lock(Mutex);
55 Cond.wait(lock, [&] { return Count == 0; });
63 void spawn(std::function<void()> f);
65 void sync() const { L.sync(); }
70 struct sequential_execution_policy {};
71 struct parallel_execution_policy {};
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> {};
79 constexpr sequential_execution_policy seq{};
80 constexpr parallel_execution_policy par{};
84 #if LLVM_ENABLE_THREADS
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);
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);
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);
103 const ptrdiff_t MinParallelSize = 1024;
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)
114 : (Comp(*Mid, *Start) ? (Comp(*(End - 1), *Mid) ? Mid : End - 1)
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);
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));
134 // Move Pivot to middle of partition.
135 std::swap(*Pivot, *(End - 1));
138 TG.spawn([=, &Comp, &TG] {
139 parallel_quick_sort(Start, Pivot, Comp, TG, Depth - 1);
141 parallel_quick_sort(Pivot + 1, End, Comp, TG, Depth - 1);
144 template <class RandomAccessIterator, class Comparator>
145 void parallel_sort(RandomAccessIterator Start, RandomAccessIterator End,
146 const Comparator &Comp) {
148 parallel_quick_sort(Start, End, Comp, TG,
149 llvm::Log2_64(std::distance(Start, End)) + 1);
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;
163 while (TaskSize <= std::distance(Begin, End)) {
164 TG.spawn([=, &Fn] { std::for_each(Begin, Begin + TaskSize, Fn); });
167 TG.spawn([=, &Fn] { std::for_each(Begin, End, Fn); });
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;
178 for (; I + TaskSize < End; I += TaskSize) {
180 for (IndexTy J = I, E = I + TaskSize; J != E; ++J)
185 for (IndexTy J = I; J < End; ++J)
194 template <typename Iter>
195 using DefComparator =
196 std::less<typename std::iterator_traits<Iter>::value_type>;
198 } // namespace detail
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);
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);
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)
225 // Parallel algorithm implementations, only available when LLVM_ENABLE_THREADS
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);
235 template <class IterTy, class FuncTy>
236 void for_each(parallel_execution_policy policy, IterTy Begin, IterTy End,
238 detail::parallel_for_each(Begin, End, Fn);
241 template <class IndexTy, class FuncTy>
242 void for_each_n(parallel_execution_policy policy, IndexTy Begin, IndexTy End,
244 detail::parallel_for_each_n(Begin, End, Fn);
248 } // namespace parallel
251 #endif // LLVM_SUPPORT_PARALLEL_H