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/omp_loop.h 26*38fd1498Szrj * @brief Parallelization of embarrassingly parallel execution by 27*38fd1498Szrj * means of an OpenMP for loop. 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_OMP_LOOP_H 34*38fd1498Szrj #define _GLIBCXX_PARALLEL_OMP_LOOP_H 1 35*38fd1498Szrj 36*38fd1498Szrj #include <omp.h> 37*38fd1498Szrj 38*38fd1498Szrj #include <parallel/settings.h> 39*38fd1498Szrj #include <parallel/basic_iterator.h> 40*38fd1498Szrj #include <parallel/base.h> 41*38fd1498Szrj 42*38fd1498Szrj namespace __gnu_parallel 43*38fd1498Szrj { 44*38fd1498Szrj /** @brief Embarrassingly parallel algorithm for random access 45*38fd1498Szrj * iterators, using an OpenMP for loop. 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, etc.). 51*38fd1498Szrj * @param __f Functor to @a process an element with __op (depends on 52*38fd1498Szrj * desired functionality, e. g. for std::for_each(), ...). 53*38fd1498Szrj * @param __r Functor to @a 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_omp_loop(_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_omp_loop(_RAIter __begin, _RAIter __end, 68*38fd1498Szrj _Op __o, _Fu& __f, _Red __r, 69*38fd1498Szrj _Result __base, 70*38fd1498Szrj _Result& __output, 71*38fd1498Szrj typename std::iterator_traits<_RAIter>::difference_type __bound) 72*38fd1498Szrj { 73*38fd1498Szrj typedef typename std::iterator_traits<_RAIter>::difference_type 74*38fd1498Szrj _DifferenceType; 75*38fd1498Szrj 76*38fd1498Szrj _DifferenceType __length = __end - __begin; 77*38fd1498Szrj _ThreadIndex __num_threads = __gnu_parallel::min<_DifferenceType> 78*38fd1498Szrj (__get_max_threads(), __length); 79*38fd1498Szrj 80*38fd1498Szrj _Result *__thread_results; 81*38fd1498Szrj 82*38fd1498Szrj # pragma omp parallel num_threads(__num_threads) 83*38fd1498Szrj { 84*38fd1498Szrj # pragma omp single 85*38fd1498Szrj { 86*38fd1498Szrj __num_threads = omp_get_num_threads(); 87*38fd1498Szrj __thread_results = new _Result[__num_threads]; 88*38fd1498Szrj 89*38fd1498Szrj for (_ThreadIndex __i = 0; __i < __num_threads; ++__i) 90*38fd1498Szrj __thread_results[__i] = _Result(); 91*38fd1498Szrj } 92*38fd1498Szrj 93*38fd1498Szrj _ThreadIndex __iam = omp_get_thread_num(); 94*38fd1498Szrj 95*38fd1498Szrj #pragma omp for schedule(dynamic, _Settings::get().workstealing_chunk_size) 96*38fd1498Szrj for (_DifferenceType __pos = 0; __pos < __length; ++__pos) 97*38fd1498Szrj __thread_results[__iam] = __r(__thread_results[__iam], 98*38fd1498Szrj __f(__o, __begin+__pos)); 99*38fd1498Szrj } //parallel 100*38fd1498Szrj 101*38fd1498Szrj for (_ThreadIndex __i = 0; __i < __num_threads; ++__i) 102*38fd1498Szrj __output = __r(__output, __thread_results[__i]); 103*38fd1498Szrj 104*38fd1498Szrj delete [] __thread_results; 105*38fd1498Szrj 106*38fd1498Szrj // Points to last element processed (needed as return value for 107*38fd1498Szrj // some algorithms like transform). 108*38fd1498Szrj __f._M_finish_iterator = __begin + __length; 109*38fd1498Szrj 110*38fd1498Szrj return __o; 111*38fd1498Szrj } 112*38fd1498Szrj 113*38fd1498Szrj } // end namespace 114*38fd1498Szrj 115*38fd1498Szrj #endif /* _GLIBCXX_PARALLEL_OMP_LOOP_H */ 116