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
31 /** @file parallel/partial_sum.h
32 * @brief Parallel implementation of std::partial_sum(), i. e. prefix
34 * This file is a GNU parallel extension to the Standard C++ Library.
37 // Written by Johannes Singler.
39 #ifndef _GLIBCXX_PARALLEL_PARTIAL_SUM_H
40 #define _GLIBCXX_PARALLEL_PARTIAL_SUM_H 1
44 #include <bits/stl_algobase.h>
45 #include <parallel/parallel.h>
46 #include <parallel/numericfwd.h>
48 namespace __gnu_parallel
50 // Problem: there is no 0-element given.
52 /** @brief Base case prefix sum routine.
53 * @param begin Begin iterator of input sequence.
54 * @param end End iterator of input sequence.
55 * @param result Begin iterator of output sequence.
56 * @param bin_op Associative binary function.
57 * @param value Start value. Must be passed since the neutral
58 * element is unknown in general.
59 * @return End iterator of output sequence. */
60 template<typename InputIterator
, typename OutputIterator
, typename BinaryOperation
>
62 parallel_partial_sum_basecase(InputIterator begin
, InputIterator end
,
63 OutputIterator result
, BinaryOperation bin_op
,
64 typename
std::iterator_traits
<InputIterator
>::value_type value
)
71 value
= bin_op(value
, *begin
);
79 /** @brief Parallel partial sum implmenetation, two-phase approach,
81 * @param begin Begin iterator of input sequence.
82 * @param end End iterator of input sequence.
83 * @param result Begin iterator of output sequence.
84 * @param bin_op Associative binary function.
85 * @param n Length of sequence.
86 * @param num_threads Number of threads to use.
87 * @return End iterator of output sequence.
89 template<typename InputIterator
, typename OutputIterator
, typename BinaryOperation
>
91 parallel_partial_sum_linear(InputIterator begin
, InputIterator end
,
92 OutputIterator result
, BinaryOperation bin_op
,
93 typename
std::iterator_traits
<InputIterator
>::difference_type n
, int num_threads
)
95 typedef std::iterator_traits
<InputIterator
> traits_type
;
96 typedef typename
traits_type::value_type value_type
;
97 typedef typename
traits_type::difference_type difference_type
;
99 if (num_threads
> (n
- 1))
100 num_threads
= static_cast<thread_index_t
>(n
- 1);
104 return parallel_partial_sum_basecase(begin
+ 1, end
, result
+ 1, bin_op
, *begin
);
107 difference_type borders
[num_threads
+ 2];
109 if (Settings::partial_sum_dilatation
== 1.0f
)
110 equally_split(n
, num_threads
+ 1, borders
);
113 difference_type chunk_length
= (int)((double)n
/ ((double)num_threads
+ Settings::partial_sum_dilatation
)), borderstart
= n
- num_threads
* chunk_length
;
115 for (int i
= 1; i
< (num_threads
+ 1); i
++)
117 borders
[i
] = borderstart
;
118 borderstart
+= chunk_length
;
120 borders
[num_threads
+ 1] = n
;
123 value_type
* sums
= new value_type
[num_threads
];
124 OutputIterator target_end
;
126 #pragma omp parallel num_threads(num_threads)
128 int id
= omp_get_thread_num();
132 parallel_partial_sum_basecase(begin
+ 1, begin
+ borders
[1], result
+ 1, bin_op
, *begin
);
133 sums
[0] = *(result
+ borders
[1] - 1);
137 sums
[id
] = std::accumulate(begin
+ borders
[id
] + 1, begin
+ borders
[id
+ 1], *(begin
+ borders
[id
]), bin_op
, __gnu_parallel::sequential_tag());
143 parallel_partial_sum_basecase(sums
+ 1, sums
+ num_threads
, sums
+ 1, bin_op
, sums
[0]);
148 parallel_partial_sum_basecase(begin
+ borders
[id
+ 1], begin
+ borders
[id
+ 2], result
+ borders
[id
+ 1], bin_op
, sums
[id
]);
156 /** @brief Parallel partial sum front-end.
157 * @param begin Begin iterator of input sequence.
158 * @param end End iterator of input sequence.
159 * @param result Begin iterator of output sequence.
160 * @param bin_op Associative binary function.
161 * @return End iterator of output sequence. */
162 template<typename InputIterator
, typename OutputIterator
, typename BinaryOperation
>
164 parallel_partial_sum(InputIterator begin
, InputIterator end
,
165 OutputIterator result
, BinaryOperation bin_op
)
167 _GLIBCXX_CALL(begin
- end
);
169 typedef std::iterator_traits
<InputIterator
> traits_type
;
170 typedef typename
traits_type::value_type value_type
;
171 typedef typename
traits_type::difference_type difference_type
;
173 difference_type n
= end
- begin
;
175 int num_threads
= get_max_threads();
177 switch (Settings::partial_sum_algorithm
)
179 case Settings::LINEAR
:
180 // Need an initial offset.
181 return parallel_partial_sum_linear(begin
, end
, result
, bin_op
,
184 // Partial_sum algorithm not implemented.
185 _GLIBCXX_PARALLEL_ASSERT(0);