]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/lld/Core/Parallel.h
Vendor import of lld trunk r302418:
[FreeBSD/FreeBSD.git] / include / lld / Core / Parallel.h
1 //===- lld/Core/Parallel.h - Parallel utilities ---------------------------===//
2 //
3 //                             The LLVM Linker
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 LLD_CORE_PARALLEL_H
11 #define LLD_CORE_PARALLEL_H
12
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"
17
18 #include <algorithm>
19
20 #if defined(_MSC_VER) && LLVM_ENABLE_THREADS
21 #include <concrt.h>
22 #include <ppl.h>
23 #endif
24
25 namespace lld {
26
27 #if !LLVM_ENABLE_THREADS
28 template <class RandomAccessIterator, class Comparator>
29 void parallel_sort(
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);
34 }
35 #elif defined(_MSC_VER)
36 // Use ppl parallel_sort on Windows.
37 template <class RandomAccessIterator, class Comparator>
38 void parallel_sort(
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);
43 }
44 #else
45 namespace detail {
46 const ptrdiff_t MinParallelSize = 1024;
47
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)
56                                        : End - 1)
57              : (Comp(*Mid, *Start) ? (Comp(*(End - 1), *Mid) ? Mid : End - 1)
58                                    : Start);
59 }
60
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);
67     return;
68   }
69
70   // Partition.
71   auto Pivot = medianOf3(Start, End, Comp);
72   // Move Pivot to End.
73   std::swap(*(End - 1), *Pivot);
74   Pivot = std::partition(Start, End - 1, [&Comp, End](decltype(*Start) V) {
75     return Comp(V, *(End - 1));
76   });
77   // Move Pivot to middle of partition.
78   std::swap(*Pivot, *(End - 1));
79
80   // Recurse.
81   TG.spawn([=, &Comp, &TG] {
82     parallel_quick_sort(Start, Pivot, Comp, TG, Depth - 1);
83   });
84   parallel_quick_sort(Pivot + 1, End, Comp, TG, Depth - 1);
85 }
86 }
87
88 template <class RandomAccessIterator, class Comparator>
89 void parallel_sort(
90     RandomAccessIterator Start, RandomAccessIterator End,
91     const Comparator &Comp = std::less<
92         typename std::iterator_traits<RandomAccessIterator>::value_type>()) {
93   TaskGroup TG;
94   detail::parallel_quick_sort(Start, End, Comp, TG,
95                               llvm::Log2_64(std::distance(Start, End)) + 1);
96 }
97 #endif
98
99 template <class T> void parallel_sort(T *Start, T *End) {
100   parallel_sort(Start, End, std::less<T>());
101 }
102
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);
107 }
108
109 template <class IndexTy, class FuncTy>
110 void parallel_for(IndexTy Begin, IndexTy End, FuncTy Fn) {
111   for (IndexTy I = Begin; I != End; ++I)
112     Fn(I);
113 }
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);
119 }
120
121 template <class IndexTy, class FuncTy>
122 void parallel_for(IndexTy Begin, IndexTy End, FuncTy Fn) {
123   concurrency::parallel_for(Begin, End, Fn);
124 }
125 #else
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;
133   if (TaskSize == 0)
134     TaskSize = 1;
135
136   TaskGroup TG;
137   while (TaskSize <= std::distance(Begin, End)) {
138     TG.spawn([=, &Fn] { std::for_each(Begin, Begin + TaskSize, Fn); });
139     Begin += TaskSize;
140   }
141   TG.spawn([=, &Fn] { std::for_each(Begin, End, Fn); });
142 }
143
144 template <class IndexTy, class FuncTy>
145 void parallel_for(IndexTy Begin, IndexTy End, FuncTy Fn) {
146   ptrdiff_t TaskSize = (End - Begin) / 1024;
147   if (TaskSize == 0)
148     TaskSize = 1;
149
150   TaskGroup TG;
151   IndexTy I = Begin;
152   for (; I + TaskSize < End; I += TaskSize) {
153     TG.spawn([=, &Fn] {
154       for (IndexTy J = I, E = I + TaskSize; J != E; ++J)
155         Fn(J);
156     });
157   }
158   TG.spawn([=, &Fn] {
159     for (IndexTy J = I; J < End; ++J)
160       Fn(J);
161   });
162 }
163 #endif
164 } // End namespace lld
165
166 #endif // LLD_CORE_PARALLEL_H