|
| 1 | +/* |
| 2 | + Copyright 2005-2014 Intel Corporation. All Rights Reserved. |
| 3 | +
|
| 4 | + This file is part of Threading Building Blocks. |
| 5 | +
|
| 6 | + Threading Building Blocks is free software; you can redistribute it |
| 7 | + and/or modify it under the terms of the GNU General Public License |
| 8 | + version 2 as published by the Free Software Foundation. |
| 9 | +
|
| 10 | + Threading Building Blocks is distributed in the hope that it will be |
| 11 | + useful, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 12 | + of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + GNU General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU General Public License |
| 16 | + along with Threading Building Blocks; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +
|
| 19 | + As a special exception, you may use this file as part of a free software |
| 20 | + library without restriction. Specifically, if other files instantiate |
| 21 | + templates or use macros or inline functions from this file, or you compile |
| 22 | + this file and link it with other files to produce an executable, this |
| 23 | + file does not by itself cause the resulting executable to be covered by |
| 24 | + the GNU General Public License. This exception does not however |
| 25 | + invalidate any other reasons why the executable file might be covered by |
| 26 | + the GNU General Public License. |
| 27 | +*/ |
| 28 | + |
| 29 | +#ifndef __TBB_SERIAL_parallel_for_H |
| 30 | +#define __TBB_SERIAL_parallel_for_H |
| 31 | + |
| 32 | +#if !TBB_USE_EXCEPTIONS && _MSC_VER |
| 33 | + // Suppress "C++ exception handler used, but unwind semantics are not enabled" warning in STL headers |
| 34 | + #pragma warning (push) |
| 35 | + #pragma warning (disable: 4530) |
| 36 | +#endif |
| 37 | + |
| 38 | +#include <stdexcept> |
| 39 | +#include <string> // required to construct std exception classes |
| 40 | + |
| 41 | +#if !TBB_USE_EXCEPTIONS && _MSC_VER |
| 42 | + #pragma warning (pop) |
| 43 | +#endif |
| 44 | + |
| 45 | +#include "tbb_annotate.h" |
| 46 | + |
| 47 | +#ifndef __TBB_NORMAL_EXECUTION |
| 48 | +#include "tbb/blocked_range.h" |
| 49 | +#include "tbb/partitioner.h" |
| 50 | +#endif |
| 51 | + |
| 52 | +namespace tbb { |
| 53 | +namespace serial { |
| 54 | +namespace interface7 { |
| 55 | + |
| 56 | +// parallel_for serial annotated implementation |
| 57 | + |
| 58 | +template< typename Range, typename Body, typename Partitioner > |
| 59 | +class start_for : tbb::internal::no_copy { |
| 60 | + Range my_range; |
| 61 | + const Body my_body; |
| 62 | + typename Partitioner::task_partition_type my_partition; |
| 63 | + void execute(); |
| 64 | + |
| 65 | + //! Constructor for root task. |
| 66 | + start_for( const Range& range, const Body& body, Partitioner& partitioner ) : |
| 67 | + my_range( range ), |
| 68 | + my_body( body ), |
| 69 | + my_partition( partitioner ) |
| 70 | + { |
| 71 | + } |
| 72 | + |
| 73 | + //! Splitting constructor used to generate children. |
| 74 | + /** this becomes left child. Newly constructed object is right child. */ |
| 75 | + start_for( start_for& parent_, typename Partitioner::split_type& split_obj ) : |
| 76 | + my_range( parent_.my_range, split_obj ), |
| 77 | + my_body( parent_.my_body ), |
| 78 | + my_partition( parent_.my_partition, split_obj ) |
| 79 | + { |
| 80 | + } |
| 81 | + |
| 82 | +public: |
| 83 | + static void run( const Range& range, const Body& body, Partitioner& partitioner ) { |
| 84 | + if( !range.empty() ) { |
| 85 | + ANNOTATE_SITE_BEGIN( tbb_parallel_for ); |
| 86 | + { |
| 87 | + start_for a( range, body, partitioner ); |
| 88 | + a.execute(); |
| 89 | + } |
| 90 | + ANNOTATE_SITE_END( tbb_parallel_for ); |
| 91 | + } |
| 92 | + } |
| 93 | +}; |
| 94 | + |
| 95 | +template< typename Range, typename Body, typename Partitioner > |
| 96 | +void start_for< Range, Body, Partitioner >::execute() { |
| 97 | + if( !my_range.is_divisible() || !my_partition.is_divisible() ) { |
| 98 | + ANNOTATE_TASK_BEGIN( tbb_parallel_for_range ); |
| 99 | + { |
| 100 | + my_body( my_range ); |
| 101 | + } |
| 102 | + ANNOTATE_TASK_END( tbb_parallel_for_range ); |
| 103 | + } else { |
| 104 | + typename Partitioner::split_type split_obj; |
| 105 | + start_for b( *this, split_obj ); |
| 106 | + this->execute(); // Execute the left interval first to keep the serial order. |
| 107 | + b.execute(); // Execute the right interval then. |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +//! Parallel iteration over range with default partitioner. |
| 112 | +/** @ingroup algorithms **/ |
| 113 | +template<typename Range, typename Body> |
| 114 | +void parallel_for( const Range& range, const Body& body ) { |
| 115 | + serial::interface7::start_for<Range,Body,const __TBB_DEFAULT_PARTITIONER>::run(range,body,__TBB_DEFAULT_PARTITIONER()); |
| 116 | +} |
| 117 | + |
| 118 | +//! Parallel iteration over range with simple partitioner. |
| 119 | +/** @ingroup algorithms **/ |
| 120 | +template<typename Range, typename Body> |
| 121 | +void parallel_for( const Range& range, const Body& body, const simple_partitioner& partitioner ) { |
| 122 | + serial::interface7::start_for<Range,Body,const simple_partitioner>::run(range,body,partitioner); |
| 123 | +} |
| 124 | + |
| 125 | +//! Parallel iteration over range with auto_partitioner. |
| 126 | +/** @ingroup algorithms **/ |
| 127 | +template<typename Range, typename Body> |
| 128 | +void parallel_for( const Range& range, const Body& body, const auto_partitioner& partitioner ) { |
| 129 | + serial::interface7::start_for<Range,Body,const auto_partitioner>::run(range,body,partitioner); |
| 130 | +} |
| 131 | + |
| 132 | +//! Parallel iteration over range with affinity_partitioner. |
| 133 | +/** @ingroup algorithms **/ |
| 134 | +template<typename Range, typename Body> |
| 135 | +void parallel_for( const Range& range, const Body& body, affinity_partitioner& partitioner ) { |
| 136 | + serial::interface7::start_for<Range,Body,affinity_partitioner>::run(range,body,partitioner); |
| 137 | +} |
| 138 | + |
| 139 | +//! Implementation of parallel iteration over stepped range of integers with explicit step and partitioner (ignored) |
| 140 | +template <typename Index, typename Function, typename Partitioner> |
| 141 | +void parallel_for_impl(Index first, Index last, Index step, const Function& f, Partitioner& ) { |
| 142 | + if (step <= 0 ) |
| 143 | + throw std::invalid_argument( "nonpositive_step" ); |
| 144 | + else if (last > first) { |
| 145 | + // Above "else" avoids "potential divide by zero" warning on some platforms |
| 146 | + ANNOTATE_SITE_BEGIN( tbb_parallel_for ); |
| 147 | + for( Index i = first; i < last; i = i + step ) { |
| 148 | + ANNOTATE_TASK_BEGIN( tbb_parallel_for_iteration ); |
| 149 | + { f( i ); } |
| 150 | + ANNOTATE_TASK_END( tbb_parallel_for_iteration ); |
| 151 | + } |
| 152 | + ANNOTATE_SITE_END( tbb_parallel_for ); |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +//! Parallel iteration over a range of integers with explicit step and default partitioner |
| 157 | +template <typename Index, typename Function> |
| 158 | +void parallel_for(Index first, Index last, Index step, const Function& f) { |
| 159 | + parallel_for_impl<Index,Function,const auto_partitioner>(first, last, step, f, auto_partitioner()); |
| 160 | +} |
| 161 | +//! Parallel iteration over a range of integers with explicit step and simple partitioner |
| 162 | +template <typename Index, typename Function> |
| 163 | +void parallel_for(Index first, Index last, Index step, const Function& f, const simple_partitioner& p) { |
| 164 | + parallel_for_impl<Index,Function,const simple_partitioner>(first, last, step, f, p); |
| 165 | +} |
| 166 | +//! Parallel iteration over a range of integers with explicit step and auto partitioner |
| 167 | +template <typename Index, typename Function> |
| 168 | +void parallel_for(Index first, Index last, Index step, const Function& f, const auto_partitioner& p) { |
| 169 | + parallel_for_impl<Index,Function,const auto_partitioner>(first, last, step, f, p); |
| 170 | +} |
| 171 | +//! Parallel iteration over a range of integers with explicit step and affinity partitioner |
| 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 | + |
| 177 | +//! Parallel iteration over a range of integers with default step and default partitioner |
| 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 | +} |
| 182 | +//! Parallel iteration over a range of integers with default step and simple partitioner |
| 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 | +} |
| 187 | +//! Parallel iteration over a range of integers with default step and auto partitioner |
| 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 | +} |
| 192 | +//! Parallel iteration over a range of integers with default step and affinity_partitioner |
| 193 | +template <typename Index, typename Function> |
| 194 | +void parallel_for(Index first, Index last, const Function& f, affinity_partitioner& p) { |
| 195 | + parallel_for_impl(first, last, static_cast<Index>(1), f, p); |
| 196 | +} |
| 197 | + |
| 198 | +} // namespace interface7 |
| 199 | + |
| 200 | +using interface7::parallel_for; |
| 201 | + |
| 202 | +} // namespace serial |
| 203 | + |
| 204 | +#ifndef __TBB_NORMAL_EXECUTION |
| 205 | +using serial::interface7::parallel_for; |
| 206 | +#endif |
| 207 | + |
| 208 | +} // namespace tbb |
| 209 | + |
| 210 | +#endif /* __TBB_SERIAL_parallel_for_H */ |
0 commit comments