ttg 1.0.0-alpha
Template Task Graph (TTG): flowgraph-based programming model for high-performance distributed-memory algorithms
Loading...
Searching...
No Matches
distributed.cc
Go to the documentation of this file.
1#include <ttg.h>
4
5const double threshold = 100.0;
6using Key2 = std::pair<int, int>;
7
8namespace std {
9 std::ostream &operator<<(std::ostream &os, const Key2 &key) {
10 os << "{" << std::get<0>(key) << ", " << std::get<1>(key) << "}";
11 return os;
12 }
13} // namespace std
14
15static void b(const Key2 &key, const double &input, std::tuple<ttg::Out<int, double>> &out) {
16 ttg::print("Called task B(", key, ") on rank", ttg::ttg_default_execution_context().rank(), "with input data ", input);
ttg::send<0>(std::get<0>(key), input + 1.0, out);
18}
19
20static void c(const int &k, const double &sum, std::tuple<ttg::Out<int, double>> &out) {
21 ttg::print("Called task C(", k, ") on rank", ttg::ttg_default_execution_context().rank(), "with input ", sum);
22 if (sum < threshold) {
23 ttg::print(" ", sum, "<", threshold, " so continuing to iterate");
25 ttg::send<0>(k + 1, sum, out);
26 } else {
27 ttg::print(" ", sum, ">=", threshold, " so stopping the iterations");
28 }
29}
30
31int main(int argc, char **argv) {
32 ttg::initialize(argc, argv, -1);
33
34 ttg::Edge<Key2, double> A_B("A(k)->B(k, i)");
35 ttg::Edge<int, double> B_C("B(k, i)->C(k)");
36 ttg::Edge<int, double> C_A("C(k)->A(k)");
37
38 auto wc(ttg::make_tt(c, ttg::edges(B_C), ttg::edges(C_A), "C", {"From B"}, {"to A"}));
39
wc->set_input_reducer<0>([](double &a, const double &b) { a += b; });
41
42 auto wa(ttg::make_tt([&](const int &k, const double &input, std::tuple<ttg::Out<Key2, double>> &out) {
43 ttg::print("Called task A(", k, ") on rank", ttg::ttg_default_execution_context().rank());
44 wc->set_argstream_size<0>(k, k+1);
45 for(int i = 0; i < k+1; i++) {
47 ttg::send<0>(Key2{k, i}, 1.0 + k + input, out);
48 }
49 }, ttg::edges(C_A), ttg::edges(A_B), "A", {"from C"}, {"to B"}));
50
51 auto wb(ttg::make_tt(b, ttg::edges(A_B), ttg::edges(B_C), "B", {"from A"}, {"to C"}));
52
53 wa->set_keymap([&](const int &k) { return 0; });
54 wb->set_keymap([&](const Key2 &k) { return std::get<1>(k) % wb->get_world().size(); });
55 wc->set_keymap([&](const int &k) { return 0; });
56
58
59 if (wa->get_world().rank() == 0) wa->invoke(0, 0.0);
60
63
65 return EXIT_SUCCESS;
66}
67
Edge is used to connect In and Out terminals.
Definition edge.h:25
const double threshold
Definition distributed.cc:5
int main(int argc, char **argv)
std::pair< int, int > Key2
Definition distributed.cc:6
STL namespace.
std::ostream & operator<<(std::ostream &os, ttg::device::Device device)
Definition device.h:83
ttg::World ttg_default_execution_context()
Definition ttg.h:1136
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 &&...)
void send()
Sends a control message (message without an accompanying task id or a value) to the template tasks at...
Definition func.h:340
ttg::World & get_default_world()
Definition world.h:80
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 finalize()
Finalizes the TTG runtime.
Definition func.h:589
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<(std::is_convertible_v< decltype(*(std::declval< TTBasePtrs >())), TTBase & > &&...), bool > make_graph_executable(TTBasePtrs &&...tts)
Make the TTG tts executable. Applies.
Definition func.h:80
auto edges(inedgesT &&...args)
Make a tuple of Edges to pass to.
Definition func.h:147