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