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-2020 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 #include "../../tbb/internal/_deprecated_header_message_guard.h"
18 
19 #if !defined(__TBB_show_deprecation_message_parallel_for_H) && defined(__TBB_show_deprecated_header_message)
20 #define __TBB_show_deprecation_message_parallel_for_H
21 #pragma message("TBB Warning: serial/tbb/parallel_for.h is deprecated. For details, please see Deprecated Features appendix in the TBB reference manual.")
22 #endif
23 
24 #if defined(__TBB_show_deprecated_header_message)
25 #undef __TBB_show_deprecated_header_message
26 #endif
27 
28 #ifndef __TBB_SERIAL_parallel_for_H
29 #define __TBB_SERIAL_parallel_for_H
30 
31 #include "tbb_annotate.h"
32 
33 #ifndef __TBB_NORMAL_EXECUTION
34 #include "tbb/blocked_range.h"
35 #include "tbb/partitioner.h"
36 #endif
37 
38 #if TBB_USE_EXCEPTIONS
39 #include <stdexcept>
40 #include <string> // required to construct std exception classes
41 #else
42 #include <cstdlib>
43 #include <iostream>
44 #endif
45 
46 namespace tbb {
47 namespace serial {
48 namespace interface9 {
49 
50 // parallel_for serial annotated implementation
51 
52 template< typename Range, typename Body, typename Partitioner >
54  Range my_range;
55  const Body my_body;
56  typename Partitioner::task_partition_type my_partition;
57  void execute();
58 
60  start_for( const Range& range, const Body& body, Partitioner& partitioner ) :
61  my_range( range ),
62  my_body( body ),
63  my_partition( partitioner )
64  {
65  }
66 
68 
69  start_for( start_for& parent_, typename Partitioner::split_type& split_obj ) :
70  my_range( parent_.my_range, split_obj ),
71  my_body( parent_.my_body ),
72  my_partition( parent_.my_partition, split_obj )
73  {
74  }
75 
76 public:
77  static void run( const Range& range, const Body& body, Partitioner& partitioner ) {
78  if( !range.empty() ) {
79  ANNOTATE_SITE_BEGIN( tbb_parallel_for );
80  {
81  start_for a( range, body, partitioner );
82  a.execute();
83  }
84  ANNOTATE_SITE_END( tbb_parallel_for );
85  }
86  }
87 };
88 
89 template< typename Range, typename Body, typename Partitioner >
91  if( !my_range.is_divisible() || !my_partition.is_divisible() ) {
92  ANNOTATE_TASK_BEGIN( tbb_parallel_for_range );
93  {
94  my_body( my_range );
95  }
96  ANNOTATE_TASK_END( tbb_parallel_for_range );
97  } else {
98  typename Partitioner::split_type split_obj;
99  start_for b( *this, split_obj );
100  this->execute(); // Execute the left interval first to keep the serial order.
101  b.execute(); // Execute the right interval then.
102  }
103 }
104 
106 
107 template<typename Range, typename Body>
108 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for( const Range& range, const Body& body ) {
110 }
111 
113 
114 template<typename Range, typename Body>
115 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for( const Range& range, const Body& body, const simple_partitioner& partitioner ) {
117 }
118 
120 
121 template<typename Range, typename Body>
122 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for( const Range& range, const Body& body, const auto_partitioner& partitioner ) {
124 }
125 
127 
128 template<typename Range, typename Body>
129 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for( const Range& range, const Body& body, const static_partitioner& partitioner ) {
131 }
132 
134 
135 template<typename Range, typename Body>
136 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for( const Range& range, const Body& body, affinity_partitioner& partitioner ) {
138 }
139 
141 template <typename Index, typename Function, typename Partitioner>
142 void parallel_for_impl(Index first, Index last, Index step, const Function& f, Partitioner& ) {
143  if (step <= 0 ) {
144 #if TBB_USE_EXCEPTIONS
145  throw std::invalid_argument( "nonpositive_step" );
146 #else
147  std::cerr << "nonpositive step in a call to parallel_for" << std::endl;
148  std::abort();
149 #endif
150  } else if (last > first) {
151  // Above "else" avoids "potential divide by zero" warning on some platforms
152  ANNOTATE_SITE_BEGIN( tbb_parallel_for );
153  for( Index i = first; i < last; i = i + step ) {
154  ANNOTATE_TASK_BEGIN( tbb_parallel_for_iteration );
155  { f( i ); }
156  ANNOTATE_TASK_END( tbb_parallel_for_iteration );
157  }
158  ANNOTATE_SITE_END( tbb_parallel_for );
159  }
160 }
161 
163 template <typename Index, typename Function>
164 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for(Index first, Index last, Index step, const Function& f) {
165  parallel_for_impl<Index,Function,const auto_partitioner>(first, last, step, f, auto_partitioner());
166 }
168 template <typename Index, typename Function>
169 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for(Index first, Index last, Index step, const Function& f, const simple_partitioner& p) {
170  parallel_for_impl<Index,Function,const simple_partitioner>(first, last, step, f, p);
171 }
173 template <typename Index, typename Function>
174 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for(Index first, Index last, Index step, const Function& f, const auto_partitioner& p) {
175  parallel_for_impl<Index,Function,const auto_partitioner>(first, last, step, f, p);
176 }
178 template <typename Index, typename Function>
179 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for(Index first, Index last, Index step, const Function& f, const static_partitioner& p) {
180  parallel_for_impl<Index,Function,const static_partitioner>(first, last, step, f, p);
181 }
183 template <typename Index, typename Function>
184 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for(Index first, Index last, Index step, const Function& f, affinity_partitioner& p) {
185  parallel_for_impl(first, last, step, f, p);
186 }
187 
189 template <typename Index, typename Function>
190 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for(Index first, Index last, const Function& f) {
191  parallel_for_impl<Index,Function,const auto_partitioner>(first, last, static_cast<Index>(1), f, auto_partitioner());
192 }
194 template <typename Index, typename Function>
195 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for(Index first, Index last, const Function& f, const simple_partitioner& p) {
196  parallel_for_impl<Index,Function,const simple_partitioner>(first, last, static_cast<Index>(1), f, p);
197 }
199 template <typename Index, typename Function>
200 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for(Index first, Index last, const Function& f, const auto_partitioner& p) {
201  parallel_for_impl<Index,Function,const auto_partitioner>(first, last, static_cast<Index>(1), f, p);
202 }
204 template <typename Index, typename Function>
205 __TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for(Index first, Index last, const Function& f, const static_partitioner& p) {
206  parallel_for_impl<Index,Function,const static_partitioner>(first, last, static_cast<Index>(1), f, p);
207 }
209 template <typename Index, typename Function>
211  parallel_for_impl(first, last, static_cast<Index>(1), f, p);
212 }
213 
214 } // namespace interfaceX
215 
217 
218 } // namespace serial
219 
220 #ifndef __TBB_NORMAL_EXECUTION
222 #endif
223 
224 } // namespace tbb
225 
226 #endif /* __TBB_SERIAL_parallel_for_H */
__TBB_DEFAULT_PARTITIONER
#define __TBB_DEFAULT_PARTITIONER
Definition: tbb_config.h:596
tbb::serial::interface9::start_for::my_body
const Body my_body
Definition: serial/tbb/parallel_for.h:55
tbb::serial::interface9::parallel_for
__TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for(const Range &range, const Body &body)
Parallel iteration over range with default partitioner.
Definition: serial/tbb/parallel_for.h:108
tbb::affinity_partitioner
An affinity partitioner.
Definition: partitioner.h:651
tbb::serial::interface9::start_for
Definition: serial/tbb/parallel_for.h:53
tbb
The graph class.
Definition: serial/tbb/parallel_for.h:46
tbb::internal::first
Container::iterator first(Container &c)
Definition: _range_iterator.h:46
blocked_range.h
tbb::internal::last
Container::iterator last(Container &c)
Definition: _range_iterator.h:52
tbb::auto_partitioner
An auto partitioner.
Definition: partitioner.h:613
partitioner.h
tbb::serial::interface9::start_for::execute
void execute()
Definition: serial/tbb/parallel_for.h:90
tbb::serial::interface9::start_for::run
static void run(const Range &range, const Body &body, Partitioner &partitioner)
Definition: serial/tbb/parallel_for.h:77
tbb::serial::interface9::start_for::start_for
start_for(start_for &parent_, typename Partitioner::split_type &split_obj)
Splitting constructor used to generate children.
Definition: serial/tbb/parallel_for.h:69
tbb::serial::interface9::parallel_for_impl
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...
Definition: serial/tbb/parallel_for.h:142
tbb_annotate.h
tbb::static_partitioner
A static partitioner.
Definition: partitioner.h:632
tbb::internal::no_copy
Base class for types that should not be copied or assigned.
Definition: tbb_stddef.h:330
tbb::simple_partitioner
A simple partitioner.
Definition: partitioner.h:586
__TBB_DEPRECATED_IN_VERBOSE_MODE
#define __TBB_DEPRECATED_IN_VERBOSE_MODE
Definition: tbb_config.h:647
tbb::flow::serial
@ serial
Definition: flow_graph.h:105
p
void const char const char int ITT_FORMAT __itt_group_sync p
Definition: ittnotify_static.h:91
tbb::serial::interface9::start_for::my_partition
Partitioner::task_partition_type my_partition
Definition: serial/tbb/parallel_for.h:56
tbb::serial::interface9::start_for::my_range
Range my_range
Definition: serial/tbb/parallel_for.h:54
tbb::serial::interface9::start_for::start_for
start_for(const Range &range, const Body &body, Partitioner &partitioner)
Constructor for root task.
Definition: serial/tbb/parallel_for.h:60

Copyright © 2005-2020 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.