algobase.h: Replace tabs by spaces; correct line breaks.
[gcc.git] / libstdc++-v3 / include / parallel / par_loop.h
1 // -*- C++ -*-
2
3 // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
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
8 // Foundation; either version 3, or (at your option) any later
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
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/>.
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>
38 #include <parallel/base.h>
39 #include <parallel/equally_split.h>
40
41 namespace __gnu_parallel
42 {
43
44 /** @brief Embarrassingly parallel algorithm for random access
45 * iterators, using hand-crafted parallelization by equal splitting
46 * the work.
47 *
48 * @param __begin Begin iterator of element __sequence.
49 * @param __end End iterator of element __sequence.
50 * @param __o User-supplied functor (comparator, predicate, adding
51 * functor, ...)
52 * @param __f Functor to "process" an element with __op (depends on
53 * desired functionality, e. g. for std::for_each(), ...).
54 * @param __r Functor to "add" a single __result to the already
55 * processed __elements (depends on functionality).
56 * @param __base Base value for reduction.
57 * @param __output Pointer to position where final result is written to
58 * @param __bound Maximum number of elements processed (e. g. for
59 * std::count_n()).
60 * @return User-supplied functor (that may contain a part of the result).
61 */
62 template<typename _RAIter,
63 typename _Op,
64 typename _Fu,
65 typename _Red,
66 typename _Result>
67 _Op
68 for_each_template_random_access_ed(
69 _RAIter __begin, _RAIter __end, _Op __o, _Fu& __f, _Red __r,
70 _Result __base, _Result& __output,
71 typename std::iterator_traits<_RAIter>::difference_type __bound)
72 {
73 typedef std::iterator_traits<_RAIter> _TraitsType;
74 typedef typename _TraitsType::difference_type _DifferenceType;
75 const _DifferenceType __length = __end - __begin;
76 _Result *__thread_results;
77 bool* __constructed;
78
79 _ThreadIndex __num_threads =
80 __gnu_parallel::min<_DifferenceType>(__get_max_threads(), __length);
81
82 # pragma omp parallel num_threads(__num_threads)
83 {
84 # pragma omp single
85 {
86 __num_threads = omp_get_num_threads();
87 __thread_results =
88 static_cast<_Result*>(
89 ::operator new(__num_threads * sizeof(_Result)));
90 __constructed = new bool[__num_threads];
91 }
92
93 _ThreadIndex __iam = omp_get_thread_num();
94
95 // Neutral element.
96 _Result* __reduct =
97 static_cast<_Result*>(::operator new(sizeof(_Result)));
98
99 _DifferenceType
100 __start = equally_split_point(__length, __num_threads, __iam),
101 __stop = equally_split_point(__length, __num_threads, __iam + 1);
102
103 if (__start < __stop)
104 {
105 new(__reduct) _Result(__f(__o, __begin + __start));
106 ++__start;
107 __constructed[__iam] = true;
108 }
109 else
110 __constructed[__iam] = false;
111
112 for (; __start < __stop; ++__start)
113 *__reduct = __r(*__reduct, __f(__o, __begin + __start));
114
115 __thread_results[__iam] = *__reduct;
116 } //parallel
117
118 for (_ThreadIndex __i = 0; __i < __num_threads; ++__i)
119 if (__constructed[__i])
120 __output = __r(__output, __thread_results[__i]);
121
122 // Points to last element processed (needed as return value for
123 // some algorithms like transform).
124 __f._M_finish_iterator = __begin + __length;
125
126 delete[] __thread_results;
127 delete[] __constructed;
128
129 return __o;
130 }
131
132 } // end namespace
133
134 #endif /* _GLIBCXX_PARALLEL_PAR_LOOP_H */