Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
serial/tbb/parallel_for.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2005-2019 Intel Corporation
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 
16 
17 
18 
19 */
20 
21 #ifndef __TBB_SERIAL_parallel_for_H
22 #define __TBB_SERIAL_parallel_for_H
23 
24 #include "tbb_annotate.h"
25 
26 #ifndef __TBB_NORMAL_EXECUTION
27 #include "tbb/blocked_range.h"
28 #include "tbb/partitioner.h"
29 #endif
30 
31 #if TBB_USE_EXCEPTIONS
32 #include <stdexcept>
33 #include <string> // required to construct std exception classes
34 #else
35 #include <cstdlib>
36 #include <iostream>
37 #endif
38 
39 namespace tbb {
40 namespace serial {
41 namespace interface9 {
42 
43 // parallel_for serial annotated implementation
44 
45 template< typename Range, typename Body, typename Partitioner >
47  Range my_range;
48  const Body my_body;
49  typename Partitioner::task_partition_type my_partition;
50  void execute();
51 
53  start_for( const Range& range, const Body& body, Partitioner& partitioner ) :
54  my_range( range ),
55  my_body( body ),
56  my_partition( partitioner )
57  {
58  }
59 
61 
62  start_for( start_for& parent_, typename Partitioner::split_type& split_obj ) :
63  my_range( parent_.my_range, split_obj ),
64  my_body( parent_.my_body ),
65  my_partition( parent_.my_partition, split_obj )
66  {
67  }
68 
69 public:
70  static void run( const Range& range, const Body& body, Partitioner& partitioner ) {
71  if( !range.empty() ) {
72  ANNOTATE_SITE_BEGIN( tbb_parallel_for );
73  {
74  start_for a( range, body, partitioner );
75  a.execute();
76  }
77  ANNOTATE_SITE_END( tbb_parallel_for );
78  }
79  }
80 };
81 
82 template< typename Range, typename Body, typename Partitioner >
84  if( !my_range.is_divisible() || !my_partition.is_divisible() ) {
85  ANNOTATE_TASK_BEGIN( tbb_parallel_for_range );
86  {
87  my_body( my_range );
88  }
89  ANNOTATE_TASK_END( tbb_parallel_for_range );
90  } else {
91  typename Partitioner::split_type split_obj;
92  start_for b( *this, split_obj );
93  this->execute(); // Execute the left interval first to keep the serial order.
94  b.execute(); // Execute the right interval then.
95  }
96 }
97 
99 
100 template<typename Range, typename Body>
101 void parallel_for( const Range& range, const Body& body ) {
103 }
104 
106 
107 template<typename Range, typename Body>
108 void parallel_for( const Range& range, const Body& body, const simple_partitioner& partitioner ) {
110 }
111 
113 
114 template<typename Range, typename Body>
115 void parallel_for( const Range& range, const Body& body, const auto_partitioner& partitioner ) {
117 }
118 
120 
121 template<typename Range, typename Body>
122 void parallel_for( const Range& range, const Body& body, const static_partitioner& partitioner ) {
124 }
125 
127 
128 template<typename Range, typename Body>
129 void parallel_for( const Range& range, const Body& body, affinity_partitioner& partitioner ) {
131 }
132 
134 template <typename Index, typename Function, typename Partitioner>
135 void parallel_for_impl(Index first, Index last, Index step, const Function& f, Partitioner& ) {
136  if (step <= 0 ) {
137 #if TBB_USE_EXCEPTIONS
138  throw std::invalid_argument( "nonpositive_step" );
139 #else
140  std::cerr << "nonpositive step in a call to parallel_for" << std::endl;
141  std::abort();
142 #endif
143  } else if (last > first) {
144  // Above "else" avoids "potential divide by zero" warning on some platforms
145  ANNOTATE_SITE_BEGIN( tbb_parallel_for );
146  for( Index i = first; i < last; i = i + step ) {
147  ANNOTATE_TASK_BEGIN( tbb_parallel_for_iteration );
148  { f( i ); }
149  ANNOTATE_TASK_END( tbb_parallel_for_iteration );
150  }
151  ANNOTATE_SITE_END( tbb_parallel_for );
152  }
153 }
154 
156 template <typename Index, typename Function>
157 void parallel_for(Index first, Index last, Index step, const Function& f) {
158  parallel_for_impl<Index,Function,const auto_partitioner>(first, last, step, f, auto_partitioner());
159 }
161 template <typename Index, typename Function>
162 void parallel_for(Index first, Index last, Index step, const Function& f, const simple_partitioner& p) {
163  parallel_for_impl<Index,Function,const simple_partitioner>(first, last, step, f, p);
164 }
166 template <typename Index, typename Function>
167 void parallel_for(Index first, Index last, Index step, const Function& f, const auto_partitioner& p) {
168  parallel_for_impl<Index,Function,const auto_partitioner>(first, last, step, f, p);
169 }
171 template <typename Index, typename Function>
172 void parallel_for(Index first, Index last, Index step, const Function& f, const static_partitioner& p) {
173  parallel_for_impl<Index,Function,const static_partitioner>(first, last, step, f, p);
174 }
176 template <typename Index, typename Function>
177 void parallel_for(Index first, Index last, Index step, const Function& f, affinity_partitioner& p) {
178  parallel_for_impl(first, last, step, f, p);
179 }
180 
182 template <typename Index, typename Function>
183 void parallel_for(Index first, Index last, const Function& f) {
184  parallel_for_impl<Index,Function,const auto_partitioner>(first, last, static_cast<Index>(1), f, auto_partitioner());
185 }
187 template <typename Index, typename Function>
188 void parallel_for(Index first, Index last, const Function& f, const simple_partitioner& p) {
189  parallel_for_impl<Index,Function,const simple_partitioner>(first, last, static_cast<Index>(1), f, p);
190 }
192 template <typename Index, typename Function>
193  void parallel_for(Index first, Index last, const Function& f, const auto_partitioner& p) {
194  parallel_for_impl<Index,Function,const auto_partitioner>(first, last, static_cast<Index>(1), f, p);
195 }
197 template <typename Index, typename Function>
198 void parallel_for(Index first, Index last, const Function& f, const static_partitioner& p) {
199  parallel_for_impl<Index,Function,const static_partitioner>(first, last, static_cast<Index>(1), f, p);
200 }
202 template <typename Index, typename Function>
203 void parallel_for(Index first, Index last, const Function& f, affinity_partitioner& p) {
204  parallel_for_impl(first, last, static_cast<Index>(1), f, p);
205 }
206 
207 } // namespace interfaceX
208 
210 
211 } // namespace serial
212 
213 #ifndef __TBB_NORMAL_EXECUTION
215 #endif
216 
217 } // namespace tbb
218 
219 #endif /* __TBB_SERIAL_parallel_for_H */
Partitioner::task_partition_type my_partition
An auto partitioner.
Definition: partitioner.h:614
auto first(Container &c) -> decltype(begin(c))
Base class for types that should not be copied or assigned.
Definition: tbb_stddef.h:335
void const char const char int ITT_FORMAT __itt_group_sync p
auto last(Container &c) -> decltype(begin(c))
start_for(const Range &range, const Body &body, Partitioner &partitioner)
Constructor for root task.
static void run(const Range &range, const Body &body, Partitioner &partitioner)
The graph class.
void parallel_for_impl(Index first, Index last, Index step, const Function &f, Partitioner &)
Implementation of parallel iteration over stepped range of integers with explicit step and partitione...
An affinity partitioner.
Definition: partitioner.h:652
start_for(start_for &parent_, typename Partitioner::split_type &split_obj)
Splitting constructor used to generate children.
#define __TBB_DEFAULT_PARTITIONER
Definition: tbb_config.h:597
void parallel_for(const Range &range, const Body &body)
Parallel iteration over range with default partitioner.
A static partitioner.
Definition: partitioner.h:633
A simple partitioner.
Definition: partitioner.h:587

Copyright © 2005-2019 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.