1*38fd1498Szrj // -*- C++ -*- 2*38fd1498Szrj 3*38fd1498Szrj // Copyright (C) 2007-2018 Free Software Foundation, Inc. 4*38fd1498Szrj // 5*38fd1498Szrj // This file is part of the GNU ISO C++ Library. This library is free 6*38fd1498Szrj // software; you can redistribute __it and/or modify __it under the terms 7*38fd1498Szrj // of the GNU General Public License as published by the Free Software 8*38fd1498Szrj // Foundation; either version 3, or (at your option) any later 9*38fd1498Szrj // version. 10*38fd1498Szrj 11*38fd1498Szrj // This library is distributed in the hope that __it will be useful, but 12*38fd1498Szrj // WITHOUT ANY WARRANTY; without even the implied warranty of 13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14*38fd1498Szrj // General Public License for more details. 15*38fd1498Szrj 16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional 17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version 18*38fd1498Szrj // 3.1, as published by the Free Software Foundation. 19*38fd1498Szrj 20*38fd1498Szrj // You should have received a copy of the GNU General Public License and 21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program; 22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23*38fd1498Szrj // <http://www.gnu.org/licenses/>. 24*38fd1498Szrj 25*38fd1498Szrj /** @file parallel/list_partition.h 26*38fd1498Szrj * @brief _Functionality to split __sequence referenced by only input 27*38fd1498Szrj * iterators. 28*38fd1498Szrj * This file is a GNU parallel extension to the Standard C++ Library. 29*38fd1498Szrj */ 30*38fd1498Szrj 31*38fd1498Szrj // Written by Leonor Frias Moya and Johannes Singler. 32*38fd1498Szrj 33*38fd1498Szrj #ifndef _GLIBCXX_PARALLEL_LIST_PARTITION_H 34*38fd1498Szrj #define _GLIBCXX_PARALLEL_LIST_PARTITION_H 1 35*38fd1498Szrj 36*38fd1498Szrj #include <parallel/parallel.h> 37*38fd1498Szrj #include <vector> 38*38fd1498Szrj 39*38fd1498Szrj namespace __gnu_parallel 40*38fd1498Szrj { 41*38fd1498Szrj /** @brief Shrinks and doubles the ranges. 42*38fd1498Szrj * @param __os_starts Start positions worked on (oversampled). 43*38fd1498Szrj * @param __count_to_two Counts up to 2. 44*38fd1498Szrj * @param __range_length Current length of a chunk. 45*38fd1498Szrj * @param __make_twice Whether the @c __os_starts is allowed to be 46*38fd1498Szrj * grown or not 47*38fd1498Szrj */ 48*38fd1498Szrj template<typename _IIter> 49*38fd1498Szrj void __shrink_and_double(std::vector<_IIter> & __os_starts,size_t & __count_to_two,size_t & __range_length,const bool __make_twice)50*38fd1498Szrj __shrink_and_double(std::vector<_IIter>& __os_starts, 51*38fd1498Szrj size_t& __count_to_two, size_t& __range_length, 52*38fd1498Szrj const bool __make_twice) 53*38fd1498Szrj { 54*38fd1498Szrj ++__count_to_two; 55*38fd1498Szrj if (!__make_twice || __count_to_two < 2) 56*38fd1498Szrj __shrink(__os_starts, __count_to_two, __range_length); 57*38fd1498Szrj else 58*38fd1498Szrj { 59*38fd1498Szrj __os_starts.resize((__os_starts.size() - 1) * 2 + 1); 60*38fd1498Szrj __count_to_two = 0; 61*38fd1498Szrj } 62*38fd1498Szrj } 63*38fd1498Szrj 64*38fd1498Szrj /** @brief Combines two ranges into one and thus halves the number of ranges. 65*38fd1498Szrj * @param __os_starts Start positions worked on (oversampled). 66*38fd1498Szrj * @param __count_to_two Counts up to 2. 67*38fd1498Szrj * @param __range_length Current length of a chunk. */ 68*38fd1498Szrj template<typename _IIter> 69*38fd1498Szrj void __shrink(std::vector<_IIter> & __os_starts,size_t & __count_to_two,size_t & __range_length)70*38fd1498Szrj __shrink(std::vector<_IIter>& __os_starts, size_t& __count_to_two, 71*38fd1498Szrj size_t& __range_length) 72*38fd1498Szrj { 73*38fd1498Szrj for (typename std::vector<_IIter>::size_type __i = 0; 74*38fd1498Szrj __i <= (__os_starts.size() / 2); ++__i) 75*38fd1498Szrj __os_starts[__i] = __os_starts[__i * 2]; 76*38fd1498Szrj __range_length *= 2; 77*38fd1498Szrj } 78*38fd1498Szrj 79*38fd1498Szrj /** @brief Splits a sequence given by input iterators into parts of 80*38fd1498Szrj * almost equal size 81*38fd1498Szrj * 82*38fd1498Szrj * The function needs only one pass over the sequence. 83*38fd1498Szrj * @param __begin Begin iterator of input sequence. 84*38fd1498Szrj * @param __end End iterator of input sequence. 85*38fd1498Szrj * @param __starts Start iterators for the resulting parts, dimension 86*38fd1498Szrj * @c __num_parts+1. For convenience, @c __starts @c [__num_parts] 87*38fd1498Szrj * contains the end iterator of the sequence. 88*38fd1498Szrj * @param __lengths Length of the resulting parts. 89*38fd1498Szrj * @param __num_parts Number of parts to split the sequence into. 90*38fd1498Szrj * @param __f Functor to be applied to each element by traversing __it 91*38fd1498Szrj * @param __oversampling Oversampling factor. If 0, then the 92*38fd1498Szrj * partitions will differ in at most 93*38fd1498Szrj * \f$\sqrt{\mathrm{end} - \mathrm{begin}}\f$ 94*38fd1498Szrj * elements. Otherwise, the ratio between the 95*38fd1498Szrj * longest and the shortest part is bounded by 96*38fd1498Szrj * \f$1/(\mathrm{oversampling} \cdot \mathrm{num\_parts})\f$ 97*38fd1498Szrj * @return Length of the whole sequence. 98*38fd1498Szrj */ 99*38fd1498Szrj template<typename _IIter, typename _FunctorType> 100*38fd1498Szrj size_t 101*38fd1498Szrj list_partition(const _IIter __begin, const _IIter __end, 102*38fd1498Szrj _IIter* __starts, size_t* __lengths, const int __num_parts, 103*38fd1498Szrj _FunctorType& __f, int __oversampling = 0) 104*38fd1498Szrj { 105*38fd1498Szrj bool __make_twice = false; 106*38fd1498Szrj 107*38fd1498Szrj // The resizing algorithm is chosen according to the oversampling factor. 108*38fd1498Szrj if (__oversampling == 0) 109*38fd1498Szrj { 110*38fd1498Szrj __make_twice = true; 111*38fd1498Szrj __oversampling = 1; 112*38fd1498Szrj } 113*38fd1498Szrj 114*38fd1498Szrj std::vector<_IIter> __os_starts(2 * __oversampling * __num_parts + 1); 115*38fd1498Szrj 116*38fd1498Szrj __os_starts[0] = __begin; 117*38fd1498Szrj _IIter __prev = __begin, __it = __begin; 118*38fd1498Szrj size_t __dist_limit = 0, __dist = 0; 119*38fd1498Szrj size_t __cur = 1, __next = 1; 120*38fd1498Szrj size_t __range_length = 1; 121*38fd1498Szrj size_t __count_to_two = 0; 122*38fd1498Szrj while (__it != __end) 123*38fd1498Szrj { 124*38fd1498Szrj __cur = __next; 125*38fd1498Szrj for (; __cur < __os_starts.size() and __it != __end; ++__cur) 126*38fd1498Szrj { 127*38fd1498Szrj for (__dist_limit += __range_length; 128*38fd1498Szrj __dist < __dist_limit and __it != __end; ++__dist) 129*38fd1498Szrj { 130*38fd1498Szrj __f(__it); 131*38fd1498Szrj ++__it; 132*38fd1498Szrj } 133*38fd1498Szrj __os_starts[__cur] = __it; 134*38fd1498Szrj } 135*38fd1498Szrj 136*38fd1498Szrj // Must compare for end and not __cur < __os_starts.size() , because 137*38fd1498Szrj // __cur could be == __os_starts.size() as well 138*38fd1498Szrj if (__it == __end) 139*38fd1498Szrj break; 140*38fd1498Szrj 141*38fd1498Szrj __shrink_and_double(__os_starts, __count_to_two, __range_length, 142*38fd1498Szrj __make_twice); 143*38fd1498Szrj __next = __os_starts.size() / 2 + 1; 144*38fd1498Szrj } 145*38fd1498Szrj 146*38fd1498Szrj // Calculation of the parts (one must be extracted from __current 147*38fd1498Szrj // because the partition beginning at end, consists only of 148*38fd1498Szrj // itself). 149*38fd1498Szrj size_t __size_part = (__cur - 1) / __num_parts; 150*38fd1498Szrj int __size_greater = static_cast<int>((__cur - 1) % __num_parts); 151*38fd1498Szrj __starts[0] = __os_starts[0]; 152*38fd1498Szrj 153*38fd1498Szrj size_t __index = 0; 154*38fd1498Szrj 155*38fd1498Szrj // Smallest partitions. 156*38fd1498Szrj for (int __i = 1; __i < (__num_parts + 1 - __size_greater); ++__i) 157*38fd1498Szrj { 158*38fd1498Szrj __lengths[__i - 1] = __size_part * __range_length; 159*38fd1498Szrj __index += __size_part; 160*38fd1498Szrj __starts[__i] = __os_starts[__index]; 161*38fd1498Szrj } 162*38fd1498Szrj 163*38fd1498Szrj // Biggest partitions. 164*38fd1498Szrj for (int __i = __num_parts + 1 - __size_greater; __i <= __num_parts; 165*38fd1498Szrj ++__i) 166*38fd1498Szrj { 167*38fd1498Szrj __lengths[__i - 1] = (__size_part+1) * __range_length; 168*38fd1498Szrj __index += (__size_part+1); 169*38fd1498Szrj __starts[__i] = __os_starts[__index]; 170*38fd1498Szrj } 171*38fd1498Szrj 172*38fd1498Szrj // Correction of the end size (the end iteration has not finished). 173*38fd1498Szrj __lengths[__num_parts - 1] -= (__dist_limit - __dist); 174*38fd1498Szrj 175*38fd1498Szrj return __dist; 176*38fd1498Szrj } 177*38fd1498Szrj } 178*38fd1498Szrj 179*38fd1498Szrj #endif /* _GLIBCXX_PARALLEL_LIST_PARTITION_H */ 180