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