summaryrefslogtreecommitdiff
path: root/libstdc++-v3/include/parallel/par_loop.h
diff options
context:
space:
mode:
authorJohannes Singler <singler@ira.uka.de>2007-11-22 10:13:08 +0000
committerJohannes Singler <singler@gcc.gnu.org>2007-11-22 10:13:08 +0000
commite683ee2a20b907e9b225dd83cd047331e87a7edf (patch)
treec5838738b1b4d3a360f76e4418890226a450834a /libstdc++-v3/include/parallel/par_loop.h
parent7861a5ce14376a46409d83eeebef942be4030fe4 (diff)
downloadgcc-e683ee2a20b907e9b225dd83cd047331e87a7edf.tar.gz
re PR libstdc++/33893 ([parallel mode] Algorithms rely on omp_set_dynamic(false))
2007-11-22 Johannes Singler <singler@ira.uka.de> PR libstdc++/33893 * include/parallel/multiway_merge.h: made omp_dynamic-safe * include/parallel/workstealing.h: made omp_dynamic-safe * include/parallel/base.h: infrastructure, cleanup * include/parallel/par_loop.h: made omp_dynamic-safe * include/parallel/features.h: activate loser tree variant * include/parallel/quicksort.h: made omp_dynamic-safe * include/parallel/compiletime_settings.h: settings overridable * include/parallel/equally_split.h: made omp_dynamic-safe * include/parallel/omp_loop_static.h: made omp_dynamic-safe * include/parallel/random_shuffle.h: made omp_dynamic-safe * include/parallel/balanced_quicksort.h: made omp_dynamic-safe * include/parallel/set_operations.h: made omp_dynamic-safe * include/parallel/unique_copy.h: made omp_dynamic-safe * include/parallel/multiway_mergesort.h: made omp_dynamic-safe * include/parallel/search.h: made omp_dynamic-safe * include/parallel/partition.h: made omp_dynamic-safe * include/parallel/partial_sum.h: made omp_dynamic-safe * include/parallel/find.h: made omp_dynamic-safe * include/parallel/omp_loop.h: made omp_dynamic-safe * include/parallel/losertree.h: avoid default constructor From-SVN: r130347
Diffstat (limited to 'libstdc++-v3/include/parallel/par_loop.h')
-rw-r--r--libstdc++-v3/include/parallel/par_loop.h101
1 files changed, 56 insertions, 45 deletions
diff --git a/libstdc++-v3/include/parallel/par_loop.h b/libstdc++-v3/include/parallel/par_loop.h
index 98604cf1da4..6954e745076 100644
--- a/libstdc++-v3/include/parallel/par_loop.h
+++ b/libstdc++-v3/include/parallel/par_loop.h
@@ -41,69 +41,80 @@
#include <omp.h>
#include <parallel/settings.h>
+#include <parallel/base.h>
namespace __gnu_parallel
{
- /** @brief Embarrassingly parallel algorithm for random access
- * iterators, using hand-crafted parallelization by equal splitting
- * the work.
- *
- * @param begin Begin iterator of element sequence.
- * @param end End iterator of element sequence.
- * @param o User-supplied functor (comparator, predicate, adding
- * functor, ...)
- * @param f Functor to "process" an element with op (depends on
- * desired functionality, e. g. for std::for_each(), ...).
- * @param r Functor to "add" a single result to the already
- * processed elements (depends on functionality).
- * @param base Base value for reduction.
- * @param output Pointer to position where final result is written to
- * @param bound Maximum number of elements processed (e. g. for
- * std::count_n()).
- * @return User-supplied functor (that may contain a part of the result).
- */
- template<typename RandomAccessIterator, typename Op, typename Fu, typename Red, typename Result>
+/** @brief Embarrassingly parallel algorithm for random access
+ * iterators, using hand-crafted parallelization by equal splitting
+ * the work.
+ *
+ * @param begin Begin iterator of element sequence.
+ * @param end End iterator of element sequence.
+ * @param o User-supplied functor (comparator, predicate, adding
+ * functor, ...)
+ * @param f Functor to "process" an element with op (depends on
+ * desired functionality, e. g. for std::for_each(), ...).
+ * @param r Functor to "add" a single result to the already
+ * processed elements (depends on functionality).
+ * @param base Base value for reduction.
+ * @param output Pointer to position where final result is written to
+ * @param bound Maximum number of elements processed (e. g. for
+ * std::count_n()).
+ * @return User-supplied functor (that may contain a part of the result).
+ */
+template<
+ typename RandomAccessIterator,
+ typename Op,
+ typename Fu,
+ typename Red,
+ typename Result>
Op
- for_each_template_random_access_ed(RandomAccessIterator begin,
- RandomAccessIterator end, Op o, Fu& f,
- Red r, Result base, Result& output,
- typename std::iterator_traits<RandomAccessIterator>::difference_type bound)
+ for_each_template_random_access_ed(
+ RandomAccessIterator begin,
+ RandomAccessIterator end,
+ Op o, Fu& f, Red r, Result base, Result& output,
+ typename std::iterator_traits<RandomAccessIterator>::
+ difference_type bound)
{
typedef std::iterator_traits<RandomAccessIterator> traits_type;
typedef typename traits_type::difference_type difference_type;
const difference_type length = end - begin;
- const difference_type settings_threads = static_cast<difference_type>(get_max_threads());
- const difference_type dmin = settings_threads < length ? settings_threads : length;
- const difference_type dmax = dmin > 1 ? dmin : 1;
+ Result *thread_results;
- thread_index_t num_threads = static_cast<thread_index_t>(dmax);
+ thread_index_t num_threads =
+ __gnu_parallel::min<difference_type>(get_max_threads(), length);
+# pragma omp parallel num_threads(num_threads)
+ {
+# pragma omp single
+ {
+ num_threads = omp_get_num_threads();
+ thread_results = new Result[num_threads];
+ }
- Result *thread_results = new Result[num_threads];
+ thread_index_t iam = omp_get_thread_num();
-#pragma omp parallel num_threads(num_threads)
- {
- // Neutral element.
- Result reduct = Result();
+ // Neutral element.
+ Result reduct = Result();
- thread_index_t p = num_threads;
- thread_index_t iam = omp_get_thread_num();
- difference_type start = iam * length / p;
- difference_type limit = (iam == p - 1) ? length : (iam + 1) * length / p;
+ difference_type
+ start = equally_split_point(length, num_threads, iam),
+ stop = equally_split_point(length, num_threads, iam + 1);
- if (start < limit)
- {
- reduct = f(o, begin + start);
- start++;
- }
+ if (start < stop)
+ {
+ reduct = f(o, begin + start);
+ ++start;
+ }
- for (; start < limit; start++)
- reduct = r(reduct, f(o, begin + start));
+ for (; start < stop; ++start)
+ reduct = r(reduct, f(o, begin + start));
- thread_results[iam] = reduct;
- }
+ thread_results[iam] = reduct;
+ } //parallel
for (thread_index_t i = 0; i < num_threads; i++)
output = r(output, thread_results[i]);