test_tbb_join.cxx
Go to the documentation of this file.
1 /**
2  This is a generic (no WC) test of how one can use the
3  tbb::flow::join_node in a way that breaks out the input ports of
4  the node into a std::vector. This test patterns needed to make the
5  graph edges in a dynamic rather than static/compiled manner.
6  */
7 
8 #include <tbb/flow_graph.h>
9 
10 #include <iostream>
11 using namespace std;
12 
13 struct CountDown {
14  const int index;
15  int count;
16  CountDown(int index, int n=10) : index(index), count(n) {
17  cerr << "CountDown("<<index << " , " <<n<<")\n";
18  }
19  bool operator()(int& x) {
20  if (!count) {
21  cerr << "CountDown("<<index<<"): EOS\n";
22  return false;
23  }
24  x = count--;
25  cerr << "CountDown("<<index<<"): " << x << endl;
26  return true;
27  }
28 };
29 
30 struct MyJoin {
31  bool operator()(const vector<int>& in, int &out) {
32  out = 0;
33  for (auto x : in) { out += x; }
34  return true;
35  }
36 };
37 
38 struct Chirp {
39  void operator()(const int& x) {
40  cerr << x << endl;
41  }
42 };
43 
44 // Get receivers of a join node in vector.
45 typedef tbb::flow::receiver<int> int_receiver;
46 
47 template<class TupleType, int N>
48 struct TupleHelper {
49  TupleHelper<TupleType, N-1> nm1helper;
50 
51  vector<int_receiver*> input_ports(tbb::flow::join_node<TupleType>& jn) {
52  vector<int_receiver*> ret = nm1helper.input_ports(jn);
53  int_receiver* rec = dynamic_cast<int_receiver*>(&tbb::flow::input_port<N-1>(jn));
54  ret.insert(ret.begin(), rec);
55  return ret;
56  }
57 
58  vector<int> values(const TupleType& t) {
59  vector<int> ret = nm1helper.values(t);
60  int val = std::get<N-1>(t);
61  ret.insert(ret.begin(), val);
62  return ret;
63  }
64 
65  // breakout tuple->vector to make this a function_node body
66  vector<int> operator()(const TupleType& t) {
67  return values(t);
68  }
69 
70 };
71 
72 template<class TupleType>
73 struct TupleHelper<TupleType,0> {
74 
75  vector<int_receiver*> input_ports(tbb::flow::join_node<TupleType>& jn) {
76  return vector<int_receiver*>();
77  }
78  vector<int> values(const TupleType& t) {
79  return vector<int>();
80  }
81 };
82 
83 // need a way to go from a number N to a collection of receivers
84 // corresponding to a tbb::flow::join_node input and the sender
85 // corresponding to a IJoinNode's output. N is given by the
86 // IJoinNode.
87 
88 struct Adder {
89  int operator()(const vector<int>& in) {
90  int tot=0;
91  std::string comma = "";
92  cerr << "Adding: ";
93  for (auto x : in) {
94  tot += x;
95  cerr << comma << x;
96  comma = " + ";
97  }
98  cerr << " = " << tot << "\n";
99  return tot;
100  }
101 };
102 
103 int main()
104 {
106 
107  typedef tbb::flow::source_node<int> int_source;
108  vector<int_source> countdowns;
109 
110  int n = 3; // explicitly nonconst
111  for (int i=0; i<n; ++i) {
112  countdowns.push_back(int_source(graph,CountDown(i),false));
113  }
114 
115  // join
116  typedef std::tuple<int,int,int> IntTriple;
118 
119  typedef tbb::flow::join_node< IntTriple > JoinInt3;
120  JoinInt3 jn(graph);
121  vector<int_receiver*> jrec = th.input_ports(jn);
122 
123  tbb::flow::function_node< std::tuple<int,int,int>, vector<int> > bo(graph, 0, th);
124  tbb::flow::function_node< vector<int>, int > fn(graph, 0, Adder());
125 
126  for (int i=0; i<n; ++i) {
127  make_edge(countdowns[i],*jrec[i]);
128  }
129  make_edge(jn, bo);
130  make_edge(bo, fn);
131  for (int i=0; i<n; ++i) {
132  countdowns[i].activate();
133  }
134 
135  graph.wait_for_all();
136 }
void operator()(const int &x)
CountDown(int index, int n=10)
vector< int > values(const TupleType &t)
std::string string
Definition: nybbler.cc:12
vector< int_receiver * > input_ports(tbb::flow::join_node< TupleType > &jn)
def graph(desc, maker=maker)
Definition: apa.py:294
STL namespace.
int operator()(const vector< int > &in)
vector< int > values(const TupleType &t)
tbb::flow::receiver< int > int_receiver
QAsciiDict< Entry > fn
vector< int_receiver * > input_ports(tbb::flow::join_node< TupleType > &jn)
vector< int > operator()(const TupleType &t)
Q_UINT16 values[128]
bool operator()(int &x)
bool operator()(const vector< int > &in, int &out)
TupleHelper< TupleType, N-1 > nm1helper
list x
Definition: train.py:276
int main()
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
std::size_t n
Definition: format.h:3399
QTextStream & endl(QTextStream &s)