ttg 1.0.0
Template Task Graph (TTG): flowgraph-based programming model for high-performance distributed-memory algorithms
Loading...
Searching...
No Matches
iterative.cc
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2#include <ttg.h>
5
6const double threshold = 100.0;
7using Key2 = std::pair<int, int>;
8
9namespace std {
10 std::ostream &operator<<(std::ostream &os, const Key2 &key) {
11 os << "{" << std::get<0>(key) << ", " << std::get<1>(key) << "}";
12 return os;
13 }
14} // namespace std
15
16static void a(const int &k, const double &input, std::tuple<ttg::Out<Key2, double>> &out) {
17 ttg::print("Called task A(", k, ")");
ttg::send <0>(Key2{k, 0}, 1.0 + input, out); ttg::send <0>(Key2{k, 1}, 2.0 + input, out);
20}
21
22static void b(const Key2 &key, const double &input, std::tuple<ttg::Out<int, double>, ttg::Out<int, double>> &out) {
23 ttg::print("Called task B(", key, ") with input data ", input);
24 if (std::get<1>(key) == 0)
ttg::send <0>(std::get<0>(key), input + 1.0, out);
26 else
ttg::send <1>(std::get<0>(key), input + 1.0, out);
28}
29
30static void c(const int &k, const double &b0, const double &b1, std::tuple<ttg::Out<int, double>> &out) {
31 ttg::print("Called task C(", k, ") with inputs ", b0, " from B(", k, " 0) and ", b1, " from B(", k, " 1)");
32 if (b0 + b1 < threshold) {
33 ttg::print(" ", b0, "+", b1, "<", threshold, " so continuing to iterate");
ttg::send <0>(k + 1, b0 + b1, out);
35 } else {
36 ttg::print(" ", b0, "+", b1, ">=", threshold, " so stopping the iterations");
37 }
38}
39
40int main(int argc, char **argv) {
41 ttg::initialize(argc, argv, -1);
42
43 ttg::Edge<Key2, double> A_B("A(k)->B(k)");
44 ttg::Edge<int, double> B_C0("B(k)->C0(k)");
45 ttg::Edge<int, double> B_C1("B(k)->C1(k)");
46 ttg::Edge<int, double> C_A("C(k)->A(k)");
47
48 auto wa(ttg::make_tt(a, ttg::edges(C_A), ttg::edges(A_B), "A", {"from C"}, {"to B"}));
49 auto wb(ttg::make_tt(b, ttg::edges(A_B), ttg::edges(B_C0, B_C1), "B", {"from A"},
50 {"to 1st input of C", "to 2nd input of C"}));
51 auto wc(ttg::make_tt(c, ttg::edges(B_C0, B_C1), ttg::edges(C_A), "C", {"From B", "From B"}, {"to A"}));
52
54
55 if (wa->get_world().rank() == 0) wa->invoke(0, 0.0);
56
59
61 return EXIT_SUCCESS;
62}
63
Edge is used to connect In and Out terminals.
Definition edge.h:26
const double threshold
Definition distributed.cc:6
int main(int argc, char **argv)
std::pair< int, int > Key2
Definition distributed.cc:7
STL namespace.
std::ostream & operator<<(std::ostream &os, ttg::device::Device device)
Definition device.h:84
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 send()
Sends a control message (message without an accompanying task id or a value) to the template tasks at...
Definition func.h:341
ttg::World & get_default_world()
Definition world.h:81
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
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:131
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