Home ⌂Doc Index ◂Up ▴
Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
_flow_graph_node_set_impl.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2019-2020 Intel Corporation
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 #ifndef __TBB_flow_graph_node_set_impl_H
18 #define __TBB_flow_graph_node_set_impl_H
19 
20 #ifndef __TBB_flow_graph_H
21 #error Do not #include this internal file directly; use public TBB headers instead.
22 #endif
23 
24 // Included in namespace tbb::flow::interfaceX (in flow_graph.h)
25 
26 namespace internal {
27 
28 #if __TBB_PREVIEW_FLOW_GRAPH_NODE_SET
29 // Visual Studio 2019 reports an error while calling predecessor_selector::get and successor_selector::get
30 // Seems like the well-formed expression in trailing decltype is treated as ill-formed
31 // TODO: investigate problems with decltype in trailing return types or find the cross-platform solution
32 #define __TBB_MSVC_DISABLE_TRAILING_DECLTYPE (_MSC_VER >= 1900)
33 
34 namespace order {
35 struct undefined {};
36 struct following {};
37 struct preceding {};
38 }
39 
40 class get_graph_helper {
41 public:
42  // TODO: consider making graph_reference() public and consistent interface to get a reference to the graph
43  // and remove get_graph_helper
44  template <typename T>
45  static graph& get(const T& object) {
46  return get_impl(object, std::is_base_of<graph_node, T>());
47  }
48 
49 private:
50  // Get graph from the object of type derived from graph_node
51  template <typename T>
52  static graph& get_impl(const T& object, std::true_type) {
53  return static_cast<const graph_node*>(&object)->my_graph;
54  }
55 
56  template <typename T>
57  static graph& get_impl(const T& object, std::false_type) {
58  return object.graph_reference();
59  }
60 };
61 
62 template<typename Order, typename... Nodes>
63 struct node_set {
64  typedef Order order_type;
65 
66  tbb::flow::tuple<Nodes&...> nodes;
67  node_set(Nodes&... ns) : nodes(ns...) {}
68 
69  template <typename... Nodes2>
70  node_set(const node_set<order::undefined, Nodes2...>& set) : nodes(set.nodes) {}
71 
72  graph& graph_reference() const {
73  return get_graph_helper::get(std::get<0>(nodes));
74  }
75 };
76 
77 namespace alias_helpers {
78 template <typename T> using output_type = typename T::output_type;
79 template <typename T> using output_ports_type = typename T::output_ports_type;
80 template <typename T> using input_type = typename T::input_type;
81 template <typename T> using input_ports_type = typename T::input_ports_type;
82 } // namespace alias_helpers
83 
84 template <typename T>
86 
87 template <typename T>
89 
90 template <typename T>
92 
93 template <typename T>
95 
96 template<typename T>
97 struct is_sender : std::is_base_of<sender<typename T::output_type>, T> {};
98 
99 template<typename T>
100 struct is_receiver : std::is_base_of<receiver<typename T::input_type>, T> {};
101 
102 template <typename Node>
103 struct is_async_node : std::false_type {};
104 
105 template <typename... Args>
106 struct is_async_node<tbb::flow::interface11::async_node<Args...>> : std::true_type {};
107 
108 template<typename FirstPredecessor, typename... Predecessors>
109 node_set<order::following, FirstPredecessor, Predecessors...>
110 follows(FirstPredecessor& first_predecessor, Predecessors&... predecessors) {
111  __TBB_STATIC_ASSERT((tbb::internal::conjunction<has_output_type<FirstPredecessor>,
112  has_output_type<Predecessors>...>::value),
113  "Not all node's predecessors has output_type typedef");
114  __TBB_STATIC_ASSERT((tbb::internal::conjunction<is_sender<FirstPredecessor>, is_sender<Predecessors>...>::value),
115  "Not all node's predecessors are senders");
116  return node_set<order::following, FirstPredecessor, Predecessors...>(first_predecessor, predecessors...);
117 }
118 
119 template<typename... Predecessors>
120 node_set<order::following, Predecessors...>
121 follows(node_set<order::undefined, Predecessors...>& predecessors_set) {
122  __TBB_STATIC_ASSERT((tbb::internal::conjunction<has_output_type<Predecessors>...>::value),
123  "Not all nodes in the set has output_type typedef");
124  __TBB_STATIC_ASSERT((tbb::internal::conjunction<is_sender<Predecessors>...>::value),
125  "Not all nodes in the set are senders");
126  return node_set<order::following, Predecessors...>(predecessors_set);
127 }
128 
129 template<typename FirstSuccessor, typename... Successors>
130 node_set<order::preceding, FirstSuccessor, Successors...>
131 precedes(FirstSuccessor& first_successor, Successors&... successors) {
132  __TBB_STATIC_ASSERT((tbb::internal::conjunction<has_input_type<FirstSuccessor>,
133  has_input_type<Successors>...>::value),
134  "Not all node's successors has input_type typedef");
135  __TBB_STATIC_ASSERT((tbb::internal::conjunction<is_receiver<FirstSuccessor>, is_receiver<Successors>...>::value),
136  "Not all node's successors are receivers");
137  return node_set<order::preceding, FirstSuccessor, Successors...>(first_successor, successors...);
138 }
139 
140 template<typename... Successors>
141 node_set<order::preceding, Successors...>
142 precedes(node_set<order::undefined, Successors...>& successors_set) {
143  __TBB_STATIC_ASSERT((tbb::internal::conjunction<has_input_type<Successors>...>::value),
144  "Not all nodes in the set has input_type typedef");
145  __TBB_STATIC_ASSERT((tbb::internal::conjunction<is_receiver<Successors>...>::value),
146  "Not all nodes in the set are receivers");
147  return node_set<order::preceding, Successors...>(successors_set);
148 }
149 
150 template <typename Node, typename... Nodes>
151 node_set<order::undefined, Node, Nodes...>
152 make_node_set(Node& first_node, Nodes&... nodes) {
153  return node_set<order::undefined, Node, Nodes...>(first_node, nodes...);
154 }
155 
156 template<size_t I>
157 class successor_selector {
158  template <typename NodeType>
159  static auto get_impl(NodeType& node, std::true_type) -> decltype(input_port<I>(node)) {
160  return input_port<I>(node);
161  }
162 
163  template <typename NodeType>
164  static NodeType& get_impl(NodeType& node, std::false_type) { return node; }
165 
166 public:
167  template <typename NodeType>
168 #if __TBB_MSVC_DISABLE_TRAILING_DECLTYPE
169  static auto& get(NodeType& node)
170 #else
171  static auto get(NodeType& node) -> decltype(get_impl(node, has_input_ports_type<NodeType>()))
172 #endif
173  {
174  return get_impl(node, has_input_ports_type<NodeType>());
175  }
176 };
177 
178 template<size_t I>
179 class predecessor_selector {
180  template <typename NodeType>
181  static auto internal_get(NodeType& node, std::true_type) -> decltype(output_port<I>(node)) {
182  return output_port<I>(node);
183  }
184 
185  template <typename NodeType>
186  static NodeType& internal_get(NodeType& node, std::false_type) { return node;}
187 
188  template <typename NodeType>
189 #if __TBB_MSVC_DISABLE_TRAILING_DECLTYPE
190  static auto& get_impl(NodeType& node, std::false_type)
191 #else
192  static auto get_impl(NodeType& node, std::false_type) -> decltype(internal_get(node, has_output_ports_type<NodeType>()))
193 #endif
194  {
195  return internal_get(node, has_output_ports_type<NodeType>());
196  }
197 
198  template <typename AsyncNode>
199  static AsyncNode& get_impl(AsyncNode& node, std::true_type) { return node; }
200 
201 public:
202  template <typename NodeType>
203 #if __TBB_MSVC_DISABLE_TRAILING_DECLTYPE
204  static auto& get(NodeType& node)
205 #else
206  static auto get(NodeType& node) -> decltype(get_impl(node, is_async_node<NodeType>()))
207 #endif
208  {
209  return get_impl(node, is_async_node<NodeType>());
210  }
211 };
212 
213 template<size_t I>
214 class make_edges_helper {
215 public:
216  template<typename PredecessorsTuple, typename NodeType>
217  static void connect_predecessors(PredecessorsTuple& predecessors, NodeType& node) {
218  make_edge(std::get<I>(predecessors), successor_selector<I>::get(node));
219  make_edges_helper<I - 1>::connect_predecessors(predecessors, node);
220  }
221 
222  template<typename SuccessorsTuple, typename NodeType>
223  static void connect_successors(NodeType& node, SuccessorsTuple& successors) {
224  make_edge(predecessor_selector<I>::get(node), std::get<I>(successors));
225  make_edges_helper<I - 1>::connect_successors(node, successors);
226  }
227 };
228 
229 template<>
230 struct make_edges_helper<0> {
231  template<typename PredecessorsTuple, typename NodeType>
232  static void connect_predecessors(PredecessorsTuple& predecessors, NodeType& node) {
233  make_edge(std::get<0>(predecessors), successor_selector<0>::get(node));
234  }
235 
236  template<typename SuccessorsTuple, typename NodeType>
237  static void connect_successors(NodeType& node, SuccessorsTuple& successors) {
238  make_edge(predecessor_selector<0>::get(node), std::get<0>(successors));
239  }
240 };
241 
242 // TODO: consider adding an overload for making edges between node sets
243 template<typename NodeType, typename OrderFlagType, typename... Args>
244 void make_edges(const node_set<OrderFlagType, Args...>& s, NodeType& node) {
245  const std::size_t SetSize = tbb::flow::tuple_size<decltype(s.nodes)>::value;
246  make_edges_helper<SetSize - 1>::connect_predecessors(s.nodes, node);
247 }
248 
249 template <typename NodeType, typename OrderFlagType, typename... Args>
250 void make_edges(NodeType& node, const node_set<OrderFlagType, Args...>& s) {
251  const std::size_t SetSize = tbb::flow::tuple_size<decltype(s.nodes)>::value;
252  make_edges_helper<SetSize - 1>::connect_successors(node, s.nodes);
253 }
254 
255 template <typename NodeType, typename... Nodes>
256 void make_edges_in_order(const node_set<order::following, Nodes...>& ns, NodeType& node) {
257  make_edges(ns, node);
258 }
259 
260 template <typename NodeType, typename... Nodes>
261 void make_edges_in_order(const node_set<order::preceding, Nodes...>& ns, NodeType& node) {
262  make_edges(node, ns);
263 }
264 
265 #endif // __TBB_CPP11_PRESENT
266 
267 } // namespace internal
268 
269 #endif // __TBB_flow_graph_node_set_impl_H
bool_constant< true > true_type
Definition: tbb_stddef.h:489
bool_constant< false > false_type
Definition: tbb_stddef.h:490
#define __TBB_STATIC_ASSERT(condition, msg)
Definition: tbb_stddef.h:553
void make_edge(sender< T > &p, receiver< T > &s)
Makes an edge between a single predecessor and a single successor.
Definition: flow_graph.h:3838
void const char const char int ITT_FORMAT __itt_group_sync s
typename supports_impl< T, void, Checks... >::type supports
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long value
The graph class.

Copyright © 2005-2020 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.