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::shared_ptr<int64_t[]> F; // F[0] = F_n, F[1] = F_{n-1}
19
20 Fn() : F(std::make_shared<int64_t[]>(2)), b(F, 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#ifdef TTG_SERIALIZATION_SUPPORTS_MADNESS
28 template <typename Archive>
29 void serialize(Archive& ar) {
30 ar & madness::archive::wrap(F.get(), 2) & b;
31 }
32#endif
33#ifdef TTG_SERIALIZATION_SUPPORTS_BOOST
34 template <typename Archive>
35 void serialize(Archive& ar, const unsigned int) {
36 ar & boost::serialization::make_array(F.get(), 2) & b;
37 }
38#endif
39};
40
41auto make_ttg_fib_lt(const int64_t F_n_max = 1000) {
44
45 auto fib = ttg::make_tt<ES>(
46 [=](int64_t n, Fn&& f_n) -> ttg::device::Task {
47 assert(n > 0);
48 ttg::trace("in fib: n=", n, " F_n=", f_n.F[0]);
49
50 co_await ttg::device::select(f_n.b);
51
52 next_value(f_n.b.current_device_ptr());
53
54 // wait for the task to complete and the values to be brought back to the host
55 co_await ttg::device::wait(f_n.b);
56
57 if (f_n.F[0] < F_n_max) {
58 co_await ttg::device::forward(ttg::device::send<0>(n + 1, std::move(f_n)));
59 } else {
60 co_await ttg::device::forward(ttg::device::sendv<1>(std::move(f_n)));
61 }
62 },
63 ttg::edges(f2f), ttg::edges(f2f, f2p), "fib");
64 auto print = ttg::make_tt(
65 [=](Fn&& f_n) {
66 std::cout << "The largest Fibonacci number smaller than " << F_n_max << " is " << f_n.F[1] << std::endl;
67 },
68 ttg::edges(f2p), ttg::edges(), "print");
69
70 auto ins = std::make_tuple(fib->template in<0>());
71 std::vector<std::unique_ptr<::ttg::TTBase>> ops;
72 ops.emplace_back(std::move(fib));
73 ops.emplace_back(std::move(print));
74 return make_ttg(std::move(ops), ins, std::make_tuple(), "Fib_n < N");
75}
76
77int main(int argc, char* argv[]) {
78 ttg::initialize(argc, argv, -1);
80 int64_t N = 1000;
81 if (argc > 1) N = std::atol(argv[1]);
82
83 // make TTG
84 auto fib = make_ttg_fib_lt(N); // computes largest F_n < N
85 // program complete, declare it executable
87 // start execution
89 // start the computation by sending the first message
90 if (ttg::default_execution_context().rank() == 0)
91 fib->template in<0>()->send(1, Fn{});;
92 // wait for the computation to finish
94
96 return 0;
97}
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
int64_t F[2]
Definition fibonacci.cc:7
Fn(const Fn &)=delete
Fn & operator=(const Fn &other)=delete
std::shared_ptr< int64_t[]> F
Fn(Fn &&other)=default
void serialize(Archive &ar)
Definition fibonacci.cc:10