Merged revisions 143552,143554,143557,143560,143562,143564-143567,143570-143573,14357...
[official-gcc.git] / libstdc++-v3 / include / parallel / par_loop.h
bloba028ecb6af6f25787d49266e7fe9587fc3e96261
1 // -*- C++ -*-
3 // Copyright (C) 2007, 2008 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 2, 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 // 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
29 // Public License.
31 /** @file parallel/par_loop.h
32 * @brief Parallelization of embarrassingly parallel execution by
33 * means of equal splitting.
34 * This file is a GNU parallel extension to the Standard C++ Library.
37 // Written by Felix Putze.
39 #ifndef _GLIBCXX_PARALLEL_PAR_LOOP_H
40 #define _GLIBCXX_PARALLEL_PAR_LOOP_H 1
42 #include <omp.h>
43 #include <parallel/settings.h>
44 #include <parallel/base.h>
45 #include <parallel/equally_split.h>
47 namespace __gnu_parallel
50 /** @brief Embarrassingly parallel algorithm for random access
51 * iterators, using hand-crafted parallelization by equal splitting
52 * the work.
54 * @param begin Begin iterator of element sequence.
55 * @param end End iterator of element sequence.
56 * @param o User-supplied functor (comparator, predicate, adding
57 * functor, ...)
58 * @param f Functor to "process" an element with op (depends on
59 * desired functionality, e. g. for std::for_each(), ...).
60 * @param r Functor to "add" a single result to the already
61 * processed elements (depends on functionality).
62 * @param base Base value for reduction.
63 * @param output Pointer to position where final result is written to
64 * @param bound Maximum number of elements processed (e. g. for
65 * std::count_n()).
66 * @return User-supplied functor (that may contain a part of the result).
68 template<typename RandomAccessIterator,
69 typename Op,
70 typename Fu,
71 typename Red,
72 typename Result>
74 for_each_template_random_access_ed(RandomAccessIterator begin,
75 RandomAccessIterator end,
76 Op o, Fu& f, Red r, Result base,
77 Result& output,
78 typename std::iterator_traits
79 <RandomAccessIterator>::
80 difference_type bound)
82 typedef std::iterator_traits<RandomAccessIterator> traits_type;
83 typedef typename traits_type::difference_type difference_type;
84 const difference_type length = end - begin;
85 Result *thread_results;
86 bool* constructed;
88 thread_index_t num_threads =
89 __gnu_parallel::min<difference_type>(get_max_threads(), length);
91 # pragma omp parallel num_threads(num_threads)
93 # pragma omp single
95 num_threads = omp_get_num_threads();
96 thread_results = static_cast<Result*>(
97 ::operator new(num_threads * sizeof(Result)));
98 constructed = new bool[num_threads];
101 thread_index_t iam = omp_get_thread_num();
103 // Neutral element.
104 Result* reduct = static_cast<Result*>(::operator new(sizeof(Result)));
106 difference_type
107 start = equally_split_point(length, num_threads, iam),
108 stop = equally_split_point(length, num_threads, iam + 1);
110 if (start < stop)
112 new(reduct) Result(f(o, begin + start));
113 ++start;
114 constructed[iam] = true;
116 else
117 constructed[iam] = false;
119 for (; start < stop; ++start)
120 *reduct = r(*reduct, f(o, begin + start));
122 thread_results[iam] = *reduct;
123 } //parallel
125 for (thread_index_t i = 0; i < num_threads; ++i)
126 if (constructed[i])
127 output = r(output, thread_results[i]);
129 // Points to last element processed (needed as return value for
130 // some algorithms like transform).
131 f.finish_iterator = begin + length;
133 delete[] thread_results;
134 delete[] constructed;
136 return o;
139 } // end namespace
141 #endif /* _GLIBCXX_PARALLEL_PAR_LOOP_H */