1*38fd1498Szrj // -*- C++ -*- 2*38fd1498Szrj 3*38fd1498Szrj // Copyright (C) 2007-2018 Free Software Foundation, Inc. 4*38fd1498Szrj // 5*38fd1498Szrj // This file is part of the GNU ISO C++ Library. This library is free 6*38fd1498Szrj // software; you can redistribute it and/or modify it under the terms 7*38fd1498Szrj // of the GNU General Public License as published by the Free Software 8*38fd1498Szrj // Foundation; either version 3, or (at your option) any later 9*38fd1498Szrj // version. 10*38fd1498Szrj 11*38fd1498Szrj // This library is distributed in the hope that it will be useful, but 12*38fd1498Szrj // WITHOUT ANY WARRANTY; without even the implied warranty of 13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14*38fd1498Szrj // General Public License for more details. 15*38fd1498Szrj 16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional 17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version 18*38fd1498Szrj // 3.1, as published by the Free Software Foundation. 19*38fd1498Szrj 20*38fd1498Szrj // You should have received a copy of the GNU General Public License and 21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program; 22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23*38fd1498Szrj // <http://www.gnu.org/licenses/>. 24*38fd1498Szrj 25*38fd1498Szrj /** @file parallel/par_loop.h 26*38fd1498Szrj * @brief Parallelization of embarrassingly parallel execution by 27*38fd1498Szrj * means of equal splitting. 28*38fd1498Szrj * This file is a GNU parallel extension to the Standard C++ Library. 29*38fd1498Szrj */ 30*38fd1498Szrj 31*38fd1498Szrj // Written by Felix Putze. 32*38fd1498Szrj 33*38fd1498Szrj #ifndef _GLIBCXX_PARALLEL_PAR_LOOP_H 34*38fd1498Szrj #define _GLIBCXX_PARALLEL_PAR_LOOP_H 1 35*38fd1498Szrj 36*38fd1498Szrj #include <omp.h> 37*38fd1498Szrj #include <parallel/settings.h> 38*38fd1498Szrj #include <parallel/base.h> 39*38fd1498Szrj #include <parallel/equally_split.h> 40*38fd1498Szrj 41*38fd1498Szrj namespace __gnu_parallel 42*38fd1498Szrj { 43*38fd1498Szrj /** @brief Embarrassingly parallel algorithm for random access 44*38fd1498Szrj * iterators, using hand-crafted parallelization by equal splitting 45*38fd1498Szrj * the work. 46*38fd1498Szrj * 47*38fd1498Szrj * @param __begin Begin iterator of element sequence. 48*38fd1498Szrj * @param __end End iterator of element sequence. 49*38fd1498Szrj * @param __o User-supplied functor (comparator, predicate, adding 50*38fd1498Szrj * functor, ...) 51*38fd1498Szrj * @param __f Functor to "process" an element with __op (depends on 52*38fd1498Szrj * desired functionality, e. g. for std::for_each(), ...). 53*38fd1498Szrj * @param __r Functor to "add" a single __result to the already 54*38fd1498Szrj * processed elements (depends on functionality). 55*38fd1498Szrj * @param __base Base value for reduction. 56*38fd1498Szrj * @param __output Pointer to position where final result is written to 57*38fd1498Szrj * @param __bound Maximum number of elements processed (e. g. for 58*38fd1498Szrj * std::count_n()). 59*38fd1498Szrj * @return User-supplied functor (that may contain a part of the result). 60*38fd1498Szrj */ 61*38fd1498Szrj template<typename _RAIter, 62*38fd1498Szrj typename _Op, 63*38fd1498Szrj typename _Fu, 64*38fd1498Szrj typename _Red, 65*38fd1498Szrj typename _Result> 66*38fd1498Szrj _Op __for_each_template_random_access_ed(_RAIter __begin,_RAIter __end,_Op __o,_Fu & __f,_Red __r,_Result __base,_Result & __output,typename std::iterator_traits<_RAIter>::difference_type __bound)67*38fd1498Szrj __for_each_template_random_access_ed(_RAIter __begin, _RAIter __end, 68*38fd1498Szrj _Op __o, _Fu& __f, _Red __r, 69*38fd1498Szrj _Result __base, _Result& __output, 70*38fd1498Szrj typename std::iterator_traits<_RAIter>::difference_type __bound) 71*38fd1498Szrj { 72*38fd1498Szrj typedef std::iterator_traits<_RAIter> _TraitsType; 73*38fd1498Szrj typedef typename _TraitsType::difference_type _DifferenceType; 74*38fd1498Szrj const _DifferenceType __length = __end - __begin; 75*38fd1498Szrj _Result *__thread_results; 76*38fd1498Szrj bool* __constructed; 77*38fd1498Szrj 78*38fd1498Szrj _ThreadIndex __num_threads = __gnu_parallel::min<_DifferenceType> 79*38fd1498Szrj (__get_max_threads(), __length); 80*38fd1498Szrj 81*38fd1498Szrj # pragma omp parallel num_threads(__num_threads) 82*38fd1498Szrj { 83*38fd1498Szrj # pragma omp single 84*38fd1498Szrj { 85*38fd1498Szrj __num_threads = omp_get_num_threads(); 86*38fd1498Szrj __thread_results = static_cast<_Result*> 87*38fd1498Szrj (::operator new(__num_threads * sizeof(_Result))); 88*38fd1498Szrj __constructed = new bool[__num_threads]; 89*38fd1498Szrj } 90*38fd1498Szrj 91*38fd1498Szrj _ThreadIndex __iam = omp_get_thread_num(); 92*38fd1498Szrj 93*38fd1498Szrj // Neutral element. 94*38fd1498Szrj _Result* __reduct; 95*38fd1498Szrj 96*38fd1498Szrj _DifferenceType 97*38fd1498Szrj __start = __equally_split_point(__length, __num_threads, __iam), 98*38fd1498Szrj __stop = __equally_split_point(__length, __num_threads, __iam + 1); 99*38fd1498Szrj 100*38fd1498Szrj if (__start < __stop) 101*38fd1498Szrj { 102*38fd1498Szrj __reduct = new _Result(__f(__o, __begin + __start)); 103*38fd1498Szrj ++__start; 104*38fd1498Szrj __constructed[__iam] = true; 105*38fd1498Szrj } 106*38fd1498Szrj else 107*38fd1498Szrj __constructed[__iam] = false; 108*38fd1498Szrj 109*38fd1498Szrj for (; __start < __stop; ++__start) 110*38fd1498Szrj *__reduct = __r(*__reduct, __f(__o, __begin + __start)); 111*38fd1498Szrj 112*38fd1498Szrj if (__constructed[__iam]) 113*38fd1498Szrj { 114*38fd1498Szrj ::new(&__thread_results[__iam]) _Result(*__reduct); 115*38fd1498Szrj delete __reduct; 116*38fd1498Szrj } 117*38fd1498Szrj } //parallel 118*38fd1498Szrj 119*38fd1498Szrj for (_ThreadIndex __i = 0; __i < __num_threads; ++__i) 120*38fd1498Szrj if (__constructed[__i]) 121*38fd1498Szrj { 122*38fd1498Szrj __output = __r(__output, __thread_results[__i]); 123*38fd1498Szrj __thread_results[__i].~_Result(); 124*38fd1498Szrj } 125*38fd1498Szrj 126*38fd1498Szrj // Points to last element processed (needed as return value for 127*38fd1498Szrj // some algorithms like transform). 128*38fd1498Szrj __f._M_finish_iterator = __begin + __length; 129*38fd1498Szrj 130*38fd1498Szrj ::operator delete(__thread_results); 131*38fd1498Szrj 132*38fd1498Szrj delete[] __constructed; 133*38fd1498Szrj 134*38fd1498Szrj return __o; 135*38fd1498Szrj } 136*38fd1498Szrj 137*38fd1498Szrj } // end namespace 138*38fd1498Szrj 139*38fd1498Szrj #endif /* _GLIBCXX_PARALLEL_PAR_LOOP_H */ 140