Install gcc-4.4.0-tdm-1-g++-2.tar.gz
[git/jnareb-git.git] / mingw / lib / gcc / mingw32 / 4.4.0 / include / c++ / parallel / par_loop.h
blobbe61d4ca223559631301911a2cff672c50f2eff3
1 // -*- C++ -*-
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.
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 // 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.
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/>.
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.
31 // Written by Felix Putze.
33 #ifndef _GLIBCXX_PARALLEL_PAR_LOOP_H
34 #define _GLIBCXX_PARALLEL_PAR_LOOP_H 1
36 #include <omp.h>
37 #include <parallel/settings.h>
38 #include <parallel/base.h>
39 #include <parallel/equally_split.h>
41 namespace __gnu_parallel
44 /** @brief Embarrassingly parallel algorithm for random access
45 * iterators, using hand-crafted parallelization by equal splitting
46 * the work.
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).
62 template<typename RandomAccessIterator,
63 typename Op,
64 typename Fu,
65 typename Red,
66 typename Result>
68 for_each_template_random_access_ed(RandomAccessIterator begin,
69 RandomAccessIterator end,
70 Op o, Fu& f, Red r, Result base,
71 Result& output,
72 typename std::iterator_traits
73 <RandomAccessIterator>::
74 difference_type bound)
76 typedef std::iterator_traits<RandomAccessIterator> traits_type;
77 typedef typename traits_type::difference_type difference_type;
78 const difference_type length = end - begin;
79 Result *thread_results;
80 bool* constructed;
82 thread_index_t num_threads =
83 __gnu_parallel::min<difference_type>(get_max_threads(), length);
85 # pragma omp parallel num_threads(num_threads)
87 # pragma omp single
89 num_threads = omp_get_num_threads();
90 thread_results = static_cast<Result*>(
91 ::operator new(num_threads * sizeof(Result)));
92 constructed = new bool[num_threads];
95 thread_index_t iam = omp_get_thread_num();
97 // Neutral element.
98 Result* reduct = static_cast<Result*>(::operator new(sizeof(Result)));
100 difference_type
101 start = equally_split_point(length, num_threads, iam),
102 stop = equally_split_point(length, num_threads, iam + 1);
104 if (start < stop)
106 new(reduct) Result(f(o, begin + start));
107 ++start;
108 constructed[iam] = true;
110 else
111 constructed[iam] = false;
113 for (; start < stop; ++start)
114 *reduct = r(*reduct, f(o, begin + start));
116 thread_results[iam] = *reduct;
117 } //parallel
119 for (thread_index_t i = 0; i < num_threads; ++i)
120 if (constructed[i])
121 output = r(output, thread_results[i]);
123 // Points to last element processed (needed as return value for
124 // some algorithms like transform).
125 f.finish_iterator = begin + length;
127 delete[] thread_results;
128 delete[] constructed;
130 return o;
133 } // end namespace
135 #endif /* _GLIBCXX_PARALLEL_PAR_LOOP_H */