Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3aa32fd94812b4c2c02cb7b1ac0d53bcc5c03363
[simgrid.git] / examples / cpp / operation-variable-load / s4u-operation-variable-load.cpp
1 /* Copyright (c) 2017-2023. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 /* This example demonstrates how to create a variable load for operations.
7  * 
8  * We consider the following graph:
9  * 
10  * comm -> exec
11  * 
12  * With a small load each comm operation is followed by an exec operation.
13  * With a heavy load there is a burst of comm before the exec operation can even finish once.
14  */
15
16 #include "simgrid/s4u.hpp"
17 #include "simgrid/plugins/operation.hpp"
18
19 XBT_LOG_NEW_DEFAULT_CATEGORY(operation_variable_load, "Messages specific for this s4u example");
20
21 static void variable_load(simgrid::plugins::OperationPtr op)
22 {
23     XBT_INFO("--- Small load ---");
24     for (int i=0; i < 3; i++) {
25         op->enqueue_execs(1);
26         simgrid::s4u::this_actor::sleep_for(100);
27     }
28     simgrid::s4u::this_actor::sleep_until(1000);
29     XBT_INFO("--- Heavy load ---");
30     for (int i=0; i < 3; i++) {
31         op->enqueue_execs(1);
32         simgrid::s4u::this_actor::sleep_for(1);
33     }
34 }
35
36 int main(int argc, char* argv[])
37 {
38     simgrid::s4u::Engine e(&argc, argv);
39     e.load_platform(argv[1]);
40     simgrid::plugins::Operation::init();
41
42     // Retreive hosts
43     auto tremblay = e.host_by_name("Tremblay");
44     auto jupiter = e.host_by_name("Jupiter");
45
46     // Create operations
47     auto comm = simgrid::plugins::CommOp::create("comm",1e7,tremblay,jupiter);
48     auto exec = simgrid::plugins::ExecOp::create("exec",1e9,jupiter);
49
50     // Create the graph by defining dependencies between operations
51     comm->add_successor(exec);
52
53     // Add a function to be called when operations end for log purpose
54     std::vector<simgrid::plugins::OperationPtr> ops{exec,comm};
55     for (auto op: ops)
56         op->on_end([](simgrid::plugins::Operation* op) {
57             XBT_INFO("Operation %s finished (%d)",op->get_name().c_str(), op->get_count());
58         });
59
60     // Create the actor that will inject load during the simulation
61     simgrid::s4u::Actor::create("input", tremblay, variable_load, comm);
62
63     // Start the simulation
64     e.run();
65     return 0;
66 }