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
46namespace tbb {
47namespace serial {
48namespace interface9 {
49
50// parallel_for serial annotated implementation
51
52template< 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
76public:
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
89template< 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
107template<typename Range, typename Body>
108__TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for( const Range& range, const Body& body ) {
110}
111
113
114template<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
121template<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
128template<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
135template<typename Range, typename Body>
136__TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for( const Range& range, const Body& body, affinity_partitioner& partitioner ) {
138}
139
141template <typename Index, typename Function, typename Partitioner>
142void 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
163template <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}
168template <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}
173template <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}
178template <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}
183template <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
189template <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}
194template <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}
199template <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}
204template <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}
209template <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 */
#define __TBB_DEFAULT_PARTITIONER
Definition: tbb_config.h:596
#define __TBB_DEPRECATED_IN_VERBOSE_MODE
Definition: tbb_config.h:647
void const char const char int ITT_FORMAT __itt_group_sync p
__TBB_DEPRECATED_IN_VERBOSE_MODE void parallel_for(const Range &range, const Body &body)
Parallel iteration over range with default 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...
auto last(Container &c) -> decltype(begin(c))
auto first(Container &c) -> decltype(begin(c))
static void run(const Range &range, const Body &body, Partitioner &partitioner)
start_for(start_for &parent_, typename Partitioner::split_type &split_obj)
Splitting constructor used to generate children.
Partitioner::task_partition_type my_partition
start_for(const Range &range, const Body &body, Partitioner &partitioner)
Constructor for root task.
A simple partitioner.
Definition: partitioner.h:586
An auto partitioner.
Definition: partitioner.h:613
A static partitioner.
Definition: partitioner.h:632
An affinity partitioner.
Definition: partitioner.h:651
Base class for types that should not be copied or assigned.
Definition: tbb_stddef.h:330

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.