broadcast.h
Go to the documentation of this file.
1 //
2 // Created by Eduard Valeyev on 12/29/17.
3 //
4 
5 #ifndef TTG_BROADCAST_H
6 #define TTG_BROADCAST_H
7 
8 #include <tuple>
9 
10 #include "ttg/func.h"
11 #include "ttg/fwd.h"
12 #include "ttg/tt.h"
13 #include "ttg/util/tree.h"
14 #include "ttg/world.h"
15 
16 namespace ttg {
17 
27  template <typename Value, typename OutKey = int>
28  class BinaryTreeBroadcast : public TT<int, std::tuple<Out<int, Value>, Out<int, Value>, Out<OutKey, Value>>,
29  BinaryTreeBroadcast<Value, OutKey>, ttg::typelist<Value>> {
30  public:
31  using baseT = typename BinaryTreeBroadcast::ttT;
32 
33  BinaryTreeBroadcast(Edge<int, Value> &in, Edge<OutKey, Value> &out, std::vector<OutKey> local_keys, int root = 0,
34  World world = ttg::default_execution_context(), int max_key = -1,
36  : baseT(edges(fuse(in, inout_l, inout_r)), edges(inout_l, inout_r, out), "BinaryTreeBroadcast",
37  {"in|inout_l|inout_r"}, {"inout_l", "inout_r", "out"}, world, [](int key) { return key; })
38  , tree_((max_key == -1 ? world.size() : max_key), root)
39  , local_keys_(std::move(local_keys)) {}
40 
41  void op(const int &key, typename baseT::input_values_tuple_type &&indata,
42  std::tuple<Out<int, Value>, Out<int, Value>, Out<int, Value>> &outdata) {
43  assert(key < tree_.size());
44  assert(key == this->get_world().rank());
45  auto children = tree_.child_keys(key);
46  if (children.first != -1) send<0>(children.first, this->template get<0, const Value &>(indata), outdata);
47  if (children.second != -1) send<1>(children.second, this->template get<0, const Value &>(indata), outdata);
48  broadcast<2>(local_keys_, this->template get<0, const Value &>(indata), outdata);
49  }
50 
51  private:
52  BinarySpanningTree tree_;
53  std::vector<OutKey> local_keys_;
54  };
55 
56 } // namespace ttg
57 
58 #endif // TTG_BROADCAST_H
a binary spanning tree of integers in the [0,size) interval
Definition: tree.h:17
const auto size() const
Definition: tree.h:26
std::pair< int, int > child_keys(const int parent_key) const
Definition: tree.h:40
generic binary broadcast of a value to a set of {key,value} pairs
Definition: broadcast.h:29
typename BinaryTreeBroadcast::ttT baseT
Definition: broadcast.h:31
void op(const int &key, typename baseT::input_values_tuple_type &&indata, std::tuple< Out< int, Value >, Out< int, Value >, Out< int, Value >> &outdata)
Definition: broadcast.h:41
BinaryTreeBroadcast(Edge< int, Value > &in, Edge< OutKey, Value > &out, std::vector< OutKey > local_keys, int root=0, World world=ttg::default_execution_context(), int max_key=-1, Edge< int, Value > inout_l=Edge< int, Value >{}, Edge< int, Value > inout_r=Edge< int, Value >{})
Definition: broadcast.h:33
Edge is used to connect In and Out terminals.
Definition: edge.h:25
std::tuple_element_t< i, std::tuple< Out< int, Value >, Out< int, Value >, Out< int, Value > > > * out()
Definition: ttg.h:4212
top-level TTG namespace contains runtime-neutral functionality
Definition: keymap.h:8
auto fuse(const Edge< keyT, valuesT > &...args)
Fuse edges into one This allows receiving one data from either of the combined edges.
Definition: func.h:137
World default_execution_context()
Accesses the default backend's default execution context.
Definition: run.h:68
int rank(World world=default_execution_context())
Definition: run.h:85
auto edges(inedgesT &&...args)
Make a tuple of Edges to pass to.
Definition: func.h:147