3 // Copyright (C) 2007 Free Software Foundation, Inc.
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
8 // Foundation; either version 2, or (at your option) any later
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.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
32 * @file parallel/numeric
34 * @brief Parallel STL function calls corresponding to stl_numeric.h.
35 * The functions defined here mainly do case switches and
36 * call the actual parallelized versions in other files.
37 * Inlining policy: Functions that basically only contain one function call,
38 * are declared inline.
39 * This file is a GNU parallel extension to the Standard C++ Library.
42 // Written by Johannes Singler and Felix Putze.
44 #ifndef _GLIBCXX_PARALLEL_NUMERIC_H
45 #define _GLIBCXX_PARALLEL_NUMERIC_H 1
49 #include <parallel/numericfwd.h>
50 #include <parallel/iterator.h>
51 #include <parallel/for_each.h>
52 #include <parallel/for_each_selectors.h>
53 #include <parallel/partial_sum.h>
59 // Sequential fallback.
60 template<typename InputIterator, typename T>
62 accumulate(InputIterator begin, InputIterator end, T init,
63 __gnu_parallel::sequential_tag)
64 { return _GLIBCXX_STD_P::accumulate(begin, end, init); }
66 template<typename InputIterator, typename T, typename BinaryOperation>
68 accumulate(InputIterator begin, InputIterator end, T init,
69 BinaryOperation binary_op, __gnu_parallel::sequential_tag)
70 { return _GLIBCXX_STD_P::accumulate(begin, end, init, binary_op); }
72 // Sequential fallback for input iterator case.
73 template<typename InputIterator, typename T, typename IteratorTag>
75 accumulate_switch(InputIterator begin, InputIterator end, T init, IteratorTag) { return accumulate(begin, end, init, __gnu_parallel::sequential_tag()); }
77 template<typename InputIterator, typename T, typename BinaryOperation, typename IteratorTag>
79 accumulate_switch(InputIterator begin, InputIterator end, T init,
80 BinaryOperation binary_op, IteratorTag)
82 return accumulate(begin, end, init, binary_op,
83 __gnu_parallel::sequential_tag());
86 // Parallel algorithm for random access iterators.
87 template<typename _RandomAccessIterator, typename T, typename BinaryOperation>
89 accumulate_switch(_RandomAccessIterator begin, _RandomAccessIterator end,
90 T init, BinaryOperation binary_op,
91 random_access_iterator_tag,
92 __gnu_parallel::parallelism parallelism_tag
93 = __gnu_parallel::parallel_unbalanced)
95 if (_GLIBCXX_PARALLEL_CONDITION(static_cast<__gnu_parallel::sequence_index_t>(end - begin) >= __gnu_parallel::Settings::accumulate_minimal_n && __gnu_parallel::is_parallel(parallelism_tag)))
98 __gnu_parallel::accumulate_selector<_RandomAccessIterator> my_selector;
99 __gnu_parallel::for_each_template_random_access(begin, end, __gnu_parallel::nothing(), my_selector, __gnu_parallel::accumulate_binop_reduct<BinaryOperation>(binary_op), res, res, -1, parallelism_tag);
103 return accumulate(begin, end, init, binary_op,
104 __gnu_parallel::sequential_tag());
108 template<typename InputIterator, typename T>
110 accumulate(InputIterator begin, InputIterator end, T init,
111 __gnu_parallel::parallelism parallelism_tag)
113 typedef std::iterator_traits<InputIterator> iterator_traits;
114 typedef typename iterator_traits::value_type value_type;
115 typedef typename iterator_traits::iterator_category iterator_category;
117 return accumulate_switch(begin, end, init, __gnu_parallel::plus<T, value_type>(),
118 iterator_category(), parallelism_tag);
121 template<typename InputIterator, typename T>
123 accumulate(InputIterator begin, InputIterator end, T init)
125 typedef std::iterator_traits<InputIterator> iterator_traits;
126 typedef typename iterator_traits::value_type value_type;
127 typedef typename iterator_traits::iterator_category iterator_category;
129 return accumulate_switch(begin, end, init, __gnu_parallel::plus<T, value_type>(),
130 iterator_category());
133 template<typename InputIterator, typename T, typename BinaryOperation>
135 accumulate(InputIterator begin, InputIterator end, T init,
136 BinaryOperation binary_op,
137 __gnu_parallel::parallelism parallelism_tag)
139 typedef iterator_traits<InputIterator> iterator_traits;
140 typedef typename iterator_traits::iterator_category iterator_category;
141 return accumulate_switch(begin, end, init, binary_op,
142 iterator_category(), parallelism_tag);
145 template<typename InputIterator, typename T, typename BinaryOperation>
147 accumulate(InputIterator begin, InputIterator end, T init,
148 BinaryOperation binary_op)
150 typedef iterator_traits<InputIterator> iterator_traits;
151 typedef typename iterator_traits::iterator_category iterator_category;
152 return accumulate_switch(begin, end, init, binary_op,
153 iterator_category());
157 // Sequential fallback.
158 template<typename InputIterator1, typename InputIterator2, typename T>
160 inner_product(InputIterator1 first1, InputIterator1 last1,
161 InputIterator2 first2, T init, __gnu_parallel::sequential_tag)
162 { return _GLIBCXX_STD_P::inner_product(first1, last1, first2, init); }
164 template<typename InputIterator1, typename InputIterator2, typename T, typename BinaryFunction1, typename BinaryFunction2>
166 inner_product(InputIterator1 first1, InputIterator1 last1,
167 InputIterator2 first2, T init, BinaryFunction1 binary_op1,
168 BinaryFunction2 binary_op2, __gnu_parallel::sequential_tag)
170 return _GLIBCXX_STD_P::inner_product(first1, last1, first2, init,
171 binary_op1, binary_op2);
174 // Parallel algorithm for random access iterators.
175 template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename T, typename BinaryFunction1, typename BinaryFunction2>
177 inner_product_switch(RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, T init, BinaryFunction1 binary_op1, BinaryFunction2 binary_op2, random_access_iterator_tag, random_access_iterator_tag, __gnu_parallel::parallelism parallelism_tag = __gnu_parallel::parallel_unbalanced)
179 if (_GLIBCXX_PARALLEL_CONDITION((last1 - first1) >= __gnu_parallel::Settings::accumulate_minimal_n && __gnu_parallel::is_parallel(parallelism_tag)))
182 __gnu_parallel::inner_product_selector<RandomAccessIterator1, RandomAccessIterator2, T> my_selector(first1, first2);
183 __gnu_parallel::for_each_template_random_access(first1, last1, binary_op2, my_selector, binary_op1, res, res, -1, parallelism_tag);
187 return inner_product(first1, last1, first2, init,
188 __gnu_parallel::sequential_tag());
191 // No parallelism for input iterators.
192 template<typename InputIterator1, typename InputIterator2, typename T, typename BinaryFunction1, typename BinaryFunction2, typename IteratorTag1, typename IteratorTag2>
194 inner_product_switch(InputIterator1 first1, InputIterator1 last1,
195 InputIterator2 first2, T init,
196 BinaryFunction1 binary_op1, BinaryFunction2 binary_op2,
197 IteratorTag1, IteratorTag2)
199 return inner_product(first1, last1, first2, init, binary_op1, binary_op2,
200 __gnu_parallel::sequential_tag());
203 template<typename InputIterator1, typename InputIterator2, typename T, typename BinaryFunction1, typename BinaryFunction2>
205 inner_product(InputIterator1 first1, InputIterator1 last1,
206 InputIterator2 first2, T init, BinaryFunction1 binary_op1,
207 BinaryFunction2 binary_op2,
208 __gnu_parallel::parallelism parallelism_tag)
210 typedef iterator_traits<InputIterator1> traits1_type;
211 typedef typename traits1_type::iterator_category iterator1_category;
213 typedef iterator_traits<InputIterator2> traits2_type;
214 typedef typename traits2_type::iterator_category iterator2_category;
216 return inner_product_switch(first1, last1, first2, init, binary_op1,
217 binary_op2, iterator1_category(),
218 iterator2_category(), parallelism_tag);
221 template<typename InputIterator1, typename InputIterator2, typename T, typename BinaryFunction1, typename BinaryFunction2>
223 inner_product(InputIterator1 first1, InputIterator1 last1,
224 InputIterator2 first2, T init, BinaryFunction1 binary_op1,
225 BinaryFunction2 binary_op2)
227 typedef iterator_traits<InputIterator1> traits1_type;
228 typedef typename traits1_type::iterator_category iterator1_category;
230 typedef iterator_traits<InputIterator2> traits2_type;
231 typedef typename traits2_type::iterator_category iterator2_category;
233 return inner_product_switch(first1, last1, first2, init, binary_op1,
234 binary_op2, iterator1_category(),
235 iterator2_category());
238 template<typename InputIterator1, typename InputIterator2, typename T>
240 inner_product(InputIterator1 first1, InputIterator1 last1,
241 InputIterator2 first2, T init,
242 __gnu_parallel::parallelism parallelism_tag)
244 typedef iterator_traits<InputIterator1> traits_type1;
245 typedef typename traits_type1::value_type value_type1;
246 typedef iterator_traits<InputIterator2> traits_type2;
247 typedef typename traits_type2::value_type value_type2;
249 typedef typename __gnu_parallel::multiplies<value_type1, value_type2>::result
250 multiplies_result_type;
251 return inner_product(first1, last1, first2, init,
252 __gnu_parallel::plus<T, multiplies_result_type>(),
253 __gnu_parallel::multiplies<value_type1, value_type2>(),
257 template<typename InputIterator1, typename InputIterator2, typename T>
259 inner_product(InputIterator1 first1, InputIterator1 last1,
260 InputIterator2 first2, T init)
262 typedef iterator_traits<InputIterator1> traits_type1;
263 typedef typename traits_type1::value_type value_type1;
264 typedef iterator_traits<InputIterator2> traits_type2;
265 typedef typename traits_type2::value_type value_type2;
267 typedef typename __gnu_parallel::multiplies<value_type1, value_type2>::result
268 multiplies_result_type;
269 return inner_product(first1, last1, first2, init,
270 __gnu_parallel::plus<T, multiplies_result_type>(),
271 __gnu_parallel::multiplies<value_type1, value_type2>());
274 // Sequential fallback.
275 template<typename InputIterator, typename OutputIterator>
276 inline OutputIterator
277 partial_sum(InputIterator begin, InputIterator end, OutputIterator result,
278 __gnu_parallel::sequential_tag)
279 { return _GLIBCXX_STD_P::partial_sum(begin, end, result); }
281 // Sequential fallback.
282 template<typename InputIterator, typename OutputIterator, typename BinaryOperation>
283 inline OutputIterator
284 partial_sum(InputIterator begin, InputIterator end, OutputIterator result,
285 BinaryOperation bin_op, __gnu_parallel::sequential_tag)
286 { return _GLIBCXX_STD_P::partial_sum(begin, end, result, bin_op); }
288 // Sequential fallback for input iterator case.
289 template<typename InputIterator, typename OutputIterator, typename BinaryOperation, typename IteratorTag1, typename IteratorTag2>
290 inline OutputIterator
291 partial_sum_switch(InputIterator begin, InputIterator end, OutputIterator result, BinaryOperation bin_op, IteratorTag1, IteratorTag2)
293 return _GLIBCXX_STD_P::partial_sum(begin, end, result, bin_op);
296 // Parallel algorithm for random access iterators.
297 template<typename InputIterator, typename OutputIterator, typename BinaryOperation>
299 partial_sum_switch(InputIterator begin, InputIterator end,
300 OutputIterator result, BinaryOperation bin_op,
301 random_access_iterator_tag, random_access_iterator_tag)
303 if (_GLIBCXX_PARALLEL_CONDITION(static_cast<__gnu_parallel::sequence_index_t>(end - begin) >= __gnu_parallel::Settings::partial_sum_minimal_n))
304 return __gnu_parallel::parallel_partial_sum(begin, end, result, bin_op);
306 return partial_sum(begin, end, result, bin_op, __gnu_parallel::sequential_tag());
310 template<typename InputIterator, typename OutputIterator>
311 inline OutputIterator
312 partial_sum(InputIterator begin, InputIterator end, OutputIterator result)
314 typedef typename iterator_traits<InputIterator>::value_type value_type;
315 return partial_sum(begin, end, result, std::plus<value_type>());
319 template<typename InputIterator, typename OutputIterator, typename BinaryOperation>
320 inline OutputIterator
321 partial_sum(InputIterator begin, InputIterator end, OutputIterator result,
322 BinaryOperation binary_op)
324 typedef iterator_traits<InputIterator> traitsi_type;
325 typedef typename traitsi_type::iterator_category iteratori_category;
327 typedef iterator_traits<OutputIterator> traitso_type;
328 typedef typename traitso_type::iterator_category iteratoro_category;
330 return partial_sum_switch(begin, end, result, binary_op,
331 iteratori_category(), iteratoro_category());
334 // Sequential fallback.
335 template<typename InputIterator, typename OutputIterator>
336 inline OutputIterator
337 adjacent_difference(InputIterator begin, InputIterator end,
338 OutputIterator result, __gnu_parallel::sequential_tag)
339 { return _GLIBCXX_STD_P::adjacent_difference(begin, end, result); }
341 // Sequential fallback.
342 template<typename InputIterator, typename OutputIterator, typename BinaryOperation>
343 inline OutputIterator
344 adjacent_difference(InputIterator begin, InputIterator end,
345 OutputIterator result, BinaryOperation bin_op,
346 __gnu_parallel::sequential_tag)
348 return _GLIBCXX_STD_P::adjacent_difference(begin, end, result, bin_op);
351 // Sequential fallback for input iterator case.
352 template<typename InputIterator, typename OutputIterator, typename BinaryOperation, typename IteratorTag1, typename IteratorTag2>
353 inline OutputIterator
354 adjacent_difference_switch(InputIterator begin, InputIterator end,
355 OutputIterator result, BinaryOperation bin_op,
356 IteratorTag1, IteratorTag2)
358 return adjacent_difference(begin, end, result, bin_op,
359 __gnu_parallel::sequential_tag());
362 // Parallel algorithm for random access iterators.
363 template<typename InputIterator, typename OutputIterator, typename BinaryOperation>
365 adjacent_difference_switch(InputIterator begin, InputIterator end,
366 OutputIterator result, BinaryOperation bin_op,
367 random_access_iterator_tag,
368 random_access_iterator_tag,
369 __gnu_parallel::parallelism parallelism_tag
370 = __gnu_parallel::parallel_balanced)
372 if (_GLIBCXX_PARALLEL_CONDITION(static_cast<__gnu_parallel::sequence_index_t>(end - begin) >= __gnu_parallel::Settings::adjacent_difference_minimal_n && __gnu_parallel::is_parallel(parallelism_tag)))
375 typedef __gnu_parallel::iterator_pair<InputIterator, OutputIterator, random_access_iterator_tag> ip;
377 ip begin_pair(begin + 1, result + 1), end_pair(end, result + (end - begin));
378 __gnu_parallel::adjacent_difference_selector<ip> functionality;
379 __gnu_parallel::for_each_template_random_access(begin_pair, end_pair, bin_op, functionality, __gnu_parallel::dummy_reduct(), dummy, dummy, -1, parallelism_tag);
380 return functionality.finish_iterator;
383 return adjacent_difference(begin, end, result, bin_op,
384 __gnu_parallel::sequential_tag());
388 template<typename InputIterator, typename OutputIterator>
389 inline OutputIterator
390 adjacent_difference(InputIterator begin, InputIterator end,
391 OutputIterator result,
392 __gnu_parallel::parallelism parallelism_tag)
394 typedef iterator_traits<InputIterator> traits_type;
395 typedef typename traits_type::value_type value_type;
396 return adjacent_difference(begin, end, result, std::minus<value_type>(),
400 template<typename InputIterator, typename OutputIterator>
401 inline OutputIterator
402 adjacent_difference(InputIterator begin, InputIterator end,
403 OutputIterator result)
405 typedef iterator_traits<InputIterator> traits_type;
406 typedef typename traits_type::value_type value_type;
407 return adjacent_difference(begin, end, result, std::minus<value_type>());
410 template<typename InputIterator, typename OutputIterator, typename BinaryOperation>
411 inline OutputIterator
412 adjacent_difference(InputIterator begin, InputIterator end,
413 OutputIterator result, BinaryOperation binary_op,
414 __gnu_parallel::parallelism parallelism_tag)
416 typedef iterator_traits<InputIterator> traitsi_type;
417 typedef typename traitsi_type::iterator_category iteratori_category;
419 typedef iterator_traits<OutputIterator> traitso_type;
420 typedef typename traitso_type::iterator_category iteratoro_category;
422 return adjacent_difference_switch(begin, end, result, binary_op,
423 iteratori_category(),
424 iteratoro_category(), parallelism_tag);
427 template<typename InputIterator, typename OutputIterator, typename BinaryOperation>
428 inline OutputIterator
429 adjacent_difference(InputIterator begin, InputIterator end,
430 OutputIterator result, BinaryOperation binary_op)
432 typedef iterator_traits<InputIterator> traitsi_type;
433 typedef typename traitsi_type::iterator_category iteratori_category;
435 typedef iterator_traits<OutputIterator> traitso_type;
436 typedef typename traitso_type::iterator_category iteratoro_category;
438 return adjacent_difference_switch(begin, end, result, binary_op,
439 iteratori_category(),
440 iteratoro_category());
445 #endif /* _GLIBCXX_NUMERIC_H */