1 /*
2     Copyright (c) 2022 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_test_common_test_join_node_multiple_predecessors_H_
18 #define __TBB_test_common_test_join_node_multiple_predecessors_H_
19 
20 #include "config.h"
21 #include "oneapi/tbb/flow_graph.h"
22 
23 namespace multiple_predecessors {
24 
25 using namespace tbb::flow;
26 
27 using join_node_t = join_node<std::tuple<continue_msg, continue_msg, continue_msg>, reserving>;
28 using queue_node_t = queue_node<std::tuple<continue_msg, continue_msg, continue_msg>>;
29 
30 void twist_join_connections(
31     buffer_node<continue_msg>& bn1, buffer_node<continue_msg>& bn2, buffer_node<continue_msg>& bn3,
32     join_node_t& jn)
33 {
34     // order, in which edges are created/destroyed, is important
35     make_edge(bn1, input_port<0>(jn));
36     make_edge(bn2, input_port<0>(jn));
37     make_edge(bn3, input_port<0>(jn));
38 
39     remove_edge(bn3, input_port<0>(jn));
40     make_edge  (bn3, input_port<2>(jn));
41 
42     remove_edge(bn2, input_port<0>(jn));
43     make_edge  (bn2, input_port<1>(jn));
44 }
45 
46 std::unique_ptr<join_node_t> connect_join_via_make_edge(
47     graph& g, buffer_node<continue_msg>& bn1, buffer_node<continue_msg>& bn2,
48     buffer_node<continue_msg>& bn3, queue_node_t& qn)
49 {
50     std::unique_ptr<join_node_t> jn( new join_node_t(g) );
51     twist_join_connections( bn1, bn2, bn3, *jn );
52     make_edge(*jn, qn);
53     return jn;
54 }
55 
56 #if TBB_PREVIEW_FLOW_GRAPH_FEATURES
57 std::unique_ptr<join_node_t> connect_join_via_follows(
58     graph&, buffer_node<continue_msg>& bn1, buffer_node<continue_msg>& bn2,
59     buffer_node<continue_msg>& bn3, queue_node_t& qn)
60 {
61     auto bn_set = make_node_set(bn1, bn2, bn3);
62     std::unique_ptr<join_node_t> jn( new join_node_t(follows(bn_set)) );
63     make_edge(*jn, qn);
64     return jn;
65 }
66 
67 std::unique_ptr<join_node_t> connect_join_via_precedes(
68     graph&, buffer_node<continue_msg>& bn1, buffer_node<continue_msg>& bn2,
69     buffer_node<continue_msg>& bn3, queue_node_t& qn)
70 {
71     auto qn_set = make_node_set(qn);
72     auto qn_copy_set = qn_set;
73     std::unique_ptr<join_node_t> jn( new join_node_t(precedes(qn_copy_set)) );
74     twist_join_connections( bn1, bn2, bn3, *jn );
75     return jn;
76 }
77 #endif // TBB_PREVIEW_FLOW_GRAPH_FEATURES
78 
79 void run_and_check(
80     graph& g, buffer_node<continue_msg>& bn1, buffer_node<continue_msg>& bn2,
81     buffer_node<continue_msg>& bn3, queue_node_t& qn, bool expected)
82 {
83     std::tuple<continue_msg, continue_msg, continue_msg> msg;
84 
85     bn1.try_put(continue_msg());
86     bn2.try_put(continue_msg());
87     bn3.try_put(continue_msg());
88     g.wait_for_all();
89 
90     CHECK_MESSAGE(
91         (qn.try_get(msg) == expected),
92         "Unexpected message absence/existence at the end of the graph."
93     );
94 }
95 
96 template<typename ConnectJoinNodeFunc>
97 void test(ConnectJoinNodeFunc&& connect_join_node) {
98     graph g;
99     buffer_node<continue_msg> bn1(g);
100     buffer_node<continue_msg> bn2(g);
101     buffer_node<continue_msg> bn3(g);
102     queue_node_t qn(g);
103 
104     auto jn = connect_join_node(g, bn1, bn2, bn3, qn);
105 
106     run_and_check(g, bn1, bn2, bn3, qn, /*expected=*/true);
107 
108     remove_edge(bn3, input_port<2>(*jn));
109     remove_edge(bn2, input_port<1>(*jn));
110     remove_edge(bn1, *jn); //Removes an edge between a sender and port 0 of a multi-input successor.
111     remove_edge(*jn, qn);
112 
113     run_and_check(g, bn1, bn2, bn3, qn, /*expected=*/false);
114 }
115 } // namespace multiple_predecessors
116 
117 #endif // __TBB_test_common_test_join_node_multiple_predecessors_H_
118