42 #ifndef _GLIBCXX_PARALLEL_BALANCED_QUICKSORT_H
43 #define _GLIBCXX_PARALLEL_BALANCED_QUICKSORT_H 1
54 #if _GLIBCXX_ASSERTIONS
58 namespace __gnu_parallel
61 template<
typename _RAIter>
69 typedef std::pair<_RAIter, _RAIter>
_Piece;
98 template<
typename _RAIter,
typename _Compare>
99 typename std::iterator_traits<_RAIter>::difference_type
105 typedef std::iterator_traits<_RAIter> _TraitsType;
106 typedef typename _TraitsType::value_type _ValueType;
107 typedef typename _TraitsType::difference_type _DifferenceType;
109 _RAIter __pivot_pos =
113 #if defined(_GLIBCXX_ASSERTIONS)
115 _DifferenceType __n = __end - __begin;
118 && !__comp(*(__begin + __n / 2),
120 || (!__comp(*__pivot_pos, *__begin)
121 && !__comp(*(__end - 1), *__pivot_pos))
122 || (!__comp(*__pivot_pos, *(__begin + __n / 2))
123 && !__comp(*__begin, *__pivot_pos))
124 || (!__comp(*__pivot_pos, *(__begin + __n / 2))
125 && !__comp(*(__end - 1), *__pivot_pos))
126 || (!__comp(*__pivot_pos, *(__end - 1))
127 && !__comp(*__begin, *__pivot_pos))
128 || (!__comp(*__pivot_pos, *(__end - 1))
129 && !__comp(*(__begin + __n / 2),
134 if (__pivot_pos != (__end - 1))
135 std::iter_swap(__pivot_pos, __end - 1);
136 __pivot_pos = __end - 1;
139 __pred(__comp, *__pivot_pos);
147 std::iter_swap(__begin + __split_pos, __pivot_pos);
148 __pivot_pos = __begin + __split_pos;
150 #if _GLIBCXX_ASSERTIONS
152 for (__r = __begin; __r != __pivot_pos; ++__r)
154 for (; __r != __end; ++__r)
169 template<
typename _RAIter,
typename _Compare>
172 _RAIter __begin, _RAIter __end,
177 typedef std::iterator_traits<_RAIter> _TraitsType;
178 typedef typename _TraitsType::value_type _ValueType;
179 typedef typename _TraitsType::difference_type _DifferenceType;
181 _DifferenceType __n = __end - __begin;
183 if (__num_threads <= 1 || __n <= 1)
194 _DifferenceType __split_pos =
197 #if _GLIBCXX_ASSERTIONS
199 __split_pos < (__end - __begin));
203 __num_threads_leftside = std::max<_ThreadIndex>
204 (1, std::min<_ThreadIndex>(__num_threads - 1, __split_pos
205 * __num_threads / __n));
211 # pragma omp parallel num_threads(2)
217 __wait = __parent_wait;
219 # pragma omp sections
224 __iam, __num_threads_leftside, __wait);
225 __wait = __parent_wait;
230 __qsb_conquer(__tls, __begin + __split_pos + 1, __end, __comp,
231 __iam + __num_threads_leftside,
232 __num_threads - __num_threads_leftside, __wait);
233 __wait = __parent_wait;
245 template<
typename _RAIter,
typename _Compare>
251 typedef std::iterator_traits<_RAIter> _TraitsType;
252 typedef typename _TraitsType::value_type _ValueType;
253 typedef typename _TraitsType::difference_type _DifferenceType;
254 typedef std::pair<_RAIter, _RAIter>
_Piece;
260 if (__base_case_n < 2)
269 _DifferenceType __elements_done = 0;
270 #if _GLIBCXX_ASSERTIONS
271 _DifferenceType __total_elements_done = 0;
277 _RAIter __begin = __current.first, __end = __current.second;
278 _DifferenceType __n = __end - __begin;
280 if (__n > __base_case_n)
283 _RAIter __pivot_pos = __begin + __rng(__n);
286 if (__pivot_pos != (__end - 1))
287 std::iter_swap(__pivot_pos, __end - 1);
288 __pivot_pos = __end - 1;
291 <_Compare, _ValueType, _ValueType,
bool>
292 __pred(__comp, *__pivot_pos);
295 _RAIter __split_pos1, __split_pos2;
296 __split_pos1 = __gnu_sequential::partition(__begin, __end - 1,
300 #if _GLIBCXX_ASSERTIONS
302 && __split_pos1 < __end);
305 if (__split_pos1 != __pivot_pos)
306 std::iter_swap(__split_pos1, __pivot_pos);
307 __pivot_pos = __split_pos1;
310 if ((__split_pos1 + 1 - __begin) < (__n >> 7)
311 || (__end - __split_pos1) < (__n >> 7))
316 <_Compare, _ValueType, _ValueType,
bool>, _ValueType>
318 <_Compare, _ValueType, _ValueType,
bool>
319 (__comp, *__pivot_pos));
322 __split_pos2 = __gnu_sequential::partition(__split_pos1 + 1,
327 __split_pos2 = __split_pos1 + 1;
330 __elements_done += (__split_pos2 - __split_pos1);
331 #if _GLIBCXX_ASSERTIONS
332 __total_elements_done += (__split_pos2 - __split_pos1);
335 if (((__split_pos1 + 1) - __begin) < (__end - (__split_pos2)))
338 if ((__split_pos2) != __end)
340 (std::make_pair(__split_pos2, __end));
343 __current.second = __split_pos1;
349 if (__begin != __split_pos1)
351 (__begin, __split_pos1));
353 __current.first = __split_pos2;
360 __gnu_sequential::sort(__begin, __end, __comp);
361 __elements_done += __n;
362 #if _GLIBCXX_ASSERTIONS
363 __total_elements_done += __n;
375 #if _GLIBCXX_ASSERTIONS
380 bool __successfully_stolen =
false;
382 && !__successfully_stolen
390 __victim = __rng(__num_threads);
393 __successfully_stolen = (__victim != __iam)
394 && __tls[__victim]->_M_leftover_parts.pop_back(__current);
395 if (!__successfully_stolen)
397 #if !defined(__ICC) && !defined(__ECC)
402 #if _GLIBCXX_ASSERTIONS
407 < (__search_start + 1.0));
410 if (!__successfully_stolen)
412 #if _GLIBCXX_ASSERTIONS
428 template<
typename _RAIter,
typename _Compare>
435 typedef std::iterator_traits<_RAIter> _TraitsType;
436 typedef typename _TraitsType::value_type _ValueType;
437 typedef typename _TraitsType::difference_type _DifferenceType;
438 typedef std::pair<_RAIter, _RAIter>
_Piece;
442 _DifferenceType __n = __end - __begin;
448 if (__num_threads > __n)
452 _TLSType** __tls =
new _TLSType*[__num_threads];
453 _DifferenceType __queue_size = (__num_threads
463 volatile _DifferenceType __elements_leftover = __n;
467 __tls[__i]->_M_num_threads = __num_threads;
468 __tls[__i]->_M_global = std::make_pair(__begin, __end);
471 __tls[__i]->_M_initial = std::make_pair(__end, __end);
476 __num_threads,
true);
478 #if _GLIBCXX_ASSERTIONS
483 !__tls[__i]->_M_leftover_parts.pop_back(__dummy));
Subsequence description.
Definition: multiway_mergesort.h:46
Random number generator based on the Mersenne twister. This file is a GNU parallel extension to the S...
#define _GLIBCXX_ASSERTIONS
Switch on many _GLIBCXX_PARALLEL_ASSERTions in parallel code. Should be switched on only locally...
Definition: compiletime_settings.h:61
_Piece _M_initial
Initial piece to work on.
Definition: balanced_quicksort.h:72
std::iterator_traits< _RAIter >::difference_type __qsb_divide(_RAIter __begin, _RAIter __end, _Compare __comp, _ThreadIndex __num_threads)
Balanced quicksort divide step.
Definition: balanced_quicksort.h:100
uint16_t _ThreadIndex
Unsigned integer to index a thread number. The maximum thread number (for each processor) must fit in...
Definition: types.h:123
Similar to std::unary_negate, but giving the argument types explicitly.
Definition: base.h:173
Routines for checking the correctness of algorithm results. This file is a GNU parallel extension to ...
void __yield()
Yield control to another thread, without waiting for the end of the time slice.
Definition: compatibility.h:121
_TraitsType::difference_type _DifferenceType
Definition: balanced_quicksort.h:65
_RestrictedBoundedConcurrentQueue< _Piece > _M_leftover_parts
Work-stealing queue.
Definition: balanced_quicksort.h:75
Parallel implementation of std::partition(), std::nth_element(), and std::partial_sort(). This file is a GNU parallel extension to the Standard C++ Library.
bool pop_front(_Tp &__t)
Pops one element from the queue at the front end. Must not be called concurrently with pop_front()...
Definition: queue.h:100
std::iterator_traits< _RAIter >::difference_type __parallel_partition(_RAIter __begin, _RAIter __end, _Predicate __pred, _ThreadIndex __num_threads)
Parallel implementation of std::partition.
Definition: partition.h:56
_QSBThreadLocal(int __queue_size)
Constructor.
Definition: balanced_quicksort.h:88
#define _GLIBCXX_CALL(__n)
Macro to produce log message when entering a function.
Definition: compiletime_settings.h:44
int omp_get_num_threads(void) __GOMP_NOTHROW
_RAIter __median_of_three_iterators(_RAIter __a, _RAIter __b, _RAIter __c, _Compare __comp)
Compute the median of three referenced elements, according to __comp.
Definition: base.h:398
_SequenceIndex sort_qsb_base_case_maximal_n
Definition: settings.h:241
std::iterator_traits< _RAIter > _TraitsType
Definition: balanced_quicksort.h:64
Lock-free double-ended queue. This file is a GNU parallel extension to the Standard C++ Library...
volatile _DifferenceType * _M_elements_leftover
Pointer to a counter of elements left over to sort.
Definition: balanced_quicksort.h:81
void __parallel_sort_qsb(_RAIter __begin, _RAIter __end, _Compare __comp, _ThreadIndex __num_threads)
Top-level quicksort routine.
Definition: balanced_quicksort.h:430
Includes the original header files concerned with iterators except for stream iterators. This file is a GNU parallel extension to the Standard C++ Library.
_Piece _M_global
The complete sequence to sort.
Definition: balanced_quicksort.h:84
_Size __rd_log2(_Size __n)
Calculates the rounded-down logarithm of __n for base 2.
Definition: base.h:102
static _GLIBCXX_CONST const _Settings & get()
Get the global settings.
Similar to std::binder2nd, but giving the argument types explicitly.
Definition: base.h:220
Information local to one thread in the parallel quicksort run.
Definition: balanced_quicksort.h:62
void __qsb_local_sort_with_helping(_QSBThreadLocal< _RAIter > **__tls, _Compare &__comp, _ThreadIndex __iam, bool __wait)
Quicksort step doing load-balanced local sort.
Definition: balanced_quicksort.h:247
Runtime settings and tuning parameters, heuristics to decide whether to use parallelized algorithms...
#define _GLIBCXX_PARALLEL_ASSERT(_Condition)
Definition: base.h:422
Random number generator, based on the Mersenne twister.
Definition: random_number.h:42
void __qsb_conquer(_QSBThreadLocal< _RAIter > **__tls, _RAIter __begin, _RAIter __end, _Compare __comp, _ThreadIndex __iam, _ThreadIndex __num_threads, bool __parent_wait)
Quicksort conquer step.
Definition: balanced_quicksort.h:171
std::pair< _RAIter, _RAIter > _Piece
Continuous part of the sequence, described by an iterator pair.
Definition: balanced_quicksort.h:69
void push_front(const _Tp &__t)
Pushes one element into the queue at the front end. Must not be called concurrently with pop_front()...
Definition: queue.h:83
double omp_get_wtime(void) __GOMP_NOTHROW
_ThreadIndex _M_num_threads
Number of threads involved in this algorithm.
Definition: balanced_quicksort.h:78
Similar to std::binder1st, but giving the argument types explicitly.
Definition: base.h:192