parallel_sort.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. Copyright (c) 2005-2020 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #ifndef __TBB_parallel_sort_H
  14. #define __TBB_parallel_sort_H
  15. #define __TBB_parallel_sort_H_include_area
  16. #include "internal/_warning_suppress_enable_notice.h"
  17. #include "parallel_for.h"
  18. #include "blocked_range.h"
  19. #include "internal/_range_iterator.h"
  20. #include <algorithm>
  21. #include <iterator>
  22. #include <functional>
  23. #if __TBB_TASK_GROUP_CONTEXT
  24. #include "tbb_profiling.h"
  25. #endif
  26. namespace tbb {
  27. namespace interface9 {
  28. //! @cond INTERNAL
  29. namespace internal {
  30. using tbb::internal::no_assign;
  31. //! Range used in quicksort to split elements into subranges based on a value.
  32. /** The split operation selects a splitter and places all elements less than or equal
  33. to the value in the first range and the remaining elements in the second range.
  34. @ingroup algorithms */
  35. template<typename RandomAccessIterator, typename Compare>
  36. class quick_sort_range: private no_assign {
  37. inline size_t median_of_three(const RandomAccessIterator &array, size_t l, size_t m, size_t r) const {
  38. return comp(array[l], array[m]) ? ( comp(array[m], array[r]) ? m : ( comp( array[l], array[r]) ? r : l ) )
  39. : ( comp(array[r], array[m]) ? m : ( comp( array[r], array[l] ) ? r : l ) );
  40. }
  41. inline size_t pseudo_median_of_nine( const RandomAccessIterator &array, const quick_sort_range &range ) const {
  42. size_t offset = range.size/8u;
  43. return median_of_three(array,
  44. median_of_three(array, 0, offset, offset*2),
  45. median_of_three(array, offset*3, offset*4, offset*5),
  46. median_of_three(array, offset*6, offset*7, range.size - 1) );
  47. }
  48. size_t split_range( quick_sort_range& range ) {
  49. using std::iter_swap;
  50. RandomAccessIterator array = range.begin;
  51. RandomAccessIterator key0 = range.begin;
  52. size_t m = pseudo_median_of_nine(array, range);
  53. if (m) iter_swap ( array, array+m );
  54. size_t i=0;
  55. size_t j=range.size;
  56. // Partition interval [i+1,j-1] with key *key0.
  57. for(;;) {
  58. __TBB_ASSERT( i<j, NULL );
  59. // Loop must terminate since array[l]==*key0.
  60. do {
  61. --j;
  62. __TBB_ASSERT( i<=j, "bad ordering relation?" );
  63. } while( comp( *key0, array[j] ));
  64. do {
  65. __TBB_ASSERT( i<=j, NULL );
  66. if( i==j ) goto partition;
  67. ++i;
  68. } while( comp( array[i],*key0 ));
  69. if( i==j ) goto partition;
  70. iter_swap( array+i, array+j );
  71. }
  72. partition:
  73. // Put the partition key were it belongs
  74. iter_swap( array+j, key0 );
  75. // array[l..j) is less or equal to key.
  76. // array(j..r) is greater or equal to key.
  77. // array[j] is equal to key
  78. i=j+1;
  79. size_t new_range_size = range.size-i;
  80. range.size = j;
  81. return new_range_size;
  82. }
  83. public:
  84. static const size_t grainsize = 500;
  85. const Compare &comp;
  86. size_t size;
  87. RandomAccessIterator begin;
  88. quick_sort_range( RandomAccessIterator begin_, size_t size_, const Compare &comp_ ) :
  89. comp(comp_), size(size_), begin(begin_) {}
  90. bool empty() const {return size==0;}
  91. bool is_divisible() const {return size>=grainsize;}
  92. quick_sort_range( quick_sort_range& range, split )
  93. : comp(range.comp)
  94. , size(split_range(range))
  95. // +1 accounts for the pivot element, which is at its correct place
  96. // already and, therefore, is not included into subranges.
  97. , begin(range.begin+range.size+1) {}
  98. };
  99. #if __TBB_TASK_GROUP_CONTEXT
  100. //! Body class used to test if elements in a range are presorted
  101. /** @ingroup algorithms */
  102. template<typename RandomAccessIterator, typename Compare>
  103. class quick_sort_pretest_body : no_assign {
  104. const Compare &comp;
  105. public:
  106. quick_sort_pretest_body(const Compare &_comp) : comp(_comp) {}
  107. void operator()( const blocked_range<RandomAccessIterator>& range ) const {
  108. task &my_task = task::self();
  109. RandomAccessIterator my_end = range.end();
  110. int i = 0;
  111. for (RandomAccessIterator k = range.begin(); k != my_end; ++k, ++i) {
  112. if ( i%64 == 0 && my_task.is_cancelled() ) break;
  113. // The k-1 is never out-of-range because the first chunk starts at begin+serial_cutoff+1
  114. if ( comp( *(k), *(k-1) ) ) {
  115. my_task.cancel_group_execution();
  116. break;
  117. }
  118. }
  119. }
  120. };
  121. #endif /* __TBB_TASK_GROUP_CONTEXT */
  122. //! Body class used to sort elements in a range that is smaller than the grainsize.
  123. /** @ingroup algorithms */
  124. template<typename RandomAccessIterator, typename Compare>
  125. struct quick_sort_body {
  126. void operator()( const quick_sort_range<RandomAccessIterator,Compare>& range ) const {
  127. //SerialQuickSort( range.begin, range.size, range.comp );
  128. std::sort( range.begin, range.begin + range.size, range.comp );
  129. }
  130. };
  131. //! Wrapper method to initiate the sort by calling parallel_for.
  132. /** @ingroup algorithms */
  133. template<typename RandomAccessIterator, typename Compare>
  134. void parallel_quick_sort( RandomAccessIterator begin, RandomAccessIterator end, const Compare& comp ) {
  135. #if __TBB_TASK_GROUP_CONTEXT
  136. task_group_context my_context(PARALLEL_SORT);
  137. const int serial_cutoff = 9;
  138. __TBB_ASSERT( begin + serial_cutoff < end, "min_parallel_size is smaller than serial cutoff?" );
  139. RandomAccessIterator k = begin;
  140. for ( ; k != begin + serial_cutoff; ++k ) {
  141. if ( comp( *(k+1), *k ) ) {
  142. goto do_parallel_quick_sort;
  143. }
  144. }
  145. parallel_for( blocked_range<RandomAccessIterator>(k+1, end),
  146. quick_sort_pretest_body<RandomAccessIterator,Compare>(comp),
  147. auto_partitioner(),
  148. my_context);
  149. if (my_context.is_group_execution_cancelled())
  150. do_parallel_quick_sort:
  151. #endif /* __TBB_TASK_GROUP_CONTEXT */
  152. parallel_for( quick_sort_range<RandomAccessIterator,Compare>(begin, end-begin, comp ),
  153. quick_sort_body<RandomAccessIterator,Compare>(),
  154. auto_partitioner() );
  155. }
  156. } // namespace internal
  157. //! @endcond
  158. } // namespace interfaceX
  159. /** \page parallel_sort_iter_req Requirements on iterators for parallel_sort
  160. Requirements on the iterator type \c It and its value type \c T for \c parallel_sort:
  161. - \code void iter_swap( It a, It b ) \endcode Swaps the values of the elements the given
  162. iterators \c a and \c b are pointing to. \c It should be a random access iterator.
  163. - \code bool Compare::operator()( const T& x, const T& y ) \endcode True if x comes before y;
  164. **/
  165. /** \name parallel_sort
  166. See also requirements on \ref parallel_sort_iter_req "iterators for parallel_sort". **/
  167. //@{
  168. //! Sorts the data in [begin,end) using the given comparator
  169. /** The compare function object is used for all comparisons between elements during sorting.
  170. The compare object must define a bool operator() function.
  171. @ingroup algorithms **/
  172. template<typename RandomAccessIterator, typename Compare>
  173. void parallel_sort( RandomAccessIterator begin, RandomAccessIterator end, const Compare& comp) {
  174. const int min_parallel_size = 500;
  175. if( end > begin ) {
  176. if (end - begin < min_parallel_size) {
  177. std::sort(begin, end, comp);
  178. } else {
  179. interface9::internal::parallel_quick_sort(begin, end, comp);
  180. }
  181. }
  182. }
  183. //! Sorts the data in [begin,end) with a default comparator \c std::less<RandomAccessIterator>
  184. /** @ingroup algorithms **/
  185. template<typename RandomAccessIterator>
  186. inline void parallel_sort( RandomAccessIterator begin, RandomAccessIterator end ) {
  187. parallel_sort( begin, end, std::less< typename std::iterator_traits<RandomAccessIterator>::value_type >() );
  188. }
  189. //! Sorts the data in rng using the given comparator
  190. /** @ingroup algorithms **/
  191. template<typename Range, typename Compare>
  192. void parallel_sort(Range& rng, const Compare& comp) {
  193. parallel_sort(tbb::internal::first(rng), tbb::internal::last(rng), comp);
  194. }
  195. //! Sorts the data in rng with a default comparator \c std::less<RandomAccessIterator>
  196. /** @ingroup algorithms **/
  197. template<typename Range>
  198. void parallel_sort(Range& rng) {
  199. parallel_sort(tbb::internal::first(rng), tbb::internal::last(rng));
  200. }
  201. //! Sorts the data in the range \c [begin,end) with a default comparator \c std::less<T>
  202. /** @ingroup algorithms **/
  203. template<typename T>
  204. inline void parallel_sort( T * begin, T * end ) {
  205. parallel_sort( begin, end, std::less< T >() );
  206. }
  207. //@}
  208. } // namespace tbb
  209. #include "internal/_warning_suppress_disable_notice.h"
  210. #undef __TBB_parallel_sort_H_include_area
  211. #endif