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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
/*
Copyright (c) 2005-2021 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 "common/config.h"
#include "tbb/flow_graph.h"
#include "common/test.h"
#include "common/utils.h"
#include "common/graph_utils.h"
#include "common/spin_barrier.h"
//! \file test_flow_graph.cpp
//! \brief Test for [flow_graph.continue_msg flow_graph.graph_node flow_graph.input_port flow_graph.output_port flow_graph.join_node flow_graph.split_node flow_graph.limiter_node flow_graph.write_once_node flow_graph.overwrite_node flow_graph.make_edge flow_graph.graph flow_graph.buffer_node flow_graph.function_node flow_graph.multifunction_node flow_graph.continue_node flow_graph.input_node] specification
const int T = 4;
const int W = 4;
struct decrement_wait : utils::NoAssign {
tbb::flow::graph * const my_graph;
bool * const my_done_flag;
decrement_wait( tbb::flow::graph &h, bool *done_flag ) : my_graph(&h), my_done_flag(done_flag) {}
void operator()(int i) const {
utils::Sleep(10 * i);
my_done_flag[i] = true;
my_graph->release_wait();
}
};
static void test_wait_count() {
tbb::flow::graph h;
for (int i = 0; i < T; ++i ) {
bool done_flag[W];
for (int j = 0; j < W; ++j ) {
for ( int w = 0; w < W; ++w ) done_flag[w] = false;
for ( int w = 0; w < j; ++w ) h.reserve_wait();
utils::NativeParallelFor( j, decrement_wait(h, done_flag) );
h.wait_for_all();
for ( int w = 0; w < W; ++w ) {
if ( w < j ) CHECK_MESSAGE( done_flag[w] == true, "" );
else CHECK_MESSAGE( done_flag[w] == false, "" );
}
}
}
}
// Encapsulate object we want to store in vector (because contained type must have
// copy constructor and assignment operator
class my_int_buffer {
tbb::flow::buffer_node<int> *b;
tbb::flow::graph& my_graph;
public:
my_int_buffer(tbb::flow::graph &g) : my_graph(g) { b = new tbb::flow::buffer_node<int>(my_graph); }
my_int_buffer(const my_int_buffer& other) : my_graph(other.my_graph) {
b = new tbb::flow::buffer_node<int>(my_graph);
}
~my_int_buffer() { delete b; }
my_int_buffer& operator=(const my_int_buffer& /*other*/) {
return *this;
}
};
// test the graph iterator, delete nodes from graph, test again
void test_iterator() {
tbb::flow::graph g;
my_int_buffer a_buffer(g);
my_int_buffer b_buffer(g);
my_int_buffer c_buffer(g);
my_int_buffer *d_buffer = new my_int_buffer(g);
my_int_buffer e_buffer(g);
std::vector< my_int_buffer > my_buffer_vector(10, c_buffer);
int count = 0;
for (tbb::flow::graph::iterator it = g.begin(); it != g.end(); ++it) {
count++;
}
CHECK_MESSAGE( (count==15), "error in iterator count");
delete d_buffer;
count = 0;
for (tbb::flow::graph::iterator it = g.begin(); it != g.end(); ++it) {
count++;
}
CHECK_MESSAGE( (count==14), "error in iterator count");
my_buffer_vector.clear();
count = 0;
for (tbb::flow::graph::iterator it = g.begin(); it != g.end(); ++it) {
count++;
}
CHECK_MESSAGE( (count==4), "error in iterator count");
}
class AddRemoveBody : utils::NoAssign {
tbb::flow::graph& g;
int nThreads;
utils::SpinBarrier &barrier;
public:
AddRemoveBody(int nthr, utils::SpinBarrier &barrier_, tbb::flow::graph& _g) :
g(_g), nThreads(nthr), barrier(barrier_)
{}
void operator()(const int /*threadID*/) const {
my_int_buffer b(g);
{
std::vector<my_int_buffer> my_buffer_vector(100, b);
barrier.wait(); // wait until all nodes are created
// now test that the proper number of nodes were created
int count = 0;
for (tbb::flow::graph::iterator it = g.begin(); it != g.end(); ++it) {
count++;
}
CHECK_MESSAGE( (count==101*nThreads), "error in iterator count");
barrier.wait(); // wait until all threads are done counting
} // all nodes but for the initial node on this thread are deleted
barrier.wait(); // wait until all threads have deleted all nodes in their vectors
// now test that all the nodes were deleted except for the initial node
int count = 0;
for (tbb::flow::graph::iterator it = g.begin(); it != g.end(); ++it) {
count++;
}
CHECK_MESSAGE( (count==nThreads), "error in iterator count");
barrier.wait(); // wait until all threads are done counting
} // initial node gets deleted
};
void test_parallel(int nThreads) {
tbb::flow::graph g;
utils::SpinBarrier barrier(nThreads);
AddRemoveBody body(nThreads, barrier, g);
NativeParallelFor(nThreads, body);
}
/*
* Functors for graph arena spawn tests
*/
inline void check_arena(tbb::task_arena* midway_arena) {
CHECK_MESSAGE(midway_arena->max_concurrency() == 2, "");
CHECK_MESSAGE(tbb::this_task_arena::max_concurrency() == 1, "");
}
struct run_functor {
tbb::task_arena* midway_arena;
int return_value;
run_functor(tbb::task_arena* a) : midway_arena(a), return_value(1) {}
int operator()() {
check_arena(midway_arena);
return return_value;
}
};
template < typename T >
struct function_body {
tbb::task_arena* midway_arena;
function_body(tbb::task_arena* a) : midway_arena(a) {}
tbb::flow::continue_msg operator()(const T& /*arg*/) {
check_arena(midway_arena);
return tbb::flow::continue_msg();
}
};
typedef tbb::flow::multifunction_node< int, std::tuple< int > > mf_node;
struct multifunction_body {
tbb::task_arena* midway_arena;
multifunction_body(tbb::task_arena* a) : midway_arena(a) {}
void operator()(const int& /*arg*/, mf_node::output_ports_type& /*outports*/) {
check_arena(midway_arena);
}
};
struct input_body {
tbb::task_arena* midway_arena;
int counter;
input_body(tbb::task_arena* a) : midway_arena(a), counter(0) {}
int operator()(tbb::flow_control &fc) {
check_arena(midway_arena);
if (counter++ >= 1) {
fc.stop();
}
return int();
}
};
struct nodes_test_functor : utils::NoAssign {
tbb::task_arena* midway_arena;
tbb::flow::graph& my_graph;
nodes_test_functor(tbb::task_arena* a, tbb::flow::graph& g) : midway_arena(a), my_graph(g) {}
void operator()() const {
// Define test nodes
// Continue, function, source nodes
tbb::flow::continue_node< tbb::flow::continue_msg > c_n(my_graph, function_body<tbb::flow::continue_msg>(midway_arena));
tbb::flow::function_node< int > f_n(my_graph, tbb::flow::unlimited, function_body<int>(midway_arena));
tbb::flow::input_node< int > s_n(my_graph, input_body(midway_arena));
// Multifunction node
mf_node m_n(my_graph, tbb::flow::unlimited, multifunction_body(midway_arena));
// Join node
tbb::flow::function_node< std::tuple< int, int > > join_f_n(
my_graph, tbb::flow::unlimited, function_body< std::tuple< int, int > >(midway_arena)
);
tbb::flow::join_node< std::tuple< int, int > > j_n(my_graph);
make_edge(j_n, join_f_n);
// Split node
tbb::flow::function_node< int > split_f_n1 = f_n;
tbb::flow::function_node< int > split_f_n2 = f_n;
tbb::flow::split_node< std::tuple< int, int > > sp_n(my_graph);
make_edge(tbb::flow::output_port<0>(sp_n), split_f_n1);
make_edge(tbb::flow::output_port<1>(sp_n), split_f_n2);
// Overwrite node
tbb::flow::function_node< int > ow_f_n = f_n;
tbb::flow::overwrite_node< int > ow_n(my_graph);
make_edge(ow_n, ow_f_n);
// Write once node
tbb::flow::function_node< int > w_f_n = f_n;
tbb::flow::write_once_node< int > w_n(my_graph);
make_edge(w_n, w_f_n);
// Buffer node
tbb::flow::function_node< int > buf_f_n = f_n;
tbb::flow::buffer_node< int > buf_n(my_graph);
make_edge(w_n, buf_f_n);
// Limiter node
tbb::flow::function_node< int > l_f_n = f_n;
tbb::flow::limiter_node< int > l_n(my_graph, 1);
make_edge(l_n, l_f_n);
// Execute nodes
c_n.try_put( tbb::flow::continue_msg() );
f_n.try_put(1);
m_n.try_put(1);
s_n.activate();
tbb::flow::input_port<0>(j_n).try_put(1);
tbb::flow::input_port<1>(j_n).try_put(1);
std::tuple< int, int > sp_tuple(1, 1);
sp_n.try_put(sp_tuple);
ow_n.try_put(1);
w_n.try_put(1);
buf_n.try_put(1);
l_n.try_put(1);
my_graph.wait_for_all();
}
};
void test_graph_arena() {
// There is only one thread for execution (external thread).
// So, if graph's tasks get spawned in different arena
// external thread won't be able to find them in its own arena.
// In this case test should hang.
tbb::task_arena arena(1);
arena.execute(
[&]() {
tbb::flow::graph g;
tbb::task_arena midway_arena;
midway_arena.initialize(2);
midway_arena.execute(nodes_test_functor(&midway_arena, g));
}
);
}
//! Test wait counts
//! \brief error_guessing
TEST_CASE("Test wait_count"){
for(unsigned int p=utils::MinThread; p<=utils::MaxThread; ++p ) {
tbb::task_arena arena(p);
arena.execute(
[&]() {
test_wait_count();
}
);
}
}
//! Test graph iterators
//! \brief interface
TEST_CASE("Test graph::iterator"){
for(unsigned int p=utils::MinThread; p<=utils::MaxThread; ++p ) {
tbb::task_arena arena(p);
arena.execute(
[&]() {
test_iterator();
}
);
}
}
//! Test parallel for body
//! \brief \ref error_guessing
TEST_CASE("Test parallel"){
for(unsigned int p=utils::MinThread; p<=utils::MaxThread; ++p ) {
tbb::task_arena arena(p);
arena.execute(
[&]() {
test_parallel(p);
}
);
}
}
//! Test separate arena isn't used
//! \brief \ref error_guessing
TEST_CASE("Test graph_arena"){
test_graph_arena();
}
//! Graph iterator
//! \brief \ref error_guessing
TEST_CASE("graph iterator") {
using namespace tbb::flow;
graph g;
auto past_end = g.end();
++past_end;
continue_node<int> n(g, [](const continue_msg &){return 1;});
size_t item_count = 0;
for(auto it = g.cbegin(); it != g.cend(); it++)
++item_count;
CHECK_MESSAGE((item_count == 1), "Should find 1 item");
item_count = 0;
auto jt(g.begin());
for(; jt != g.end(); jt++)
++item_count;
CHECK_MESSAGE((item_count == 1), "Should find 1 item");
graph g2;
continue_node<int> n2(g, [](const continue_msg &){return 1;});
CHECK_MESSAGE((g.begin() != g2.begin()), "Different graphs should have different iterators");
}
|