ttg 1.0.0
Template Task Graph (TTG): flowgraph-based programming model for high-performance distributed-memory algorithms
Loading...
Searching...
No Matches
distributed.cc

This is the iterative diamond DAG with variable number of inputs using the reducing terminals of Template Task Graph, adapted to run in distributed: iteratively, a reducing diamond of data-dependent width is run, until the amount of data gathered at the bottom of the diamond exceeds a given threshold. First and last tasks of each diamond are run on rank 0, while the tasks inside the diamond are distributed between the ranks in a round-robin fashion.

// SPDX-License-Identifier: BSD-3-Clause
#include <ttg.h>
const double threshold = 100.0;
using Key2 = std::pair<int, int>;
namespace std {
std::ostream &operator<<(std::ostream &os, const Key2 &key) {
os << "{" << std::get<0>(key) << ", " << std::get<1>(key) << "}";
return os;
}
} // namespace std
static void b(const Key2 &key, const double &input, std::tuple<ttg::Out<int, double>> &out) {
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);
}
static void c(const int &k, const double &sum, std::tuple<ttg::Out<int, double>> &out) {
ttg::print("Called task C(", k, ") on rank", ttg::ttg_default_execution_context().rank(), "with input ", sum);
if (sum < threshold) {
ttg::print(" ", sum, "<", threshold, " so continuing to iterate");
ttg::send<0>(k + 1, sum, out);
} else {
ttg::print(" ", sum, ">=", threshold, " so stopping the iterations");
}
}
int main(int argc, char **argv) {
ttg::initialize(argc, argv, -1);
ttg::Edge<Key2, double> A_B("A(k)->B(k, i)");
ttg::Edge<int, double> B_C("B(k, i)->C(k)");
ttg::Edge<int, double> C_A("C(k)->A(k)");
auto wc(ttg::make_tt(c, ttg::edges(B_C), ttg::edges(C_A), "C", {"From B"}, {"to A"}));
wc->set_input_reducer<0>([](double &a, const double &b) { a += b; });
auto wa(ttg::make_tt([&](const int &k, const double &input, std::tuple<ttg::Out<Key2, double>> &out) {
ttg::print("Called task A(", k, ") on rank", ttg::ttg_default_execution_context().rank());
wc->set_argstream_size<0>(k, k+1);
for(int i = 0; i < k+1; i++) {
ttg::send<0>(Key2{k, i}, 1.0 + k + input, out);
}
}, ttg::edges(C_A), ttg::edges(A_B), "A", {"from C"}, {"to B"}));
auto wb(ttg::make_tt(b, ttg::edges(A_B), ttg::edges(B_C), "B", {"from A"}, {"to C"}));
wa->set_keymap([&](const int &k) { return 0; });
wb->set_keymap([&](const Key2 &k) { return std::get<1>(k) % wb->get_world().size(); });
wc->set_keymap([&](const int &k) { return 0; });
if (wa->get_world().rank() == 0) wa->invoke(0, 0.0);
return EXIT_SUCCESS;
}
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
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 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
int rank(World world=default_execution_context())
Definition run.h:127
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