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 create a variable load for tasks.
8 * We consider the following graph:
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.
16 #include "simgrid/s4u.hpp"
18 XBT_LOG_NEW_DEFAULT_CATEGORY(task_variable_load, "Messages specific for this s4u example");
19 namespace sg4 = simgrid::s4u;
21 static void variable_load(sg4::TaskPtr t)
23 XBT_INFO("--- Small load ---");
24 for (int i = 0; i < 3; i++) {
25 t->enqueue_firings(1);
26 sg4::this_actor::sleep_for(100);
28 sg4::this_actor::sleep_until(1000);
29 XBT_INFO("--- Heavy load ---");
30 for (int i = 0; i < 3; i++) {
31 t->enqueue_firings(1);
32 sg4::this_actor::sleep_for(1);
36 int main(int argc, char* argv[])
38 sg4::Engine e(&argc, argv);
39 e.load_platform(argv[1]);
42 auto* tremblay = e.host_by_name("Tremblay");
43 auto* jupiter = e.host_by_name("Jupiter");
46 auto comm = sg4::CommTask::init("comm", 1e7, tremblay, jupiter);
47 auto exec = sg4::ExecTask::init("exec", 1e9, jupiter);
49 // Create the graph by defining dependencies between tasks
50 comm->add_successor(exec);
52 // Add a function to be called when tasks end for log purpose
53 sg4::Task::on_completion_cb(
54 [](const sg4::Task* t) { XBT_INFO("Task %s finished (%d)", t->get_name().c_str(), t->get_count()); });
56 // Create the actor that will inject load during the simulation
57 sg4::Actor::create("input", tremblay, variable_load, comm);
59 // Start the simulation