list_partition.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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/list_partition.h
  21. * @brief _Functionality to split __sequence referenced by only input
  22. * iterators.
  23. * This file is a GNU parallel extension to the Standard C++ Library.
  24. */
  25. // Written by Leonor Frias Moya and Johannes Singler.
  26. #ifndef _GLIBCXX_PARALLEL_LIST_PARTITION_H
  27. #define _GLIBCXX_PARALLEL_LIST_PARTITION_H 1
  28. #include <parallel/parallel.h>
  29. #include <vector>
  30. namespace __gnu_parallel
  31. {
  32. /** @brief Shrinks and doubles the ranges.
  33. * @param __os_starts Start positions worked on (oversampled).
  34. * @param __count_to_two Counts up to 2.
  35. * @param __range_length Current length of a chunk.
  36. * @param __make_twice Whether the @c __os_starts is allowed to be
  37. * grown or not
  38. */
  39. template<typename _IIter>
  40. void
  41. __shrink_and_double(std::vector<_IIter>& __os_starts,
  42. size_t& __count_to_two, size_t& __range_length,
  43. const bool __make_twice)
  44. {
  45. ++__count_to_two;
  46. if (!__make_twice || __count_to_two < 2)
  47. __shrink(__os_starts, __count_to_two, __range_length);
  48. else
  49. {
  50. __os_starts.resize((__os_starts.size() - 1) * 2 + 1);
  51. __count_to_two = 0;
  52. }
  53. }
  54. /** @brief Combines two ranges into one and thus halves the number of ranges.
  55. * @param __os_starts Start positions worked on (oversampled).
  56. * @param __count_to_two Counts up to 2.
  57. * @param __range_length Current length of a chunk. */
  58. template<typename _IIter>
  59. void
  60. __shrink(std::vector<_IIter>& __os_starts, size_t& __count_to_two,
  61. size_t& __range_length)
  62. {
  63. for (typename std::vector<_IIter>::size_type __i = 0;
  64. __i <= (__os_starts.size() / 2); ++__i)
  65. __os_starts[__i] = __os_starts[__i * 2];
  66. __range_length *= 2;
  67. }
  68. /** @brief Splits a sequence given by input iterators into parts of
  69. * almost equal size
  70. *
  71. * The function needs only one pass over the sequence.
  72. * @param __begin Begin iterator of input sequence.
  73. * @param __end End iterator of input sequence.
  74. * @param __starts Start iterators for the resulting parts, dimension
  75. * @c __num_parts+1. For convenience, @c __starts @c [__num_parts]
  76. * contains the end iterator of the sequence.
  77. * @param __lengths Length of the resulting parts.
  78. * @param __num_parts Number of parts to split the sequence into.
  79. * @param __f Functor to be applied to each element by traversing __it
  80. * @param __oversampling Oversampling factor. If 0, then the
  81. * partitions will differ in at most
  82. * \f$\sqrt{\mathrm{end} - \mathrm{begin}}\f$
  83. * elements. Otherwise, the ratio between the
  84. * longest and the shortest part is bounded by
  85. * \f$1/(\mathrm{oversampling} \cdot \mathrm{num\_parts})\f$
  86. * @return Length of the whole sequence.
  87. */
  88. template<typename _IIter, typename _FunctorType>
  89. size_t
  90. list_partition(const _IIter __begin, const _IIter __end,
  91. _IIter* __starts, size_t* __lengths, const int __num_parts,
  92. _FunctorType& __f, int __oversampling = 0)
  93. {
  94. bool __make_twice = false;
  95. // The resizing algorithm is chosen according to the oversampling factor.
  96. if (__oversampling == 0)
  97. {
  98. __make_twice = true;
  99. __oversampling = 1;
  100. }
  101. std::vector<_IIter> __os_starts(2 * __oversampling * __num_parts + 1);
  102. __os_starts[0] = __begin;
  103. _IIter __prev = __begin, __it = __begin;
  104. size_t __dist_limit = 0, __dist = 0;
  105. size_t __cur = 1, __next = 1;
  106. size_t __range_length = 1;
  107. size_t __count_to_two = 0;
  108. while (__it != __end)
  109. {
  110. __cur = __next;
  111. for (; __cur < __os_starts.size() and __it != __end; ++__cur)
  112. {
  113. for (__dist_limit += __range_length;
  114. __dist < __dist_limit and __it != __end; ++__dist)
  115. {
  116. __f(__it);
  117. ++__it;
  118. }
  119. __os_starts[__cur] = __it;
  120. }
  121. // Must compare for end and not __cur < __os_starts.size() , because
  122. // __cur could be == __os_starts.size() as well
  123. if (__it == __end)
  124. break;
  125. __shrink_and_double(__os_starts, __count_to_two, __range_length,
  126. __make_twice);
  127. __next = __os_starts.size() / 2 + 1;
  128. }
  129. // Calculation of the parts (one must be extracted from __current
  130. // because the partition beginning at end, consists only of
  131. // itself).
  132. size_t __size_part = (__cur - 1) / __num_parts;
  133. int __size_greater = static_cast<int>((__cur - 1) % __num_parts);
  134. __starts[0] = __os_starts[0];
  135. size_t __index = 0;
  136. // Smallest partitions.
  137. for (int __i = 1; __i < (__num_parts + 1 - __size_greater); ++__i)
  138. {
  139. __lengths[__i - 1] = __size_part * __range_length;
  140. __index += __size_part;
  141. __starts[__i] = __os_starts[__index];
  142. }
  143. // Biggest partitions.
  144. for (int __i = __num_parts + 1 - __size_greater; __i <= __num_parts;
  145. ++__i)
  146. {
  147. __lengths[__i - 1] = (__size_part+1) * __range_length;
  148. __index += (__size_part+1);
  149. __starts[__i] = __os_starts[__index];
  150. }
  151. // Correction of the end size (the end iteration has not finished).
  152. __lengths[__num_parts - 1] -= (__dist_limit - __dist);
  153. return __dist;
  154. }
  155. }
  156. #endif /* _GLIBCXX_PARALLEL_LIST_PARTITION_H */