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)
33 struct sequential_execution_policy {};
34 struct parallel_execution_policy {};
37 struct is_execution_policy
38 : public std::integral_constant<
39 bool, llvm::is_one_of<T, sequential_execution_policy,
40 parallel_execution_policy>::value> {};
42 constexpr sequential_execution_policy seq{};
43 constexpr parallel_execution_policy par{};
47 #if LLVM_ENABLE_THREADS
51 mutable std::mutex Mutex;
52 mutable std::condition_variable Cond;
55 explicit Latch(uint32_t Count = 0) : Count(Count) {}
59 std::lock_guard<std::mutex> lock(Mutex);
64 std::lock_guard<std::mutex> lock(Mutex);
70 std::unique_lock<std::mutex> lock(Mutex);
71 Cond.wait(lock, [&] { return Count == 0; });
79 void spawn(std::function<void()> f);
81 void sync() const { L.sync(); }
85 template <class RandomAccessIterator, class Comparator>
86 void parallel_sort(RandomAccessIterator Start, RandomAccessIterator End,
87 const Comparator &Comp) {
88 concurrency::parallel_sort(Start, End, Comp);
90 template <class IterTy, class FuncTy>
91 void parallel_for_each(IterTy Begin, IterTy End, FuncTy Fn) {
92 concurrency::parallel_for_each(Begin, End, Fn);
95 template <class IndexTy, class FuncTy>
96 void parallel_for_each_n(IndexTy Begin, IndexTy End, FuncTy Fn) {
97 concurrency::parallel_for(Begin, End, Fn);
101 const ptrdiff_t MinParallelSize = 1024;
103 /// Inclusive median.
104 template <class RandomAccessIterator, class Comparator>
105 RandomAccessIterator medianOf3(RandomAccessIterator Start,
106 RandomAccessIterator End,
107 const Comparator &Comp) {
108 RandomAccessIterator Mid = Start + (std::distance(Start, End) / 2);
109 return Comp(*Start, *(End - 1))
110 ? (Comp(*Mid, *(End - 1)) ? (Comp(*Start, *Mid) ? Mid : Start)
112 : (Comp(*Mid, *Start) ? (Comp(*(End - 1), *Mid) ? Mid : End - 1)
116 template <class RandomAccessIterator, class Comparator>
117 void parallel_quick_sort(RandomAccessIterator Start, RandomAccessIterator End,
118 const Comparator &Comp, TaskGroup &TG, size_t Depth) {
119 // Do a sequential sort for small inputs.
120 if (std::distance(Start, End) < detail::MinParallelSize || Depth == 0) {
121 llvm::sort(Start, End, Comp);
126 auto Pivot = medianOf3(Start, End, Comp);
127 // Move Pivot to End.
128 std::swap(*(End - 1), *Pivot);
129 Pivot = std::partition(Start, End - 1, [&Comp, End](decltype(*Start) V) {
130 return Comp(V, *(End - 1));
132 // Move Pivot to middle of partition.
133 std::swap(*Pivot, *(End - 1));
136 TG.spawn([=, &Comp, &TG] {
137 parallel_quick_sort(Start, Pivot, Comp, TG, Depth - 1);
139 parallel_quick_sort(Pivot + 1, End, Comp, TG, Depth - 1);
142 template <class RandomAccessIterator, class Comparator>
143 void parallel_sort(RandomAccessIterator Start, RandomAccessIterator End,
144 const Comparator &Comp) {
146 parallel_quick_sort(Start, End, Comp, TG,
147 llvm::Log2_64(std::distance(Start, End)) + 1);
150 template <class IterTy, class FuncTy>
151 void parallel_for_each(IterTy Begin, IterTy End, FuncTy Fn) {
152 // TaskGroup has a relatively high overhead, so we want to reduce
153 // the number of spawn() calls. We'll create up to 1024 tasks here.
154 // (Note that 1024 is an arbitrary number. This code probably needs
155 // improving to take the number of available cores into account.)
156 ptrdiff_t TaskSize = std::distance(Begin, End) / 1024;
161 while (TaskSize < std::distance(Begin, End)) {
162 TG.spawn([=, &Fn] { std::for_each(Begin, Begin + TaskSize, Fn); });
165 std::for_each(Begin, End, Fn);
168 template <class IndexTy, class FuncTy>
169 void parallel_for_each_n(IndexTy Begin, IndexTy End, FuncTy Fn) {
170 ptrdiff_t TaskSize = (End - Begin) / 1024;
176 for (; I + TaskSize < End; I += TaskSize) {
178 for (IndexTy J = I, E = I + TaskSize; J != E; ++J)
182 for (IndexTy J = I; J < End; ++J)
190 template <typename Iter>
191 using DefComparator =
192 std::less<typename std::iterator_traits<Iter>::value_type>;
194 } // namespace detail
196 // sequential algorithm implementations.
197 template <class Policy, class RandomAccessIterator,
198 class Comparator = detail::DefComparator<RandomAccessIterator>>
199 void sort(Policy policy, RandomAccessIterator Start, RandomAccessIterator End,
200 const Comparator &Comp = Comparator()) {
201 static_assert(is_execution_policy<Policy>::value,
202 "Invalid execution policy!");
203 llvm::sort(Start, End, Comp);
206 template <class Policy, class IterTy, class FuncTy>
207 void for_each(Policy policy, IterTy Begin, IterTy End, FuncTy Fn) {
208 static_assert(is_execution_policy<Policy>::value,
209 "Invalid execution policy!");
210 std::for_each(Begin, End, Fn);
213 template <class Policy, class IndexTy, class FuncTy>
214 void for_each_n(Policy policy, IndexTy Begin, IndexTy End, FuncTy Fn) {
215 static_assert(is_execution_policy<Policy>::value,
216 "Invalid execution policy!");
217 for (IndexTy I = Begin; I != End; ++I)
221 // Parallel algorithm implementations, only available when LLVM_ENABLE_THREADS
223 #if LLVM_ENABLE_THREADS
224 template <class RandomAccessIterator,
225 class Comparator = detail::DefComparator<RandomAccessIterator>>
226 void sort(parallel_execution_policy policy, RandomAccessIterator Start,
227 RandomAccessIterator End, const Comparator &Comp = Comparator()) {
228 detail::parallel_sort(Start, End, Comp);
231 template <class IterTy, class FuncTy>
232 void for_each(parallel_execution_policy policy, IterTy Begin, IterTy End,
234 detail::parallel_for_each(Begin, End, Fn);
237 template <class IndexTy, class FuncTy>
238 void for_each_n(parallel_execution_policy policy, IndexTy Begin, IndexTy End,
240 detail::parallel_for_each_n(Begin, End, Fn);
244 } // namespace parallel
247 #endif // LLVM_SUPPORT_PARALLEL_H