]>
Commit | Line | Data |
---|---|---|
c2ba9709 JS |
1 | // -*- C++ -*- |
2 | ||
83ffe9cd | 3 | // Copyright (C) 2007-2023 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/par_loop.h | |
26 | * @brief Parallelization of embarrassingly parallel execution by | |
27 | * means of equal splitting. | |
28 | * This file is a GNU parallel extension to the Standard C++ Library. | |
29 | */ | |
30 | ||
31 | // Written by Felix Putze. | |
32 | ||
33 | #ifndef _GLIBCXX_PARALLEL_PAR_LOOP_H | |
34 | #define _GLIBCXX_PARALLEL_PAR_LOOP_H 1 | |
35 | ||
36 | #include <omp.h> | |
37 | #include <parallel/settings.h> | |
e683ee2a | 38 | #include <parallel/base.h> |
22ec53ec | 39 | #include <parallel/equally_split.h> |
c2ba9709 JS |
40 | |
41 | namespace __gnu_parallel | |
42 | { | |
338311e5 PC |
43 | /** @brief Embarrassingly parallel algorithm for random access |
44 | * iterators, using hand-crafted parallelization by equal splitting | |
45 | * the work. | |
46 | * | |
47 | * @param __begin Begin iterator of element sequence. | |
48 | * @param __end End iterator of element sequence. | |
49 | * @param __o User-supplied functor (comparator, predicate, adding | |
50 | * functor, ...) | |
51 | * @param __f Functor to "process" an element with __op (depends on | |
52 | * desired functionality, e. g. for std::for_each(), ...). | |
53 | * @param __r Functor to "add" a single __result to the already | |
54 | * processed elements (depends on functionality). | |
55 | * @param __base Base value for reduction. | |
56 | * @param __output Pointer to position where final result is written to | |
57 | * @param __bound Maximum number of elements processed (e. g. for | |
58 | * std::count_n()). | |
59 | * @return User-supplied functor (that may contain a part of the result). | |
60 | */ | |
61 | template<typename _RAIter, | |
62 | typename _Op, | |
63 | typename _Fu, | |
64 | typename _Red, | |
65 | typename _Result> | |
66 | _Op | |
67 | __for_each_template_random_access_ed(_RAIter __begin, _RAIter __end, | |
68 | _Op __o, _Fu& __f, _Red __r, | |
69 | _Result __base, _Result& __output, | |
70 | typename std::iterator_traits<_RAIter>::difference_type __bound) | |
71 | { | |
72 | typedef std::iterator_traits<_RAIter> _TraitsType; | |
73 | typedef typename _TraitsType::difference_type _DifferenceType; | |
74 | const _DifferenceType __length = __end - __begin; | |
75 | _Result *__thread_results; | |
76 | bool* __constructed; | |
77 | ||
77d16198 PC |
78 | _ThreadIndex __num_threads = __gnu_parallel::min<_DifferenceType> |
79 | (__get_max_threads(), __length); | |
338311e5 PC |
80 | |
81 | # pragma omp parallel num_threads(__num_threads) | |
e683ee2a JS |
82 | { |
83 | # pragma omp single | |
338311e5 PC |
84 | { |
85 | __num_threads = omp_get_num_threads(); | |
77d16198 PC |
86 | __thread_results = static_cast<_Result*> |
87 | (::operator new(__num_threads * sizeof(_Result))); | |
338311e5 PC |
88 | __constructed = new bool[__num_threads]; |
89 | } | |
90 | ||
91 | _ThreadIndex __iam = omp_get_thread_num(); | |
92 | ||
93 | // Neutral element. | |
0ecca7a6 | 94 | _Result* __reduct; |
338311e5 PC |
95 | |
96 | _DifferenceType | |
6b77089f PC |
97 | __start = __equally_split_point(__length, __num_threads, __iam), |
98 | __stop = __equally_split_point(__length, __num_threads, __iam + 1); | |
338311e5 PC |
99 | |
100 | if (__start < __stop) | |
101 | { | |
0ecca7a6 | 102 | __reduct = new _Result(__f(__o, __begin + __start)); |
338311e5 PC |
103 | ++__start; |
104 | __constructed[__iam] = true; | |
105 | } | |
106 | else | |
107 | __constructed[__iam] = false; | |
108 | ||
109 | for (; __start < __stop; ++__start) | |
110 | *__reduct = __r(*__reduct, __f(__o, __begin + __start)); | |
111 | ||
0ecca7a6 PC |
112 | if (__constructed[__iam]) |
113 | { | |
114 | ::new(&__thread_results[__iam]) _Result(*__reduct); | |
115 | delete __reduct; | |
116 | } | |
e683ee2a | 117 | } //parallel |
c2ba9709 | 118 | |
338311e5 PC |
119 | for (_ThreadIndex __i = 0; __i < __num_threads; ++__i) |
120 | if (__constructed[__i]) | |
0ecca7a6 PC |
121 | { |
122 | __output = __r(__output, __thread_results[__i]); | |
123 | __thread_results[__i].~_Result(); | |
124 | } | |
c2ba9709 | 125 | |
338311e5 PC |
126 | // Points to last element processed (needed as return value for |
127 | // some algorithms like transform). | |
128 | __f._M_finish_iterator = __begin + __length; | |
c2ba9709 | 129 | |
0ecca7a6 PC |
130 | ::operator delete(__thread_results); |
131 | ||
338311e5 | 132 | delete[] __constructed; |
22ec53ec | 133 | |
338311e5 PC |
134 | return __o; |
135 | } | |
c2ba9709 JS |
136 | |
137 | } // end namespace | |
138 | ||
cbcd1e45 | 139 | #endif /* _GLIBCXX_PARALLEL_PAR_LOOP_H */ |