3 // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 3, or (at your option) any later
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file parallel/quicksort.h
26 * @brief Implementation of a unbalanced parallel quicksort (in-place).
27 * This file is a GNU parallel extension to the Standard C++ Library.
30 // Written by Johannes Singler.
32 #ifndef _GLIBCXX_PARALLEL_QUICKSORT_H
33 #define _GLIBCXX_PARALLEL_QUICKSORT_H 1
35 #include <parallel/parallel.h>
36 #include <parallel/partition.h>
38 namespace __gnu_parallel
40 /** @brief Unbalanced quicksort divide step.
41 * @param __begin Begin iterator of subsequence.
42 * @param __end End iterator of subsequence.
43 * @param __comp Comparator.
44 * @param __pivot_rank Desired __rank of the pivot.
45 * @param __num_samples Choose pivot from that many samples.
46 * @param __num_threads Number of threads that are allowed to work on
49 template<typename _RAIter, typename _Compare>
50 typename std::iterator_traits<_RAIter>::difference_type
51 __parallel_sort_qs_divide(_RAIter __begin,
53 _Compare __comp, typename std::iterator_traits
54 <_RAIter>::difference_type __pivot_rank,
55 typename std::iterator_traits
56 <_RAIter>::difference_type
57 __num_samples, _ThreadIndex __num_threads)
59 typedef std::iterator_traits<_RAIter> _TraitsType;
60 typedef typename _TraitsType::value_type _ValueType;
61 typedef typename _TraitsType::difference_type _DifferenceType;
63 _DifferenceType __n = __end - __begin;
64 __num_samples = std::min(__num_samples, __n);
66 // Allocate uninitialized, to avoid default constructor.
67 _ValueType* __samples =
68 static_cast<_ValueType*>(::operator new(__num_samples
69 * sizeof(_ValueType)));
71 for (_DifferenceType __s = 0; __s < __num_samples; ++__s)
73 const unsigned long long __index
74 = static_cast<unsigned long long>(__s) * __n / __num_samples;
75 ::new(&(__samples[__s])) _ValueType(__begin[__index]);
78 __gnu_sequential::sort(__samples, __samples + __num_samples, __comp);
80 _ValueType& pivot = __samples[__pivot_rank * __num_samples / __n];
82 __gnu_parallel::binder2nd<_Compare, _ValueType, _ValueType, bool>
83 __pred(__comp, pivot);
84 _DifferenceType __split =
85 __parallel_partition(__begin, __end, __pred, __num_threads);
87 ::operator delete(__samples);
92 /** @brief Unbalanced quicksort conquer step.
93 * @param __begin Begin iterator of subsequence.
94 * @param __end End iterator of subsequence.
95 * @param __comp Comparator.
96 * @param __num_threads Number of threads that are allowed to work on
99 template<typename _RAIter, typename _Compare>
101 __parallel_sort_qs_conquer(_RAIter __begin,
104 _ThreadIndex __num_threads)
106 typedef std::iterator_traits<_RAIter> _TraitsType;
107 typedef typename _TraitsType::value_type _ValueType;
108 typedef typename _TraitsType::difference_type _DifferenceType;
110 if (__num_threads <= 1)
112 __gnu_sequential::sort(__begin, __end, __comp);
116 _DifferenceType __n = __end - __begin, __pivot_rank;
121 _ThreadIndex __num_threads_left;
123 if ((__num_threads % 2) == 1)
124 __num_threads_left = __num_threads / 2 + 1;
126 __num_threads_left = __num_threads / 2;
128 __pivot_rank = __n * __num_threads_left / __num_threads;
130 _DifferenceType __split =
131 __parallel_sort_qs_divide(__begin, __end, __comp, __pivot_rank,
132 _Settings::get().sort_qs_num_samples_preset,
135 #pragma omp parallel sections num_threads(2)
138 __parallel_sort_qs_conquer(__begin, __begin + __split,
139 __comp, __num_threads_left);
141 __parallel_sort_qs_conquer(__begin + __split, __end,
142 __comp, __num_threads - __num_threads_left);
148 /** @brief Unbalanced quicksort main call.
149 * @param __begin Begin iterator of input sequence.
150 * @param __end End iterator input sequence, ignored.
151 * @param __comp Comparator.
152 * @param __num_threads Number of threads that are allowed to work on
155 template<typename _RAIter, typename _Compare>
157 __parallel_sort_qs(_RAIter __begin,
160 _ThreadIndex __num_threads)
164 typedef std::iterator_traits<_RAIter> _TraitsType;
165 typedef typename _TraitsType::value_type _ValueType;
166 typedef typename _TraitsType::difference_type _DifferenceType;
168 _DifferenceType __n = __end - __begin;
170 // At least one element per processor.
171 if (__num_threads > __n)
172 __num_threads = static_cast<_ThreadIndex>(__n);
174 __parallel_sort_qs_conquer(
175 __begin, __begin + __n, __comp, __num_threads);
178 } //namespace __gnu_parallel
180 #endif /* _GLIBCXX_PARALLEL_QUICKSORT_H */