quicksort.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // -*- C++ -*-
  2. // Copyright (C) 2007-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the terms
  6. // of the GNU General Public License as published by the Free Software
  7. // Foundation; either version 3, or (at your option) any later
  8. // version.
  9. // This library is distributed in the hope that it will be useful, but
  10. // WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file parallel/quicksort.h
  21. * @brief Implementation of a unbalanced parallel quicksort (in-place).
  22. * This file is a GNU parallel extension to the Standard C++ Library.
  23. */
  24. // Written by Johannes Singler.
  25. #ifndef _GLIBCXX_PARALLEL_QUICKSORT_H
  26. #define _GLIBCXX_PARALLEL_QUICKSORT_H 1
  27. #include <parallel/parallel.h>
  28. #include <parallel/partition.h>
  29. namespace __gnu_parallel
  30. {
  31. /** @brief Unbalanced quicksort divide step.
  32. * @param __begin Begin iterator of subsequence.
  33. * @param __end End iterator of subsequence.
  34. * @param __comp Comparator.
  35. * @param __pivot_rank Desired __rank of the pivot.
  36. * @param __num_samples Choose pivot from that many samples.
  37. * @param __num_threads Number of threads that are allowed to work on
  38. * this part.
  39. */
  40. template<typename _RAIter, typename _Compare>
  41. typename std::iterator_traits<_RAIter>::difference_type
  42. __parallel_sort_qs_divide(_RAIter __begin, _RAIter __end,
  43. _Compare __comp, typename std::iterator_traits
  44. <_RAIter>::difference_type __pivot_rank,
  45. typename std::iterator_traits
  46. <_RAIter>::difference_type
  47. __num_samples, _ThreadIndex __num_threads)
  48. {
  49. typedef std::iterator_traits<_RAIter> _TraitsType;
  50. typedef typename _TraitsType::value_type _ValueType;
  51. typedef typename _TraitsType::difference_type _DifferenceType;
  52. _DifferenceType __n = __end - __begin;
  53. __num_samples = std::min(__num_samples, __n);
  54. // Allocate uninitialized, to avoid default constructor.
  55. _ValueType* __samples = static_cast<_ValueType*>
  56. (::operator new(__num_samples * sizeof(_ValueType)));
  57. for (_DifferenceType __s = 0; __s < __num_samples; ++__s)
  58. {
  59. const unsigned long long __index = static_cast<unsigned long long>
  60. (__s) * __n / __num_samples;
  61. ::new(&(__samples[__s])) _ValueType(__begin[__index]);
  62. }
  63. __gnu_sequential::sort(__samples, __samples + __num_samples, __comp);
  64. _ValueType& __pivot = __samples[__pivot_rank * __num_samples / __n];
  65. __gnu_parallel::__binder2nd<_Compare, _ValueType, _ValueType, bool>
  66. __pred(__comp, __pivot);
  67. _DifferenceType __split = __parallel_partition(__begin, __end,
  68. __pred, __num_threads);
  69. for (_DifferenceType __s = 0; __s < __num_samples; ++__s)
  70. __samples[__s].~_ValueType();
  71. ::operator delete(__samples);
  72. return __split;
  73. }
  74. /** @brief Unbalanced quicksort conquer step.
  75. * @param __begin Begin iterator of subsequence.
  76. * @param __end End iterator of subsequence.
  77. * @param __comp Comparator.
  78. * @param __num_threads Number of threads that are allowed to work on
  79. * this part.
  80. */
  81. template<typename _RAIter, typename _Compare>
  82. void
  83. __parallel_sort_qs_conquer(_RAIter __begin, _RAIter __end,
  84. _Compare __comp,
  85. _ThreadIndex __num_threads)
  86. {
  87. typedef std::iterator_traits<_RAIter> _TraitsType;
  88. typedef typename _TraitsType::value_type _ValueType;
  89. typedef typename _TraitsType::difference_type _DifferenceType;
  90. if (__num_threads <= 1)
  91. {
  92. __gnu_sequential::sort(__begin, __end, __comp);
  93. return;
  94. }
  95. _DifferenceType __n = __end - __begin, __pivot_rank;
  96. if (__n <= 1)
  97. return;
  98. _ThreadIndex __num_threads_left;
  99. if ((__num_threads % 2) == 1)
  100. __num_threads_left = __num_threads / 2 + 1;
  101. else
  102. __num_threads_left = __num_threads / 2;
  103. __pivot_rank = __n * __num_threads_left / __num_threads;
  104. _DifferenceType __split = __parallel_sort_qs_divide
  105. (__begin, __end, __comp, __pivot_rank,
  106. _Settings::get().sort_qs_num_samples_preset, __num_threads);
  107. #pragma omp parallel sections num_threads(2)
  108. {
  109. #pragma omp section
  110. __parallel_sort_qs_conquer(__begin, __begin + __split,
  111. __comp, __num_threads_left);
  112. #pragma omp section
  113. __parallel_sort_qs_conquer(__begin + __split, __end,
  114. __comp, __num_threads - __num_threads_left);
  115. }
  116. }
  117. /** @brief Unbalanced quicksort main call.
  118. * @param __begin Begin iterator of input sequence.
  119. * @param __end End iterator input sequence, ignored.
  120. * @param __comp Comparator.
  121. * @param __num_threads Number of threads that are allowed to work on
  122. * this part.
  123. */
  124. template<typename _RAIter, typename _Compare>
  125. void
  126. __parallel_sort_qs(_RAIter __begin, _RAIter __end,
  127. _Compare __comp,
  128. _ThreadIndex __num_threads)
  129. {
  130. _GLIBCXX_CALL(__n)
  131. typedef std::iterator_traits<_RAIter> _TraitsType;
  132. typedef typename _TraitsType::value_type _ValueType;
  133. typedef typename _TraitsType::difference_type _DifferenceType;
  134. _DifferenceType __n = __end - __begin;
  135. // At least one element per processor.
  136. if (__num_threads > __n)
  137. __num_threads = static_cast<_ThreadIndex>(__n);
  138. __parallel_sort_qs_conquer(
  139. __begin, __begin + __n, __comp, __num_threads);
  140. }
  141. } //namespace __gnu_parallel
  142. #endif /* _GLIBCXX_PARALLEL_QUICKSORT_H */