fibonacci_device.cc
Go to the documentation of this file.
1 #include <ttg.h>
2 
3 #if defined(TTG_HAVE_CUDA)
4 #define ES ttg::ExecutionSpace::CUDA
5 #include "cuda_runtime.h"
7 #else
8 #error " CUDA is required to build this test!"
9 #endif
10 
11 #include "ttg/serialization.h"
12 
13 const int64_t F_n_max = 1000;
15 struct Fn : public ttg::TTValue<Fn> {
16  std::unique_ptr<int64_t[]> F; // F[0] = F_n, F[1] = F_{n-1}
18 
19  Fn() : F(std::make_unique<int64_t[]>(2)), b(F.get(), 2) { F[0] = 1; F[1] = 0; }
20 
21  Fn(const Fn&) = delete;
22  Fn(Fn&& other) = default;
23  Fn& operator=(const Fn& other) = delete;
24  Fn& operator=(Fn&& other) = default;
25 
26  template <typename Archive>
27  void serialize(Archive& ar) {
29  }
30  template <typename Archive>
31  void serialize(Archive& ar, const unsigned int) {
33  }
34 };
35 
36 auto make_ttg_fib_lt(const int64_t F_n_max = 1000) {
39 
40  auto fib = ttg::make_tt<ES>(
41  [=](int64_t n, Fn&& f_n) -> ttg::device::Task {
42  assert(n > 0);
43  ttg::trace("in fib: n=", n, " F_n=", f_n.F[0]);
44 
45  co_await ttg::device::select(f_n.b);
46 
47  next_value(f_n.b.current_device_ptr());
48 
49  // wait for the task to complete and the values to be brought back to the host
50  co_await ttg::device::wait(f_n.b);
51 
52  if (f_n.F[0] < F_n_max) {
53  co_await ttg::device::forward(ttg::device::send<0>(n + 1, std::move(f_n)));
54  } else {
55  co_await ttg::device::forward(ttg::device::sendv<1>(std::move(f_n)));
56  }
57  },
58  ttg::edges(f2f), ttg::edges(f2f, f2p), "fib");
59  auto print = ttg::make_tt(
60  [=](Fn&& f_n) {
61  std::cout << "The largest Fibonacci number smaller than " << F_n_max << " is " << f_n.F[1] << std::endl;
62  },
63  ttg::edges(f2p), ttg::edges(), "print");
64 
65  auto ins = std::make_tuple(fib->template in<0>());
66  std::vector<std::unique_ptr<::ttg::TTBase>> ops;
67  ops.emplace_back(std::move(fib));
68  ops.emplace_back(std::move(print));
69  return make_ttg(std::move(ops), ins, std::make_tuple(), "Fib_n < N");
70 }
71 
72 int main(int argc, char* argv[]) {
73  ttg::initialize(argc, argv, -1);
74  ttg::trace_on();
75  int64_t N = 1000;
76  if (argc > 1) N = std::atol(argv[1]);
77 
78  // make TTG
79  auto fib = make_ttg_fib_lt(N); // computes largest F_n < N
80  // program complete, declare it executable
81  ttg::make_graph_executable(fib.get());
82  // start execution
84  // start the computation by sending the first message
86  fib->template in<0>()->send(1, Fn{});;
87  // wait for the computation to finish
89 
90  ttg::finalize();
91  return 0;
92 }
Edge is used to connect In and Out terminals.
Definition: edge.h:25
void next_value(int64_t *fn_and_fnm1)
int main(int argc, char *argv[])
auto make_ttg_fib_lt(const int64_t F_n_max=1000)
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
constexpr auto get(span< E, S > s) -> decltype(s[N])
Definition: span.h:492
void ttg_abort()
Definition: ttg.h:138
ttg::World ttg_default_execution_context()
Definition: ttg.h:137
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
void trace_on()
enables tracing; if trace_enabled()==true this has no effect
Definition: trace.h:36
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
void trace(const T &t, const Ts &... ts)
Definition: trace.h:43
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
ttg::Buffer< int64_t > b
std::unique_ptr< int64_t[]> F
int64_t F[2]
Definition: fibonacci.cc:6
Fn(const Fn &)=delete
void serialize(Archive &ar, const unsigned int)
Fn & operator=(Fn &&other)=default
Fn(Fn &&other)=default
Fn & operator=(const Fn &other)=delete
void serialize(Archive &ar)