1 //===- lld/Core/Parallel.h - Parallel utilities ---------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLD_CORE_PARALLEL_H
11 #define LLD_CORE_PARALLEL_H
13 #include "lld/Core/LLVM.h"
14 #include "lld/Core/TaskGroup.h"
15 #include "llvm/Support/MathExtras.h"
16 #include "llvm/Config/llvm-config.h"
20 #if defined(_MSC_VER) && LLVM_ENABLE_THREADS
27 #if !LLVM_ENABLE_THREADS
28 template <class RandomAccessIterator, class Comparator>
30 RandomAccessIterator Start, RandomAccessIterator End,
31 const Comparator &Comp = std::less<
32 typename std::iterator_traits<RandomAccessIterator>::value_type>()) {
33 std::sort(Start, End, Comp);
35 #elif defined(_MSC_VER)
36 // Use ppl parallel_sort on Windows.
37 template <class RandomAccessIterator, class Comparator>
39 RandomAccessIterator Start, RandomAccessIterator End,
40 const Comparator &Comp = std::less<
41 typename std::iterator_traits<RandomAccessIterator>::value_type>()) {
42 concurrency::parallel_sort(Start, End, Comp);
46 const ptrdiff_t MinParallelSize = 1024;
48 /// \brief Inclusive median.
49 template <class RandomAccessIterator, class Comparator>
50 RandomAccessIterator medianOf3(RandomAccessIterator Start,
51 RandomAccessIterator End,
52 const Comparator &Comp) {
53 RandomAccessIterator Mid = Start + (std::distance(Start, End) / 2);
54 return Comp(*Start, *(End - 1))
55 ? (Comp(*Mid, *(End - 1)) ? (Comp(*Start, *Mid) ? Mid : Start)
57 : (Comp(*Mid, *Start) ? (Comp(*(End - 1), *Mid) ? Mid : End - 1)
61 template <class RandomAccessIterator, class Comparator>
62 void parallel_quick_sort(RandomAccessIterator Start, RandomAccessIterator End,
63 const Comparator &Comp, TaskGroup &TG, size_t Depth) {
64 // Do a sequential sort for small inputs.
65 if (std::distance(Start, End) < detail::MinParallelSize || Depth == 0) {
66 std::sort(Start, End, Comp);
71 auto Pivot = medianOf3(Start, End, Comp);
73 std::swap(*(End - 1), *Pivot);
74 Pivot = std::partition(Start, End - 1, [&Comp, End](decltype(*Start) V) {
75 return Comp(V, *(End - 1));
77 // Move Pivot to middle of partition.
78 std::swap(*Pivot, *(End - 1));
81 TG.spawn([=, &Comp, &TG] {
82 parallel_quick_sort(Start, Pivot, Comp, TG, Depth - 1);
84 parallel_quick_sort(Pivot + 1, End, Comp, TG, Depth - 1);
88 template <class RandomAccessIterator, class Comparator>
90 RandomAccessIterator Start, RandomAccessIterator End,
91 const Comparator &Comp = std::less<
92 typename std::iterator_traits<RandomAccessIterator>::value_type>()) {
94 detail::parallel_quick_sort(Start, End, Comp, TG,
95 llvm::Log2_64(std::distance(Start, End)) + 1);
99 template <class T> void parallel_sort(T *Start, T *End) {
100 parallel_sort(Start, End, std::less<T>());
103 #if !LLVM_ENABLE_THREADS
104 template <class IterTy, class FuncTy>
105 void parallel_for_each(IterTy Begin, IterTy End, FuncTy Fn) {
106 std::for_each(Begin, End, Fn);
109 template <class IndexTy, class FuncTy>
110 void parallel_for(IndexTy Begin, IndexTy End, FuncTy Fn) {
111 for (IndexTy I = Begin; I != End; ++I)
114 #elif defined(_MSC_VER)
115 // Use ppl parallel_for_each on Windows.
116 template <class IterTy, class FuncTy>
117 void parallel_for_each(IterTy Begin, IterTy End, FuncTy Fn) {
118 concurrency::parallel_for_each(Begin, End, Fn);
121 template <class IndexTy, class FuncTy>
122 void parallel_for(IndexTy Begin, IndexTy End, FuncTy Fn) {
123 concurrency::parallel_for(Begin, End, Fn);
126 template <class IterTy, class FuncTy>
127 void parallel_for_each(IterTy Begin, IterTy End, FuncTy Fn) {
128 // TaskGroup has a relatively high overhead, so we want to reduce
129 // the number of spawn() calls. We'll create up to 1024 tasks here.
130 // (Note that 1024 is an arbitrary number. This code probably needs
131 // improving to take the number of available cores into account.)
132 ptrdiff_t TaskSize = std::distance(Begin, End) / 1024;
137 while (TaskSize <= std::distance(Begin, End)) {
138 TG.spawn([=, &Fn] { std::for_each(Begin, Begin + TaskSize, Fn); });
141 TG.spawn([=, &Fn] { std::for_each(Begin, End, Fn); });
144 template <class IndexTy, class FuncTy>
145 void parallel_for(IndexTy Begin, IndexTy End, FuncTy Fn) {
146 ptrdiff_t TaskSize = (End - Begin) / 1024;
152 for (; I + TaskSize < End; I += TaskSize) {
154 for (IndexTy J = I, E = I + TaskSize; J != E; ++J)
159 for (IndexTy J = I; J < End; ++J)
164 } // End namespace lld
166 #endif // LLD_CORE_PARALLEL_H