00001 // -*- C++ -*- 00002 00003 // Copyright (C) 2007, 2008, 2009 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file parallel/omp_loop_static.h 00026 * @brief Parallelization of embarrassingly parallel execution by 00027 * means of an OpenMP for loop with static scheduling. 00028 * This file is a GNU parallel extension to the Standard C++ Library. 00029 */ 00030 00031 // Written by Felix Putze. 00032 00033 #ifndef _GLIBCXX_PARALLEL_OMP_LOOP_STATIC_H 00034 #define _GLIBCXX_PARALLEL_OMP_LOOP_STATIC_H 1 00035 00036 #include <omp.h> 00037 00038 #include <parallel/settings.h> 00039 #include <parallel/basic_iterator.h> 00040 00041 namespace __gnu_parallel 00042 { 00043 00044 /** @brief Embarrassingly parallel algorithm for random access 00045 * iterators, using an OpenMP for loop with static scheduling. 00046 * 00047 * @param begin Begin iterator of element sequence. 00048 * @param end End iterator of element sequence. 00049 * @param o User-supplied functor (comparator, predicate, adding 00050 * functor, ...). 00051 * @param f Functor to "process" an element with op (depends on 00052 * desired functionality, e. g. for std::for_each(), ...). 00053 * @param r Functor to "add" a single result to the already processed 00054 * elements (depends on functionality). 00055 * @param base Base value for reduction. 00056 * @param output Pointer to position where final result is written to 00057 * @param bound Maximum number of elements processed (e. g. for 00058 * std::count_n()). 00059 * @return User-supplied functor (that may contain a part of the result). 00060 */ 00061 template<typename RandomAccessIterator, 00062 typename Op, 00063 typename Fu, 00064 typename Red, 00065 typename Result> 00066 Op 00067 for_each_template_random_access_omp_loop_static(RandomAccessIterator begin, 00068 RandomAccessIterator end, 00069 Op o, Fu& f, Red r, 00070 Result base, Result& output, 00071 typename std::iterator_traits 00072 <RandomAccessIterator>:: 00073 difference_type bound) 00074 { 00075 typedef typename 00076 std::iterator_traits<RandomAccessIterator>::difference_type 00077 difference_type; 00078 00079 difference_type length = end - begin; 00080 thread_index_t num_threads = 00081 std::min<difference_type>(get_max_threads(), length); 00082 00083 Result *thread_results; 00084 00085 # pragma omp parallel num_threads(num_threads) 00086 { 00087 # pragma omp single 00088 { 00089 num_threads = omp_get_num_threads(); 00090 thread_results = new Result[num_threads]; 00091 00092 for (thread_index_t i = 0; i < num_threads; ++i) 00093 thread_results[i] = Result(); 00094 } 00095 00096 thread_index_t iam = omp_get_thread_num(); 00097 00098 # pragma omp for schedule(static, _Settings::get().workstealing_chunk_size) 00099 for (difference_type pos = 0; pos < length; ++pos) 00100 thread_results[iam] = r(thread_results[iam], f(o, begin+pos)); 00101 } //parallel 00102 00103 for (thread_index_t i = 0; i < num_threads; ++i) 00104 output = r(output, thread_results[i]); 00105 00106 delete [] thread_results; 00107 00108 // Points to last element processed (needed as return value for 00109 // some algorithms like transform). 00110 f.finish_iterator = begin + length; 00111 00112 return o; 00113 } 00114 00115 } // end namespace 00116 00117 #endif /* _GLIBCXX_PARALLEL_OMP_LOOP_STATIC_H */