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