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/omp_loop_static.h 00032 * @brief Parallelization of embarrassingly parallel execution by 00033 * means of an OpenMP for loop with static scheduling. 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_OMP_LOOP_STATIC_H 00040 #define _GLIBCXX_PARALLEL_OMP_LOOP_STATIC_H 1 00041 00042 #include <omp.h> 00043 00044 #include <parallel/settings.h> 00045 #include <parallel/basic_iterator.h> 00046 00047 namespace __gnu_parallel 00048 { 00049 00050 /** @brief Embarrassingly parallel algorithm for random access 00051 * iterators, using an OpenMP for loop with static scheduling. 00052 * 00053 * @param begin Begin iterator of element sequence. 00054 * @param end End iterator of element sequence. 00055 * @param o User-supplied functor (comparator, predicate, adding 00056 * functor, ...). 00057 * @param f Functor to "process" an element with op (depends on 00058 * desired functionality, e. g. for std::for_each(), ...). 00059 * @param r Functor to "add" a single result to the already processed 00060 * elements (depends on functionality). 00061 * @param base Base value for reduction. 00062 * @param output Pointer to position where final result is written to 00063 * @param bound Maximum number of elements processed (e. g. for 00064 * std::count_n()). 00065 * @return User-supplied functor (that may contain a part of the result). 00066 */ 00067 template<typename RandomAccessIterator, 00068 typename Op, 00069 typename Fu, 00070 typename Red, 00071 typename Result> 00072 Op 00073 for_each_template_random_access_omp_loop_static(RandomAccessIterator begin, 00074 RandomAccessIterator end, 00075 Op o, Fu& f, Red r, 00076 Result base, Result& output, 00077 typename std::iterator_traits 00078 <RandomAccessIterator>:: 00079 difference_type bound) 00080 { 00081 typedef typename 00082 std::iterator_traits<RandomAccessIterator>::difference_type 00083 difference_type; 00084 00085 difference_type length = end - begin; 00086 thread_index_t num_threads = 00087 std::min<difference_type>(get_max_threads(), length); 00088 00089 Result *thread_results; 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 = new Result[num_threads]; 00097 00098 for (thread_index_t i = 0; i < num_threads; ++i) 00099 thread_results[i] = Result(); 00100 } 00101 00102 thread_index_t iam = omp_get_thread_num(); 00103 00104 # pragma omp for schedule(static, _Settings::get().workstealing_chunk_size) 00105 for (difference_type pos = 0; pos < length; ++pos) 00106 thread_results[iam] = r(thread_results[iam], f(o, begin+pos)); 00107 } //parallel 00108 00109 for (thread_index_t i = 0; i < num_threads; ++i) 00110 output = r(output, thread_results[i]); 00111 00112 delete [] thread_results; 00113 00114 // Points to last element processed (needed as return value for 00115 // some algorithms like transform). 00116 f.finish_iterator = begin + length; 00117 00118 return o; 00119 } 00120 00121 } // end namespace 00122 00123 #endif