_flow_graph_node_impl.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /*
  2. Copyright (c) 2005-2020 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #ifndef __TBB__flow_graph_node_impl_H
  14. #define __TBB__flow_graph_node_impl_H
  15. #ifndef __TBB_flow_graph_H
  16. #error Do not #include this internal file directly; use public TBB headers instead.
  17. #endif
  18. #include "_flow_graph_item_buffer_impl.h"
  19. //! @cond INTERNAL
  20. namespace internal {
  21. using tbb::internal::aggregated_operation;
  22. using tbb::internal::aggregating_functor;
  23. using tbb::internal::aggregator;
  24. template< typename T, typename A >
  25. class function_input_queue : public item_buffer<T,A> {
  26. public:
  27. bool empty() const {
  28. return this->buffer_empty();
  29. }
  30. const T& front() const {
  31. return this->item_buffer<T, A>::front();
  32. }
  33. bool pop( T& t ) {
  34. return this->pop_front( t );
  35. }
  36. void pop() {
  37. this->destroy_front();
  38. }
  39. bool push( T& t ) {
  40. return this->push_back( t );
  41. }
  42. };
  43. //! Input and scheduling for a function node that takes a type Input as input
  44. // The only up-ref is apply_body_impl, which should implement the function
  45. // call and any handling of the result.
  46. template< typename Input, typename Policy, typename A, typename ImplType >
  47. class function_input_base : public receiver<Input>, tbb::internal::no_assign {
  48. enum op_type {reg_pred, rem_pred, try_fwd, tryput_bypass, app_body_bypass, occupy_concurrency
  49. #if TBB_DEPRECATED_FLOW_NODE_EXTRACTION
  50. , add_blt_pred, del_blt_pred,
  51. blt_pred_cnt, blt_pred_cpy // create vector copies of preds and succs
  52. #endif
  53. };
  54. typedef function_input_base<Input, Policy, A, ImplType> class_type;
  55. public:
  56. //! The input type of this receiver
  57. typedef Input input_type;
  58. typedef typename receiver<input_type>::predecessor_type predecessor_type;
  59. typedef predecessor_cache<input_type, null_mutex > predecessor_cache_type;
  60. typedef function_input_queue<input_type, A> input_queue_type;
  61. typedef typename tbb::internal::allocator_rebind<A, input_queue_type>::type queue_allocator_type;
  62. __TBB_STATIC_ASSERT(!((internal::has_policy<queueing, Policy>::value) && (internal::has_policy<rejecting, Policy>::value)),
  63. "queueing and rejecting policies can't be specified simultaneously");
  64. #if TBB_DEPRECATED_FLOW_NODE_EXTRACTION
  65. typedef typename predecessor_cache_type::built_predecessors_type built_predecessors_type;
  66. typedef typename receiver<input_type>::predecessor_list_type predecessor_list_type;
  67. #endif
  68. //! Constructor for function_input_base
  69. function_input_base(
  70. graph &g, __TBB_FLOW_GRAPH_PRIORITY_ARG1(size_t max_concurrency, node_priority_t priority)
  71. ) : my_graph_ref(g), my_max_concurrency(max_concurrency)
  72. , __TBB_FLOW_GRAPH_PRIORITY_ARG1(my_concurrency(0), my_priority(priority))
  73. , my_queue(!internal::has_policy<rejecting, Policy>::value ? new input_queue_type() : NULL)
  74. , forwarder_busy(false)
  75. {
  76. my_predecessors.set_owner(this);
  77. my_aggregator.initialize_handler(handler_type(this));
  78. }
  79. //! Copy constructor
  80. function_input_base( const function_input_base& src)
  81. : receiver<Input>(), tbb::internal::no_assign()
  82. , my_graph_ref(src.my_graph_ref), my_max_concurrency(src.my_max_concurrency)
  83. , __TBB_FLOW_GRAPH_PRIORITY_ARG1(my_concurrency(0), my_priority(src.my_priority))
  84. , my_queue(src.my_queue ? new input_queue_type() : NULL), forwarder_busy(false)
  85. {
  86. my_predecessors.set_owner(this);
  87. my_aggregator.initialize_handler(handler_type(this));
  88. }
  89. //! Destructor
  90. // The queue is allocated by the constructor for {multi}function_node.
  91. // TODO: pass the graph_buffer_policy to the base so it can allocate the queue instead.
  92. // This would be an interface-breaking change.
  93. virtual ~function_input_base() {
  94. if ( my_queue ) delete my_queue;
  95. }
  96. task* try_put_task( const input_type& t) __TBB_override {
  97. return try_put_task_impl(t, internal::has_policy<lightweight, Policy>());
  98. }
  99. //! Adds src to the list of cached predecessors.
  100. bool register_predecessor( predecessor_type &src ) __TBB_override {
  101. operation_type op_data(reg_pred);
  102. op_data.r = &src;
  103. my_aggregator.execute(&op_data);
  104. return true;
  105. }
  106. //! Removes src from the list of cached predecessors.
  107. bool remove_predecessor( predecessor_type &src ) __TBB_override {
  108. operation_type op_data(rem_pred);
  109. op_data.r = &src;
  110. my_aggregator.execute(&op_data);
  111. return true;
  112. }
  113. #if TBB_DEPRECATED_FLOW_NODE_EXTRACTION
  114. //! Adds to list of predecessors added by make_edge
  115. void internal_add_built_predecessor( predecessor_type &src) __TBB_override {
  116. operation_type op_data(add_blt_pred);
  117. op_data.r = &src;
  118. my_aggregator.execute(&op_data);
  119. }
  120. //! removes from to list of predecessors (used by remove_edge)
  121. void internal_delete_built_predecessor( predecessor_type &src) __TBB_override {
  122. operation_type op_data(del_blt_pred);
  123. op_data.r = &src;
  124. my_aggregator.execute(&op_data);
  125. }
  126. size_t predecessor_count() __TBB_override {
  127. operation_type op_data(blt_pred_cnt);
  128. my_aggregator.execute(&op_data);
  129. return op_data.cnt_val;
  130. }
  131. void copy_predecessors(predecessor_list_type &v) __TBB_override {
  132. operation_type op_data(blt_pred_cpy);
  133. op_data.predv = &v;
  134. my_aggregator.execute(&op_data);
  135. }
  136. built_predecessors_type &built_predecessors() __TBB_override {
  137. return my_predecessors.built_predecessors();
  138. }
  139. #endif /* TBB_DEPRECATED_FLOW_NODE_EXTRACTION */
  140. protected:
  141. void reset_function_input_base( reset_flags f) {
  142. my_concurrency = 0;
  143. if(my_queue) {
  144. my_queue->reset();
  145. }
  146. reset_receiver(f);
  147. forwarder_busy = false;
  148. }
  149. graph& my_graph_ref;
  150. const size_t my_max_concurrency;
  151. size_t my_concurrency;
  152. __TBB_FLOW_GRAPH_PRIORITY_EXPR( node_priority_t my_priority; )
  153. input_queue_type *my_queue;
  154. predecessor_cache<input_type, null_mutex > my_predecessors;
  155. void reset_receiver( reset_flags f) __TBB_override {
  156. if( f & rf_clear_edges) my_predecessors.clear();
  157. else
  158. my_predecessors.reset();
  159. __TBB_ASSERT(!(f & rf_clear_edges) || my_predecessors.empty(), "function_input_base reset failed");
  160. }
  161. graph& graph_reference() const __TBB_override {
  162. return my_graph_ref;
  163. }
  164. task* try_get_postponed_task(const input_type& i) {
  165. operation_type op_data(i, app_body_bypass); // tries to pop an item or get_item
  166. my_aggregator.execute(&op_data);
  167. return op_data.bypass_t;
  168. }
  169. private:
  170. friend class apply_body_task_bypass< class_type, input_type >;
  171. friend class forward_task_bypass< class_type >;
  172. class operation_type : public aggregated_operation< operation_type > {
  173. public:
  174. char type;
  175. union {
  176. input_type *elem;
  177. predecessor_type *r;
  178. #if TBB_DEPRECATED_FLOW_NODE_EXTRACTION
  179. size_t cnt_val;
  180. predecessor_list_type *predv;
  181. #endif /* TBB_DEPRECATED_FLOW_NODE_EXTRACTION */
  182. };
  183. tbb::task *bypass_t;
  184. operation_type(const input_type& e, op_type t) :
  185. type(char(t)), elem(const_cast<input_type*>(&e)) {}
  186. operation_type(op_type t) : type(char(t)), r(NULL) {}
  187. };
  188. bool forwarder_busy;
  189. typedef internal::aggregating_functor<class_type, operation_type> handler_type;
  190. friend class internal::aggregating_functor<class_type, operation_type>;
  191. aggregator< handler_type, operation_type > my_aggregator;
  192. task* perform_queued_requests() {
  193. task* new_task = NULL;
  194. if(my_queue) {
  195. if(!my_queue->empty()) {
  196. ++my_concurrency;
  197. new_task = create_body_task(my_queue->front());
  198. my_queue->pop();
  199. }
  200. }
  201. else {
  202. input_type i;
  203. if(my_predecessors.get_item(i)) {
  204. ++my_concurrency;
  205. new_task = create_body_task(i);
  206. }
  207. }
  208. return new_task;
  209. }
  210. void handle_operations(operation_type *op_list) {
  211. operation_type *tmp;
  212. while (op_list) {
  213. tmp = op_list;
  214. op_list = op_list->next;
  215. switch (tmp->type) {
  216. case reg_pred:
  217. my_predecessors.add(*(tmp->r));
  218. __TBB_store_with_release(tmp->status, SUCCEEDED);
  219. if (!forwarder_busy) {
  220. forwarder_busy = true;
  221. spawn_forward_task();
  222. }
  223. break;
  224. case rem_pred:
  225. my_predecessors.remove(*(tmp->r));
  226. __TBB_store_with_release(tmp->status, SUCCEEDED);
  227. break;
  228. case app_body_bypass: {
  229. tmp->bypass_t = NULL;
  230. __TBB_ASSERT(my_max_concurrency != 0, NULL);
  231. --my_concurrency;
  232. if(my_concurrency<my_max_concurrency)
  233. tmp->bypass_t = perform_queued_requests();
  234. __TBB_store_with_release(tmp->status, SUCCEEDED);
  235. }
  236. break;
  237. case tryput_bypass: internal_try_put_task(tmp); break;
  238. case try_fwd: internal_forward(tmp); break;
  239. case occupy_concurrency:
  240. if (my_concurrency < my_max_concurrency) {
  241. ++my_concurrency;
  242. __TBB_store_with_release(tmp->status, SUCCEEDED);
  243. } else {
  244. __TBB_store_with_release(tmp->status, FAILED);
  245. }
  246. break;
  247. #if TBB_DEPRECATED_FLOW_NODE_EXTRACTION
  248. case add_blt_pred: {
  249. my_predecessors.internal_add_built_predecessor(*(tmp->r));
  250. __TBB_store_with_release(tmp->status, SUCCEEDED);
  251. }
  252. break;
  253. case del_blt_pred:
  254. my_predecessors.internal_delete_built_predecessor(*(tmp->r));
  255. __TBB_store_with_release(tmp->status, SUCCEEDED);
  256. break;
  257. case blt_pred_cnt:
  258. tmp->cnt_val = my_predecessors.predecessor_count();
  259. __TBB_store_with_release(tmp->status, SUCCEEDED);
  260. break;
  261. case blt_pred_cpy:
  262. my_predecessors.copy_predecessors( *(tmp->predv) );
  263. __TBB_store_with_release(tmp->status, SUCCEEDED);
  264. break;
  265. #endif /* TBB_DEPRECATED_FLOW_NODE_EXTRACTION */
  266. }
  267. }
  268. }
  269. //! Put to the node, but return the task instead of enqueueing it
  270. void internal_try_put_task(operation_type *op) {
  271. __TBB_ASSERT(my_max_concurrency != 0, NULL);
  272. if (my_concurrency < my_max_concurrency) {
  273. ++my_concurrency;
  274. task * new_task = create_body_task(*(op->elem));
  275. op->bypass_t = new_task;
  276. __TBB_store_with_release(op->status, SUCCEEDED);
  277. } else if ( my_queue && my_queue->push(*(op->elem)) ) {
  278. op->bypass_t = SUCCESSFULLY_ENQUEUED;
  279. __TBB_store_with_release(op->status, SUCCEEDED);
  280. } else {
  281. op->bypass_t = NULL;
  282. __TBB_store_with_release(op->status, FAILED);
  283. }
  284. }
  285. //! Creates tasks for postponed messages if available and if concurrency allows
  286. void internal_forward(operation_type *op) {
  287. op->bypass_t = NULL;
  288. if (my_concurrency < my_max_concurrency || !my_max_concurrency)
  289. op->bypass_t = perform_queued_requests();
  290. if(op->bypass_t)
  291. __TBB_store_with_release(op->status, SUCCEEDED);
  292. else {
  293. forwarder_busy = false;
  294. __TBB_store_with_release(op->status, FAILED);
  295. }
  296. }
  297. task* internal_try_put_bypass( const input_type& t ) {
  298. operation_type op_data(t, tryput_bypass);
  299. my_aggregator.execute(&op_data);
  300. if( op_data.status == internal::SUCCEEDED ) {
  301. return op_data.bypass_t;
  302. }
  303. return NULL;
  304. }
  305. task* try_put_task_impl( const input_type& t, /*lightweight=*/tbb::internal::true_type ) {
  306. if( my_max_concurrency == 0 ) {
  307. return apply_body_bypass(t);
  308. } else {
  309. operation_type check_op(t, occupy_concurrency);
  310. my_aggregator.execute(&check_op);
  311. if( check_op.status == internal::SUCCEEDED ) {
  312. return apply_body_bypass(t);
  313. }
  314. return internal_try_put_bypass(t);
  315. }
  316. }
  317. task* try_put_task_impl( const input_type& t, /*lightweight=*/tbb::internal::false_type ) {
  318. if( my_max_concurrency == 0 ) {
  319. return create_body_task(t);
  320. } else {
  321. return internal_try_put_bypass(t);
  322. }
  323. }
  324. //! Applies the body to the provided input
  325. // then decides if more work is available
  326. task * apply_body_bypass( const input_type &i ) {
  327. return static_cast<ImplType *>(this)->apply_body_impl_bypass(i);
  328. }
  329. //! allocates a task to apply a body
  330. inline task * create_body_task( const input_type &input ) {
  331. return (internal::is_graph_active(my_graph_ref)) ?
  332. new( task::allocate_additional_child_of(*(my_graph_ref.root_task())) )
  333. apply_body_task_bypass < class_type, input_type >(
  334. *this, __TBB_FLOW_GRAPH_PRIORITY_ARG1(input, my_priority))
  335. : NULL;
  336. }
  337. //! This is executed by an enqueued task, the "forwarder"
  338. task* forward_task() {
  339. operation_type op_data(try_fwd);
  340. task* rval = NULL;
  341. do {
  342. op_data.status = WAIT;
  343. my_aggregator.execute(&op_data);
  344. if(op_data.status == SUCCEEDED) {
  345. task* ttask = op_data.bypass_t;
  346. __TBB_ASSERT( ttask && ttask != SUCCESSFULLY_ENQUEUED, NULL );
  347. rval = combine_tasks(my_graph_ref, rval, ttask);
  348. }
  349. } while (op_data.status == SUCCEEDED);
  350. return rval;
  351. }
  352. inline task *create_forward_task() {
  353. return (internal::is_graph_active(my_graph_ref)) ?
  354. new( task::allocate_additional_child_of(*(my_graph_ref.root_task())) )
  355. forward_task_bypass< class_type >( __TBB_FLOW_GRAPH_PRIORITY_ARG1(*this, my_priority) )
  356. : NULL;
  357. }
  358. //! Spawns a task that calls forward()
  359. inline void spawn_forward_task() {
  360. task* tp = create_forward_task();
  361. if(tp) {
  362. internal::spawn_in_graph_arena(graph_reference(), *tp);
  363. }
  364. }
  365. }; // function_input_base
  366. //! Implements methods for a function node that takes a type Input as input and sends
  367. // a type Output to its successors.
  368. template< typename Input, typename Output, typename Policy, typename A>
  369. class function_input : public function_input_base<Input, Policy, A, function_input<Input,Output,Policy,A> > {
  370. public:
  371. typedef Input input_type;
  372. typedef Output output_type;
  373. typedef function_body<input_type, output_type> function_body_type;
  374. typedef function_input<Input, Output, Policy,A> my_class;
  375. typedef function_input_base<Input, Policy, A, my_class> base_type;
  376. typedef function_input_queue<input_type, A> input_queue_type;
  377. // constructor
  378. template<typename Body>
  379. function_input(
  380. graph &g, size_t max_concurrency,
  381. __TBB_FLOW_GRAPH_PRIORITY_ARG1(Body& body, node_priority_t priority)
  382. ) : base_type(g, __TBB_FLOW_GRAPH_PRIORITY_ARG1(max_concurrency, priority))
  383. , my_body( new internal::function_body_leaf< input_type, output_type, Body>(body) )
  384. , my_init_body( new internal::function_body_leaf< input_type, output_type, Body>(body) ) {
  385. }
  386. //! Copy constructor
  387. function_input( const function_input& src ) :
  388. base_type(src),
  389. my_body( src.my_init_body->clone() ),
  390. my_init_body(src.my_init_body->clone() ) {
  391. }
  392. ~function_input() {
  393. delete my_body;
  394. delete my_init_body;
  395. }
  396. template< typename Body >
  397. Body copy_function_object() {
  398. function_body_type &body_ref = *this->my_body;
  399. return dynamic_cast< internal::function_body_leaf<input_type, output_type, Body> & >(body_ref).get_body();
  400. }
  401. output_type apply_body_impl( const input_type& i) {
  402. // There is an extra copied needed to capture the
  403. // body execution without the try_put
  404. tbb::internal::fgt_begin_body( my_body );
  405. output_type v = (*my_body)(i);
  406. tbb::internal::fgt_end_body( my_body );
  407. return v;
  408. }
  409. //TODO: consider moving into the base class
  410. task * apply_body_impl_bypass( const input_type &i) {
  411. output_type v = apply_body_impl(i);
  412. #if TBB_DEPRECATED_MESSAGE_FLOW_ORDER
  413. task* successor_task = successors().try_put_task(v);
  414. #endif
  415. task* postponed_task = NULL;
  416. if( base_type::my_max_concurrency != 0 ) {
  417. postponed_task = base_type::try_get_postponed_task(i);
  418. __TBB_ASSERT( !postponed_task || postponed_task != SUCCESSFULLY_ENQUEUED, NULL );
  419. }
  420. #if TBB_DEPRECATED_MESSAGE_FLOW_ORDER
  421. graph& g = base_type::my_graph_ref;
  422. return combine_tasks(g, successor_task, postponed_task);
  423. #else
  424. if( postponed_task ) {
  425. // make the task available for other workers since we do not know successors'
  426. // execution policy
  427. internal::spawn_in_graph_arena(base_type::graph_reference(), *postponed_task);
  428. }
  429. task* successor_task = successors().try_put_task(v);
  430. #if _MSC_VER && !__INTEL_COMPILER
  431. #pragma warning (push)
  432. #pragma warning (disable: 4127) /* suppress conditional expression is constant */
  433. #endif
  434. if(internal::has_policy<lightweight, Policy>::value) {
  435. #if _MSC_VER && !__INTEL_COMPILER
  436. #pragma warning (pop)
  437. #endif
  438. if(!successor_task) {
  439. // Return confirmative status since current
  440. // node's body has been executed anyway
  441. successor_task = SUCCESSFULLY_ENQUEUED;
  442. }
  443. }
  444. return successor_task;
  445. #endif /* TBB_DEPRECATED_MESSAGE_FLOW_ORDER */
  446. }
  447. protected:
  448. void reset_function_input(reset_flags f) {
  449. base_type::reset_function_input_base(f);
  450. if(f & rf_reset_bodies) {
  451. function_body_type *tmp = my_init_body->clone();
  452. delete my_body;
  453. my_body = tmp;
  454. }
  455. }
  456. function_body_type *my_body;
  457. function_body_type *my_init_body;
  458. virtual broadcast_cache<output_type > &successors() = 0;
  459. }; // function_input
  460. // helper templates to clear the successor edges of the output ports of an multifunction_node
  461. template<int N> struct clear_element {
  462. template<typename P> static void clear_this(P &p) {
  463. (void)tbb::flow::get<N-1>(p).successors().clear();
  464. clear_element<N-1>::clear_this(p);
  465. }
  466. template<typename P> static bool this_empty(P &p) {
  467. if(tbb::flow::get<N-1>(p).successors().empty())
  468. return clear_element<N-1>::this_empty(p);
  469. return false;
  470. }
  471. };
  472. template<> struct clear_element<1> {
  473. template<typename P> static void clear_this(P &p) {
  474. (void)tbb::flow::get<0>(p).successors().clear();
  475. }
  476. template<typename P> static bool this_empty(P &p) {
  477. return tbb::flow::get<0>(p).successors().empty();
  478. }
  479. };
  480. #if TBB_DEPRECATED_FLOW_NODE_EXTRACTION
  481. // helper templates to extract the output ports of an multifunction_node from graph
  482. template<int N> struct extract_element {
  483. template<typename P> static void extract_this(P &p) {
  484. (void)tbb::flow::get<N-1>(p).successors().built_successors().sender_extract(tbb::flow::get<N-1>(p));
  485. extract_element<N-1>::extract_this(p);
  486. }
  487. };
  488. template<> struct extract_element<1> {
  489. template<typename P> static void extract_this(P &p) {
  490. (void)tbb::flow::get<0>(p).successors().built_successors().sender_extract(tbb::flow::get<0>(p));
  491. }
  492. };
  493. #endif
  494. template <typename OutputTuple>
  495. struct init_output_ports {
  496. #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT
  497. template <typename... Args>
  498. static OutputTuple call(graph& g, const tbb::flow::tuple<Args...>&) {
  499. return OutputTuple(Args(g)...);
  500. }
  501. #else // __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT
  502. template <typename T1>
  503. static OutputTuple call(graph& g, const tbb::flow::tuple<T1>&) {
  504. return OutputTuple(T1(g));
  505. }
  506. template <typename T1, typename T2>
  507. static OutputTuple call(graph& g, const tbb::flow::tuple<T1, T2>&) {
  508. return OutputTuple(T1(g), T2(g));
  509. }
  510. template <typename T1, typename T2, typename T3>
  511. static OutputTuple call(graph& g, const tbb::flow::tuple<T1, T2, T3>&) {
  512. return OutputTuple(T1(g), T2(g), T3(g));
  513. }
  514. template <typename T1, typename T2, typename T3, typename T4>
  515. static OutputTuple call(graph& g, const tbb::flow::tuple<T1, T2, T3, T4>&) {
  516. return OutputTuple(T1(g), T2(g), T3(g), T4(g));
  517. }
  518. template <typename T1, typename T2, typename T3, typename T4, typename T5>
  519. static OutputTuple call(graph& g, const tbb::flow::tuple<T1, T2, T3, T4, T5>&) {
  520. return OutputTuple(T1(g), T2(g), T3(g), T4(g), T5(g));
  521. }
  522. #if __TBB_VARIADIC_MAX >= 6
  523. template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
  524. static OutputTuple call(graph& g, const tbb::flow::tuple<T1, T2, T3, T4, T5, T6>&) {
  525. return OutputTuple(T1(g), T2(g), T3(g), T4(g), T5(g), T6(g));
  526. }
  527. #endif
  528. #if __TBB_VARIADIC_MAX >= 7
  529. template <typename T1, typename T2, typename T3, typename T4,
  530. typename T5, typename T6, typename T7>
  531. static OutputTuple call(graph& g,
  532. const tbb::flow::tuple<T1, T2, T3, T4, T5, T6, T7>&) {
  533. return OutputTuple(T1(g), T2(g), T3(g), T4(g), T5(g), T6(g), T7(g));
  534. }
  535. #endif
  536. #if __TBB_VARIADIC_MAX >= 8
  537. template <typename T1, typename T2, typename T3, typename T4,
  538. typename T5, typename T6, typename T7, typename T8>
  539. static OutputTuple call(graph& g,
  540. const tbb::flow::tuple<T1, T2, T3, T4, T5, T6, T7, T8>&) {
  541. return OutputTuple(T1(g), T2(g), T3(g), T4(g), T5(g), T6(g), T7(g), T8(g));
  542. }
  543. #endif
  544. #if __TBB_VARIADIC_MAX >= 9
  545. template <typename T1, typename T2, typename T3, typename T4,
  546. typename T5, typename T6, typename T7, typename T8, typename T9>
  547. static OutputTuple call(graph& g,
  548. const tbb::flow::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9>&) {
  549. return OutputTuple(T1(g), T2(g), T3(g), T4(g), T5(g), T6(g), T7(g), T8(g), T9(g));
  550. }
  551. #endif
  552. #if __TBB_VARIADIC_MAX >= 9
  553. template <typename T1, typename T2, typename T3, typename T4, typename T5,
  554. typename T6, typename T7, typename T8, typename T9, typename T10>
  555. static OutputTuple call(graph& g,
  556. const tbb::flow::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>&) {
  557. return OutputTuple(T1(g), T2(g), T3(g), T4(g), T5(g), T6(g), T7(g), T8(g), T9(g), T10(g));
  558. }
  559. #endif
  560. #endif // __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT
  561. }; // struct init_output_ports
  562. //! Implements methods for a function node that takes a type Input as input
  563. // and has a tuple of output ports specified.
  564. template< typename Input, typename OutputPortSet, typename Policy, typename A>
  565. class multifunction_input : public function_input_base<Input, Policy, A, multifunction_input<Input,OutputPortSet,Policy,A> > {
  566. public:
  567. static const int N = tbb::flow::tuple_size<OutputPortSet>::value;
  568. typedef Input input_type;
  569. typedef OutputPortSet output_ports_type;
  570. typedef multifunction_body<input_type, output_ports_type> multifunction_body_type;
  571. typedef multifunction_input<Input, OutputPortSet, Policy, A> my_class;
  572. typedef function_input_base<Input, Policy, A, my_class> base_type;
  573. typedef function_input_queue<input_type, A> input_queue_type;
  574. // constructor
  575. template<typename Body>
  576. multifunction_input(graph &g, size_t max_concurrency,
  577. __TBB_FLOW_GRAPH_PRIORITY_ARG1(Body& body, node_priority_t priority)
  578. ) : base_type(g, __TBB_FLOW_GRAPH_PRIORITY_ARG1(max_concurrency, priority))
  579. , my_body( new internal::multifunction_body_leaf<input_type, output_ports_type, Body>(body) )
  580. , my_init_body( new internal::multifunction_body_leaf<input_type, output_ports_type, Body>(body) )
  581. , my_output_ports(init_output_ports<output_ports_type>::call(g, my_output_ports)){
  582. }
  583. //! Copy constructor
  584. multifunction_input( const multifunction_input& src ) :
  585. base_type(src),
  586. my_body( src.my_init_body->clone() ),
  587. my_init_body(src.my_init_body->clone() ),
  588. my_output_ports( init_output_ports<output_ports_type>::call(src.my_graph_ref, my_output_ports) ) {
  589. }
  590. ~multifunction_input() {
  591. delete my_body;
  592. delete my_init_body;
  593. }
  594. template< typename Body >
  595. Body copy_function_object() {
  596. multifunction_body_type &body_ref = *this->my_body;
  597. return *static_cast<Body*>(dynamic_cast< internal::multifunction_body_leaf<input_type, output_ports_type, Body> & >(body_ref).get_body_ptr());
  598. }
  599. // for multifunction nodes we do not have a single successor as such. So we just tell
  600. // the task we were successful.
  601. //TODO: consider moving common parts with implementation in function_input into separate function
  602. task * apply_body_impl_bypass( const input_type &i) {
  603. tbb::internal::fgt_begin_body( my_body );
  604. (*my_body)(i, my_output_ports);
  605. tbb::internal::fgt_end_body( my_body );
  606. task* ttask = NULL;
  607. if(base_type::my_max_concurrency != 0) {
  608. ttask = base_type::try_get_postponed_task(i);
  609. }
  610. return ttask ? ttask : SUCCESSFULLY_ENQUEUED;
  611. }
  612. output_ports_type &output_ports(){ return my_output_ports; }
  613. protected:
  614. #if TBB_DEPRECATED_FLOW_NODE_EXTRACTION
  615. void extract() {
  616. extract_element<N>::extract_this(my_output_ports);
  617. }
  618. #endif
  619. void reset(reset_flags f) {
  620. base_type::reset_function_input_base(f);
  621. if(f & rf_clear_edges)clear_element<N>::clear_this(my_output_ports);
  622. if(f & rf_reset_bodies) {
  623. multifunction_body_type *tmp = my_init_body->clone();
  624. delete my_body;
  625. my_body = tmp;
  626. }
  627. __TBB_ASSERT(!(f & rf_clear_edges) || clear_element<N>::this_empty(my_output_ports), "multifunction_node reset failed");
  628. }
  629. multifunction_body_type *my_body;
  630. multifunction_body_type *my_init_body;
  631. output_ports_type my_output_ports;
  632. }; // multifunction_input
  633. // template to refer to an output port of a multifunction_node
  634. template<size_t N, typename MOP>
  635. typename tbb::flow::tuple_element<N, typename MOP::output_ports_type>::type &output_port(MOP &op) {
  636. return tbb::flow::get<N>(op.output_ports());
  637. }
  638. inline void check_task_and_spawn(graph& g, task* t) {
  639. if (t && t != SUCCESSFULLY_ENQUEUED) {
  640. internal::spawn_in_graph_arena(g, *t);
  641. }
  642. }
  643. // helper structs for split_node
  644. template<int N>
  645. struct emit_element {
  646. template<typename T, typename P>
  647. static task* emit_this(graph& g, const T &t, P &p) {
  648. // TODO: consider to collect all the tasks in task_list and spawn them all at once
  649. task* last_task = tbb::flow::get<N-1>(p).try_put_task(tbb::flow::get<N-1>(t));
  650. check_task_and_spawn(g, last_task);
  651. return emit_element<N-1>::emit_this(g,t,p);
  652. }
  653. };
  654. template<>
  655. struct emit_element<1> {
  656. template<typename T, typename P>
  657. static task* emit_this(graph& g, const T &t, P &p) {
  658. task* last_task = tbb::flow::get<0>(p).try_put_task(tbb::flow::get<0>(t));
  659. check_task_and_spawn(g, last_task);
  660. return SUCCESSFULLY_ENQUEUED;
  661. }
  662. };
  663. //! Implements methods for an executable node that takes continue_msg as input
  664. template< typename Output, typename Policy>
  665. class continue_input : public continue_receiver {
  666. public:
  667. //! The input type of this receiver
  668. typedef continue_msg input_type;
  669. //! The output type of this receiver
  670. typedef Output output_type;
  671. typedef function_body<input_type, output_type> function_body_type;
  672. typedef continue_input<output_type, Policy> class_type;
  673. template< typename Body >
  674. continue_input( graph &g, __TBB_FLOW_GRAPH_PRIORITY_ARG1(Body& body, node_priority_t priority) )
  675. : continue_receiver(__TBB_FLOW_GRAPH_PRIORITY_ARG1(/*number_of_predecessors=*/0, priority))
  676. , my_graph_ref(g)
  677. , my_body( new internal::function_body_leaf< input_type, output_type, Body>(body) )
  678. , my_init_body( new internal::function_body_leaf< input_type, output_type, Body>(body) )
  679. { }
  680. template< typename Body >
  681. continue_input( graph &g, int number_of_predecessors,
  682. __TBB_FLOW_GRAPH_PRIORITY_ARG1(Body& body, node_priority_t priority)
  683. ) : continue_receiver( __TBB_FLOW_GRAPH_PRIORITY_ARG1(number_of_predecessors, priority) )
  684. , my_graph_ref(g)
  685. , my_body( new internal::function_body_leaf< input_type, output_type, Body>(body) )
  686. , my_init_body( new internal::function_body_leaf< input_type, output_type, Body>(body) )
  687. { }
  688. continue_input( const continue_input& src ) : continue_receiver(src),
  689. my_graph_ref(src.my_graph_ref),
  690. my_body( src.my_init_body->clone() ),
  691. my_init_body( src.my_init_body->clone() ) {}
  692. ~continue_input() {
  693. delete my_body;
  694. delete my_init_body;
  695. }
  696. template< typename Body >
  697. Body copy_function_object() {
  698. function_body_type &body_ref = *my_body;
  699. return dynamic_cast< internal::function_body_leaf<input_type, output_type, Body> & >(body_ref).get_body();
  700. }
  701. void reset_receiver( reset_flags f) __TBB_override {
  702. continue_receiver::reset_receiver(f);
  703. if(f & rf_reset_bodies) {
  704. function_body_type *tmp = my_init_body->clone();
  705. delete my_body;
  706. my_body = tmp;
  707. }
  708. }
  709. protected:
  710. graph& my_graph_ref;
  711. function_body_type *my_body;
  712. function_body_type *my_init_body;
  713. virtual broadcast_cache<output_type > &successors() = 0;
  714. friend class apply_body_task_bypass< class_type, continue_msg >;
  715. //! Applies the body to the provided input
  716. task *apply_body_bypass( input_type ) {
  717. // There is an extra copied needed to capture the
  718. // body execution without the try_put
  719. tbb::internal::fgt_begin_body( my_body );
  720. output_type v = (*my_body)( continue_msg() );
  721. tbb::internal::fgt_end_body( my_body );
  722. return successors().try_put_task( v );
  723. }
  724. task* execute() __TBB_override {
  725. if(!internal::is_graph_active(my_graph_ref)) {
  726. return NULL;
  727. }
  728. #if _MSC_VER && !__INTEL_COMPILER
  729. #pragma warning (push)
  730. #pragma warning (disable: 4127) /* suppress conditional expression is constant */
  731. #endif
  732. if(internal::has_policy<lightweight, Policy>::value) {
  733. #if _MSC_VER && !__INTEL_COMPILER
  734. #pragma warning (pop)
  735. #endif
  736. return apply_body_bypass( continue_msg() );
  737. }
  738. else {
  739. return new ( task::allocate_additional_child_of( *(my_graph_ref.root_task()) ) )
  740. apply_body_task_bypass< class_type, continue_msg >(
  741. *this, __TBB_FLOW_GRAPH_PRIORITY_ARG1(continue_msg(), my_priority) );
  742. }
  743. }
  744. graph& graph_reference() const __TBB_override {
  745. return my_graph_ref;
  746. }
  747. }; // continue_input
  748. //! Implements methods for both executable and function nodes that puts Output to its successors
  749. template< typename Output >
  750. class function_output : public sender<Output> {
  751. public:
  752. template<int N> friend struct clear_element;
  753. typedef Output output_type;
  754. typedef typename sender<output_type>::successor_type successor_type;
  755. typedef broadcast_cache<output_type> broadcast_cache_type;
  756. #if TBB_DEPRECATED_FLOW_NODE_EXTRACTION
  757. typedef typename sender<output_type>::built_successors_type built_successors_type;
  758. typedef typename sender<output_type>::successor_list_type successor_list_type;
  759. #endif
  760. function_output( graph& g) : my_graph_ref(g) { my_successors.set_owner(this); }
  761. function_output(const function_output & other) : sender<output_type>(), my_graph_ref(other.my_graph_ref) {
  762. my_successors.set_owner(this);
  763. }
  764. //! Adds a new successor to this node
  765. bool register_successor( successor_type &r ) __TBB_override {
  766. successors().register_successor( r );
  767. return true;
  768. }
  769. //! Removes a successor from this node
  770. bool remove_successor( successor_type &r ) __TBB_override {
  771. successors().remove_successor( r );
  772. return true;
  773. }
  774. #if TBB_DEPRECATED_FLOW_NODE_EXTRACTION
  775. built_successors_type &built_successors() __TBB_override { return successors().built_successors(); }
  776. void internal_add_built_successor( successor_type &r) __TBB_override {
  777. successors().internal_add_built_successor( r );
  778. }
  779. void internal_delete_built_successor( successor_type &r) __TBB_override {
  780. successors().internal_delete_built_successor( r );
  781. }
  782. size_t successor_count() __TBB_override {
  783. return successors().successor_count();
  784. }
  785. void copy_successors( successor_list_type &v) __TBB_override {
  786. successors().copy_successors(v);
  787. }
  788. #endif /* TBB_DEPRECATED_FLOW_NODE_EXTRACTION */
  789. // for multifunction_node. The function_body that implements
  790. // the node will have an input and an output tuple of ports. To put
  791. // an item to a successor, the body should
  792. //
  793. // get<I>(output_ports).try_put(output_value);
  794. //
  795. // if task pointer is returned will always spawn and return true, else
  796. // return value will be bool returned from successors.try_put.
  797. task *try_put_task(const output_type &i) { // not a virtual method in this class
  798. return my_successors.try_put_task(i);
  799. }
  800. broadcast_cache_type &successors() { return my_successors; }
  801. graph& graph_reference() const { return my_graph_ref; }
  802. protected:
  803. broadcast_cache_type my_successors;
  804. graph& my_graph_ref;
  805. }; // function_output
  806. template< typename Output >
  807. class multifunction_output : public function_output<Output> {
  808. public:
  809. typedef Output output_type;
  810. typedef function_output<output_type> base_type;
  811. using base_type::my_successors;
  812. multifunction_output(graph& g) : base_type(g) {my_successors.set_owner(this);}
  813. multifunction_output( const multifunction_output& other) : base_type(other.my_graph_ref) { my_successors.set_owner(this); }
  814. bool try_put(const output_type &i) {
  815. task *res = try_put_task(i);
  816. if(!res) return false;
  817. if(res != SUCCESSFULLY_ENQUEUED) {
  818. FLOW_SPAWN(*res); // TODO: Spawn task inside arena
  819. }
  820. return true;
  821. }
  822. using base_type::graph_reference;
  823. protected:
  824. task* try_put_task(const output_type &i) {
  825. return my_successors.try_put_task(i);
  826. }
  827. template <int N> friend struct emit_element;
  828. }; // multifunction_output
  829. //composite_node
  830. #if __TBB_FLOW_GRAPH_CPP11_FEATURES
  831. template<typename CompositeType>
  832. void add_nodes_impl(CompositeType*, bool) {}
  833. template< typename CompositeType, typename NodeType1, typename... NodeTypes >
  834. void add_nodes_impl(CompositeType *c_node, bool visible, const NodeType1& n1, const NodeTypes&... n) {
  835. void *addr = const_cast<NodeType1 *>(&n1);
  836. fgt_alias_port(c_node, addr, visible);
  837. add_nodes_impl(c_node, visible, n...);
  838. }
  839. #endif
  840. } // internal
  841. #endif // __TBB__flow_graph_node_impl_H