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 #ifndef __TBB_SERIAL_parallel_for_H
18 #define __TBB_SERIAL_parallel_for_H
19 
20 #include "tbb_annotate.h"
21 
22 #ifndef __TBB_NORMAL_EXECUTION
23 #include "tbb/blocked_range.h"
24 #include "tbb/partitioner.h"
25 #endif
26 
27 #if TBB_USE_EXCEPTIONS
28 #include <stdexcept>
29 #include <string> // required to construct std exception classes
30 #else
31 #include <cstdlib>
32 #include <iostream>
33 #endif
34 
35 namespace tbb {
36 namespace serial {
37 namespace interface9 {
38 
39 // parallel_for serial annotated implementation
40 
41 template< typename Range, typename Body, typename Partitioner >
43  Range my_range;
44  const Body my_body;
45  typename Partitioner::task_partition_type my_partition;
46  void execute();
47 
49  start_for( const Range& range, const Body& body, Partitioner& partitioner ) :
50  my_range( range ),
51  my_body( body ),
52  my_partition( partitioner )
53  {
54  }
55 
57 
58  start_for( start_for& parent_, typename Partitioner::split_type& split_obj ) :
59  my_range( parent_.my_range, split_obj ),
60  my_body( parent_.my_body ),
61  my_partition( parent_.my_partition, split_obj )
62  {
63  }
64 
65 public:
66  static void run( const Range& range, const Body& body, Partitioner& partitioner ) {
67  if( !range.empty() ) {
68  ANNOTATE_SITE_BEGIN( tbb_parallel_for );
69  {
70  start_for a( range, body, partitioner );
71  a.execute();
72  }
73  ANNOTATE_SITE_END( tbb_parallel_for );
74  }
75  }
76 };
77 
78 template< typename Range, typename Body, typename Partitioner >
80  if( !my_range.is_divisible() || !my_partition.is_divisible() ) {
81  ANNOTATE_TASK_BEGIN( tbb_parallel_for_range );
82  {
83  my_body( my_range );
84  }
85  ANNOTATE_TASK_END( tbb_parallel_for_range );
86  } else {
87  typename Partitioner::split_type split_obj;
88  start_for b( *this, split_obj );
89  this->execute(); // Execute the left interval first to keep the serial order.
90  b.execute(); // Execute the right interval then.
91  }
92 }
93 
95 
96 template<typename Range, typename Body>
97 void parallel_for( const Range& range, const Body& body ) {
99 }
100 
102 
103 template<typename Range, typename Body>
104 void parallel_for( const Range& range, const Body& body, const simple_partitioner& partitioner ) {
106 }
107 
109 
110 template<typename Range, typename Body>
111 void parallel_for( const Range& range, const Body& body, const auto_partitioner& partitioner ) {
113 }
114 
116 
117 template<typename Range, typename Body>
118 void parallel_for( const Range& range, const Body& body, const static_partitioner& partitioner ) {
120 }
121 
123 
124 template<typename Range, typename Body>
125 void parallel_for( const Range& range, const Body& body, affinity_partitioner& partitioner ) {
127 }
128 
130 template <typename Index, typename Function, typename Partitioner>
131 void parallel_for_impl(Index first, Index last, Index step, const Function& f, Partitioner& ) {
132  if (step <= 0 ) {
133 #if TBB_USE_EXCEPTIONS
134  throw std::invalid_argument( "nonpositive_step" );
135 #else
136  std::cerr << "nonpositive step in a call to parallel_for" << std::endl;
137  std::abort();
138 #endif
139  } else if (last > first) {
140  // Above "else" avoids "potential divide by zero" warning on some platforms
141  ANNOTATE_SITE_BEGIN( tbb_parallel_for );
142  for( Index i = first; i < last; i = i + step ) {
143  ANNOTATE_TASK_BEGIN( tbb_parallel_for_iteration );
144  { f( i ); }
145  ANNOTATE_TASK_END( tbb_parallel_for_iteration );
146  }
147  ANNOTATE_SITE_END( tbb_parallel_for );
148  }
149 }
150 
152 template <typename Index, typename Function>
153 void parallel_for(Index first, Index last, Index step, const Function& f) {
154  parallel_for_impl<Index,Function,const auto_partitioner>(first, last, step, f, auto_partitioner());
155 }
157 template <typename Index, typename Function>
158 void parallel_for(Index first, Index last, Index step, const Function& f, const simple_partitioner& p) {
159  parallel_for_impl<Index,Function,const simple_partitioner>(first, last, step, f, p);
160 }
162 template <typename Index, typename Function>
163 void parallel_for(Index first, Index last, Index step, const Function& f, const auto_partitioner& p) {
164  parallel_for_impl<Index,Function,const auto_partitioner>(first, last, step, f, p);
165 }
167 template <typename Index, typename Function>
168 void parallel_for(Index first, Index last, Index step, const Function& f, const static_partitioner& p) {
169  parallel_for_impl<Index,Function,const static_partitioner>(first, last, step, f, p);
170 }
172 template <typename Index, typename Function>
173 void parallel_for(Index first, Index last, Index step, const Function& f, affinity_partitioner& p) {
174  parallel_for_impl(first, last, step, f, p);
175 }
176 
178 template <typename Index, typename Function>
179 void parallel_for(Index first, Index last, const Function& f) {
180  parallel_for_impl<Index,Function,const auto_partitioner>(first, last, static_cast<Index>(1), f, auto_partitioner());
181 }
183 template <typename Index, typename Function>
184 void parallel_for(Index first, Index last, const Function& f, const simple_partitioner& p) {
185  parallel_for_impl<Index,Function,const simple_partitioner>(first, last, static_cast<Index>(1), f, p);
186 }
188 template <typename Index, typename Function>
189  void parallel_for(Index first, Index last, const Function& f, const auto_partitioner& p) {
190  parallel_for_impl<Index,Function,const auto_partitioner>(first, last, static_cast<Index>(1), f, p);
191 }
193 template <typename Index, typename Function>
194 void parallel_for(Index first, Index last, const Function& f, const static_partitioner& p) {
195  parallel_for_impl<Index,Function,const static_partitioner>(first, last, static_cast<Index>(1), f, p);
196 }
198 template <typename Index, typename Function>
199 void parallel_for(Index first, Index last, const Function& f, affinity_partitioner& p) {
200  parallel_for_impl(first, last, static_cast<Index>(1), f, p);
201 }
202 
203 } // namespace interfaceX
204 
206 
207 } // namespace serial
208 
209 #ifndef __TBB_NORMAL_EXECUTION
211 #endif
212 
213 } // namespace tbb
214 
215 #endif /* __TBB_SERIAL_parallel_for_H */
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...
static void run(const Range &range, const Body &body, Partitioner &partitioner)
An affinity partitioner.
Definition: partitioner.h:648
Base class for types that should not be copied or assigned.
Definition: tbb_stddef.h:331
The graph class.
void parallel_for(const Range &range, const Body &body)
Parallel iteration over range with default partitioner.
A static partitioner.
Definition: partitioner.h:629
A simple partitioner.
Definition: partitioner.h:583
#define __TBB_DEFAULT_PARTITIONER
Definition: tbb_config.h:586
auto first(Container &c) -> decltype(begin(c))
An auto partitioner.
Definition: partitioner.h:610
start_for(const Range &range, const Body &body, Partitioner &partitioner)
Constructor for root task.
auto last(Container &c) -> decltype(begin(c))
start_for(start_for &parent_, typename Partitioner::split_type &split_obj)
Splitting constructor used to generate children.
void const char const char int ITT_FORMAT __itt_group_sync p
Partitioner::task_partition_type my_partition

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.