Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- llvm/Support/Parallel.h - Parallel algorithms ----------------------===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_SUPPORT_PARALLEL_H |
| 10 | #define LLVM_SUPPORT_PARALLEL_H |
| 11 | |
| 12 | #include "llvm/ADT/STLExtras.h" |
| 13 | #include "llvm/Config/llvm-config.h" |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 14 | #include "llvm/Support/Error.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 15 | #include "llvm/Support/MathExtras.h" |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 16 | #include "llvm/Support/Threading.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 17 | |
| 18 | #include <algorithm> |
| 19 | #include <condition_variable> |
| 20 | #include <functional> |
| 21 | #include <mutex> |
| 22 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 23 | namespace llvm { |
| 24 | |
| 25 | namespace parallel { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 26 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 27 | // Strategy for the default executor used by the parallel routines provided by |
| 28 | // this file. It defaults to using all hardware threads and should be |
| 29 | // initialized before the first use of parallel routines. |
| 30 | extern ThreadPoolStrategy strategy; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 31 | |
| 32 | namespace detail { |
| 33 | |
| 34 | #if LLVM_ENABLE_THREADS |
| 35 | |
| 36 | class Latch { |
| 37 | uint32_t Count; |
| 38 | mutable std::mutex Mutex; |
| 39 | mutable std::condition_variable Cond; |
| 40 | |
| 41 | public: |
| 42 | explicit Latch(uint32_t Count = 0) : Count(Count) {} |
| 43 | ~Latch() { sync(); } |
| 44 | |
| 45 | void inc() { |
| 46 | std::lock_guard<std::mutex> lock(Mutex); |
| 47 | ++Count; |
| 48 | } |
| 49 | |
| 50 | void dec() { |
| 51 | std::lock_guard<std::mutex> lock(Mutex); |
| 52 | if (--Count == 0) |
| 53 | Cond.notify_all(); |
| 54 | } |
| 55 | |
| 56 | void sync() const { |
| 57 | std::unique_lock<std::mutex> lock(Mutex); |
| 58 | Cond.wait(lock, [&] { return Count == 0; }); |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | class TaskGroup { |
| 63 | Latch L; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 64 | bool Parallel; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 65 | |
| 66 | public: |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 67 | TaskGroup(); |
| 68 | ~TaskGroup(); |
| 69 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 70 | void spawn(std::function<void()> f); |
| 71 | |
| 72 | void sync() const { L.sync(); } |
| 73 | }; |
| 74 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 75 | const ptrdiff_t MinParallelSize = 1024; |
| 76 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 77 | /// Inclusive median. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 78 | template <class RandomAccessIterator, class Comparator> |
| 79 | RandomAccessIterator medianOf3(RandomAccessIterator Start, |
| 80 | RandomAccessIterator End, |
| 81 | const Comparator &Comp) { |
| 82 | RandomAccessIterator Mid = Start + (std::distance(Start, End) / 2); |
| 83 | return Comp(*Start, *(End - 1)) |
| 84 | ? (Comp(*Mid, *(End - 1)) ? (Comp(*Start, *Mid) ? Mid : Start) |
| 85 | : End - 1) |
| 86 | : (Comp(*Mid, *Start) ? (Comp(*(End - 1), *Mid) ? Mid : End - 1) |
| 87 | : Start); |
| 88 | } |
| 89 | |
| 90 | template <class RandomAccessIterator, class Comparator> |
| 91 | void parallel_quick_sort(RandomAccessIterator Start, RandomAccessIterator End, |
| 92 | const Comparator &Comp, TaskGroup &TG, size_t Depth) { |
| 93 | // Do a sequential sort for small inputs. |
| 94 | if (std::distance(Start, End) < detail::MinParallelSize || Depth == 0) { |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 95 | llvm::sort(Start, End, Comp); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 96 | return; |
| 97 | } |
| 98 | |
| 99 | // Partition. |
| 100 | auto Pivot = medianOf3(Start, End, Comp); |
| 101 | // Move Pivot to End. |
| 102 | std::swap(*(End - 1), *Pivot); |
| 103 | Pivot = std::partition(Start, End - 1, [&Comp, End](decltype(*Start) V) { |
| 104 | return Comp(V, *(End - 1)); |
| 105 | }); |
| 106 | // Move Pivot to middle of partition. |
| 107 | std::swap(*Pivot, *(End - 1)); |
| 108 | |
| 109 | // Recurse. |
| 110 | TG.spawn([=, &Comp, &TG] { |
| 111 | parallel_quick_sort(Start, Pivot, Comp, TG, Depth - 1); |
| 112 | }); |
| 113 | parallel_quick_sort(Pivot + 1, End, Comp, TG, Depth - 1); |
| 114 | } |
| 115 | |
| 116 | template <class RandomAccessIterator, class Comparator> |
| 117 | void parallel_sort(RandomAccessIterator Start, RandomAccessIterator End, |
| 118 | const Comparator &Comp) { |
| 119 | TaskGroup TG; |
| 120 | parallel_quick_sort(Start, End, Comp, TG, |
| 121 | llvm::Log2_64(std::distance(Start, End)) + 1); |
| 122 | } |
| 123 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 124 | // TaskGroup has a relatively high overhead, so we want to reduce |
| 125 | // the number of spawn() calls. We'll create up to 1024 tasks here. |
| 126 | // (Note that 1024 is an arbitrary number. This code probably needs |
| 127 | // improving to take the number of available cores into account.) |
| 128 | enum { MaxTasksPerGroup = 1024 }; |
| 129 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 130 | template <class IterTy, class FuncTy> |
| 131 | void parallel_for_each(IterTy Begin, IterTy End, FuncTy Fn) { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 132 | // Limit the number of tasks to MaxTasksPerGroup to limit job scheduling |
| 133 | // overhead on large inputs. |
| 134 | ptrdiff_t TaskSize = std::distance(Begin, End) / MaxTasksPerGroup; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 135 | if (TaskSize == 0) |
| 136 | TaskSize = 1; |
| 137 | |
| 138 | TaskGroup TG; |
| 139 | while (TaskSize < std::distance(Begin, End)) { |
| 140 | TG.spawn([=, &Fn] { std::for_each(Begin, Begin + TaskSize, Fn); }); |
| 141 | Begin += TaskSize; |
| 142 | } |
| 143 | std::for_each(Begin, End, Fn); |
| 144 | } |
| 145 | |
| 146 | template <class IndexTy, class FuncTy> |
| 147 | void parallel_for_each_n(IndexTy Begin, IndexTy End, FuncTy Fn) { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 148 | // Limit the number of tasks to MaxTasksPerGroup to limit job scheduling |
| 149 | // overhead on large inputs. |
| 150 | ptrdiff_t TaskSize = (End - Begin) / MaxTasksPerGroup; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 151 | if (TaskSize == 0) |
| 152 | TaskSize = 1; |
| 153 | |
| 154 | TaskGroup TG; |
| 155 | IndexTy I = Begin; |
| 156 | for (; I + TaskSize < End; I += TaskSize) { |
| 157 | TG.spawn([=, &Fn] { |
| 158 | for (IndexTy J = I, E = I + TaskSize; J != E; ++J) |
| 159 | Fn(J); |
| 160 | }); |
| 161 | } |
| 162 | for (IndexTy J = I; J < End; ++J) |
| 163 | Fn(J); |
| 164 | } |
| 165 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 166 | template <class IterTy, class ResultTy, class ReduceFuncTy, |
| 167 | class TransformFuncTy> |
| 168 | ResultTy parallel_transform_reduce(IterTy Begin, IterTy End, ResultTy Init, |
| 169 | ReduceFuncTy Reduce, |
| 170 | TransformFuncTy Transform) { |
| 171 | // Limit the number of tasks to MaxTasksPerGroup to limit job scheduling |
| 172 | // overhead on large inputs. |
| 173 | size_t NumInputs = std::distance(Begin, End); |
| 174 | if (NumInputs == 0) |
| 175 | return std::move(Init); |
| 176 | size_t NumTasks = std::min(static_cast<size_t>(MaxTasksPerGroup), NumInputs); |
| 177 | std::vector<ResultTy> Results(NumTasks, Init); |
| 178 | { |
| 179 | // Each task processes either TaskSize or TaskSize+1 inputs. Any inputs |
| 180 | // remaining after dividing them equally amongst tasks are distributed as |
| 181 | // one extra input over the first tasks. |
| 182 | TaskGroup TG; |
| 183 | size_t TaskSize = NumInputs / NumTasks; |
| 184 | size_t RemainingInputs = NumInputs % NumTasks; |
| 185 | IterTy TBegin = Begin; |
| 186 | for (size_t TaskId = 0; TaskId < NumTasks; ++TaskId) { |
| 187 | IterTy TEnd = TBegin + TaskSize + (TaskId < RemainingInputs ? 1 : 0); |
| 188 | TG.spawn([=, &Transform, &Reduce, &Results] { |
| 189 | // Reduce the result of transformation eagerly within each task. |
| 190 | ResultTy R = Init; |
| 191 | for (IterTy It = TBegin; It != TEnd; ++It) |
| 192 | R = Reduce(R, Transform(*It)); |
| 193 | Results[TaskId] = R; |
| 194 | }); |
| 195 | TBegin = TEnd; |
| 196 | } |
| 197 | assert(TBegin == End); |
| 198 | } |
| 199 | |
| 200 | // Do a final reduction. There are at most 1024 tasks, so this only adds |
| 201 | // constant single-threaded overhead for large inputs. Hopefully most |
| 202 | // reductions are cheaper than the transformation. |
| 203 | ResultTy FinalResult = std::move(Results.front()); |
| 204 | for (ResultTy &PartialResult : |
| 205 | makeMutableArrayRef(Results.data() + 1, Results.size() - 1)) |
| 206 | FinalResult = Reduce(FinalResult, std::move(PartialResult)); |
| 207 | return std::move(FinalResult); |
| 208 | } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 209 | |
| 210 | #endif |
| 211 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 212 | } // namespace detail |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 213 | } // namespace parallel |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 214 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 215 | template <class RandomAccessIterator, |
| 216 | class Comparator = std::less< |
| 217 | typename std::iterator_traits<RandomAccessIterator>::value_type>> |
| 218 | void parallelSort(RandomAccessIterator Start, RandomAccessIterator End, |
| 219 | const Comparator &Comp = Comparator()) { |
| 220 | #if LLVM_ENABLE_THREADS |
| 221 | if (parallel::strategy.ThreadsRequested != 1) { |
| 222 | parallel::detail::parallel_sort(Start, End, Comp); |
| 223 | return; |
| 224 | } |
| 225 | #endif |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 226 | llvm::sort(Start, End, Comp); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 229 | template <class IterTy, class FuncTy> |
| 230 | void parallelForEach(IterTy Begin, IterTy End, FuncTy Fn) { |
| 231 | #if LLVM_ENABLE_THREADS |
| 232 | if (parallel::strategy.ThreadsRequested != 1) { |
| 233 | parallel::detail::parallel_for_each(Begin, End, Fn); |
| 234 | return; |
| 235 | } |
| 236 | #endif |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 237 | std::for_each(Begin, End, Fn); |
| 238 | } |
| 239 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 240 | template <class FuncTy> |
| 241 | void parallelForEachN(size_t Begin, size_t End, FuncTy Fn) { |
| 242 | #if LLVM_ENABLE_THREADS |
| 243 | if (parallel::strategy.ThreadsRequested != 1) { |
| 244 | parallel::detail::parallel_for_each_n(Begin, End, Fn); |
| 245 | return; |
| 246 | } |
| 247 | #endif |
| 248 | for (size_t I = Begin; I != End; ++I) |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 249 | Fn(I); |
| 250 | } |
| 251 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 252 | template <class IterTy, class ResultTy, class ReduceFuncTy, |
| 253 | class TransformFuncTy> |
| 254 | ResultTy parallelTransformReduce(IterTy Begin, IterTy End, ResultTy Init, |
| 255 | ReduceFuncTy Reduce, |
| 256 | TransformFuncTy Transform) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 257 | #if LLVM_ENABLE_THREADS |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 258 | if (parallel::strategy.ThreadsRequested != 1) { |
| 259 | return parallel::detail::parallel_transform_reduce(Begin, End, Init, Reduce, |
| 260 | Transform); |
| 261 | } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 262 | #endif |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 263 | for (IterTy I = Begin; I != End; ++I) |
| 264 | Init = Reduce(std::move(Init), Transform(*I)); |
| 265 | return std::move(Init); |
| 266 | } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 267 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 268 | // Range wrappers. |
| 269 | template <class RangeTy, |
| 270 | class Comparator = std::less<decltype(*std::begin(RangeTy()))>> |
| 271 | void parallelSort(RangeTy &&R, const Comparator &Comp = Comparator()) { |
| 272 | parallelSort(std::begin(R), std::end(R), Comp); |
| 273 | } |
| 274 | |
| 275 | template <class RangeTy, class FuncTy> |
| 276 | void parallelForEach(RangeTy &&R, FuncTy Fn) { |
| 277 | parallelForEach(std::begin(R), std::end(R), Fn); |
| 278 | } |
| 279 | |
| 280 | template <class RangeTy, class ResultTy, class ReduceFuncTy, |
| 281 | class TransformFuncTy> |
| 282 | ResultTy parallelTransformReduce(RangeTy &&R, ResultTy Init, |
| 283 | ReduceFuncTy Reduce, |
| 284 | TransformFuncTy Transform) { |
| 285 | return parallelTransformReduce(std::begin(R), std::end(R), Init, Reduce, |
| 286 | Transform); |
| 287 | } |
| 288 | |
| 289 | // Parallel for-each, but with error handling. |
| 290 | template <class RangeTy, class FuncTy> |
| 291 | Error parallelForEachError(RangeTy &&R, FuncTy Fn) { |
| 292 | // The transform_reduce algorithm requires that the initial value be copyable. |
| 293 | // Error objects are uncopyable. We only need to copy initial success values, |
| 294 | // so work around this mismatch via the C API. The C API represents success |
| 295 | // values with a null pointer. The joinErrors discards null values and joins |
| 296 | // multiple errors into an ErrorList. |
| 297 | return unwrap(parallelTransformReduce( |
| 298 | std::begin(R), std::end(R), wrap(Error::success()), |
| 299 | [](LLVMErrorRef Lhs, LLVMErrorRef Rhs) { |
| 300 | return wrap(joinErrors(unwrap(Lhs), unwrap(Rhs))); |
| 301 | }, |
| 302 | [&Fn](auto &&V) { return wrap(Fn(V)); })); |
| 303 | } |
| 304 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 305 | } // namespace llvm |
| 306 | |
| 307 | #endif // LLVM_SUPPORT_PARALLEL_H |