_flow_graph_body_impl.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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_body_impl_H
  14. #define __TBB__flow_graph_body_impl_H
  15. #include "tbb/internal/_template_helpers.h"
  16. #ifndef __TBB_flow_graph_H
  17. #error Do not #include this internal file directly; use public TBB headers instead.
  18. #endif
  19. // included in namespace tbb::flow::interfaceX (in flow_graph.h)
  20. namespace internal {
  21. typedef tbb::internal::uint64_t tag_value;
  22. using tbb::internal::strip;
  23. #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT
  24. template<typename ... Policies> struct Policy {};
  25. template<typename ... Policies> struct has_policy;
  26. template<typename ExpectedPolicy, typename FirstPolicy, typename ...Policies>
  27. struct has_policy<ExpectedPolicy, FirstPolicy, Policies...> :
  28. tbb::internal::bool_constant<has_policy<ExpectedPolicy, FirstPolicy>::value ||
  29. has_policy<ExpectedPolicy, Policies...>::value> {};
  30. template<typename ExpectedPolicy, typename SinglePolicy>
  31. struct has_policy<ExpectedPolicy, SinglePolicy> :
  32. tbb::internal::bool_constant<tbb::internal::is_same_type<ExpectedPolicy, SinglePolicy>::value> {};
  33. template<typename ExpectedPolicy, typename ...Policies>
  34. struct has_policy<ExpectedPolicy, Policy<Policies...> > : has_policy<ExpectedPolicy, Policies...> {};
  35. #else
  36. template<typename P1, typename P2 = void> struct Policy {};
  37. template<typename ExpectedPolicy, typename SinglePolicy>
  38. struct has_policy : tbb::internal::bool_constant<tbb::internal::is_same_type<ExpectedPolicy, SinglePolicy>::value> {};
  39. template<typename ExpectedPolicy, typename P>
  40. struct has_policy<ExpectedPolicy, Policy<P> > : has_policy<ExpectedPolicy, P> {};
  41. template<typename ExpectedPolicy, typename P1, typename P2>
  42. struct has_policy<ExpectedPolicy, Policy<P1, P2> > :
  43. tbb::internal::bool_constant<has_policy<ExpectedPolicy, P1>::value || has_policy<ExpectedPolicy, P2>::value> {};
  44. #endif
  45. namespace graph_policy_namespace {
  46. struct rejecting { };
  47. struct reserving { };
  48. struct queueing { };
  49. struct lightweight { };
  50. // K == type of field used for key-matching. Each tag-matching port will be provided
  51. // functor that, given an object accepted by the port, will return the
  52. /// field of type K being used for matching.
  53. template<typename K, typename KHash=tbb_hash_compare<typename strip<K>::type > >
  54. struct key_matching {
  55. typedef K key_type;
  56. typedef typename strip<K>::type base_key_type;
  57. typedef KHash hash_compare_type;
  58. };
  59. // old tag_matching join's new specifier
  60. typedef key_matching<tag_value> tag_matching;
  61. // Aliases for Policy combinations
  62. typedef interface11::internal::Policy<queueing, lightweight> queueing_lightweight;
  63. typedef interface11::internal::Policy<rejecting, lightweight> rejecting_lightweight;
  64. } // namespace graph_policy_namespace
  65. // -------------- function_body containers ----------------------
  66. //! A functor that takes no input and generates a value of type Output
  67. template< typename Output >
  68. class input_body : tbb::internal::no_assign {
  69. public:
  70. virtual ~input_body() {}
  71. #if TBB_DEPRECATED_INPUT_NODE_BODY
  72. virtual bool operator()(Output &output) = 0;
  73. #else
  74. virtual Output operator()(flow_control& fc) = 0;
  75. #endif
  76. virtual input_body* clone() = 0;
  77. };
  78. template <typename Body>
  79. void check_input_node_body_input_type_impl(Body) {
  80. __TBB_STATIC_ASSERT((tbb::internal::is_same_type<typename tbb::internal::body_arg_detector<Body>::arg_type, flow_control&>::value),
  81. "TBB Warning: input_node body requirements have been changed."
  82. "To temporarily enforce deprecated API specify TBB_DEPRECATED_INPUT_NODE_BODY.");
  83. }
  84. template <typename Body>
  85. void check_input_node_body_input_type(Body) {
  86. check_input_node_body_input_type_impl(&Body::operator());
  87. }
  88. template <typename ReturnType, typename T>
  89. void check_input_node_body_input_type(ReturnType(*)(T)) {
  90. __TBB_STATIC_ASSERT((tbb::internal::is_same_type<T, flow_control&>::value),
  91. "TBB Warning: input_node body requirements have been changed."
  92. "To temporarily enforce deprecated API specify TBB_DEPRECATED_INPUT_NODE_BODY.");
  93. }
  94. //! The leaf for input_body
  95. template< typename Output, typename Body>
  96. class input_body_leaf : public input_body<Output> {
  97. public:
  98. input_body_leaf( const Body &_body ) : body(_body) { }
  99. #if TBB_DEPRECATED_INPUT_NODE_BODY
  100. bool operator()(Output &output) __TBB_override { return body( output ); }
  101. #else
  102. Output operator()(flow_control& fc) __TBB_override {
  103. check_input_node_body_input_type(body);
  104. return body(fc);
  105. }
  106. #endif
  107. input_body_leaf* clone() __TBB_override {
  108. return new input_body_leaf< Output, Body >(body);
  109. }
  110. Body get_body() { return body; }
  111. private:
  112. Body body;
  113. };
  114. template< typename Output >
  115. class source_body : tbb::internal::no_assign {
  116. public:
  117. virtual ~source_body() {}
  118. virtual bool operator()(Output &output) = 0;
  119. virtual source_body* clone() = 0;
  120. };
  121. //! The leaf for source_body
  122. template< typename Output, typename Body>
  123. class source_body_leaf : public source_body<Output> {
  124. public:
  125. source_body_leaf( const Body &_body ) : body(_body) { }
  126. bool operator()(Output &output) __TBB_override { return body( output ); }
  127. source_body_leaf* clone() __TBB_override {
  128. return new source_body_leaf< Output, Body >(body);
  129. }
  130. Body get_body() { return body; }
  131. private:
  132. Body body;
  133. };
  134. //! A functor that takes an Input and generates an Output
  135. template< typename Input, typename Output >
  136. class function_body : tbb::internal::no_assign {
  137. public:
  138. virtual ~function_body() {}
  139. virtual Output operator()(const Input &input) = 0;
  140. virtual function_body* clone() = 0;
  141. };
  142. //! the leaf for function_body
  143. template <typename Input, typename Output, typename B>
  144. class function_body_leaf : public function_body< Input, Output > {
  145. public:
  146. function_body_leaf( const B &_body ) : body(_body) { }
  147. Output operator()(const Input &i) __TBB_override { return body(i); }
  148. B get_body() { return body; }
  149. function_body_leaf* clone() __TBB_override {
  150. return new function_body_leaf< Input, Output, B >(body);
  151. }
  152. private:
  153. B body;
  154. };
  155. //! the leaf for function_body specialized for Input and output of continue_msg
  156. template <typename B>
  157. class function_body_leaf< continue_msg, continue_msg, B> : public function_body< continue_msg, continue_msg > {
  158. public:
  159. function_body_leaf( const B &_body ) : body(_body) { }
  160. continue_msg operator()( const continue_msg &i ) __TBB_override {
  161. body(i);
  162. return i;
  163. }
  164. B get_body() { return body; }
  165. function_body_leaf* clone() __TBB_override {
  166. return new function_body_leaf< continue_msg, continue_msg, B >(body);
  167. }
  168. private:
  169. B body;
  170. };
  171. //! the leaf for function_body specialized for Output of continue_msg
  172. template <typename Input, typename B>
  173. class function_body_leaf< Input, continue_msg, B> : public function_body< Input, continue_msg > {
  174. public:
  175. function_body_leaf( const B &_body ) : body(_body) { }
  176. continue_msg operator()(const Input &i) __TBB_override {
  177. body(i);
  178. return continue_msg();
  179. }
  180. B get_body() { return body; }
  181. function_body_leaf* clone() __TBB_override {
  182. return new function_body_leaf< Input, continue_msg, B >(body);
  183. }
  184. private:
  185. B body;
  186. };
  187. //! the leaf for function_body specialized for Input of continue_msg
  188. template <typename Output, typename B>
  189. class function_body_leaf< continue_msg, Output, B > : public function_body< continue_msg, Output > {
  190. public:
  191. function_body_leaf( const B &_body ) : body(_body) { }
  192. Output operator()(const continue_msg &i) __TBB_override {
  193. return body(i);
  194. }
  195. B get_body() { return body; }
  196. function_body_leaf* clone() __TBB_override {
  197. return new function_body_leaf< continue_msg, Output, B >(body);
  198. }
  199. private:
  200. B body;
  201. };
  202. //! function_body that takes an Input and a set of output ports
  203. template<typename Input, typename OutputSet>
  204. class multifunction_body : tbb::internal::no_assign {
  205. public:
  206. virtual ~multifunction_body () {}
  207. virtual void operator()(const Input &/* input*/, OutputSet &/*oset*/) = 0;
  208. virtual multifunction_body* clone() = 0;
  209. virtual void* get_body_ptr() = 0;
  210. };
  211. //! leaf for multifunction. OutputSet can be a std::tuple or a vector.
  212. template<typename Input, typename OutputSet, typename B >
  213. class multifunction_body_leaf : public multifunction_body<Input, OutputSet> {
  214. public:
  215. multifunction_body_leaf(const B &_body) : body(_body) { }
  216. void operator()(const Input &input, OutputSet &oset) __TBB_override {
  217. body(input, oset); // body may explicitly put() to one or more of oset.
  218. }
  219. void* get_body_ptr() __TBB_override { return &body; }
  220. multifunction_body_leaf* clone() __TBB_override {
  221. return new multifunction_body_leaf<Input, OutputSet,B>(body);
  222. }
  223. private:
  224. B body;
  225. };
  226. // ------ function bodies for hash_buffers and key-matching joins.
  227. template<typename Input, typename Output>
  228. class type_to_key_function_body : tbb::internal::no_assign {
  229. public:
  230. virtual ~type_to_key_function_body() {}
  231. virtual Output operator()(const Input &input) = 0; // returns an Output
  232. virtual type_to_key_function_body* clone() = 0;
  233. };
  234. // specialization for ref output
  235. template<typename Input, typename Output>
  236. class type_to_key_function_body<Input,Output&> : tbb::internal::no_assign {
  237. public:
  238. virtual ~type_to_key_function_body() {}
  239. virtual const Output & operator()(const Input &input) = 0; // returns a const Output&
  240. virtual type_to_key_function_body* clone() = 0;
  241. };
  242. template <typename Input, typename Output, typename B>
  243. class type_to_key_function_body_leaf : public type_to_key_function_body<Input, Output> {
  244. public:
  245. type_to_key_function_body_leaf( const B &_body ) : body(_body) { }
  246. Output operator()(const Input &i) __TBB_override { return body(i); }
  247. B get_body() { return body; }
  248. type_to_key_function_body_leaf* clone() __TBB_override {
  249. return new type_to_key_function_body_leaf< Input, Output, B>(body);
  250. }
  251. private:
  252. B body;
  253. };
  254. template <typename Input, typename Output, typename B>
  255. class type_to_key_function_body_leaf<Input,Output&,B> : public type_to_key_function_body< Input, Output&> {
  256. public:
  257. type_to_key_function_body_leaf( const B &_body ) : body(_body) { }
  258. const Output& operator()(const Input &i) __TBB_override {
  259. return body(i);
  260. }
  261. B get_body() { return body; }
  262. type_to_key_function_body_leaf* clone() __TBB_override {
  263. return new type_to_key_function_body_leaf< Input, Output&, B>(body);
  264. }
  265. private:
  266. B body;
  267. };
  268. // --------------------------- end of function_body containers ------------------------
  269. // --------------------------- node task bodies ---------------------------------------
  270. //! A task that calls a node's forward_task function
  271. template< typename NodeType >
  272. class forward_task_bypass : public graph_task {
  273. NodeType &my_node;
  274. public:
  275. forward_task_bypass( NodeType &n
  276. #if __TBB_PREVIEW_FLOW_GRAPH_PRIORITIES
  277. , node_priority_t node_priority = no_priority
  278. ) : graph_task(node_priority),
  279. #else
  280. ) :
  281. #endif
  282. my_node(n) {}
  283. task *execute() __TBB_override {
  284. task * new_task = my_node.forward_task();
  285. if (new_task == SUCCESSFULLY_ENQUEUED) new_task = NULL;
  286. return new_task;
  287. }
  288. };
  289. //! A task that calls a node's apply_body_bypass function, passing in an input of type Input
  290. // return the task* unless it is SUCCESSFULLY_ENQUEUED, in which case return NULL
  291. template< typename NodeType, typename Input >
  292. class apply_body_task_bypass : public graph_task {
  293. NodeType &my_node;
  294. Input my_input;
  295. public:
  296. apply_body_task_bypass( NodeType &n, const Input &i
  297. #if __TBB_PREVIEW_FLOW_GRAPH_PRIORITIES
  298. , node_priority_t node_priority = no_priority
  299. ) : graph_task(node_priority),
  300. #else
  301. ) :
  302. #endif
  303. my_node(n), my_input(i) {}
  304. task *execute() __TBB_override {
  305. task * next_task = my_node.apply_body_bypass( my_input );
  306. if(next_task == SUCCESSFULLY_ENQUEUED) next_task = NULL;
  307. return next_task;
  308. }
  309. };
  310. //! A task that calls a node's apply_body_bypass function with no input
  311. template< typename NodeType >
  312. class source_task_bypass : public graph_task {
  313. NodeType &my_node;
  314. public:
  315. source_task_bypass( NodeType &n ) : my_node(n) {}
  316. task *execute() __TBB_override {
  317. task *new_task = my_node.apply_body_bypass( );
  318. if(new_task == SUCCESSFULLY_ENQUEUED) return NULL;
  319. return new_task;
  320. }
  321. };
  322. // ------------------------ end of node task bodies -----------------------------------
  323. //! An empty functor that takes an Input and returns a default constructed Output
  324. template< typename Input, typename Output >
  325. struct empty_body {
  326. Output operator()( const Input & ) const { return Output(); }
  327. };
  328. template<typename T, typename DecrementType, typename DummyType = void>
  329. class decrementer;
  330. template<typename T, typename DecrementType>
  331. class decrementer<T, DecrementType,
  332. typename tbb::internal::enable_if<
  333. tbb::internal::is_integral<DecrementType>::value, void>::type
  334. > : public receiver<DecrementType>, tbb::internal::no_copy {
  335. T* my_node;
  336. protected:
  337. task* try_put_task( const DecrementType& value ) __TBB_override {
  338. task* result = my_node->decrement_counter( value );
  339. if( !result )
  340. result = SUCCESSFULLY_ENQUEUED;
  341. return result;
  342. }
  343. graph& graph_reference() const __TBB_override {
  344. return my_node->my_graph;
  345. }
  346. template<typename U, typename V> friend class tbb::flow::interface11::limiter_node;
  347. void reset_receiver( reset_flags f ) __TBB_override {
  348. #if TBB_DEPRECATED_FLOW_NODE_EXTRACTION
  349. if (f & rf_clear_edges)
  350. my_built_predecessors.clear();
  351. #else
  352. tbb::internal::suppress_unused_warning( f );
  353. #endif
  354. }
  355. public:
  356. // Since decrementer does not make use of possibly unconstructed owner inside its
  357. // constructor, my_node can be directly initialized with 'this' pointer passed from the
  358. // owner, hence making method 'set_owner' needless.
  359. decrementer() : my_node(NULL) {}
  360. void set_owner( T *node ) { my_node = node; }
  361. #if TBB_DEPRECATED_FLOW_NODE_EXTRACTION
  362. spin_mutex my_mutex;
  363. //! The predecessor type for this node
  364. typedef typename receiver<DecrementType>::predecessor_type predecessor_type;
  365. typedef internal::edge_container<predecessor_type> built_predecessors_type;
  366. typedef typename built_predecessors_type::edge_list_type predecessor_list_type;
  367. built_predecessors_type &built_predecessors() __TBB_override { return my_built_predecessors; }
  368. void internal_add_built_predecessor( predecessor_type &s) __TBB_override {
  369. spin_mutex::scoped_lock l(my_mutex);
  370. my_built_predecessors.add_edge( s );
  371. }
  372. void internal_delete_built_predecessor( predecessor_type &s) __TBB_override {
  373. spin_mutex::scoped_lock l(my_mutex);
  374. my_built_predecessors.delete_edge(s);
  375. }
  376. void copy_predecessors( predecessor_list_type &v) __TBB_override {
  377. spin_mutex::scoped_lock l(my_mutex);
  378. my_built_predecessors.copy_edges(v);
  379. }
  380. size_t predecessor_count() __TBB_override {
  381. spin_mutex::scoped_lock l(my_mutex);
  382. return my_built_predecessors.edge_count();
  383. }
  384. protected:
  385. built_predecessors_type my_built_predecessors;
  386. #endif /* TBB_DEPRECATED_FLOW_NODE_EXTRACTION */
  387. };
  388. template<typename T>
  389. class decrementer<T, continue_msg, void> : public continue_receiver, tbb::internal::no_copy {
  390. T *my_node;
  391. task *execute() __TBB_override {
  392. return my_node->decrement_counter( 1 );
  393. }
  394. protected:
  395. graph& graph_reference() const __TBB_override {
  396. return my_node->my_graph;
  397. }
  398. public:
  399. typedef continue_msg input_type;
  400. typedef continue_msg output_type;
  401. decrementer( int number_of_predecessors = 0 )
  402. : continue_receiver(
  403. __TBB_FLOW_GRAPH_PRIORITY_ARG1(number_of_predecessors, tbb::flow::internal::no_priority)
  404. )
  405. // Since decrementer does not make use of possibly unconstructed owner inside its
  406. // constructor, my_node can be directly initialized with 'this' pointer passed from the
  407. // owner, hence making method 'set_owner' needless.
  408. , my_node(NULL)
  409. {}
  410. void set_owner( T *node ) { my_node = node; }
  411. };
  412. } // namespace internal
  413. #endif // __TBB__flow_graph_body_impl_H