libstdc++
omp_loop_static.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 // Copyright (C) 2007-2014 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 3, or (at your option) any later
9 // version.
10 
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /** @file parallel/omp_loop_static.h
26  * @brief Parallelization of embarrassingly parallel execution by
27  * means of an OpenMP for loop with static scheduling.
28  * This file is a GNU parallel extension to the Standard C++ Library.
29  */
30 
31 // Written by Felix Putze.
32 
33 #ifndef _GLIBCXX_PARALLEL_OMP_LOOP_STATIC_H
34 #define _GLIBCXX_PARALLEL_OMP_LOOP_STATIC_H 1
35 
36 #include <omp.h>
37 
38 #include <parallel/settings.h>
40 
41 namespace __gnu_parallel
42 {
43  /** @brief Embarrassingly parallel algorithm for random access
44  * iterators, using an OpenMP for loop with static scheduling.
45  *
46  * @param __begin Begin iterator of element sequence.
47  * @param __end End iterator of element sequence.
48  * @param __o User-supplied functor (comparator, predicate, adding
49  * functor, ...).
50  * @param __f Functor to @a process an element with __op (depends on
51  * desired functionality, e. g. for std::for_each(), ...).
52  * @param __r Functor to @a add a single __result to the already processed
53  * __elements (depends on functionality).
54  * @param __base Base value for reduction.
55  * @param __output Pointer to position where final result is written to
56  * @param __bound Maximum number of elements processed (e. g. for
57  * std::count_n()).
58  * @return User-supplied functor (that may contain a part of the result).
59  */
60  template<typename _RAIter,
61  typename _Op,
62  typename _Fu,
63  typename _Red,
64  typename _Result>
65  _Op
67  _RAIter __end, _Op __o,
68  _Fu& __f, _Red __r,
69  _Result __base,
70  _Result& __output,
71  typename std::iterator_traits<_RAIter>::difference_type __bound)
72  {
73  typedef typename std::iterator_traits<_RAIter>::difference_type
74  _DifferenceType;
75 
76  _DifferenceType __length = __end - __begin;
77  _ThreadIndex __num_threads = std::min<_DifferenceType>
78  (__get_max_threads(), __length);
79 
80  _Result *__thread_results;
81 
82 # pragma omp parallel num_threads(__num_threads)
83  {
84 # pragma omp single
85  {
86  __num_threads = omp_get_num_threads();
87  __thread_results = new _Result[__num_threads];
88 
89  for (_ThreadIndex __i = 0; __i < __num_threads; ++__i)
90  __thread_results[__i] = _Result();
91  }
92 
93  _ThreadIndex __iam = omp_get_thread_num();
94 
95 #pragma omp for schedule(static, _Settings::get().workstealing_chunk_size)
96  for (_DifferenceType __pos = 0; __pos < __length; ++__pos)
97  __thread_results[__iam] = __r(__thread_results[__iam],
98  __f(__o, __begin+__pos));
99  } //parallel
100 
101  for (_ThreadIndex __i = 0; __i < __num_threads; ++__i)
102  __output = __r(__output, __thread_results[__i]);
103 
104  delete [] __thread_results;
105 
106  // Points to last element processed (needed as return value for
107  // some algorithms like transform).
108  __f.finish_iterator = __begin + __length;
109 
110  return __o;
111  }
112 
113 } // end namespace
114 
115 #endif /* _GLIBCXX_PARALLEL_OMP_LOOP_STATIC_H */
_Op __for_each_template_random_access_omp_loop_static(_RAIter __begin, _RAIter __end, _Op __o, _Fu &__f, _Red __r, _Result __base, _Result &__output, typename std::iterator_traits< _RAIter >::difference_type __bound)
Embarrassingly parallel algorithm for random access iterators, using an OpenMP for loop with static s...
Includes the original header files concerned with iterators except for stream iterators. This file is a GNU parallel extension to the Standard C++ Library.
uint16_t _ThreadIndex
Unsigned integer to index a thread number. The maximum thread number (for each processor) must fit in...
Definition: types.h:123
Runtime settings and tuning parameters, heuristics to decide whether to use parallelized algorithms...
_Siter_base< _Iterator >::iterator_type __base(_Iterator __it)
Definition: functions.h:558
GNU parallel code for public use.