ttg 1.0.0
Template Task Graph (TTG): flowgraph-based programming model for high-performance distributed-memory algorithms
Loading...
Searching...
No Matches
fibonacci.cc
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2#include <ttg.h>
3#include "ttg/serialization.h"
4
6struct Fn {
7 int64_t F[2]; // F[0] = F_n, F[1] = F_{n-1}
8 Fn() { F[0] = 1; F[1] = 0; }
9 template <typename Archive>
10 void serialize(Archive& ar) {
11 ar & F;
12 }
13 template <typename Archive>
14 void serialize(Archive& ar, const unsigned int) {
15 ar & F;
16 }
17};
18auto make_ttg_fib_lt(const int64_t F_n_max = 1000) {
21
22 auto fib = ttg::make_tt(
23 [=](int64_t n, Fn&& f_n) {
24 int64_t next_f_n = f_n.F[0] + f_n.F[1];
25 f_n.F[1] = f_n.F[0];
26 f_n.F[0] = next_f_n;
27 if (next_f_n < F_n_max) {
28 ttg::send<0>(n + 1, f_n);
29 } else {
30 ttg::sendv<1>(f_n);
31 }
32 },
33 ttg::edges(f2f), ttg::edges(f2f, f2p), "fib");
34
35 auto print = ttg::make_tt(
36 [=](Fn&& f_n) {
37 std::cout << "The largest Fibonacci number smaller than " << F_n_max << " is " << f_n.F[1] << std::endl;
38 },
39 ttg::edges(f2p), ttg::edges(), "print");
40 auto ins = std::make_tuple(fib->template in<0>());
41 std::vector<std::unique_ptr<ttg::TTBase>> ops;
42 ops.emplace_back(std::move(fib));
43 ops.emplace_back(std::move(print));
44 return make_ttg(std::move(ops), ins, std::make_tuple(), "Fib_n < N");
45}
46
47int main(int argc, char* argv[]) {
48 ttg::initialize(argc, argv, -1);
49 int64_t N = (argc > 1) ? std::atol(argv[1]) : 1000;
50
51 // make TTG
52 auto fib = make_ttg_fib_lt(N);
53 // program complete, declare it executable
55 // start execution
57 // start the computation by sending the first message
58 if (ttg::default_execution_context().rank() == 0)
59 fib->template in<0>()->send(1, Fn{});;
60 // wait for the computation to finish
61 ttg::fence();
62
64 return 0;
65}
Edge is used to connect In and Out terminals.
Definition edge.h:26
int main(int argc, char *argv[])
Definition fibonacci.cc:47
auto make_ttg_fib_lt(const int64_t F_n_max=1000)
Definition fibonacci.cc:18
const int64_t F_n_max
void execute(ttg::World world)
Starts the execution in the given execution context.
Definition run.h:116
void initialize(int argc, char **argv, int num_threads=-1, RestOfArgs &&...)
World default_execution_context()
Accesses the default backend's default execution context.
Definition run.h:110
void fence(ttg::World world)
Returns when all tasks associated with the given execution context have finished on all ranks.
Definition run.h:123
void finalize()
Finalizes the TTG runtime.
Definition func.h:590
std::enable_if_t<(std::is_convertible_v< decltype(*(std::declval< TTBasePtrs >())), TTBase & > &&...), bool > make_graph_executable(TTBasePtrs &&...tts)
Make the TTG tts executable. Applies.
Definition func.h:81
auto edges(inedgesT &&...args)
Make a tuple of Edges to pass to.
Definition func.h:148
N.B. contains values of F_n and F_{n-1}.
Definition fibonacci.cc:6
int64_t F[2]
Definition fibonacci.cc:7
void serialize(Archive &ar, const unsigned int)
Definition fibonacci.cc:14
Fn()
Definition fibonacci.cc:8
void serialize(Archive &ar)
Definition fibonacci.cc:10