1 /*
2     Copyright (c) 2020-2021 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 #if __INTEL_COMPILER && _MSC_VER
18 #pragma warning(disable : 2586) // decorated name length exceeded, name was truncated
19 #endif
20 
21 #include "common/test.h"
22 
23 #include "common/utils.h"
24 #include "common/graph_utils.h"
25 
26 #include "oneapi/tbb/flow_graph.h"
27 #include "oneapi/tbb/task_arena.h"
28 #include "oneapi/tbb/global_control.h"
29 
30 #include "conformance_flowgraph.h"
31 
32 //! \file conformance_async_node.cpp
33 //! \brief Test for [flow_graph.async_node] specification
34 
35 /*
36 TODO: implement missing conformance tests for async_node:
37   - [ ] Write `test_forwarding()'.
38   - [ ] Improve test of the node's copy-constructor.
39   - [ ] Write `test_priority'.
40   - [ ] Rename `test_discarding' to `test_buffering'.
41   - [ ] Write inheritance test.
42   - [ ] Constructor with explicitly passed Policy parameter.
43   - [ ] Concurrency testing of the node: make a loop over possible concurrency levels. It is
44     important to test at least on five values: 1, oneapi::tbb::flow::serial, `max_allowed_parallelism'
45     obtained from `oneapi::tbb::global_control', `oneapi::tbb::flow::unlimited', and, if `max allowed
46     parallelism' is > 2, use something in the middle of the [1, max_allowed_parallelism]
47     interval. Use `utils::ExactConcurrencyLevel' entity (extending it if necessary).
48   - [ ] Write `test_rejecting', where avoid dependency on OS scheduling of the threads; add check
49     that `try_put()' returns `false'
50   - [ ] The `copy_body' function copies altered body (e.g. after successful `try_put()' call).
51   - [ ] The copy constructor and copy assignment are called for the node's input and output types.
52   - [ ] Add CTAD test.
53 */
54 
55 template<typename I, typename O>
56 void test_inheritance(){
57     using namespace oneapi::tbb::flow;
58 
59     CHECK_MESSAGE( (std::is_base_of<graph_node, async_node<I, O>>::value), "async_node should be derived from graph_node");
60     CHECK_MESSAGE( (std::is_base_of<receiver<I>, async_node<I, O>>::value), "async_node should be derived from receiver<Input>");
61     CHECK_MESSAGE( (std::is_base_of<sender<O>, async_node<I, O>>::value), "async_node should be derived from sender<Output>");
62 }
63 
64 template< typename OutputType >
65 struct as_inc_functor {
66     std::thread my_thread;
67 
68     std::atomic<size_t>& local_execute_count;
69 
70     as_inc_functor(std::atomic<size_t>& execute_count ) :
71         local_execute_count (execute_count)
72     {  }
73 
74     as_inc_functor( const as_inc_functor &f ) : local_execute_count(f.local_execute_count) { }
75     void operator=(const as_inc_functor &f) { local_execute_count = size_t(f.local_execute_count); }
76 
77     void operator()( int num , oneapi::tbb::flow::async_node<int, int>::gateway_type& g) {
78         ++local_execute_count;
79         g.try_put(num);
80         //    my_thread = std::thread([&](){
81         //                                g.try_put(num);
82         //                            });
83     }
84 
85 };
86 
87 void test_async_body(){
88     oneapi::tbb::flow::graph g;
89 
90     std::atomic<size_t> local_count(0);
91     as_inc_functor<int> fun(local_count);
92 
93     oneapi::tbb::flow::async_node<int, int> node1(g, oneapi::tbb::flow::unlimited, fun);
94 
95     const size_t n = 10;
96     for(size_t i = 0; i < n; ++i) {
97         CHECK_MESSAGE((node1.try_put(1) == true), "try_put needs to return true");
98     }
99 
100     //fun.my_thread.join();
101     g.wait_for_all();
102 
103     CHECK_MESSAGE( (fun.local_execute_count.load() == n), "Body of the node needs to be executed N times");
104 }
105 
106 void test_copy(){
107     oneapi::tbb::flow::graph g;
108     std::atomic<size_t> local_count(0);
109     as_inc_functor<int> fun(local_count);
110 
111     oneapi::tbb::flow::async_node<int, int> node1(g, oneapi::tbb::flow::unlimited, fun);
112     oneapi::tbb::flow::async_node<int, int> node2(node1);
113 }
114 
115 void test_priority(){
116     oneapi::tbb::flow::graph g;
117     std::atomic<size_t> local_count(0);
118     as_inc_functor<int> fun(local_count);
119 
120     oneapi::tbb::flow::async_node<int, int> node1(g, oneapi::tbb::flow::unlimited, fun, oneapi::tbb::flow::no_priority);
121 }
122 
123 void test_discarding(){
124     oneapi::tbb::flow::graph g;
125 
126     std::atomic<size_t> local_count(0);
127     as_inc_functor<int> fun(local_count);
128 
129     oneapi::tbb::flow::async_node<int, int> node1(g, oneapi::tbb::flow::unlimited, fun);
130 
131     oneapi::tbb::flow::limiter_node< int > rejecter1( g,0);
132     oneapi::tbb::flow::limiter_node< int > rejecter2( g,0);
133 
134     make_edge(node1, rejecter2);
135     make_edge(node1, rejecter1);
136 
137     node1.try_put(1);
138 
139     int tmp = -1;
140     CHECK_MESSAGE((node1.try_get(tmp) == false), "Value should be discarded after rejection");
141 
142     g.wait_for_all();
143 }
144 
145 //! Test discarding property
146 //! \brief \ref requirement
147 TEST_CASE("async_node discarding") {
148     test_discarding();
149 
150 }
151 
152 //! Test async_node priority interface
153 //! \brief \ref interface
154 TEST_CASE("async_node priority interface"){
155     test_priority();
156 }
157 
158 //! Test async_node copy
159 //! \brief \ref interface
160 TEST_CASE("async_node copy"){
161     test_copy();
162 }
163 
164 //! Test calling async body
165 //! \brief \ref interface \ref requirement
166 TEST_CASE("Test async_node body") {
167     test_async_body();
168 }
169 
170 //! Test async_node inheritance relations
171 //! \brief \ref interface
172 TEST_CASE("async_node superclasses"){
173     test_inheritance<int, int>();
174     test_inheritance<void*, float>();
175 }
176