]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/parallel/partial_sum.h
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / parallel / partial_sum.h
CommitLineData
c2ba9709
JS
1// -*- C++ -*-
2
a945c346 3// Copyright (C) 2007-2024 Free Software Foundation, Inc.
c2ba9709
JS
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the terms
7// of the GNU General Public License as published by the Free Software
748086b7 8// Foundation; either version 3, or (at your option) any later
c2ba9709
JS
9// version.
10
11// This library is distributed in the hope that it will be useful, but
12// WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15
748086b7
JJ
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
c2ba9709
JS
24
25/** @file parallel/partial_sum.h
1acba85b
JS
26 * @brief Parallel implementation of std::partial_sum(), i.e. prefix
27* sums.
c2ba9709
JS
28 * This file is a GNU parallel extension to the Standard C++ Library.
29 */
30
31// Written by Johannes Singler.
32
33#ifndef _GLIBCXX_PARALLEL_PARTIAL_SUM_H
34#define _GLIBCXX_PARALLEL_PARTIAL_SUM_H 1
35
c2ba9709 36#include <omp.h>
58a6ef4b 37#include <new>
c2ba9709
JS
38#include <bits/stl_algobase.h>
39#include <parallel/parallel.h>
40#include <parallel/numericfwd.h>
41
42namespace __gnu_parallel
43{
44 // Problem: there is no 0-element given.
45
338311e5
PC
46 /** @brief Base case prefix sum routine.
47 * @param __begin Begin iterator of input sequence.
48 * @param __end End iterator of input sequence.
49 * @param __result Begin iterator of output sequence.
50 * @param __bin_op Associative binary function.
51 * @param __value Start value. Must be passed since the neutral
52 * element is unknown in general.
53 * @return End iterator of output sequence. */
54 template<typename _IIter,
55 typename _OutputIterator,
56 typename _BinaryOperation>
57 _OutputIterator
58 __parallel_partial_sum_basecase(_IIter __begin, _IIter __end,
59 _OutputIterator __result,
60 _BinaryOperation __bin_op,
61 typename std::iterator_traits <_IIter>::value_type __value)
62 {
63 if (__begin == __end)
64 return __result;
65
66 while (__begin != __end)
67 {
68 __value = __bin_op(__value, *__begin);
69 *__result = __value;
70 ++__result;
71 ++__begin;
72 }
1acba85b 73 return __result;
338311e5
PC
74 }
75
76 /** @brief Parallel partial sum implementation, two-phase approach,
77 no recursion.
78 * @param __begin Begin iterator of input sequence.
79 * @param __end End iterator of input sequence.
80 * @param __result Begin iterator of output sequence.
81 * @param __bin_op Associative binary function.
82 * @param __n Length of sequence.
338311e5
PC
83 * @return End iterator of output sequence.
84 */
85 template<typename _IIter,
86 typename _OutputIterator,
87 typename _BinaryOperation>
88 _OutputIterator
89 __parallel_partial_sum_linear(_IIter __begin, _IIter __end,
90 _OutputIterator __result,
91 _BinaryOperation __bin_op,
92 typename std::iterator_traits<_IIter>::difference_type __n)
93 {
94 typedef std::iterator_traits<_IIter> _TraitsType;
95 typedef typename _TraitsType::value_type _ValueType;
96 typedef typename _TraitsType::difference_type _DifferenceType;
97
98 if (__begin == __end)
99 return __result;
100
101 _ThreadIndex __num_threads =
1acba85b 102 std::min<_DifferenceType>(__get_max_threads(), __n - 1);
e683ee2a 103
338311e5
PC
104 if (__num_threads < 2)
105 {
106 *__result = *__begin;
107 return __parallel_partial_sum_basecase(__begin + 1, __end,
108 __result + 1, __bin_op,
109 *__begin);
110 }
c2ba9709 111
338311e5
PC
112 _DifferenceType* __borders;
113 _ValueType* __sums;
c2ba9709 114
338311e5 115 const _Settings& __s = _Settings::get();
ee1b5fc5 116
338311e5 117# pragma omp parallel num_threads(__num_threads)
c2ba9709 118 {
e683ee2a 119# pragma omp single
338311e5
PC
120 {
121 __num_threads = omp_get_num_threads();
122
123 __borders = new _DifferenceType[__num_threads + 2];
124
125 if (__s.partial_sum_dilation == 1.0f)
6b77089f 126 __equally_split(__n, __num_threads + 1, __borders);
338311e5
PC
127 else
128 {
736cc331
JS
129 _DifferenceType __first_part_length =
130 std::max<_DifferenceType>(1,
131 __n / (1.0f + __s.partial_sum_dilation * __num_threads));
338311e5 132 _DifferenceType __chunk_length =
736cc331
JS
133 (__n - __first_part_length) / __num_threads;
134 _DifferenceType __borderstart =
135 __n - __num_threads * __chunk_length;
338311e5 136 __borders[0] = 0;
8b0c13a8 137 for (_ThreadIndex __i = 1; __i < (__num_threads + 1); ++__i)
338311e5
PC
138 {
139 __borders[__i] = __borderstart;
140 __borderstart += __chunk_length;
141 }
142 __borders[__num_threads + 1] = __n;
143 }
144
145 __sums = static_cast<_ValueType*>(::operator new(sizeof(_ValueType)
15ac3c72 146 * __num_threads));
338311e5
PC
147 _OutputIterator __target_end;
148 } //single
e683ee2a 149
1acba85b
JS
150 _ThreadIndex __iam = omp_get_thread_num();
151 if (__iam == 0)
e683ee2a 152 {
1acba85b 153 *__result = *__begin;
77d16198
PC
154 __parallel_partial_sum_basecase(__begin + 1,
155 __begin + __borders[1],
156 __result + 1,
157 __bin_op, *__begin);
1acba85b 158 ::new(&(__sums[__iam])) _ValueType(*(__result + __borders[1] - 1));
e683ee2a
JS
159 }
160 else
161 {
1acba85b 162 ::new(&(__sums[__iam]))
0e50f335
JS
163 _ValueType(__gnu_parallel::accumulate(
164 __begin + __borders[__iam] + 1,
15ac3c72
JS
165 __begin + __borders[__iam + 1],
166 *(__begin + __borders[__iam]),
167 __bin_op,
168 __gnu_parallel::sequential_tag()));
e683ee2a
JS
169 }
170
171# pragma omp barrier
172
173# pragma omp single
338311e5 174 __parallel_partial_sum_basecase(__sums + 1, __sums + __num_threads,
77d16198 175 __sums + 1, __bin_op, __sums[0]);
e683ee2a
JS
176
177# pragma omp barrier
178
338311e5
PC
179 // Still same team.
180 __parallel_partial_sum_basecase(__begin + __borders[__iam + 1],
181 __begin + __borders[__iam + 2],
182 __result + __borders[__iam + 1],
183 __bin_op, __sums[__iam]);
e683ee2a
JS
184 } //parallel
185
0ecca7a6
PC
186 for (_ThreadIndex __i = 0; __i < __num_threads; ++__i)
187 __sums[__i].~_ValueType();
338311e5 188 ::operator delete(__sums);
0ecca7a6 189
338311e5
PC
190 delete[] __borders;
191
192 return __result + __n;
193 }
194
195 /** @brief Parallel partial sum front-__end.
196 * @param __begin Begin iterator of input sequence.
197 * @param __end End iterator of input sequence.
198 * @param __result Begin iterator of output sequence.
199 * @param __bin_op Associative binary function.
200 * @return End iterator of output sequence. */
201 template<typename _IIter,
202 typename _OutputIterator,
203 typename _BinaryOperation>
204 _OutputIterator
205 __parallel_partial_sum(_IIter __begin, _IIter __end,
206 _OutputIterator __result, _BinaryOperation __bin_op)
207 {
208 _GLIBCXX_CALL(__begin - __end)
209
210 typedef std::iterator_traits<_IIter> _TraitsType;
211 typedef typename _TraitsType::value_type _ValueType;
212 typedef typename _TraitsType::difference_type _DifferenceType;
213
214 _DifferenceType __n = __end - __begin;
215
216 switch (_Settings::get().partial_sum_algorithm)
217 {
218 case LINEAR:
219 // Need an initial offset.
220 return __parallel_partial_sum_linear(__begin, __end, __result,
221 __bin_op, __n);
222 default:
223 // Partial_sum algorithm not implemented.
224 _GLIBCXX_PARALLEL_ASSERT(0);
225 return __result + __n;
226 }
227 }
c2ba9709
JS
228}
229
cbcd1e45 230#endif /* _GLIBCXX_PARALLEL_PARTIAL_SUM_H */