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 operations.
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/s4u.hpp"
20 #include "simgrid/plugins/operation.hpp"
22 XBT_LOG_NEW_DEFAULT_CATEGORY(operation_switch_host, "Messages specific for this operation example");
24 int main(int argc, char* argv[])
26 simgrid::s4u::Engine e(&argc, argv);
27 e.load_platform(argv[1]);
28 simgrid::plugins::Operation::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::CommOp::create("comm0",1e7,tremblay,jupiter);
37 auto exec1 = simgrid::plugins::ExecOp::create("exec1",1e9,jupiter);
38 auto exec2 = simgrid::plugins::ExecOp::create("exec2",1e9,fafard);
39 auto comm1 = simgrid::plugins::CommOp::create("comm1",1e7,jupiter,tremblay);
40 auto comm2 = simgrid::plugins::CommOp::create("comm2",1e7,fafard,tremblay);
42 // Create the initial graph by defining dependencies between operations
43 comm0->add_successor(exec2);
44 exec1->add_successor(comm1);
45 exec2->add_successor(comm2);
47 // Add a function to be called when operations end for log purpose
48 std::vector<simgrid::plugins::OperationPtr> v =
49 {comm0,exec1,exec2,comm1,comm2};
51 op->on_end([](simgrid::plugins::Operation* op) {
52 XBT_INFO("Operation %s finished (%d)",op->get_name().c_str(), op->get_count());
55 // Add a function to be called before each executions of comm0
56 // This function modifies the graph of operations by adding or removing
57 // successors to comm0
59 comm0->on_start([&](simgrid::plugins::Operation* op) {
61 comm0->set_destination(jupiter);
62 comm0->add_successor(exec1);
63 comm0->remove_successor(exec2);
66 comm0->set_destination(fafard);
67 comm0->add_successor(exec2);
68 comm0->remove_successor(exec1);
73 // Enqueue four executions for operation comm0
74 comm0->enqueue_execs(4);
76 // Start the simulation