BRE12
parallel_for_each.h
1 /*
2  Copyright 2005-2016 Intel Corporation. All Rights Reserved.
3 
4  This file is part of Threading Building Blocks. Threading Building Blocks is free software;
5  you can redistribute it and/or modify it under the terms of the GNU General Public License
6  version 2 as published by the Free Software Foundation. Threading Building Blocks is
7  distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
8  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9  See the GNU General Public License for more details. You should have received a copy of
10  the GNU General Public License along with Threading Building Blocks; if not, write to the
11  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
12 
13  As a special exception, you may use this file as part of a free software library without
14  restriction. Specifically, if other files instantiate templates or use macros or inline
15  functions from this file, or you compile this file and link it with other files to produce
16  an executable, this file does not by itself cause the resulting executable to be covered
17  by the GNU General Public License. This exception does not however invalidate any other
18  reasons why the executable file might be covered by the GNU General Public License.
19 */
20 
21 #ifndef __TBB_parallel_for_each_H
22 #define __TBB_parallel_for_each_H
23 
24 #include "parallel_do.h"
25 #include "parallel_for.h"
26 
27 namespace tbb {
28 
30 namespace internal {
31  // The class calls user function in operator()
32  template <typename Function, typename Iterator>
33  class parallel_for_each_body_do : internal::no_assign {
34  const Function &my_func;
35  public:
36  parallel_for_each_body_do(const Function &_func) : my_func(_func) {}
37 
38  void operator()(typename std::iterator_traits<Iterator>::reference value) const {
39  my_func(value);
40  }
41  };
42 
43  // The class calls user function in operator()
44  template <typename Function, typename Iterator>
45  class parallel_for_each_body_for : internal::no_assign {
46  const Function &my_func;
47  public:
48  parallel_for_each_body_for(const Function &_func) : my_func(_func) {}
49 
50  void operator()(tbb::blocked_range<Iterator> range) const {
51 #if __INTEL_COMPILER
52 #pragma ivdep
53 #endif
54  for(Iterator it = range.begin(), end = range.end(); it != end; ++it) {
55  my_func(*it);
56  }
57  }
58  };
59 
60  template<typename Iterator, typename Function, typename Generic>
61  struct parallel_for_each_impl {
62 #if __TBB_TASK_GROUP_CONTEXT
63  static void doit(Iterator first, Iterator last, const Function& f, task_group_context &context) {
64  internal::parallel_for_each_body_do<Function, Iterator> body(f);
65  tbb::parallel_do(first, last, body, context);
66  }
67 #endif
68  static void doit(Iterator first, Iterator last, const Function& f) {
69  internal::parallel_for_each_body_do<Function, Iterator> body(f);
70  tbb::parallel_do(first, last, body);
71  }
72  };
73  template<typename Iterator, typename Function>
74  struct parallel_for_each_impl<Iterator, Function, std::random_access_iterator_tag> {
75 #if __TBB_TASK_GROUP_CONTEXT
76  static void doit(Iterator first, Iterator last, const Function& f, task_group_context &context) {
77  internal::parallel_for_each_body_for<Function, Iterator> body(f);
78  tbb::parallel_for(tbb::blocked_range<Iterator>(first, last), body, context);
79  }
80 #endif
81  static void doit(Iterator first, Iterator last, const Function& f) {
82  internal::parallel_for_each_body_for<Function, Iterator> body(f);
84  }
85  };
86 } // namespace internal
88 
92 
94 #if __TBB_TASK_GROUP_CONTEXT
95 template<typename Iterator, typename Function>
96 void parallel_for_each(Iterator first, Iterator last, const Function& f, task_group_context &context) {
97  internal::parallel_for_each_impl<Iterator, Function, typename std::iterator_traits<Iterator>::iterator_category>::doit(first, last, f, context);
98 }
99 
101 
102 template<typename Range, typename Function>
103 void parallel_for_each(Range& rng, const Function& f, task_group_context& context) {
104  parallel_for_each(tbb::internal::first(rng), tbb::internal::last(rng), f, context);
105 }
106 
108 
109 template<typename Range, typename Function>
110 void parallel_for_each(const Range& rng, const Function& f, task_group_context& context) {
111  parallel_for_each(tbb::internal::first(rng), tbb::internal::last(rng), f, context);
112 }
113 #endif /* __TBB_TASK_GROUP_CONTEXT */
114 
116 template<typename Iterator, typename Function>
117 void parallel_for_each(Iterator first, Iterator last, const Function& f) {
118  internal::parallel_for_each_impl<Iterator, Function, typename std::iterator_traits<Iterator>::iterator_category>::doit(first, last, f);
119 }
120 
122 template<typename Range, typename Function>
123 void parallel_for_each(Range& rng, const Function& f) {
124  parallel_for_each(tbb::internal::first(rng), tbb::internal::last(rng), f);
125 }
126 
128 template<typename Range, typename Function>
129 void parallel_for_each(const Range& rng, const Function& f) {
130  parallel_for_each(tbb::internal::first(rng), tbb::internal::last(rng), f);
131 }
132 
134 
135 } // namespace
136 
137 #endif /* __TBB_parallel_for_each_H */
const_iterator begin() const
Beginning of range.
Definition: blocked_range.h:62
Definition: _tbb_windef.h:37
A range over which to iterate.
Definition: blocked_range.h:40
const_iterator end() const
One past last value in range.
Definition: blocked_range.h:65
void parallel_for(const Range &range, const Body &body)
Parallel iteration over range with default partitioner.
Definition: parallel_for.h:185
void parallel_for_each(Iterator first, Iterator last, const Function &f, task_group_context &context)
Calls function f for all items from [first, last) interval using user-supplied context.
Definition: parallel_for_each.h:96
Definition: _flow_graph_async_msg_impl.h:32
The namespace tbb contains all components of the library.
Definition: parallel_for.h:44
void parallel_do(Iterator first, Iterator last, const Body &body)
Parallel iteration over a range, with optional addition of more work.
Definition: parallel_do.h:455