_test_tbb_composite.cxx
Go to the documentation of this file.
1 /*
2  This test requires TBB to be built with C++11 support which to really
3  make happens requires a patch to its build system at least as of
4  tbb44_20151115oss something like:
5 
6 $ diff -u build/linux.gcc.inc{~,}
7 --- build/linux.gcc.inc~ 2015-11-25 05:48:43.000000000 -0500
8 +++ build/linux.gcc.inc 2016-01-07 17:56:54.594682208 -0500
9 @@ -42,6 +42,10 @@
10  ifneq (,$(shell gcc -dumpversion | egrep "^(4\.[4-9]|[5-9])"))
11  CPP11_FLAGS = -std=c++0x -D_TBB_CPP0X
12  endif
13 +# gcc 4.8 and higher support -std=c++11
14 +ifneq (,$(shell gcc -dumpversion | egrep "^(4\.[8-9]|[5-9])"))
15 + CPP11_FLAGS = -std=c++11 -D_TBB_CPP0X
16 +endif
17 
18  # gcc 4.2 and higher support OpenMP
19  ifneq (,$(shell gcc -dumpversion | egrep "^(4\.[2-9]|[5-9])"))
20 
21 
22 
23 */
24 
25 #include <tbb/flow_graph.h>
26 
27 #include <iostream>
28 #include <tuple>
29 
30 struct CountDown {
31  const int index;
32  int count;
33  CountDown(int index, int n=10) : index(index), count(n) {
34  std::cerr << "CountDown("<<index << " , " <<n<<")\n";
35  }
36  bool operator()(int& x) {
37  if (!count) {
38  std::cerr << "CountDown("<<index<<"): EOS\n";
39  return false;
40  }
41  x = count--;
42  std::cerr << "CountDown("<<index<<"): " << x << std::endl;
43  return true;
44  }
45 };
46 
47 class Adder : public tbb::flow::composite_node< std::tuple< int, int >, std::tuple< int > > {
48  tbb::flow::join_node< std::tuple<int, int>, queueing > j;
49  tbb::flow::function_node< std::tuple< int, int >, int > f;
50  typedef tbb::flow::composite_node< std::tuple< int, int >, std::tuple< int > > base_type;
51 
52  struct f_body {
53  int operator()( const std::tuple< int, int > &t ) {
54  int n = (std::get<1>(t)+1)/2;
55  int sum = std::get<0>(t) + std::get<1>(t);
56  std::cout << "Sum of the first " << n <<" positive odd numbers is " << n <<" squared: " << sum << std::endl;
57  return sum;
58  }
59  };
60 
61 public:
62  Adder( tbb::flow::graph &g) : base_type(g), j(g), f(g, tbb::flow::unlimited, f_body() ) {
63  make_edge( j, f );
64  base_type::input_ports_type input_tuple(input_port<0>(j), input_port<1>(j));
65  base_type::output_ports_type output_tuple(f);
66  base_type::set_external_ports(input_tuple, output_tuple);
67  }
68 };
CountDown(int index, int n=10)
tbb::flow::function_node< std::tuple< int, int >, int > f
Adder(tbb::flow::graph &g)
def graph(desc, maker=maker)
Definition: apa.py:294
static const double g
Definition: Units.h:145
tbb::flow::composite_node< std::tuple< int, int >, std::tuple< int > > base_type
bool operator()(int &x)
IFrame::pointer sum(std::vector< IFrame::pointer > frames, int ident)
Definition: FrameUtil.cxx:15
tbb::flow::join_node< std::tuple< int, int >, queueing > j
list x
Definition: train.py:276
int operator()(const std::tuple< int, int > &t)
std::size_t n
Definition: format.h:3399
QTextStream & endl(QTextStream &s)