Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Clang-tidy: readability-qualified-auto.
[simgrid.git] / examples / cpp / task-variable-load / s4u-task-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 tasks.
7  *
8  * We consider the following graph:
9  *
10  * comm -> exec
11  *
12  * With a small load each comm task is followed by an exec task.
13  * With a heavy load there is a burst of comm before the exec task can even finish once.
14  */
15
16 #include "simgrid/plugins/task.hpp"
17 #include "simgrid/s4u.hpp"
18
19 XBT_LOG_NEW_DEFAULT_CATEGORY(task_variable_load, "Messages specific for this s4u example");
20
21 static void variable_load(simgrid::plugins::TaskPtr t)
22 {
23   XBT_INFO("--- Small load ---");
24   for (int i = 0; i < 3; i++) {
25     t->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     t->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::Task::init();
41
42   // Retreive hosts
43   auto* tremblay = e.host_by_name("Tremblay");
44   auto* jupiter  = e.host_by_name("Jupiter");
45
46   // Create tasks
47   auto comm = simgrid::plugins::CommTask::init("comm", 1e7, tremblay, jupiter);
48   auto exec = simgrid::plugins::ExecTask::init("exec", 1e9, jupiter);
49
50   // Create the graph by defining dependencies between tasks
51   comm->add_successor(exec);
52
53   // Add a function to be called when tasks end for log purpose
54   simgrid::plugins::Task::on_end_cb([](const simgrid::plugins::Task* t) {
55     XBT_INFO("Task %s finished (%d)", t->get_name().c_str(), t->get_count());
56   });
57
58   // Create the actor that will inject load during the simulation
59   simgrid::s4u::Actor::create("input", tremblay, variable_load, comm);
60
61   // Start the simulation
62   e.run();
63   return 0;
64 }