xref: /oneTBB/include/oneapi/tbb/parallel_sort.h (revision b2cbc926)
1 /*
2     Copyright (c) 2005-2021 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #ifndef __TBB_parallel_sort_H
18 #define __TBB_parallel_sort_H
19 
20 #include "detail/_namespace_injection.h"
21 #include "parallel_for.h"
22 #include "blocked_range.h"
23 #include "profiling.h"
24 
25 #include <algorithm>
26 #include <iterator>
27 #include <functional>
28 #include <cstddef>
29 
30 namespace tbb {
31 namespace detail {
32 #if __TBB_CPP20_CONCEPTS_PRESENT
33 inline namespace d0 {
34 
35 // TODO: consider using std::strict_weak_order concept
36 template <typename Compare, typename Iterator>
37 concept compare = requires( const std::remove_reference_t<Compare>& comp, typename std::iterator_traits<Iterator>::reference value ) {
38     // Forward via iterator_traits::reference
39     { comp(typename std::iterator_traits<Iterator>::reference(value),
40            typename std::iterator_traits<Iterator>::reference(value)) } -> std::convertible_to<bool>;
41 };
42 
43 // Inspired by std::__PartiallyOrderedWith exposition only concept
44 template <typename T>
45 concept less_than_comparable = requires( const std::remove_reference_t<T>& lhs,
46                                          const std::remove_reference_t<T>& rhs ) {
47     { lhs < rhs } -> boolean_testable;
48 };
49 
50 } // namespace d0
51 #endif // __TBB_CPP20_CONCEPTS_PRESENT
52 namespace d1 {
53 
54 //! Range used in quicksort to split elements into subranges based on a value.
55 /** The split operation selects a splitter and places all elements less than or equal
56     to the value in the first range and the remaining elements in the second range.
57     @ingroup algorithms */
58 template<typename RandomAccessIterator, typename Compare>
59 class quick_sort_range {
60     std::size_t median_of_three( const RandomAccessIterator& array, std::size_t l, std::size_t m, std::size_t r ) const {
61         return comp(array[l], array[m]) ? ( comp(array[m], array[r]) ? m : ( comp(array[l], array[r]) ? r : l ) )
62                                         : ( comp(array[r], array[m]) ? m : ( comp(array[r], array[l]) ? r : l ) );
63     }
64 
65     std::size_t pseudo_median_of_nine( const RandomAccessIterator& array, const quick_sort_range& range ) const {
66         std::size_t offset = range.size / 8u;
67         return median_of_three(array,
68                                median_of_three(array, 0 , offset, offset * 2),
69                                median_of_three(array, offset * 3, offset * 4, offset * 5),
70                                median_of_three(array, offset * 6, offset * 7, range.size - 1));
71 
72     }
73 
74     std::size_t split_range( quick_sort_range& range ) {
75         RandomAccessIterator array = range.begin;
76         RandomAccessIterator first_element = range.begin;
77         std::size_t m = pseudo_median_of_nine(array, range);
78         if( m != 0 ) std::iter_swap(array, array + m);
79 
80         std::size_t i = 0;
81         std::size_t j = range.size;
82         // Partition interval [i + 1,j - 1] with key *first_element.
83         for(;;) {
84             __TBB_ASSERT( i < j, nullptr );
85             // Loop must terminate since array[l] == *first_element.
86             do {
87                 --j;
88                 __TBB_ASSERT( i <= j, "bad ordering relation?" );
89             } while( comp(*first_element, array[j]) );
90             do {
91                 __TBB_ASSERT( i <= j, nullptr );
92                 if( i == j ) goto partition;
93                 ++i;
94             } while( comp(array[i], *first_element) );
95             if( i == j ) goto partition;
96             std::iter_swap(array + i, array + j);
97         }
98 partition:
99         // Put the partition key were it belongs
100         std::iter_swap(array + j, first_element);
101         // array[l..j) is less or equal to key.
102         // array(j..r) is greater or equal to key.
103         // array[j] is equal to key
104         i = j + 1;
105         std::size_t new_range_size = range.size - i;
106         range.size = j;
107         return new_range_size;
108     }
109 
110 public:
111     quick_sort_range() = default;
112     quick_sort_range( const quick_sort_range& ) = default;
113     void operator=( const quick_sort_range& ) = delete;
114 
115     static constexpr std::size_t grainsize = 500;
116     const Compare& comp;
117     std::size_t size;
118     RandomAccessIterator begin;
119 
120     quick_sort_range( RandomAccessIterator begin_, std::size_t size_, const Compare& comp_ ) :
121         comp(comp_), size(size_), begin(begin_) {}
122 
123     bool empty() const { return size == 0; }
124     bool is_divisible() const { return size >= grainsize; }
125 
126     quick_sort_range( quick_sort_range& range, split )
127         : comp(range.comp)
128         , size(split_range(range))
129           // +1 accounts for the pivot element, which is at its correct place
130           // already and, therefore, is not included into subranges.
131         , begin(range.begin + range.size + 1) {}
132 };
133 
134 //! Body class used to test if elements in a range are presorted
135 /** @ingroup algorithms */
136 template<typename RandomAccessIterator, typename Compare>
137 class quick_sort_pretest_body {
138     const Compare& comp;
139     task_group_context& context;
140 
141 public:
142     quick_sort_pretest_body() = default;
143     quick_sort_pretest_body( const quick_sort_pretest_body& ) = default;
144     void operator=( const quick_sort_pretest_body& ) = delete;
145 
146     quick_sort_pretest_body( const Compare& _comp, task_group_context& _context ) : comp(_comp), context(_context) {}
147 
148     void operator()( const blocked_range<RandomAccessIterator>& range ) const {
149         RandomAccessIterator my_end = range.end();
150 
151         int i = 0;
152         //TODO: consider using std::is_sorted() for each 64 iterations (requires performance measurements)
153         for( RandomAccessIterator k = range.begin(); k != my_end; ++k, ++i ) {
154             if( i % 64 == 0 && context.is_group_execution_cancelled() ) break;
155 
156             // The k - 1 is never out-of-range because the first chunk starts at begin+serial_cutoff+1
157             if( comp(*(k), *(k - 1)) ) {
158                 context.cancel_group_execution();
159                 break;
160             }
161         }
162     }
163 };
164 
165 //! Body class used to sort elements in a range that is smaller than the grainsize.
166 /** @ingroup algorithms */
167 template<typename RandomAccessIterator, typename Compare>
168 struct quick_sort_body {
169     void operator()( const quick_sort_range<RandomAccessIterator,Compare>& range ) const {
170         std::sort(range.begin, range.begin + range.size, range.comp);
171     }
172 };
173 
174 //! Method to perform parallel_for based quick sort.
175 /** @ingroup algorithms */
176 template<typename RandomAccessIterator, typename Compare>
177 void do_parallel_quick_sort( RandomAccessIterator begin, RandomAccessIterator end, const Compare& comp ) {
178     parallel_for(quick_sort_range<RandomAccessIterator,Compare>(begin, end - begin, comp),
179                  quick_sort_body<RandomAccessIterator,Compare>(),
180                  auto_partitioner());
181 }
182 
183 //! Wrapper method to initiate the sort by calling parallel_for.
184 /** @ingroup algorithms */
185 template<typename RandomAccessIterator, typename Compare>
186 void parallel_quick_sort( RandomAccessIterator begin, RandomAccessIterator end, const Compare& comp ) {
187     task_group_context my_context(PARALLEL_SORT);
188     constexpr int serial_cutoff = 9;
189 
190     __TBB_ASSERT( begin + serial_cutoff < end, "min_parallel_size is smaller than serial cutoff?" );
191     RandomAccessIterator k = begin;
192     for( ; k != begin + serial_cutoff; ++k ) {
193         if( comp(*(k + 1), *k) ) {
194             do_parallel_quick_sort(begin, end, comp);
195             return;
196         }
197     }
198 
199     // Check is input range already sorted
200     parallel_for(blocked_range<RandomAccessIterator>(k + 1, end),
201                  quick_sort_pretest_body<RandomAccessIterator, Compare>(comp, my_context),
202                  auto_partitioner(),
203                  my_context);
204 
205     if( my_context.is_group_execution_cancelled() )
206         do_parallel_quick_sort(begin, end, comp);
207 }
208 
209 /** \page parallel_sort_iter_req Requirements on iterators for parallel_sort
210     Requirements on the iterator type \c It and its value type \c T for \c parallel_sort:
211 
212     - \code void iter_swap( It a, It b ) \endcode Swaps the values of the elements the given
213     iterators \c a and \c b are pointing to. \c It should be a random access iterator.
214 
215     - \code bool Compare::operator()( const T& x, const T& y ) \endcode True if x comes before y;
216 **/
217 
218 /** \name parallel_sort
219     See also requirements on \ref parallel_sort_iter_req "iterators for parallel_sort". **/
220 //@{
221 
222 //! Sorts the data in [begin,end) using the given comparator
223 /** The compare function object is used for all comparisons between elements during sorting.
224     The compare object must define a bool operator() function.
225     @ingroup algorithms **/
226 template<typename RandomAccessIterator, typename Compare>
227     __TBB_requires(std::random_access_iterator<RandomAccessIterator> &&
228                    compare<Compare, RandomAccessIterator>)
229 void parallel_sort( RandomAccessIterator begin, RandomAccessIterator end, const Compare& comp ) {
230     constexpr int min_parallel_size = 500;
231     if( end > begin ) {
232         if( end - begin < min_parallel_size ) {
233             std::sort(begin, end, comp);
234         } else {
235             parallel_quick_sort(begin, end, comp);
236         }
237     }
238 }
239 
240 //! Sorts the data in [begin,end) with a default comparator \c std::less
241 /** @ingroup algorithms **/
242 template<typename RandomAccessIterator>
243     __TBB_requires(std::random_access_iterator<RandomAccessIterator> &&
244                    less_than_comparable<typename std::iterator_traits<RandomAccessIterator>::value_type>)
245 void parallel_sort( RandomAccessIterator begin, RandomAccessIterator end ) {
246     parallel_sort(begin, end, std::less<typename std::iterator_traits<RandomAccessIterator>::value_type>());
247 }
248 
249 //! Sorts the data in rng using the given comparator
250 /** @ingroup algorithms **/
251 template<typename Range, typename Compare>
252     __TBB_requires(container_based_sequence<Range, std::random_access_iterator_tag> &&
253                    compare<Compare, range_iterator_type<Range>>)
254 void parallel_sort( Range&& rng, const Compare& comp ) {
255     parallel_sort(std::begin(rng), std::end(rng), comp);
256 }
257 
258 //! Sorts the data in rng with a default comparator \c std::less
259 /** @ingroup algorithms **/
260 template<typename Range>
261     __TBB_requires(container_based_sequence<Range, std::random_access_iterator_tag> &&
262                    less_than_comparable<typename std::iterator_traits<range_iterator_type<Range>>::value_type>)
263 void parallel_sort( Range&& rng ) {
264     parallel_sort(std::begin(rng), std::end(rng));
265 }
266 //@}
267 
268 } // namespace d1
269 } // namespace detail
270 
271 inline namespace v1 {
272     using detail::d1::parallel_sort;
273 } // namespace v1
274 } // namespace tbb
275 
276 #endif /*__TBB_parallel_sort_H*/
277