1 /* Copyright (c) 2017-2023. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 /* This example demonstrates how to dynamically modify a graph of tasks.
8 * Assuming we have two instances of a service placed on different hosts,
9 * we want to send data alternatively to thoses instances.
11 * We consider the following graph:
13 * comm0 -> exec1 -> comm1
16 * With exec1 and exec2 on different hosts.
19 #include "simgrid/plugins/task.hpp"
20 #include "simgrid/s4u.hpp"
22 XBT_LOG_NEW_DEFAULT_CATEGORY(task_switch_host, "Messages specific for this task example");
24 int main(int argc, char* argv[])
26 simgrid::s4u::Engine e(&argc, argv);
27 e.load_platform(argv[1]);
28 simgrid::plugins::Task::init();
31 auto* tremblay = e.host_by_name("Tremblay");
32 auto* jupiter = e.host_by_name("Jupiter");
33 auto* fafard = e.host_by_name("Fafard");
36 auto comm0 = simgrid::plugins::CommTask::init("comm0");
37 comm0->set_bytes(1e7);
38 comm0->set_source(tremblay);
39 auto exec1 = simgrid::plugins::ExecTask::init("exec1", 1e9, jupiter);
40 auto exec2 = simgrid::plugins::ExecTask::init("exec2", 1e9, fafard);
41 auto comm1 = simgrid::plugins::CommTask::init("comm1", 1e7, jupiter, tremblay);
42 auto comm2 = simgrid::plugins::CommTask::init("comm2", 1e7, fafard, tremblay);
44 // Create the initial graph by defining dependencies between tasks
45 comm0->add_successor(exec2);
46 exec1->add_successor(comm1);
47 exec2->add_successor(comm2);
49 // Add a function to be called when tasks end for log purpose
50 simgrid::plugins::Task::on_end_cb([](const simgrid::plugins::Task* t) {
51 XBT_INFO("Task %s finished (%d)", t->get_name().c_str(), t->get_count());
54 // Add a function to be called before each executions of comm0
55 // This function modifies the graph of tasks by adding or removing
56 // successors to comm0
57 comm0->on_this_start_cb([exec1, exec2, jupiter, fafard](simgrid::plugins::Task* t) {
58 auto* comm0 = dynamic_cast<simgrid::plugins::CommTask*>(t);
61 comm0->set_destination(jupiter);
62 comm0->add_successor(exec1);
63 comm0->remove_successor(exec2);
65 comm0->set_destination(fafard);
66 comm0->add_successor(exec2);
67 comm0->remove_successor(exec1);
72 // Enqueue four executions for task comm0
73 comm0->enqueue_execs(4);
75 // Start the simulation