parallel_for.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. Copyright 2005-2013 Intel Corporation. All Rights Reserved.
  3. This file is part of Threading Building Blocks.
  4. Threading Building Blocks is free software; you can redistribute it
  5. and/or modify it under the terms of the GNU General Public License
  6. version 2 as published by the Free Software Foundation.
  7. Threading Building Blocks is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  9. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Threading Building Blocks; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. As a special exception, you may use this file as part of a free software
  15. library without restriction. Specifically, if other files instantiate
  16. templates or use macros or inline functions from this file, or you compile
  17. this file and link it with other files to produce an executable, this
  18. file does not by itself cause the resulting executable to be covered by
  19. the GNU General Public License. This exception does not however
  20. invalidate any other reasons why the executable file might be covered by
  21. the GNU General Public License.
  22. */
  23. #ifndef __TBB_parallel_for_H
  24. #define __TBB_parallel_for_H
  25. #include <new>
  26. #include "task.h"
  27. #include "partitioner.h"
  28. #include "blocked_range.h"
  29. #include "tbb_exception.h"
  30. namespace tbb {
  31. namespace interface6 {
  32. //! @cond INTERNAL
  33. namespace internal {
  34. //! Task type used in parallel_for
  35. /** @ingroup algorithms */
  36. template<typename Range, typename Body, typename Partitioner>
  37. class start_for: public task {
  38. Range my_range;
  39. const Body my_body;
  40. typename Partitioner::task_partition_type my_partition;
  41. /*override*/ task* execute();
  42. public:
  43. //! Constructor for root task.
  44. start_for( const Range& range, const Body& body, Partitioner& partitioner ) :
  45. my_range(range),
  46. my_body(body),
  47. my_partition(partitioner)
  48. {
  49. }
  50. //! Splitting constructor used to generate children.
  51. /** parent_ becomes left child. Newly constructed object is right child. */
  52. start_for( start_for& parent_, split ) :
  53. my_range(parent_.my_range,split()),
  54. my_body(parent_.my_body),
  55. my_partition(parent_.my_partition, split())
  56. {
  57. my_partition.set_affinity(*this);
  58. }
  59. //! Construct right child from the given range as response to the demand.
  60. /** parent_ remains left child. Newly constructed object is right child. */
  61. start_for( start_for& parent_, const Range& r, depth_t d ) :
  62. my_range(r),
  63. my_body(parent_.my_body),
  64. my_partition(parent_.my_partition,split())
  65. {
  66. my_partition.set_affinity(*this);
  67. my_partition.align_depth( d );
  68. }
  69. //! Update affinity info, if any.
  70. /*override*/ void note_affinity( affinity_id id ) {
  71. my_partition.note_affinity( id );
  72. }
  73. static void run( const Range& range, const Body& body, Partitioner& partitioner ) {
  74. if( !range.empty() ) {
  75. #if !__TBB_TASK_GROUP_CONTEXT || TBB_JOIN_OUTER_TASK_GROUP
  76. start_for& a = *new(task::allocate_root()) start_for(range,body,partitioner);
  77. #else
  78. // Bound context prevents exceptions from body to affect nesting or sibling algorithms,
  79. // and allows users to handle exceptions safely by wrapping parallel_for in the try-block.
  80. task_group_context context;
  81. start_for& a = *new(task::allocate_root(context)) start_for(range,body,partitioner);
  82. #endif /* __TBB_TASK_GROUP_CONTEXT && !TBB_JOIN_OUTER_TASK_GROUP */
  83. task::spawn_root_and_wait(a);
  84. }
  85. }
  86. #if __TBB_TASK_GROUP_CONTEXT
  87. static void run( const Range& range, const Body& body, Partitioner& partitioner, task_group_context& context ) {
  88. if( !range.empty() ) {
  89. start_for& a = *new(task::allocate_root(context)) start_for(range,body,partitioner);
  90. task::spawn_root_and_wait(a);
  91. }
  92. }
  93. #endif /* __TBB_TASK_GROUP_CONTEXT */
  94. //! create a continuation task, serve as callback for partitioner
  95. flag_task *create_continuation() {
  96. return new( allocate_continuation() ) flag_task();
  97. }
  98. //! Run body for range
  99. void run_body( Range &r ) { my_body( r ); }
  100. };
  101. template<typename Range, typename Body, typename Partitioner>
  102. task* start_for<Range,Body,Partitioner>::execute() {
  103. my_partition.check_being_stolen( *this );
  104. my_partition.execute(*this, my_range);
  105. return NULL;
  106. }
  107. } // namespace internal
  108. //! @endcond
  109. } // namespace interfaceX
  110. //! @cond INTERNAL
  111. namespace internal {
  112. using interface6::internal::start_for;
  113. //! Calls the function with values from range [begin, end) with a step provided
  114. template<typename Function, typename Index>
  115. class parallel_for_body : internal::no_assign {
  116. const Function &my_func;
  117. const Index my_begin;
  118. const Index my_step;
  119. public:
  120. parallel_for_body( const Function& _func, Index& _begin, Index& _step)
  121. : my_func(_func), my_begin(_begin), my_step(_step) {}
  122. void operator()( tbb::blocked_range<Index>& r ) const {
  123. #if __INTEL_COMPILER
  124. #pragma ivdep
  125. #endif
  126. for( Index i = r.begin(), k = my_begin + i * my_step; i < r.end(); i++, k = k + my_step)
  127. my_func( k );
  128. }
  129. };
  130. } // namespace internal
  131. //! @endcond
  132. // Requirements on Range concept are documented in blocked_range.h
  133. /** \page parallel_for_body_req Requirements on parallel_for body
  134. Class \c Body implementing the concept of parallel_for body must define:
  135. - \code Body::Body( const Body& ); \endcode Copy constructor
  136. - \code Body::~Body(); \endcode Destructor
  137. - \code void Body::operator()( Range& r ) const; \endcode Function call operator applying the body to range \c r.
  138. **/
  139. /** \name parallel_for
  140. See also requirements on \ref range_req "Range" and \ref parallel_for_body_req "parallel_for Body". **/
  141. //@{
  142. //! Parallel iteration over range with default partitioner.
  143. /** @ingroup algorithms **/
  144. template<typename Range, typename Body>
  145. void parallel_for( const Range& range, const Body& body ) {
  146. internal::start_for<Range,Body,const __TBB_DEFAULT_PARTITIONER>::run(range,body,__TBB_DEFAULT_PARTITIONER());
  147. }
  148. //! Parallel iteration over range with simple partitioner.
  149. /** @ingroup algorithms **/
  150. template<typename Range, typename Body>
  151. void parallel_for( const Range& range, const Body& body, const simple_partitioner& partitioner ) {
  152. internal::start_for<Range,Body,const simple_partitioner>::run(range,body,partitioner);
  153. }
  154. //! Parallel iteration over range with auto_partitioner.
  155. /** @ingroup algorithms **/
  156. template<typename Range, typename Body>
  157. void parallel_for( const Range& range, const Body& body, const auto_partitioner& partitioner ) {
  158. internal::start_for<Range,Body,const auto_partitioner>::run(range,body,partitioner);
  159. }
  160. //! Parallel iteration over range with affinity_partitioner.
  161. /** @ingroup algorithms **/
  162. template<typename Range, typename Body>
  163. void parallel_for( const Range& range, const Body& body, affinity_partitioner& partitioner ) {
  164. internal::start_for<Range,Body,affinity_partitioner>::run(range,body,partitioner);
  165. }
  166. #if __TBB_TASK_GROUP_CONTEXT
  167. //! Parallel iteration over range with default partitioner and user-supplied context.
  168. /** @ingroup algorithms **/
  169. template<typename Range, typename Body>
  170. void parallel_for( const Range& range, const Body& body, task_group_context& context ) {
  171. internal::start_for<Range,Body,const __TBB_DEFAULT_PARTITIONER>::run(range, body, __TBB_DEFAULT_PARTITIONER(), context);
  172. }
  173. //! Parallel iteration over range with simple partitioner and user-supplied context.
  174. /** @ingroup algorithms **/
  175. template<typename Range, typename Body>
  176. void parallel_for( const Range& range, const Body& body, const simple_partitioner& partitioner, task_group_context& context ) {
  177. internal::start_for<Range,Body,const simple_partitioner>::run(range, body, partitioner, context);
  178. }
  179. //! Parallel iteration over range with auto_partitioner and user-supplied context.
  180. /** @ingroup algorithms **/
  181. template<typename Range, typename Body>
  182. void parallel_for( const Range& range, const Body& body, const auto_partitioner& partitioner, task_group_context& context ) {
  183. internal::start_for<Range,Body,const auto_partitioner>::run(range, body, partitioner, context);
  184. }
  185. //! Parallel iteration over range with affinity_partitioner and user-supplied context.
  186. /** @ingroup algorithms **/
  187. template<typename Range, typename Body>
  188. void parallel_for( const Range& range, const Body& body, affinity_partitioner& partitioner, task_group_context& context ) {
  189. internal::start_for<Range,Body,affinity_partitioner>::run(range,body,partitioner, context);
  190. }
  191. #endif /* __TBB_TASK_GROUP_CONTEXT */
  192. //@}
  193. namespace strict_ppl {
  194. //@{
  195. //! Implementation of parallel iteration over stepped range of integers with explicit step and partitioner
  196. template <typename Index, typename Function, typename Partitioner>
  197. void parallel_for_impl(Index first, Index last, Index step, const Function& f, Partitioner& partitioner) {
  198. if (step <= 0 )
  199. internal::throw_exception(internal::eid_nonpositive_step); // throws std::invalid_argument
  200. else if (last > first) {
  201. // Above "else" avoids "potential divide by zero" warning on some platforms
  202. Index end = (last - first - Index(1)) / step + Index(1);
  203. tbb::blocked_range<Index> range(static_cast<Index>(0), end);
  204. internal::parallel_for_body<Function, Index> body(f, first, step);
  205. tbb::parallel_for(range, body, partitioner);
  206. }
  207. }
  208. //! Parallel iteration over a range of integers with a step provided and default partitioner
  209. template <typename Index, typename Function>
  210. void parallel_for(Index first, Index last, Index step, const Function& f) {
  211. parallel_for_impl<Index,Function,const auto_partitioner>(first, last, step, f, auto_partitioner());
  212. }
  213. //! Parallel iteration over a range of integers with a step provided and simple partitioner
  214. template <typename Index, typename Function>
  215. void parallel_for(Index first, Index last, Index step, const Function& f, const simple_partitioner& partitioner) {
  216. parallel_for_impl<Index,Function,const simple_partitioner>(first, last, step, f, partitioner);
  217. }
  218. //! Parallel iteration over a range of integers with a step provided and auto partitioner
  219. template <typename Index, typename Function>
  220. void parallel_for(Index first, Index last, Index step, const Function& f, const auto_partitioner& partitioner) {
  221. parallel_for_impl<Index,Function,const auto_partitioner>(first, last, step, f, partitioner);
  222. }
  223. //! Parallel iteration over a range of integers with a step provided and affinity partitioner
  224. template <typename Index, typename Function>
  225. void parallel_for(Index first, Index last, Index step, const Function& f, affinity_partitioner& partitioner) {
  226. parallel_for_impl(first, last, step, f, partitioner);
  227. }
  228. //! Parallel iteration over a range of integers with a default step value and default partitioner
  229. template <typename Index, typename Function>
  230. void parallel_for(Index first, Index last, const Function& f) {
  231. parallel_for_impl<Index,Function,const auto_partitioner>(first, last, static_cast<Index>(1), f, auto_partitioner());
  232. }
  233. //! Parallel iteration over a range of integers with a default step value and simple partitioner
  234. template <typename Index, typename Function>
  235. void parallel_for(Index first, Index last, const Function& f, const simple_partitioner& partitioner) {
  236. parallel_for_impl<Index,Function,const simple_partitioner>(first, last, static_cast<Index>(1), f, partitioner);
  237. }
  238. //! Parallel iteration over a range of integers with a default step value and auto partitioner
  239. template <typename Index, typename Function>
  240. void parallel_for(Index first, Index last, const Function& f, const auto_partitioner& partitioner) {
  241. parallel_for_impl<Index,Function,const auto_partitioner>(first, last, static_cast<Index>(1), f, partitioner);
  242. }
  243. //! Parallel iteration over a range of integers with a default step value and affinity partitioner
  244. template <typename Index, typename Function>
  245. void parallel_for(Index first, Index last, const Function& f, affinity_partitioner& partitioner) {
  246. parallel_for_impl(first, last, static_cast<Index>(1), f, partitioner);
  247. }
  248. #if __TBB_TASK_GROUP_CONTEXT
  249. //! Implementation of parallel iteration over stepped range of integers with explicit step, task group context, and partitioner
  250. template <typename Index, typename Function, typename Partitioner>
  251. void parallel_for_impl(Index first, Index last, Index step, const Function& f, Partitioner& partitioner, tbb::task_group_context &context) {
  252. if (step <= 0 )
  253. internal::throw_exception(internal::eid_nonpositive_step); // throws std::invalid_argument
  254. else if (last > first) {
  255. // Above "else" avoids "potential divide by zero" warning on some platforms
  256. Index end = (last - first - Index(1)) / step + Index(1);
  257. tbb::blocked_range<Index> range(static_cast<Index>(0), end);
  258. internal::parallel_for_body<Function, Index> body(f, first, step);
  259. tbb::parallel_for(range, body, partitioner, context);
  260. }
  261. }
  262. //! Parallel iteration over a range of integers with explicit step, task group context, and default partitioner
  263. template <typename Index, typename Function>
  264. void parallel_for(Index first, Index last, Index step, const Function& f, tbb::task_group_context &context) {
  265. parallel_for_impl<Index,Function,const auto_partitioner>(first, last, step, f, auto_partitioner(), context);
  266. }
  267. //! Parallel iteration over a range of integers with explicit step, task group context, and simple partitioner
  268. template <typename Index, typename Function>
  269. void parallel_for(Index first, Index last, Index step, const Function& f, const simple_partitioner& partitioner, tbb::task_group_context &context) {
  270. parallel_for_impl<Index,Function,const simple_partitioner>(first, last, step, f, partitioner, context);
  271. }
  272. //! Parallel iteration over a range of integers with explicit step, task group context, and auto partitioner
  273. template <typename Index, typename Function>
  274. void parallel_for(Index first, Index last, Index step, const Function& f, const auto_partitioner& partitioner, tbb::task_group_context &context) {
  275. parallel_for_impl<Index,Function,const auto_partitioner>(first, last, step, f, partitioner, context);
  276. }
  277. //! Parallel iteration over a range of integers with explicit step, task group context, and affinity partitioner
  278. template <typename Index, typename Function>
  279. void parallel_for(Index first, Index last, Index step, const Function& f, affinity_partitioner& partitioner, tbb::task_group_context &context) {
  280. parallel_for_impl(first, last, step, f, partitioner, context);
  281. }
  282. //! Parallel iteration over a range of integers with a default step value, explicit task group context, and default partitioner
  283. template <typename Index, typename Function>
  284. void parallel_for(Index first, Index last, const Function& f, tbb::task_group_context &context) {
  285. parallel_for_impl<Index,Function,const auto_partitioner>(first, last, static_cast<Index>(1), f, auto_partitioner(), context);
  286. }
  287. //! Parallel iteration over a range of integers with a default step value, explicit task group context, and simple partitioner
  288. template <typename Index, typename Function, typename Partitioner>
  289. void parallel_for(Index first, Index last, const Function& f, const simple_partitioner& partitioner, tbb::task_group_context &context) {
  290. parallel_for_impl<Index,Function,const simple_partitioner>(first, last, static_cast<Index>(1), f, partitioner, context);
  291. }
  292. //! Parallel iteration over a range of integers with a default step value, explicit task group context, and auto partitioner
  293. template <typename Index, typename Function, typename Partitioner>
  294. void parallel_for(Index first, Index last, const Function& f, const auto_partitioner& partitioner, tbb::task_group_context &context) {
  295. parallel_for_impl<Index,Function,const auto_partitioner>(first, last, static_cast<Index>(1), f, partitioner, context);
  296. }
  297. //! Parallel iteration over a range of integers with a default step value, explicit task group context, and affinity_partitioner
  298. template <typename Index, typename Function, typename Partitioner>
  299. void parallel_for(Index first, Index last, const Function& f, affinity_partitioner& partitioner, tbb::task_group_context &context) {
  300. parallel_for_impl(first, last, static_cast<Index>(1), f, partitioner, context);
  301. }
  302. #endif /* __TBB_TASK_GROUP_CONTEXT */
  303. //@}
  304. } // namespace strict_ppl
  305. using strict_ppl::parallel_for;
  306. } // namespace tbb
  307. #if TBB_PREVIEW_SERIAL_SUBSET
  308. #define __TBB_NORMAL_EXECUTION
  309. #include "../serial/tbb/parallel_for.h"
  310. #undef __TBB_NORMAL_EXECUTION
  311. #endif
  312. #endif /* __TBB_parallel_for_H */