_flow_graph_indexer_impl.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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_indexer_impl_H
  14. #define __TBB__flow_graph_indexer_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_types_impl.h"
  19. namespace internal {
  20. // Output of the indexer_node is a tbb::flow::tagged_msg, and will be of
  21. // the form tagged_msg<tag, result>
  22. // where the value of tag will indicate which result was put to the
  23. // successor.
  24. template<typename IndexerNodeBaseType, typename T, size_t K>
  25. task* do_try_put(const T &v, void *p) {
  26. typename IndexerNodeBaseType::output_type o(K, v);
  27. return reinterpret_cast<IndexerNodeBaseType *>(p)->try_put_task(&o);
  28. }
  29. template<typename TupleTypes,int N>
  30. struct indexer_helper {
  31. template<typename IndexerNodeBaseType, typename PortTuple>
  32. static inline void set_indexer_node_pointer(PortTuple &my_input, IndexerNodeBaseType *p, graph& g) {
  33. typedef typename tuple_element<N-1, TupleTypes>::type T;
  34. task *(*indexer_node_put_task)(const T&, void *) = do_try_put<IndexerNodeBaseType, T, N-1>;
  35. tbb::flow::get<N-1>(my_input).set_up(p, indexer_node_put_task, g);
  36. indexer_helper<TupleTypes,N-1>::template set_indexer_node_pointer<IndexerNodeBaseType,PortTuple>(my_input, p, g);
  37. }
  38. template<typename InputTuple>
  39. static inline void reset_inputs(InputTuple &my_input, reset_flags f) {
  40. indexer_helper<TupleTypes,N-1>::reset_inputs(my_input, f);
  41. tbb::flow::get<N-1>(my_input).reset_receiver(f);
  42. }
  43. #if TBB_DEPRECATED_FLOW_NODE_EXTRACTION
  44. template<typename InputTuple>
  45. static inline void extract(InputTuple &my_input) {
  46. indexer_helper<TupleTypes,N-1>::extract(my_input);
  47. tbb::flow::get<N-1>(my_input).extract_receiver();
  48. }
  49. #endif
  50. };
  51. template<typename TupleTypes>
  52. struct indexer_helper<TupleTypes,1> {
  53. template<typename IndexerNodeBaseType, typename PortTuple>
  54. static inline void set_indexer_node_pointer(PortTuple &my_input, IndexerNodeBaseType *p, graph& g) {
  55. typedef typename tuple_element<0, TupleTypes>::type T;
  56. task *(*indexer_node_put_task)(const T&, void *) = do_try_put<IndexerNodeBaseType, T, 0>;
  57. tbb::flow::get<0>(my_input).set_up(p, indexer_node_put_task, g);
  58. }
  59. template<typename InputTuple>
  60. static inline void reset_inputs(InputTuple &my_input, reset_flags f) {
  61. tbb::flow::get<0>(my_input).reset_receiver(f);
  62. }
  63. #if TBB_DEPRECATED_FLOW_NODE_EXTRACTION
  64. template<typename InputTuple>
  65. static inline void extract(InputTuple &my_input) {
  66. tbb::flow::get<0>(my_input).extract_receiver();
  67. }
  68. #endif
  69. };
  70. template<typename T>
  71. class indexer_input_port : public receiver<T> {
  72. private:
  73. void* my_indexer_ptr;
  74. typedef task* (* forward_function_ptr)(T const &, void* );
  75. forward_function_ptr my_try_put_task;
  76. #if TBB_DEPRECATED_FLOW_NODE_EXTRACTION
  77. spin_mutex my_pred_mutex;
  78. typedef typename receiver<T>::built_predecessors_type built_predecessors_type;
  79. built_predecessors_type my_built_predecessors;
  80. #endif /* TBB_DEPRECATED_FLOW_NODE_EXTRACTION */
  81. graph* my_graph;
  82. public:
  83. #if TBB_DEPRECATED_FLOW_NODE_EXTRACTION
  84. indexer_input_port() : my_pred_mutex(), my_graph(NULL) {}
  85. indexer_input_port( const indexer_input_port & other) : receiver<T>(), my_pred_mutex(), my_graph(other.my_graph) {
  86. }
  87. #endif /* TBB_DEPRECATED_FLOW_NODE_EXTRACTION */
  88. void set_up(void* p, forward_function_ptr f, graph& g) {
  89. my_indexer_ptr = p;
  90. my_try_put_task = f;
  91. my_graph = &g;
  92. }
  93. #if TBB_DEPRECATED_FLOW_NODE_EXTRACTION
  94. typedef typename receiver<T>::predecessor_list_type predecessor_list_type;
  95. typedef typename receiver<T>::predecessor_type predecessor_type;
  96. built_predecessors_type &built_predecessors() __TBB_override { return my_built_predecessors; }
  97. size_t predecessor_count() __TBB_override {
  98. spin_mutex::scoped_lock l(my_pred_mutex);
  99. return my_built_predecessors.edge_count();
  100. }
  101. void internal_add_built_predecessor(predecessor_type &p) __TBB_override {
  102. spin_mutex::scoped_lock l(my_pred_mutex);
  103. my_built_predecessors.add_edge(p);
  104. }
  105. void internal_delete_built_predecessor(predecessor_type &p) __TBB_override {
  106. spin_mutex::scoped_lock l(my_pred_mutex);
  107. my_built_predecessors.delete_edge(p);
  108. }
  109. void copy_predecessors( predecessor_list_type &v) __TBB_override {
  110. spin_mutex::scoped_lock l(my_pred_mutex);
  111. my_built_predecessors.copy_edges(v);
  112. }
  113. #endif /* TBB_DEPRECATED_FLOW_NODE_EXTRACTION */
  114. protected:
  115. template< typename R, typename B > friend class run_and_put_task;
  116. template<typename X, typename Y> friend class internal::broadcast_cache;
  117. template<typename X, typename Y> friend class internal::round_robin_cache;
  118. task *try_put_task(const T &v) __TBB_override {
  119. return my_try_put_task(v, my_indexer_ptr);
  120. }
  121. graph& graph_reference() const __TBB_override {
  122. return *my_graph;
  123. }
  124. public:
  125. #if TBB_DEPRECATED_FLOW_NODE_EXTRACTION
  126. void reset_receiver(reset_flags f) __TBB_override { if(f&rf_clear_edges) my_built_predecessors.clear(); }
  127. #else
  128. void reset_receiver(reset_flags /*f*/) __TBB_override { }
  129. #endif
  130. #if TBB_DEPRECATED_FLOW_NODE_EXTRACTION
  131. void extract_receiver() { my_built_predecessors.receiver_extract(*this); }
  132. #endif
  133. };
  134. template<typename InputTuple, typename OutputType, typename StructTypes>
  135. class indexer_node_FE {
  136. public:
  137. static const int N = tbb::flow::tuple_size<InputTuple>::value;
  138. typedef OutputType output_type;
  139. typedef InputTuple input_type;
  140. // Some versions of Intel(R) C++ Compiler fail to generate an implicit constructor for the class which has std::tuple as a member.
  141. indexer_node_FE() : my_inputs() {}
  142. input_type &input_ports() { return my_inputs; }
  143. protected:
  144. input_type my_inputs;
  145. };
  146. //! indexer_node_base
  147. template<typename InputTuple, typename OutputType, typename StructTypes>
  148. class indexer_node_base : public graph_node, public indexer_node_FE<InputTuple, OutputType,StructTypes>,
  149. public sender<OutputType> {
  150. protected:
  151. using graph_node::my_graph;
  152. public:
  153. static const size_t N = tbb::flow::tuple_size<InputTuple>::value;
  154. typedef OutputType output_type;
  155. typedef StructTypes tuple_types;
  156. typedef typename sender<output_type>::successor_type successor_type;
  157. typedef indexer_node_FE<InputTuple, output_type,StructTypes> input_ports_type;
  158. #if TBB_DEPRECATED_FLOW_NODE_EXTRACTION
  159. typedef typename sender<output_type>::built_successors_type built_successors_type;
  160. typedef typename sender<output_type>::successor_list_type successor_list_type;
  161. #endif
  162. private:
  163. // ----------- Aggregator ------------
  164. enum op_type { reg_succ, rem_succ, try__put_task
  165. #if TBB_DEPRECATED_FLOW_NODE_EXTRACTION
  166. , add_blt_succ, del_blt_succ,
  167. blt_succ_cnt, blt_succ_cpy
  168. #endif
  169. };
  170. typedef indexer_node_base<InputTuple,output_type,StructTypes> class_type;
  171. class indexer_node_base_operation : public aggregated_operation<indexer_node_base_operation> {
  172. public:
  173. char type;
  174. union {
  175. output_type const *my_arg;
  176. successor_type *my_succ;
  177. task *bypass_t;
  178. #if TBB_DEPRECATED_FLOW_NODE_EXTRACTION
  179. size_t cnt_val;
  180. successor_list_type *succv;
  181. #endif
  182. };
  183. indexer_node_base_operation(const output_type* e, op_type t) :
  184. type(char(t)), my_arg(e) {}
  185. indexer_node_base_operation(const successor_type &s, op_type t) : type(char(t)),
  186. my_succ(const_cast<successor_type *>(&s)) {}
  187. indexer_node_base_operation(op_type t) : type(char(t)) {}
  188. };
  189. typedef internal::aggregating_functor<class_type, indexer_node_base_operation> handler_type;
  190. friend class internal::aggregating_functor<class_type, indexer_node_base_operation>;
  191. aggregator<handler_type, indexer_node_base_operation> my_aggregator;
  192. void handle_operations(indexer_node_base_operation* op_list) {
  193. indexer_node_base_operation *current;
  194. while(op_list) {
  195. current = op_list;
  196. op_list = op_list->next;
  197. switch(current->type) {
  198. case reg_succ:
  199. my_successors.register_successor(*(current->my_succ));
  200. __TBB_store_with_release(current->status, SUCCEEDED);
  201. break;
  202. case rem_succ:
  203. my_successors.remove_successor(*(current->my_succ));
  204. __TBB_store_with_release(current->status, SUCCEEDED);
  205. break;
  206. case try__put_task: {
  207. current->bypass_t = my_successors.try_put_task(*(current->my_arg));
  208. __TBB_store_with_release(current->status, SUCCEEDED); // return of try_put_task actual return value
  209. }
  210. break;
  211. #if TBB_DEPRECATED_FLOW_NODE_EXTRACTION
  212. case add_blt_succ:
  213. my_successors.internal_add_built_successor(*(current->my_succ));
  214. __TBB_store_with_release(current->status, SUCCEEDED);
  215. break;
  216. case del_blt_succ:
  217. my_successors.internal_delete_built_successor(*(current->my_succ));
  218. __TBB_store_with_release(current->status, SUCCEEDED);
  219. break;
  220. case blt_succ_cnt:
  221. current->cnt_val = my_successors.successor_count();
  222. __TBB_store_with_release(current->status, SUCCEEDED);
  223. break;
  224. case blt_succ_cpy:
  225. my_successors.copy_successors(*(current->succv));
  226. __TBB_store_with_release(current->status, SUCCEEDED);
  227. break;
  228. #endif /* TBB_DEPRECATED_FLOW_NODE_EXTRACTION */
  229. }
  230. }
  231. }
  232. // ---------- end aggregator -----------
  233. public:
  234. indexer_node_base(graph& g) : graph_node(g), input_ports_type() {
  235. indexer_helper<StructTypes,N>::set_indexer_node_pointer(this->my_inputs, this, g);
  236. my_successors.set_owner(this);
  237. my_aggregator.initialize_handler(handler_type(this));
  238. }
  239. indexer_node_base(const indexer_node_base& other) : graph_node(other.my_graph), input_ports_type(), sender<output_type>() {
  240. indexer_helper<StructTypes,N>::set_indexer_node_pointer(this->my_inputs, this, other.my_graph);
  241. my_successors.set_owner(this);
  242. my_aggregator.initialize_handler(handler_type(this));
  243. }
  244. bool register_successor(successor_type &r) __TBB_override {
  245. indexer_node_base_operation op_data(r, reg_succ);
  246. my_aggregator.execute(&op_data);
  247. return op_data.status == SUCCEEDED;
  248. }
  249. bool remove_successor( successor_type &r) __TBB_override {
  250. indexer_node_base_operation op_data(r, rem_succ);
  251. my_aggregator.execute(&op_data);
  252. return op_data.status == SUCCEEDED;
  253. }
  254. task * try_put_task(output_type const *v) { // not a virtual method in this class
  255. indexer_node_base_operation op_data(v, try__put_task);
  256. my_aggregator.execute(&op_data);
  257. return op_data.bypass_t;
  258. }
  259. #if TBB_DEPRECATED_FLOW_NODE_EXTRACTION
  260. built_successors_type &built_successors() __TBB_override { return my_successors.built_successors(); }
  261. void internal_add_built_successor( successor_type &r) __TBB_override {
  262. indexer_node_base_operation op_data(r, add_blt_succ);
  263. my_aggregator.execute(&op_data);
  264. }
  265. void internal_delete_built_successor( successor_type &r) __TBB_override {
  266. indexer_node_base_operation op_data(r, del_blt_succ);
  267. my_aggregator.execute(&op_data);
  268. }
  269. size_t successor_count() __TBB_override {
  270. indexer_node_base_operation op_data(blt_succ_cnt);
  271. my_aggregator.execute(&op_data);
  272. return op_data.cnt_val;
  273. }
  274. void copy_successors( successor_list_type &v) __TBB_override {
  275. indexer_node_base_operation op_data(blt_succ_cpy);
  276. op_data.succv = &v;
  277. my_aggregator.execute(&op_data);
  278. }
  279. void extract() __TBB_override {
  280. my_successors.built_successors().sender_extract(*this);
  281. indexer_helper<StructTypes,N>::extract(this->my_inputs);
  282. }
  283. #endif /* TBB_DEPRECATED_FLOW_NODE_EXTRACTION */
  284. protected:
  285. void reset_node(reset_flags f) __TBB_override {
  286. if(f & rf_clear_edges) {
  287. my_successors.clear();
  288. indexer_helper<StructTypes,N>::reset_inputs(this->my_inputs,f);
  289. }
  290. }
  291. private:
  292. broadcast_cache<output_type, null_rw_mutex> my_successors;
  293. }; //indexer_node_base
  294. template<int N, typename InputTuple> struct input_types;
  295. template<typename InputTuple>
  296. struct input_types<1, InputTuple> {
  297. typedef typename tuple_element<0, InputTuple>::type first_type;
  298. typedef typename internal::tagged_msg<size_t, first_type > type;
  299. };
  300. template<typename InputTuple>
  301. struct input_types<2, InputTuple> {
  302. typedef typename tuple_element<0, InputTuple>::type first_type;
  303. typedef typename tuple_element<1, InputTuple>::type second_type;
  304. typedef typename internal::tagged_msg<size_t, first_type, second_type> type;
  305. };
  306. template<typename InputTuple>
  307. struct input_types<3, InputTuple> {
  308. typedef typename tuple_element<0, InputTuple>::type first_type;
  309. typedef typename tuple_element<1, InputTuple>::type second_type;
  310. typedef typename tuple_element<2, InputTuple>::type third_type;
  311. typedef typename internal::tagged_msg<size_t, first_type, second_type, third_type> type;
  312. };
  313. template<typename InputTuple>
  314. struct input_types<4, InputTuple> {
  315. typedef typename tuple_element<0, InputTuple>::type first_type;
  316. typedef typename tuple_element<1, InputTuple>::type second_type;
  317. typedef typename tuple_element<2, InputTuple>::type third_type;
  318. typedef typename tuple_element<3, InputTuple>::type fourth_type;
  319. typedef typename internal::tagged_msg<size_t, first_type, second_type, third_type,
  320. fourth_type> type;
  321. };
  322. template<typename InputTuple>
  323. struct input_types<5, InputTuple> {
  324. typedef typename tuple_element<0, InputTuple>::type first_type;
  325. typedef typename tuple_element<1, InputTuple>::type second_type;
  326. typedef typename tuple_element<2, InputTuple>::type third_type;
  327. typedef typename tuple_element<3, InputTuple>::type fourth_type;
  328. typedef typename tuple_element<4, InputTuple>::type fifth_type;
  329. typedef typename internal::tagged_msg<size_t, first_type, second_type, third_type,
  330. fourth_type, fifth_type> type;
  331. };
  332. template<typename InputTuple>
  333. struct input_types<6, InputTuple> {
  334. typedef typename tuple_element<0, InputTuple>::type first_type;
  335. typedef typename tuple_element<1, InputTuple>::type second_type;
  336. typedef typename tuple_element<2, InputTuple>::type third_type;
  337. typedef typename tuple_element<3, InputTuple>::type fourth_type;
  338. typedef typename tuple_element<4, InputTuple>::type fifth_type;
  339. typedef typename tuple_element<5, InputTuple>::type sixth_type;
  340. typedef typename internal::tagged_msg<size_t, first_type, second_type, third_type,
  341. fourth_type, fifth_type, sixth_type> type;
  342. };
  343. template<typename InputTuple>
  344. struct input_types<7, InputTuple> {
  345. typedef typename tuple_element<0, InputTuple>::type first_type;
  346. typedef typename tuple_element<1, InputTuple>::type second_type;
  347. typedef typename tuple_element<2, InputTuple>::type third_type;
  348. typedef typename tuple_element<3, InputTuple>::type fourth_type;
  349. typedef typename tuple_element<4, InputTuple>::type fifth_type;
  350. typedef typename tuple_element<5, InputTuple>::type sixth_type;
  351. typedef typename tuple_element<6, InputTuple>::type seventh_type;
  352. typedef typename internal::tagged_msg<size_t, first_type, second_type, third_type,
  353. fourth_type, fifth_type, sixth_type,
  354. seventh_type> type;
  355. };
  356. template<typename InputTuple>
  357. struct input_types<8, InputTuple> {
  358. typedef typename tuple_element<0, InputTuple>::type first_type;
  359. typedef typename tuple_element<1, InputTuple>::type second_type;
  360. typedef typename tuple_element<2, InputTuple>::type third_type;
  361. typedef typename tuple_element<3, InputTuple>::type fourth_type;
  362. typedef typename tuple_element<4, InputTuple>::type fifth_type;
  363. typedef typename tuple_element<5, InputTuple>::type sixth_type;
  364. typedef typename tuple_element<6, InputTuple>::type seventh_type;
  365. typedef typename tuple_element<7, InputTuple>::type eighth_type;
  366. typedef typename internal::tagged_msg<size_t, first_type, second_type, third_type,
  367. fourth_type, fifth_type, sixth_type,
  368. seventh_type, eighth_type> type;
  369. };
  370. template<typename InputTuple>
  371. struct input_types<9, InputTuple> {
  372. typedef typename tuple_element<0, InputTuple>::type first_type;
  373. typedef typename tuple_element<1, InputTuple>::type second_type;
  374. typedef typename tuple_element<2, InputTuple>::type third_type;
  375. typedef typename tuple_element<3, InputTuple>::type fourth_type;
  376. typedef typename tuple_element<4, InputTuple>::type fifth_type;
  377. typedef typename tuple_element<5, InputTuple>::type sixth_type;
  378. typedef typename tuple_element<6, InputTuple>::type seventh_type;
  379. typedef typename tuple_element<7, InputTuple>::type eighth_type;
  380. typedef typename tuple_element<8, InputTuple>::type nineth_type;
  381. typedef typename internal::tagged_msg<size_t, first_type, second_type, third_type,
  382. fourth_type, fifth_type, sixth_type,
  383. seventh_type, eighth_type, nineth_type> type;
  384. };
  385. template<typename InputTuple>
  386. struct input_types<10, InputTuple> {
  387. typedef typename tuple_element<0, InputTuple>::type first_type;
  388. typedef typename tuple_element<1, InputTuple>::type second_type;
  389. typedef typename tuple_element<2, InputTuple>::type third_type;
  390. typedef typename tuple_element<3, InputTuple>::type fourth_type;
  391. typedef typename tuple_element<4, InputTuple>::type fifth_type;
  392. typedef typename tuple_element<5, InputTuple>::type sixth_type;
  393. typedef typename tuple_element<6, InputTuple>::type seventh_type;
  394. typedef typename tuple_element<7, InputTuple>::type eighth_type;
  395. typedef typename tuple_element<8, InputTuple>::type nineth_type;
  396. typedef typename tuple_element<9, InputTuple>::type tenth_type;
  397. typedef typename internal::tagged_msg<size_t, first_type, second_type, third_type,
  398. fourth_type, fifth_type, sixth_type,
  399. seventh_type, eighth_type, nineth_type,
  400. tenth_type> type;
  401. };
  402. // type generators
  403. template<typename OutputTuple>
  404. struct indexer_types : public input_types<tuple_size<OutputTuple>::value, OutputTuple> {
  405. static const int N = tbb::flow::tuple_size<OutputTuple>::value;
  406. typedef typename input_types<N, OutputTuple>::type output_type;
  407. typedef typename wrap_tuple_elements<N,indexer_input_port,OutputTuple>::type input_ports_type;
  408. typedef internal::indexer_node_FE<input_ports_type,output_type,OutputTuple> indexer_FE_type;
  409. typedef internal::indexer_node_base<input_ports_type, output_type, OutputTuple> indexer_base_type;
  410. };
  411. template<class OutputTuple>
  412. class unfolded_indexer_node : public indexer_types<OutputTuple>::indexer_base_type {
  413. public:
  414. typedef typename indexer_types<OutputTuple>::input_ports_type input_ports_type;
  415. typedef OutputTuple tuple_types;
  416. typedef typename indexer_types<OutputTuple>::output_type output_type;
  417. private:
  418. typedef typename indexer_types<OutputTuple>::indexer_base_type base_type;
  419. public:
  420. unfolded_indexer_node(graph& g) : base_type(g) {}
  421. unfolded_indexer_node(const unfolded_indexer_node &other) : base_type(other) {}
  422. };
  423. } /* namespace internal */
  424. #endif /* __TBB__flow_graph_indexer_impl_H */