iterative.cc
Go to the documentation of this file.
1 #include <ttg.h>
4 
5 const double threshold = 100.0;
6 using Key2 = std::pair<int, int>;
7 
8 namespace std {
9  std::ostream &operator<<(std::ostream &os, const Key2 &key) {
10  os << "{" << std::get<0>(key) << ", " << std::get<1>(key) << "}";
11  return os;
12  }
13 } // namespace std
14 
15 static void a(const int &k, const double &input, std::tuple<ttg::Out<Key2, double>> &out) {
16  ttg::print("Called task A(", k, ")"); ttg::send <0>(Key2{k, 0}, 1.0 + input, out); ttg::send <0>(Key2{k, 1}, 2.0 + input, out);
19 }
20 
21 static void b(const Key2 &key, const double &input, std::tuple<ttg::Out<int, double>, ttg::Out<int, double>> &out) {
22  ttg::print("Called task B(", key, ") with input data ", input);
23  if (std::get<1>(key) == 0) ttg::send <0>(std::get<0>(key), input + 1.0, out);
25  else ttg::send <1>(std::get<0>(key), input + 1.0, out);
27 }
28 
29 static void c(const int &k, const double &b0, const double &b1, std::tuple<ttg::Out<int, double>> &out) {
30  ttg::print("Called task C(", k, ") with inputs ", b0, " from B(", k, " 0) and ", b1, " from B(", k, " 1)");
31  if (b0 + b1 < threshold) {
32  ttg::print(" ", b0, "+", b1, "<", threshold, " so continuing to iterate"); ttg::send <0>(k + 1, b0 + b1, out);
34  } else {
35  ttg::print(" ", b0, "+", b1, ">=", threshold, " so stopping the iterations");
36  }
37 }
38 
39 int main(int argc, char **argv) {
40  ttg::initialize(argc, argv, -1);
41 
42  ttg::Edge<Key2, double> A_B("A(k)->B(k)");
43  ttg::Edge<int, double> B_C0("B(k)->C0(k)");
44  ttg::Edge<int, double> B_C1("B(k)->C1(k)");
45  ttg::Edge<int, double> C_A("C(k)->A(k)");
46 
47  auto wa(ttg::make_tt(a, ttg::edges(C_A), ttg::edges(A_B), "A", {"from C"}, {"to B"}));
48  auto wb(ttg::make_tt(b, ttg::edges(A_B), ttg::edges(B_C0, B_C1), "B", {"from A"},
49  {"to 1st input of C", "to 2nd input of C"}));
50  auto wc(ttg::make_tt(c, ttg::edges(B_C0, B_C1), ttg::edges(C_A), "C", {"From B", "From B"}, {"to A"}));
51 
53 
54  if (wa->get_world().rank() == 0) wa->invoke(0, 0.0);
55 
56  ttg::execute();
58 
59  ttg::finalize();
60  return EXIT_SUCCESS;
61 }
62 
Edge is used to connect In and Out terminals.
Definition: edge.h:25
std::pair< int, int > Key2
Definition: distributed.cc:6
const double threshold
Definition: iterative.cc:5
int main(int argc, char **argv)
Definition: iterative.cc:39
auto make_tt(funcT &&func, const std::tuple< ttg::Edge< keyT, input_edge_valuesT >... > &inedges=std::tuple<>{}, const std::tuple< output_edgesT... > &outedges=std::tuple<>{}, const std::string &name="wrapper", const std::vector< std::string > &innames=std::vector< std::string >(sizeof...(input_edge_valuesT), "input"), const std::vector< std::string > &outnames=std::vector< std::string >(sizeof...(output_edgesT), "output"))
Factory function to assist in wrapping a callable with signature.
Definition: make_tt.h:496
void execute(ttg::World world)
Starts the execution in the given execution context.
Definition: run.h:74
void send(const keyT &key, valueT &&value, ttg::Out< keyT, valueT > &t)
Sends a task id and a value to the given output terminal.
Definition: func.h:158
void initialize(int argc, char **argv, int num_threads=-1, RestOfArgs &&...)
std::enable_if_t<(std::is_convertible_v< decltype(*(std::declval< TTBasePtrs >))), TTBase & > bool make_graph_executable(TTBasePtrs &&...tts)
Definition: func.h:80
void fence(ttg::World world)
Returns when all tasks associated with the given execution context have finished on all ranks.
Definition: run.h:81
void print(const T &t, const Ts &... ts)
atomically prints to std::cout a sequence of items (separated by ttg::print_separator) followed by st...
Definition: print.h:130
std::enable_if_t<!meta::is_void_v< keyT >, void > finalize(const keyT &key, ttg::Out< out_keyT, out_valueT > &t)
Finalize streaming input terminals connecting to the given output terminal for tasks identified by ke...
Definition: func.h:543
ttg::World & get_default_world()
Definition: world.h:80
auto edges(inedgesT &&...args)
Make a tuple of Edges to pass to.
Definition: func.h:147