File: conformance_join_node.cpp

package info (click to toggle)
onetbb 2022.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,440 kB
  • sloc: cpp: 129,228; ansic: 9,745; python: 808; xml: 183; objc: 176; makefile: 66; sh: 66; awk: 41; javascript: 37
file content (317 lines) | stat: -rw-r--r-- 12,888 bytes parent folder | download | duplicates (4)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
    Copyright (c) 2020-2023 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.
*/

#if __INTEL_COMPILER && _MSC_VER
#pragma warning(disable : 2586) // decorated name length exceeded, name was truncated
#endif

#include "conformance_flowgraph.h"
#include "common/test_invoke.h"

//! \file conformance_join_node.cpp
//! \brief Test for [flow_graph.join_node] specification

using input_msg = conformance::message</*default_ctor*/true, /*copy_ctor*/true, /*copy_assign*/true>;
using my_input_tuple = std::tuple<int, float, input_msg>;

std::vector<my_input_tuple> get_values( conformance::test_push_receiver<my_input_tuple>& rr ) {
    std::vector<my_input_tuple> messages;
    my_input_tuple tmp(0, 0.f, input_msg(0));
    while(rr.try_get(tmp)) {
        messages.push_back(tmp);
    }
    return messages;
}

#if __TBB_CPP17_DEDUCTION_GUIDES_PRESENT
void test_deduction_guides() {
    using namespace tbb::flow;

    graph g;
    using tuple_type = std::tuple<int, int, int>;
    broadcast_node<int> b1(g), b2(g), b3(g);
    broadcast_node<tuple_type> b4(g);
    join_node<tuple_type> j0(g);

#if __TBB_PREVIEW_FLOW_GRAPH_NODE_SET
    join_node j1(follows(b1, b2, b3));
    static_assert(std::is_same_v<decltype(j1), join_node<tuple_type>>);

    join_node j2(follows(b1, b2, b3), reserving());
    static_assert(std::is_same_v<decltype(j2), join_node<tuple_type, reserving>>);

    join_node j3(precedes(b4));
    static_assert(std::is_same_v<decltype(j3), join_node<tuple_type>>);

    join_node j4(precedes(b4), reserving());
    static_assert(std::is_same_v<decltype(j4), join_node<tuple_type, reserving>>);
#endif

    join_node j5(j0);
    static_assert(std::is_same_v<decltype(j5), join_node<tuple_type>>);
}

#endif

//! The node that is constructed has a reference to the same graph object as src.
//! The list of predecessors, messages in the input ports, and successors are not copied.
//! \brief \ref interface
TEST_CASE("join_node copy constructor"){
    oneapi::tbb::flow::graph g;
    oneapi::tbb::flow::continue_node<int> node0( g,
                                [](oneapi::tbb::flow::continue_msg) { return 1; } );

    oneapi::tbb::flow::join_node<std::tuple<int>> node1(g);
    conformance::test_push_receiver<std::tuple<int>> node2(g);
    conformance::test_push_receiver<std::tuple<int>> node3(g);

    oneapi::tbb::flow::make_edge(node0, oneapi::tbb::flow::input_port<0>(node1));
    oneapi::tbb::flow::make_edge(node1, node2);
    oneapi::tbb::flow::join_node<std::tuple<int>> node_copy(node1);

    oneapi::tbb::flow::make_edge(node_copy, node3);

    oneapi::tbb::flow::input_port<0>(node_copy).try_put(1);
    g.wait_for_all();

    auto values = conformance::get_values(node3);
    CHECK_MESSAGE((conformance::get_values(node2).size() == 0 && values.size() == 1), "Copied node doesn`t copy successor");

    node0.try_put(oneapi::tbb::flow::continue_msg());
    g.wait_for_all();

    CHECK_MESSAGE((conformance::get_values(node2).size() == 1 && conformance::get_values(node3).size() == 0), "Copied node doesn`t copy predecessor");

    oneapi::tbb::flow::remove_edge(node1, node2);
    oneapi::tbb::flow::input_port<0>(node1).try_put(1);
    g.wait_for_all();
    oneapi::tbb::flow::join_node<std::tuple<int>> node_copy2(node1);
    oneapi::tbb::flow::make_edge(node_copy2, node3);
    oneapi::tbb::flow::input_port<0>(node_copy2).try_put(2);
    g.wait_for_all();
    CHECK_MESSAGE((std::get<0>(conformance::get_values(node3)[0]) == 2), "Copied node doesn`t copy messages in the input ports");
}

//! Test inheritance relations
//! \brief \ref interface
TEST_CASE("join_node inheritance"){
    CHECK_MESSAGE((std::is_base_of<oneapi::tbb::flow::graph_node,
                   oneapi::tbb::flow::join_node<my_input_tuple>>::value),
                   "join_node should be derived from graph_node");
    CHECK_MESSAGE((std::is_base_of<oneapi::tbb::flow::sender<my_input_tuple>,
                   oneapi::tbb::flow::join_node<my_input_tuple>>::value),
                   "join_node should be derived from sender<input_tuple>");
}

//! Test join_node<queueing> behavior and broadcast property
//! \brief \ref requirement
TEST_CASE("join_node queueing policy and broadcast property") {
    oneapi::tbb::flow::graph g;
    oneapi::tbb::flow::function_node<int, int>
        f1( g, oneapi::tbb::flow::unlimited, [](const int &i) { return i; } );
    oneapi::tbb::flow::function_node<float, float>
        f2( g, oneapi::tbb::flow::unlimited, [](const float &f) { return f; } );
    oneapi::tbb::flow::continue_node<input_msg> c1( g,
                            [](oneapi::tbb::flow::continue_msg) { return input_msg(1); } );

    oneapi::tbb::flow::join_node<my_input_tuple, oneapi::tbb::flow::queueing> testing_node(g);

    conformance::test_push_receiver<my_input_tuple> q_node(g);

    std::atomic<int> number{1};
    oneapi::tbb::flow::function_node<my_input_tuple, my_input_tuple>
        f3( g, oneapi::tbb::flow::unlimited,
            [&]( const my_input_tuple &t ) {
                CHECK_MESSAGE((std::get<0>(t) == number), "Messages must be in first-in first-out order" );
                CHECK_MESSAGE((std::get<1>(t) == static_cast<float>(number) + 0.5f), "Messages must be in first-in first-out order" );
                CHECK_MESSAGE((std::get<2>(t) == 1), "Messages must be in first-in first-out order" );
                ++number;
                return t;
            } );

    oneapi::tbb::flow::make_edge(f1, oneapi::tbb::flow::input_port<0>(testing_node));
    oneapi::tbb::flow::make_edge(f2, oneapi::tbb::flow::input_port<1>(testing_node));
    oneapi::tbb::flow::make_edge(c1, oneapi::tbb::flow::input_port<2>(testing_node));
    make_edge(testing_node, f3);
    make_edge(f3, q_node);

    f1.try_put(1);
    g.wait_for_all();
    CHECK_MESSAGE((get_values(q_node).size() == 0),
        "join_node must broadcast when there is at least one message at each input port");
    f1.try_put(2);
    f2.try_put(1.5f);
    g.wait_for_all();
    CHECK_MESSAGE((get_values(q_node).size() == 0),
        "join_node must broadcast when there is at least one message at each input port");
    f1.try_put(3);
    f2.try_put(2.5f);
    c1.try_put(oneapi::tbb::flow::continue_msg());
    g.wait_for_all();
    CHECK_MESSAGE((get_values(q_node).size() == 1),
        "join_node must broadcast when there is at least one message at each input port");
    f2.try_put(3.5f);
    c1.try_put(oneapi::tbb::flow::continue_msg());
    g.wait_for_all();
    CHECK_MESSAGE((get_values(q_node).size() == 1),
        "If at least one successor accepts the tuple, the head of each input port’s queue is removed");
    c1.try_put(oneapi::tbb::flow::continue_msg());
    g.wait_for_all();
    CHECK_MESSAGE((get_values(q_node).size() == 1),
        "If at least one successor accepts the tuple, the head of each input port’s queue is removed");
    c1.try_put(oneapi::tbb::flow::continue_msg());
    g.wait_for_all();
    CHECK_MESSAGE((get_values(q_node).size() == 0),
        "join_node must broadcast when there is at least one message at each input port");

    oneapi::tbb::flow::remove_edge(testing_node, f3);

    f1.try_put(1);
    f2.try_put(1);
    c1.try_put(oneapi::tbb::flow::continue_msg());
    g.wait_for_all();

    my_input_tuple tmp(0, 0.f, input_msg(0));
    CHECK_MESSAGE((testing_node.try_get(tmp)), "If no one successor accepts the tuple the messages\
        must remain in their respective input port queues");
    CHECK_MESSAGE((tmp == my_input_tuple(1, 1.f, input_msg(1))), "If no one successor accepts the tuple\
        the messages must remain in their respective input port queues");
}

//! Test join_node<reserving> behavior
//! \brief \ref requirement
TEST_CASE("join_node reserving policy") {
    conformance::test_with_reserving_join_node_class<oneapi::tbb::flow::write_once_node<int>>();
}

template<typename KeyType>
struct MyHash{
    std::size_t hash(const KeyType &k) const {
        return k * 2000 + 3;
    }

    bool equal(const KeyType &k1, const KeyType &k2) const{
        return hash(k1) == hash(k2);
    }
};

//! Test join_node<key_matching> behavior
//! \brief \ref requirement
TEST_CASE("join_node key_matching policy"){
    oneapi::tbb::flow::graph g;
    auto body1 = [](const oneapi::tbb::flow::continue_msg &) -> int { return 1; };
    auto body2 = [](const float &val) -> int { return static_cast<int>(val); };

    oneapi::tbb::flow::join_node<std::tuple<oneapi::tbb::flow::continue_msg, float>,
        oneapi::tbb::flow::key_matching<int, MyHash<int>>> testing_node(g, body1, body2);

    oneapi::tbb::flow::input_port<0>(testing_node).try_put(oneapi::tbb::flow::continue_msg());
    oneapi::tbb::flow::input_port<1>(testing_node).try_put(1.3f);

    g.wait_for_all();

    std::tuple<oneapi::tbb::flow::continue_msg, float> tmp;
    CHECK_MESSAGE((testing_node.try_get(tmp)), "Mapped keys should match.\
        If no successor accepts the tuple, it is must been saved and will be forwarded on a subsequent try_get");
    CHECK_MESSAGE((!testing_node.try_get(tmp)), "Message should not exist after item is consumed");
}

//! Test join_node<tag_matching> behavior
//! \brief \ref requirement
TEST_CASE("join_node tag_matching policy"){
    oneapi::tbb::flow::graph g;
    auto body1 = [](const oneapi::tbb::flow::continue_msg &) -> oneapi::tbb::flow::tag_value { return 1; };
    auto body2 = [](const float &val) -> oneapi::tbb::flow::tag_value { return static_cast<oneapi::tbb::flow::tag_value>(val); };

    oneapi::tbb::flow::join_node<std::tuple<oneapi::tbb::flow::continue_msg, float>,
        oneapi::tbb::flow::tag_matching> testing_node(g, body1, body2);

    oneapi::tbb::flow::input_port<0>(testing_node).try_put(oneapi::tbb::flow::continue_msg());
    oneapi::tbb::flow::input_port<1>(testing_node).try_put(1.3f);

    g.wait_for_all();

    std::tuple<oneapi::tbb::flow::continue_msg, float> tmp;
    CHECK_MESSAGE((testing_node.try_get(tmp) == true), "Mapped keys should match");
}

#if __TBB_CPP17_DEDUCTION_GUIDES_PRESENT
//! Test deduction guides
//! \brief \ref requirement
TEST_CASE("Deduction guides test"){
    test_deduction_guides();
}
#endif

//! Test join_node input_ports() returns a tuple of input ports.
//! \brief \ref interface \ref requirement
TEST_CASE("join_node output_ports") {
    oneapi::tbb::flow::graph g;
    oneapi::tbb::flow::join_node<std::tuple<int>> node(g);

    CHECK_MESSAGE((std::is_same<oneapi::tbb::flow::join_node<std::tuple<int>>::input_ports_type&,
        decltype(node.input_ports())>::value), "join_node input_ports should returns a tuple of input ports");
}

#if __TBB_CPP17_INVOKE_PRESENT

template <typename K, typename Body1, typename Body2>
void test_invoke_basic(Body1 body1, Body2 body2) {
    static_assert(std::is_same_v<std::decay_t<K>, std::size_t>, "incorrect test setup");
    using namespace oneapi::tbb::flow;
    auto generator = [](std::size_t n) { return test_invoke::SmartID<std::size_t>(n); };
    graph g;

    function_node<std::size_t, test_invoke::SmartID<std::size_t>> f1(g, unlimited, generator);
    function_node<std::size_t, test_invoke::SmartID<std::size_t>> f2(g, unlimited, generator);

    using tuple_type = std::tuple<test_invoke::SmartID<std::size_t>, test_invoke::SmartID<std::size_t>>;
    using join_type = join_node<tuple_type, key_matching<K>>;


    join_type j(g, body1, body2);

    buffer_node<tuple_type> buf(g);

    make_edge(f1, input_port<0>(j));
    make_edge(f2, input_port<1>(j));
    make_edge(j, buf);

    std::size_t objects_count = 100;
    for (std::size_t i = 0; i < objects_count; ++i) {
        f1.try_put(i);
        f2.try_put(objects_count - i - 1);
    }

    g.wait_for_all();

    std::size_t buf_size = 0;
    tuple_type tpl;

    while(buf.try_get(tpl)) {
        ++buf_size;
        CHECK(std::get<0>(tpl).id == std::get<1>(tpl).id);
    }
    CHECK(buf_size == objects_count);
}

//! Test that key_matching join_node uses std::invoke to run the body
//! \brief \ref requirement
TEST_CASE("key_matching join_node invoke semantics") {
    test_invoke_basic</*K = */std::size_t>(&test_invoke::SmartID<std::size_t>::get_id, &test_invoke::SmartID<std::size_t>::id);
    test_invoke_basic</*K = */const std::size_t&>(&test_invoke::SmartID<std::size_t>::get_id_ref, &test_invoke::SmartID<std::size_t>::get_id_ref);
}
#endif // __TBB_CPP17_INVOKE_PRESENT