You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

541 lines
25 KiB

  1. /*
  2. Copyright 2005-2014 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_reduce_H
  24. #define __TBB_parallel_reduce_H
  25. #include <new>
  26. #include "task.h"
  27. #include "aligned_space.h"
  28. #include "partitioner.h"
  29. #include "tbb_profiling.h"
  30. namespace tbb {
  31. namespace interface6 {
  32. //! @cond INTERNAL
  33. namespace internal {
  34. using namespace tbb::internal;
  35. /** Values for reduction_context. */
  36. enum {
  37. root_task, left_child, right_child
  38. };
  39. /** Represented as a char, not enum, for compactness. */
  40. typedef char reduction_context;
  41. //! Task type used to combine the partial results of parallel_reduce.
  42. /** @ingroup algorithms */
  43. template<typename Body>
  44. class finish_reduce: public flag_task {
  45. //! Pointer to body, or NULL if the left child has not yet finished.
  46. bool has_right_zombie;
  47. const reduction_context my_context;
  48. Body* my_body;
  49. aligned_space<Body,1> zombie_space;
  50. finish_reduce( reduction_context context_ ) :
  51. has_right_zombie(false), // TODO: substitute by flag_task::child_stolen?
  52. my_context(context_),
  53. my_body(NULL)
  54. {
  55. }
  56. ~finish_reduce() {
  57. if( has_right_zombie )
  58. zombie_space.begin()->~Body();
  59. }
  60. task* execute() {
  61. if( has_right_zombie ) {
  62. // Right child was stolen.
  63. Body* s = zombie_space.begin();
  64. my_body->join( *s );
  65. // Body::join() won't be called if canceled. Defer destruction to destructor
  66. }
  67. if( my_context==left_child )
  68. itt_store_word_with_release( static_cast<finish_reduce*>(parent())->my_body, my_body );
  69. return NULL;
  70. }
  71. template<typename Range,typename Body_, typename Partitioner>
  72. friend class start_reduce;
  73. };
  74. //! allocate right task with new parent
  75. void allocate_sibling(task* start_reduce_task, task *tasks[], size_t start_bytes, size_t finish_bytes);
  76. //! Task type used to split the work of parallel_reduce.
  77. /** @ingroup algorithms */
  78. template<typename Range, typename Body, typename Partitioner>
  79. class start_reduce: public task {
  80. typedef finish_reduce<Body> finish_type;
  81. Body* my_body;
  82. Range my_range;
  83. typename Partitioner::task_partition_type my_partition;
  84. reduction_context my_context;
  85. /*override*/ task* execute();
  86. //! Update affinity info, if any
  87. /*override*/ void note_affinity( affinity_id id ) {
  88. my_partition.note_affinity( id );
  89. }
  90. template<typename Body_>
  91. friend class finish_reduce;
  92. public:
  93. //! Constructor used for root task
  94. start_reduce( const Range& range, Body* body, Partitioner& partitioner ) :
  95. my_body(body),
  96. my_range(range),
  97. my_partition(partitioner),
  98. my_context(root_task)
  99. {
  100. }
  101. //! Splitting constructor used to generate children.
  102. /** parent_ becomes left child. Newly constructed object is right child. */
  103. start_reduce( start_reduce& parent_, split ) :
  104. my_body(parent_.my_body),
  105. my_range(parent_.my_range, split()),
  106. my_partition(parent_.my_partition, split()),
  107. my_context(right_child)
  108. {
  109. my_partition.set_affinity(*this);
  110. parent_.my_context = left_child;
  111. }
  112. //! Construct right child from the given range as response to the demand.
  113. /** parent_ remains left child. Newly constructed object is right child. */
  114. start_reduce( start_reduce& parent_, const Range& r, depth_t d ) :
  115. my_body(parent_.my_body),
  116. my_range(r),
  117. my_partition(parent_.my_partition, split()),
  118. my_context(right_child)
  119. {
  120. my_partition.set_affinity(*this);
  121. my_partition.align_depth( d ); // TODO: move into constructor of partitioner
  122. parent_.my_context = left_child;
  123. }
  124. static void run( const Range& range, Body& body, Partitioner& partitioner ) {
  125. if( !range.empty() ) {
  126. #if !__TBB_TASK_GROUP_CONTEXT || TBB_JOIN_OUTER_TASK_GROUP
  127. task::spawn_root_and_wait( *new(task::allocate_root()) start_reduce(range,&body,partitioner) );
  128. #else
  129. // Bound context prevents exceptions from body to affect nesting or sibling algorithms,
  130. // and allows users to handle exceptions safely by wrapping parallel_for in the try-block.
  131. task_group_context context;
  132. task::spawn_root_and_wait( *new(task::allocate_root(context)) start_reduce(range,&body,partitioner) );
  133. #endif /* __TBB_TASK_GROUP_CONTEXT && !TBB_JOIN_OUTER_TASK_GROUP */
  134. }
  135. }
  136. #if __TBB_TASK_GROUP_CONTEXT
  137. static void run( const Range& range, Body& body, Partitioner& partitioner, task_group_context& context ) {
  138. if( !range.empty() )
  139. task::spawn_root_and_wait( *new(task::allocate_root(context)) start_reduce(range,&body,partitioner) );
  140. }
  141. #endif /* __TBB_TASK_GROUP_CONTEXT */
  142. //! Run body for range
  143. void run_body( Range &r ) { (*my_body)( r ); }
  144. //! spawn right task, serves as callback for partitioner
  145. // TODO: remove code duplication from 'offer_work' methods
  146. void offer_work(split) {
  147. task *tasks[2];
  148. allocate_sibling(static_cast<task*>(this), tasks, sizeof(start_reduce), sizeof(finish_type));
  149. new((void*)tasks[0]) finish_type(my_context);
  150. new((void*)tasks[1]) start_reduce(*this, split());
  151. spawn(*tasks[1]);
  152. }
  153. //! spawn right task, serves as callback for partitioner
  154. void offer_work(const Range& r, depth_t d = 0) {
  155. task *tasks[2];
  156. allocate_sibling(static_cast<task*>(this), tasks, sizeof(start_reduce), sizeof(finish_type));
  157. new((void*)tasks[0]) finish_type(my_context);
  158. new((void*)tasks[1]) start_reduce(*this, r, d);
  159. spawn(*tasks[1]);
  160. }
  161. };
  162. //! allocate right task with new parent
  163. // TODO: 'inline' here is to avoid multiple definition error but for sake of code size this should not be inlined
  164. inline void allocate_sibling(task* start_reduce_task, task *tasks[], size_t start_bytes, size_t finish_bytes) {
  165. tasks[0] = &start_reduce_task->allocate_continuation().allocate(finish_bytes);
  166. start_reduce_task->set_parent(tasks[0]);
  167. tasks[0]->set_ref_count(2);
  168. tasks[1] = &tasks[0]->allocate_child().allocate(start_bytes);
  169. }
  170. template<typename Range, typename Body, typename Partitioner>
  171. task* start_reduce<Range,Body,Partitioner>::execute() {
  172. my_partition.check_being_stolen( *this );
  173. if( my_context==right_child ) {
  174. finish_type* parent_ptr = static_cast<finish_type*>(parent());
  175. if( !itt_load_word_with_acquire(parent_ptr->my_body) ) { // TODO: replace by is_stolen_task() or by parent_ptr->ref_count() == 2???
  176. my_body = new( parent_ptr->zombie_space.begin() ) Body(*my_body,split());
  177. parent_ptr->has_right_zombie = true;
  178. }
  179. } else __TBB_ASSERT(my_context==root_task,NULL);// because left leaf spawns right leafs without recycling
  180. my_partition.execute(*this, my_range);
  181. if( my_context==left_child ) {
  182. finish_type* parent_ptr = static_cast<finish_type*>(parent());
  183. __TBB_ASSERT(my_body!=parent_ptr->zombie_space.begin(),NULL);
  184. itt_store_word_with_release(parent_ptr->my_body, my_body );
  185. }
  186. return NULL;
  187. }
  188. //! Task type used to combine the partial results of parallel_deterministic_reduce.
  189. /** @ingroup algorithms */
  190. template<typename Body>
  191. class finish_deterministic_reduce: public task {
  192. Body &my_left_body;
  193. Body my_right_body;
  194. finish_deterministic_reduce( Body &body ) :
  195. my_left_body( body ),
  196. my_right_body( body, split() )
  197. {
  198. }
  199. task* execute() {
  200. my_left_body.join( my_right_body );
  201. return NULL;
  202. }
  203. template<typename Range,typename Body_>
  204. friend class start_deterministic_reduce;
  205. };
  206. //! Task type used to split the work of parallel_deterministic_reduce.
  207. /** @ingroup algorithms */
  208. template<typename Range, typename Body>
  209. class start_deterministic_reduce: public task {
  210. typedef finish_deterministic_reduce<Body> finish_type;
  211. Body &my_body;
  212. Range my_range;
  213. /*override*/ task* execute();
  214. //! Constructor used for root task
  215. start_deterministic_reduce( const Range& range, Body& body ) :
  216. my_body( body ),
  217. my_range( range )
  218. {
  219. }
  220. //! Splitting constructor used to generate children.
  221. /** parent_ becomes left child. Newly constructed object is right child. */
  222. start_deterministic_reduce( start_deterministic_reduce& parent_, finish_type& c ) :
  223. my_body( c.my_right_body ),
  224. my_range( parent_.my_range, split() )
  225. {
  226. }
  227. public:
  228. static void run( const Range& range, Body& body ) {
  229. if( !range.empty() ) {
  230. #if !__TBB_TASK_GROUP_CONTEXT || TBB_JOIN_OUTER_TASK_GROUP
  231. task::spawn_root_and_wait( *new(task::allocate_root()) start_deterministic_reduce(range,&body) );
  232. #else
  233. // Bound context prevents exceptions from body to affect nesting or sibling algorithms,
  234. // and allows users to handle exceptions safely by wrapping parallel_for in the try-block.
  235. task_group_context context;
  236. task::spawn_root_and_wait( *new(task::allocate_root(context)) start_deterministic_reduce(range,body) );
  237. #endif /* __TBB_TASK_GROUP_CONTEXT && !TBB_JOIN_OUTER_TASK_GROUP */
  238. }
  239. }
  240. #if __TBB_TASK_GROUP_CONTEXT
  241. static void run( const Range& range, Body& body, task_group_context& context ) {
  242. if( !range.empty() )
  243. task::spawn_root_and_wait( *new(task::allocate_root(context)) start_deterministic_reduce(range,body) );
  244. }
  245. #endif /* __TBB_TASK_GROUP_CONTEXT */
  246. };
  247. template<typename Range, typename Body>
  248. task* start_deterministic_reduce<Range,Body>::execute() {
  249. if( !my_range.is_divisible() ) {
  250. my_body( my_range );
  251. return NULL;
  252. } else {
  253. finish_type& c = *new( allocate_continuation() ) finish_type( my_body );
  254. recycle_as_child_of(c);
  255. c.set_ref_count(2);
  256. start_deterministic_reduce& b = *new( c.allocate_child() ) start_deterministic_reduce( *this, c );
  257. task::spawn(b);
  258. return this;
  259. }
  260. }
  261. } // namespace internal
  262. //! @endcond
  263. } //namespace interfaceX
  264. //! @cond INTERNAL
  265. namespace internal {
  266. using interface6::internal::start_reduce;
  267. using interface6::internal::start_deterministic_reduce;
  268. //! Auxiliary class for parallel_reduce; for internal use only.
  269. /** The adaptor class that implements \ref parallel_reduce_body_req "parallel_reduce Body"
  270. using given \ref parallel_reduce_lambda_req "anonymous function objects".
  271. **/
  272. /** @ingroup algorithms */
  273. template<typename Range, typename Value, typename RealBody, typename Reduction>
  274. class lambda_reduce_body {
  275. //FIXME: decide if my_real_body, my_reduction, and identity_element should be copied or referenced
  276. // (might require some performance measurements)
  277. const Value& identity_element;
  278. const RealBody& my_real_body;
  279. const Reduction& my_reduction;
  280. Value my_value;
  281. lambda_reduce_body& operator= ( const lambda_reduce_body& other );
  282. public:
  283. lambda_reduce_body( const Value& identity, const RealBody& body, const Reduction& reduction )
  284. : identity_element(identity)
  285. , my_real_body(body)
  286. , my_reduction(reduction)
  287. , my_value(identity)
  288. { }
  289. lambda_reduce_body( const lambda_reduce_body& other )
  290. : identity_element(other.identity_element)
  291. , my_real_body(other.my_real_body)
  292. , my_reduction(other.my_reduction)
  293. , my_value(other.my_value)
  294. { }
  295. lambda_reduce_body( lambda_reduce_body& other, tbb::split )
  296. : identity_element(other.identity_element)
  297. , my_real_body(other.my_real_body)
  298. , my_reduction(other.my_reduction)
  299. , my_value(other.identity_element)
  300. { }
  301. void operator()(Range& range) {
  302. my_value = my_real_body(range, const_cast<const Value&>(my_value));
  303. }
  304. void join( lambda_reduce_body& rhs ) {
  305. my_value = my_reduction(const_cast<const Value&>(my_value), const_cast<const Value&>(rhs.my_value));
  306. }
  307. Value result() const {
  308. return my_value;
  309. }
  310. };
  311. } // namespace internal
  312. //! @endcond
  313. // Requirements on Range concept are documented in blocked_range.h
  314. /** \page parallel_reduce_body_req Requirements on parallel_reduce body
  315. Class \c Body implementing the concept of parallel_reduce body must define:
  316. - \code Body::Body( Body&, split ); \endcode Splitting constructor.
  317. Must be able to run concurrently with operator() and method \c join
  318. - \code Body::~Body(); \endcode Destructor
  319. - \code void Body::operator()( Range& r ); \endcode Function call operator applying body to range \c r
  320. and accumulating the result
  321. - \code void Body::join( Body& b ); \endcode Join results.
  322. The result in \c b should be merged into the result of \c this
  323. **/
  324. /** \page parallel_reduce_lambda_req Requirements on parallel_reduce anonymous function objects (lambda functions)
  325. TO BE DOCUMENTED
  326. **/
  327. /** \name parallel_reduce
  328. See also requirements on \ref range_req "Range" and \ref parallel_reduce_body_req "parallel_reduce Body". **/
  329. //@{
  330. //! Parallel iteration with reduction and default partitioner.
  331. /** @ingroup algorithms **/
  332. template<typename Range, typename Body>
  333. void parallel_reduce( const Range& range, Body& body ) {
  334. internal::start_reduce<Range,Body, const __TBB_DEFAULT_PARTITIONER>::run( range, body, __TBB_DEFAULT_PARTITIONER() );
  335. }
  336. //! Parallel iteration with reduction and simple_partitioner
  337. /** @ingroup algorithms **/
  338. template<typename Range, typename Body>
  339. void parallel_reduce( const Range& range, Body& body, const simple_partitioner& partitioner ) {
  340. internal::start_reduce<Range,Body,const simple_partitioner>::run( range, body, partitioner );
  341. }
  342. //! Parallel iteration with reduction and auto_partitioner
  343. /** @ingroup algorithms **/
  344. template<typename Range, typename Body>
  345. void parallel_reduce( const Range& range, Body& body, const auto_partitioner& partitioner ) {
  346. internal::start_reduce<Range,Body,const auto_partitioner>::run( range, body, partitioner );
  347. }
  348. //! Parallel iteration with reduction and affinity_partitioner
  349. /** @ingroup algorithms **/
  350. template<typename Range, typename Body>
  351. void parallel_reduce( const Range& range, Body& body, affinity_partitioner& partitioner ) {
  352. internal::start_reduce<Range,Body,affinity_partitioner>::run( range, body, partitioner );
  353. }
  354. #if __TBB_TASK_GROUP_CONTEXT
  355. //! Parallel iteration with reduction, simple partitioner and user-supplied context.
  356. /** @ingroup algorithms **/
  357. template<typename Range, typename Body>
  358. void parallel_reduce( const Range& range, Body& body, const simple_partitioner& partitioner, task_group_context& context ) {
  359. internal::start_reduce<Range,Body,const simple_partitioner>::run( range, body, partitioner, context );
  360. }
  361. //! Parallel iteration with reduction, auto_partitioner and user-supplied context
  362. /** @ingroup algorithms **/
  363. template<typename Range, typename Body>
  364. void parallel_reduce( const Range& range, Body& body, const auto_partitioner& partitioner, task_group_context& context ) {
  365. internal::start_reduce<Range,Body,const auto_partitioner>::run( range, body, partitioner, context );
  366. }
  367. //! Parallel iteration with reduction, affinity_partitioner and user-supplied context
  368. /** @ingroup algorithms **/
  369. template<typename Range, typename Body>
  370. void parallel_reduce( const Range& range, Body& body, affinity_partitioner& partitioner, task_group_context& context ) {
  371. internal::start_reduce<Range,Body,affinity_partitioner>::run( range, body, partitioner, context );
  372. }
  373. #endif /* __TBB_TASK_GROUP_CONTEXT */
  374. /** parallel_reduce overloads that work with anonymous function objects
  375. (see also \ref parallel_reduce_lambda_req "requirements on parallel_reduce anonymous function objects"). **/
  376. //! Parallel iteration with reduction and default partitioner.
  377. /** @ingroup algorithms **/
  378. template<typename Range, typename Value, typename RealBody, typename Reduction>
  379. Value parallel_reduce( const Range& range, const Value& identity, const RealBody& real_body, const Reduction& reduction ) {
  380. internal::lambda_reduce_body<Range,Value,RealBody,Reduction> body(identity, real_body, reduction);
  381. internal::start_reduce<Range,internal::lambda_reduce_body<Range,Value,RealBody,Reduction>,const __TBB_DEFAULT_PARTITIONER>
  382. ::run(range, body, __TBB_DEFAULT_PARTITIONER() );
  383. return body.result();
  384. }
  385. //! Parallel iteration with reduction and simple_partitioner.
  386. /** @ingroup algorithms **/
  387. template<typename Range, typename Value, typename RealBody, typename Reduction>
  388. Value parallel_reduce( const Range& range, const Value& identity, const RealBody& real_body, const Reduction& reduction,
  389. const simple_partitioner& partitioner ) {
  390. internal::lambda_reduce_body<Range,Value,RealBody,Reduction> body(identity, real_body, reduction);
  391. internal::start_reduce<Range,internal::lambda_reduce_body<Range,Value,RealBody,Reduction>,const simple_partitioner>
  392. ::run(range, body, partitioner );
  393. return body.result();
  394. }
  395. //! Parallel iteration with reduction and auto_partitioner
  396. /** @ingroup algorithms **/
  397. template<typename Range, typename Value, typename RealBody, typename Reduction>
  398. Value parallel_reduce( const Range& range, const Value& identity, const RealBody& real_body, const Reduction& reduction,
  399. const auto_partitioner& partitioner ) {
  400. internal::lambda_reduce_body<Range,Value,RealBody,Reduction> body(identity, real_body, reduction);
  401. internal::start_reduce<Range,internal::lambda_reduce_body<Range,Value,RealBody,Reduction>,const auto_partitioner>
  402. ::run( range, body, partitioner );
  403. return body.result();
  404. }
  405. //! Parallel iteration with reduction and affinity_partitioner
  406. /** @ingroup algorithms **/
  407. template<typename Range, typename Value, typename RealBody, typename Reduction>
  408. Value parallel_reduce( const Range& range, const Value& identity, const RealBody& real_body, const Reduction& reduction,
  409. affinity_partitioner& partitioner ) {
  410. internal::lambda_reduce_body<Range,Value,RealBody,Reduction> body(identity, real_body, reduction);
  411. internal::start_reduce<Range,internal::lambda_reduce_body<Range,Value,RealBody,Reduction>,affinity_partitioner>
  412. ::run( range, body, partitioner );
  413. return body.result();
  414. }
  415. #if __TBB_TASK_GROUP_CONTEXT
  416. //! Parallel iteration with reduction, simple partitioner and user-supplied context.
  417. /** @ingroup algorithms **/
  418. template<typename Range, typename Value, typename RealBody, typename Reduction>
  419. Value parallel_reduce( const Range& range, const Value& identity, const RealBody& real_body, const Reduction& reduction,
  420. const simple_partitioner& partitioner, task_group_context& context ) {
  421. internal::lambda_reduce_body<Range,Value,RealBody,Reduction> body(identity, real_body, reduction);
  422. internal::start_reduce<Range,internal::lambda_reduce_body<Range,Value,RealBody,Reduction>,const simple_partitioner>
  423. ::run( range, body, partitioner, context );
  424. return body.result();
  425. }
  426. //! Parallel iteration with reduction, auto_partitioner and user-supplied context
  427. /** @ingroup algorithms **/
  428. template<typename Range, typename Value, typename RealBody, typename Reduction>
  429. Value parallel_reduce( const Range& range, const Value& identity, const RealBody& real_body, const Reduction& reduction,
  430. const auto_partitioner& partitioner, task_group_context& context ) {
  431. internal::lambda_reduce_body<Range,Value,RealBody,Reduction> body(identity, real_body, reduction);
  432. internal::start_reduce<Range,internal::lambda_reduce_body<Range,Value,RealBody,Reduction>,const auto_partitioner>
  433. ::run( range, body, partitioner, context );
  434. return body.result();
  435. }
  436. //! Parallel iteration with reduction, affinity_partitioner and user-supplied context
  437. /** @ingroup algorithms **/
  438. template<typename Range, typename Value, typename RealBody, typename Reduction>
  439. Value parallel_reduce( const Range& range, const Value& identity, const RealBody& real_body, const Reduction& reduction,
  440. affinity_partitioner& partitioner, task_group_context& context ) {
  441. internal::lambda_reduce_body<Range,Value,RealBody,Reduction> body(identity, real_body, reduction);
  442. internal::start_reduce<Range,internal::lambda_reduce_body<Range,Value,RealBody,Reduction>,affinity_partitioner>
  443. ::run( range, body, partitioner, context );
  444. return body.result();
  445. }
  446. #endif /* __TBB_TASK_GROUP_CONTEXT */
  447. //! Parallel iteration with deterministic reduction and default partitioner.
  448. /** @ingroup algorithms **/
  449. template<typename Range, typename Body>
  450. void parallel_deterministic_reduce( const Range& range, Body& body ) {
  451. internal::start_deterministic_reduce<Range,Body>::run( range, body );
  452. }
  453. #if __TBB_TASK_GROUP_CONTEXT
  454. //! Parallel iteration with deterministic reduction, simple partitioner and user-supplied context.
  455. /** @ingroup algorithms **/
  456. template<typename Range, typename Body>
  457. void parallel_deterministic_reduce( const Range& range, Body& body, task_group_context& context ) {
  458. internal::start_deterministic_reduce<Range,Body>::run( range, body, context );
  459. }
  460. #endif /* __TBB_TASK_GROUP_CONTEXT */
  461. /** parallel_reduce overloads that work with anonymous function objects
  462. (see also \ref parallel_reduce_lambda_req "requirements on parallel_reduce anonymous function objects"). **/
  463. //! Parallel iteration with deterministic reduction and default partitioner.
  464. /** @ingroup algorithms **/
  465. template<typename Range, typename Value, typename RealBody, typename Reduction>
  466. Value parallel_deterministic_reduce( const Range& range, const Value& identity, const RealBody& real_body, const Reduction& reduction ) {
  467. internal::lambda_reduce_body<Range,Value,RealBody,Reduction> body(identity, real_body, reduction);
  468. internal::start_deterministic_reduce<Range,internal::lambda_reduce_body<Range,Value,RealBody,Reduction> >
  469. ::run(range, body);
  470. return body.result();
  471. }
  472. #if __TBB_TASK_GROUP_CONTEXT
  473. //! Parallel iteration with deterministic reduction, simple partitioner and user-supplied context.
  474. /** @ingroup algorithms **/
  475. template<typename Range, typename Value, typename RealBody, typename Reduction>
  476. Value parallel_deterministic_reduce( const Range& range, const Value& identity, const RealBody& real_body, const Reduction& reduction,
  477. task_group_context& context ) {
  478. internal::lambda_reduce_body<Range,Value,RealBody,Reduction> body(identity, real_body, reduction);
  479. internal::start_deterministic_reduce<Range,internal::lambda_reduce_body<Range,Value,RealBody,Reduction> >
  480. ::run( range, body, context );
  481. return body.result();
  482. }
  483. #endif /* __TBB_TASK_GROUP_CONTEXT */
  484. //@}
  485. } // namespace tbb
  486. #endif /* __TBB_parallel_reduce_H */