fibonacci.cc
Go to the documentation of this file.
1 #include <ttg.h>
2 #include "ttg/serialization.h"
3 
5 struct Fn {
6  int64_t F[2]; // F[0] = F_n, F[1] = F_{n-1}
7  Fn() { F[0] = 1; F[1] = 0; }
8  template <typename Archive>
9  void serialize(Archive& ar) {
10  ar & F;
11  }
12  template <typename Archive>
13  void serialize(Archive& ar, const unsigned int) {
14  ar & F;
15  }
16 };
17 auto make_ttg_fib_lt(const int64_t F_n_max = 1000) {
20 
21  auto fib = ttg::make_tt(
22  [=](int64_t n, Fn&& f_n) {
23  int64_t next_f_n = f_n.F[0] + f_n.F[1];
24  f_n.F[1] = f_n.F[0];
25  f_n.F[0] = next_f_n;
26  if (next_f_n < F_n_max) {
27  ttg::send<0>(n + 1, f_n);
28  } else {
29  ttg::sendv<1>(f_n);
30  }
31  },
32  ttg::edges(f2f), ttg::edges(f2f, f2p), "fib");
33 
34  auto print = ttg::make_tt(
35  [=](Fn&& f_n) {
36  std::cout << "The largest Fibonacci number smaller than " << F_n_max << " is " << f_n.F[1] << std::endl;
37  },
38  ttg::edges(f2p), ttg::edges(), "print");
39  auto ins = std::make_tuple(fib->template in<0>());
40  std::vector<std::unique_ptr<ttg::TTBase>> ops;
41  ops.emplace_back(std::move(fib));
42  ops.emplace_back(std::move(print));
43  return make_ttg(std::move(ops), ins, std::make_tuple(), "Fib_n < N");
44 }
45 
46 int main(int argc, char* argv[]) {
47  ttg::initialize(argc, argv, -1);
48  int64_t N = (argc > 1) ? std::atol(argv[1]) : 1000;
49 
50  // make TTG
51  auto fib = make_ttg_fib_lt(N);
52  // program complete, declare it executable
53  ttg::make_graph_executable(fib.get());
54  // start execution
55  ttg::execute();
56  // start the computation by sending the first message
58  fib->template in<0>()->send(1, Fn{});;
59  // wait for the computation to finish
60  ttg::fence();
61 
62  ttg::finalize();
63  return 0;
64 }
Edge is used to connect In and Out terminals.
Definition: edge.h:25
int main(int argc, char *argv[])
Definition: fibonacci.cc:46
auto make_ttg_fib_lt(const int64_t F_n_max=1000)
Definition: fibonacci.cc:17
const int64_t F_n_max
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 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
World default_execution_context()
Accesses the default backend's default execution context.
Definition: run.h:68
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
int rank(World world=default_execution_context())
Definition: run.h:85
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
auto make_ttg(ttseqT &&tts, const input_terminalsT &ins, const output_terminalsT &outs, const std::string &name="ttg")
Definition: tt.h:113
auto edges(inedgesT &&...args)
Make a tuple of Edges to pass to.
Definition: func.h:147
N.B. contains values of F_n and F_{n-1}.
Definition: fibonacci.cc:5
int64_t F[2]
Definition: fibonacci.cc:6
void serialize(Archive &ar, const unsigned int)
Definition: fibonacci.cc:13
Fn()
Definition: fibonacci.cc:7
void serialize(Archive &ar)
Definition: fibonacci.cc:9