par_loop.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2007, 2008 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the terms
00007 // of the GNU General Public License as published by the Free Software
00008 // Foundation; either version 2, or (at your option) any later
00009 // version.
00010 
00011 // This library is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // General Public License for more details.
00015 
00016 // You should have received a copy of the GNU General Public License
00017 // along with this library; see the file COPYING.  If not, write to
00018 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
00019 // MA 02111-1307, USA.
00020 
00021 // As a special exception, you may use this file as part of a free
00022 // software library without restriction.  Specifically, if other files
00023 // instantiate templates or use macros or inline functions from this
00024 // file, or you compile this file and link it with other files to
00025 // produce an executable, this file does not by itself cause the
00026 // resulting executable to be covered by the GNU General Public
00027 // License.  This exception does not however invalidate any other
00028 // reasons why the executable file might be covered by the GNU General
00029 // Public License.
00030 
00031 /** @file parallel/par_loop.h
00032  *  @brief Parallelization of embarrassingly parallel execution by
00033  *  means of equal splitting.
00034  *  This file is a GNU parallel extension to the Standard C++ Library.
00035  */
00036 
00037 // Written by Felix Putze.
00038 
00039 #ifndef _GLIBCXX_PARALLEL_PAR_LOOP_H
00040 #define _GLIBCXX_PARALLEL_PAR_LOOP_H 1
00041 
00042 #include <omp.h>
00043 #include <parallel/settings.h>
00044 #include <parallel/base.h>
00045 #include <parallel/equally_split.h>
00046 
00047 namespace __gnu_parallel
00048 {
00049 
00050 /** @brief Embarrassingly parallel algorithm for random access
00051   * iterators, using hand-crafted parallelization by equal splitting
00052   * the work.
00053   *
00054   *  @param begin Begin iterator of element sequence.
00055   *  @param end End iterator of element sequence.
00056   *  @param o User-supplied functor (comparator, predicate, adding
00057   *  functor, ...)
00058   *  @param f Functor to "process" an element with op (depends on
00059   *  desired functionality, e. g. for std::for_each(), ...).
00060   *  @param r Functor to "add" a single result to the already
00061   *  processed elements (depends on functionality).
00062   *  @param base Base value for reduction.
00063   *  @param output Pointer to position where final result is written to
00064   *  @param bound Maximum number of elements processed (e. g. for
00065   *  std::count_n()).
00066   *  @return User-supplied functor (that may contain a part of the result).
00067   */
00068 template<typename RandomAccessIterator,
00069      typename Op,
00070      typename Fu,
00071      typename Red,
00072      typename Result>
00073   Op
00074   for_each_template_random_access_ed(RandomAccessIterator begin,
00075                      RandomAccessIterator end,
00076                      Op o, Fu& f, Red r, Result base,
00077                      Result& output,
00078                      typename std::iterator_traits
00079                      <RandomAccessIterator>::
00080                      difference_type bound)
00081   {
00082     typedef std::iterator_traits<RandomAccessIterator> traits_type;
00083     typedef typename traits_type::difference_type difference_type;
00084     const difference_type length = end - begin;
00085     Result *thread_results;
00086     bool* constructed;
00087 
00088     thread_index_t num_threads =
00089       __gnu_parallel::min<difference_type>(get_max_threads(), length);
00090 
00091 #   pragma omp parallel num_threads(num_threads)
00092       {
00093 #       pragma omp single
00094           {
00095             num_threads = omp_get_num_threads();
00096             thread_results = static_cast<Result*>(
00097                                 ::operator new(num_threads * sizeof(Result)));
00098             constructed = new bool[num_threads];
00099           }
00100 
00101         thread_index_t iam = omp_get_thread_num();
00102 
00103         // Neutral element.
00104         Result* reduct = static_cast<Result*>(::operator new(sizeof(Result)));
00105 
00106         difference_type
00107             start = equally_split_point(length, num_threads, iam),
00108             stop = equally_split_point(length, num_threads, iam + 1);
00109 
00110         if (start < stop)
00111           {
00112             new(reduct) Result(f(o, begin + start));
00113             ++start;
00114             constructed[iam] = true;
00115           }
00116         else
00117           constructed[iam] = false;
00118 
00119         for (; start < stop; ++start)
00120           *reduct = r(*reduct, f(o, begin + start));
00121 
00122         thread_results[iam] = *reduct;
00123       } //parallel
00124 
00125     for (thread_index_t i = 0; i < num_threads; ++i)
00126         if (constructed[i])
00127             output = r(output, thread_results[i]);
00128 
00129     // Points to last element processed (needed as return value for
00130     // some algorithms like transform).
00131     f.finish_iterator = begin + length;
00132 
00133     delete[] thread_results;
00134     delete[] constructed;
00135 
00136     return o;
00137   }
00138 
00139 } // end namespace
00140 
00141 #endif

Generated on Sat Dec 12 09:40:13 2009 for libstdc++ by  doxygen 1.5.6