Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add possibility to dispatch tasks (work in progress)
[simgrid.git] / examples / cpp / task-dispatch / s4u-task-dispatch.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 #include "simgrid/s4u.hpp"
7
8 XBT_LOG_NEW_DEFAULT_CATEGORY(task_dispatch, "Messages specific for this s4u example");
9 namespace sg4 = simgrid::s4u;
10
11 static void manager(sg4::ExecTaskPtr t)
12 {
13   auto PM0 = sg4::Engine::get_instance()->host_by_name("PM0");
14   auto PM1 = sg4::Engine::get_instance()->host_by_name("PM1");
15
16   XBT_INFO("Test set_flops");
17   t->enqueue_firings(2);
18   sg4::this_actor::sleep_for(50);
19   XBT_INFO("Set instance_0 flops to 50.");
20   t->set_flops(50 * PM0->get_speed());
21   sg4::this_actor::sleep_for(250);
22   t->set_flops(100 * PM0->get_speed());
23
24   XBT_INFO("Test set_parallelism degree");
25   t->enqueue_firings(3);
26   sg4::this_actor::sleep_for(50);
27   XBT_INFO("Set Task parallelism degree to 2.");
28   t->set_parallelism_degree(2);
29   sg4::this_actor::sleep_for(250);
30   t->set_parallelism_degree(1);
31
32   XBT_INFO("Test set_host dispatcher");
33   t->enqueue_firings(2);
34   sg4::this_actor::sleep_for(50);
35   XBT_INFO("Move dispatcher to PM1");
36   t->set_host(PM1, "dispatcher");
37   t->set_internal_bytes(1e6, "dispatcher");
38   sg4::this_actor::sleep_for(250);
39   t->set_host(PM0, "dispatcher");
40
41   XBT_INFO("Test set_host instance_0");
42   t->enqueue_firings(2);
43   sg4::this_actor::sleep_for(50);
44   XBT_INFO("Move instance_0 to PM1");
45   t->set_host(PM1, "instance_0");
46   t->set_flops(100 * PM1->get_speed());
47   t->set_internal_bytes(1e6, "instance_0");
48   sg4::this_actor::sleep_for(250);
49   t->set_host(PM0, "instance_0");
50   t->set_flops(100 * PM0->get_speed());
51
52   XBT_INFO("Test set_host collector");
53   t->enqueue_firings(2);
54   sg4::this_actor::sleep_for(50);
55   XBT_INFO("Move collector to PM1");
56   t->set_host(PM1, "collector");
57   sg4::this_actor::sleep_for(250);
58   t->set_host(PM0, "collector");
59
60   XBT_INFO("Test add_instances");
61   t->enqueue_firings(1);
62   sg4::this_actor::sleep_for(50);
63   XBT_INFO("Add 1 instance and update load balancing function");
64   t->add_instances(1);
65   t->set_load_balancing_function([]() {
66     static int round_robin_counter = 0;
67     int ret                        = round_robin_counter;
68     round_robin_counter            = round_robin_counter == 1 ? 0 : round_robin_counter + 1;
69     return "instance_" + std::to_string(ret);
70   });
71   t->enqueue_firings(2);
72   sg4::this_actor::sleep_for(250);
73
74   XBT_INFO("Test remove_instances");
75   XBT_INFO("Remove 1 instance and update load balancing function");
76   t->remove_instances(1);
77   t->set_load_balancing_function([]() { return "instance_0"; });
78   t->enqueue_firings(2);
79   sg4::this_actor::sleep_for(300);
80 }
81
82 int main(int argc, char* argv[])
83 {
84   sg4::Engine e(&argc, argv);
85   e.load_platform(argv[1]);
86   auto PM0 = e.host_by_name("PM0");
87   auto PM1 = sg4::Engine::get_instance()->host_by_name("PM1");
88
89   auto a = sg4::ExecTask::init("A", 100 * PM0->get_speed(), PM0);
90   auto b = sg4::ExecTask::init("B", 50 * PM0->get_speed(), PM0);
91   auto c = sg4::CommTask::init("C", 1e6, PM1, PM0);
92
93   a->add_successor(b);
94
95   sg4::Task::on_completion_cb(
96       [](const sg4::Task* t) { XBT_INFO("Task %s finished (%d)", t->get_name().c_str(), t->get_count()); });
97   sg4::Task::on_start_cb([](const sg4::Task* t) { XBT_INFO("Task %s start", t->get_name().c_str()); });
98
99   sg4::Actor::create("manager", PM0, manager, a);
100
101   e.run();
102   return 0;
103 }