33 #ifndef _GLIBCXX_PARALLEL_PARTIAL_SUM_H
34 #define _GLIBCXX_PARALLEL_PARTIAL_SUM_H 1
42 namespace __gnu_parallel
54 template<
typename _IIter,
55 typename _OutputIterator,
56 typename _BinaryOperation>
59 _OutputIterator __result,
60 _BinaryOperation __bin_op,
61 typename std::iterator_traits <_IIter>::value_type __value)
66 while (__begin != __end)
68 __value = __bin_op(__value, *__begin);
86 template<
typename _IIter,
87 typename _OutputIterator,
88 typename _BinaryOperation>
91 _OutputIterator __result,
92 _BinaryOperation __bin_op,
93 typename std::iterator_traits<_IIter>::difference_type __n)
95 typedef std::iterator_traits<_IIter> _TraitsType;
96 typedef typename _TraitsType::value_type _ValueType;
97 typedef typename _TraitsType::difference_type _DifferenceType;
103 std::min<_DifferenceType>(__get_max_threads(), __n - 1);
105 if (__num_threads < 2)
107 *__result = *__begin;
109 __result + 1, __bin_op,
113 _DifferenceType* __borders;
118 # pragma omp parallel num_threads(__num_threads)
122 __num_threads = omp_get_num_threads();
124 __borders =
new _DifferenceType[__num_threads + 2];
130 _DifferenceType __first_part_length =
131 std::max<_DifferenceType>(1,
133 _DifferenceType __chunk_length =
134 (__n - __first_part_length) / __num_threads;
135 _DifferenceType __borderstart =
136 __n - __num_threads * __chunk_length;
138 for (
_ThreadIndex __i = 1; __i < (__num_threads + 1); ++__i)
140 __borders[__i] = __borderstart;
141 __borderstart += __chunk_length;
143 __borders[__num_threads + 1] = __n;
146 __sums =
static_cast<_ValueType*
>(::operator
new(
sizeof(_ValueType)
148 _OutputIterator __target_end;
154 *__result = *__begin;
156 __begin + __borders[1],
159 ::new(&(__sums[__iam])) _ValueType(*(__result + __borders[1] - 1));
163 ::new(&(__sums[__iam]))
164 _ValueType(__gnu_parallel::accumulate(
165 __begin + __borders[__iam] + 1,
166 __begin + __borders[__iam + 1],
167 *(__begin + __borders[__iam]),
176 __sums + 1, __bin_op, __sums[0]);
182 __begin + __borders[__iam + 2],
183 __result + __borders[__iam + 1],
184 __bin_op, __sums[__iam]);
188 __sums[__i].~_ValueType();
189 ::operator
delete(__sums);
193 return __result + __n;
202 template<
typename _IIter,
203 typename _OutputIterator,
204 typename _BinaryOperation>
207 _OutputIterator __result, _BinaryOperation __bin_op)
211 typedef std::iterator_traits<_IIter> _TraitsType;
212 typedef typename _TraitsType::value_type _ValueType;
213 typedef typename _TraitsType::difference_type _DifferenceType;
215 _DifferenceType __n = __end - __begin;
225 _GLIBCXX_PARALLEL_ASSERT(0);
226 return __result + __n;