Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2023.
[simgrid.git] / examples / cpp / plugin-host-load / s4u-plugin-host-load.cpp
1 /* Copyright (c) 2007-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/plugins/load.h"
7 #include "simgrid/s4u.hpp"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
10 namespace sg4 = simgrid::s4u;
11
12 static void execute_load_test()
13 {
14   s4u_Host* host = sg4::Host::by_name("MyHost1");
15
16   XBT_INFO("Initial peak speed: %.0E flop/s; number of flops computed so far: %.0E (should be 0) and current average "
17            "load: %.5f (should be 0)",
18            host->get_speed(), sg_host_get_computed_flops(host), sg_host_get_avg_load(host));
19
20   double start = sg4::Engine::get_clock();
21   XBT_INFO("Sleep for 10 seconds");
22   sg4::this_actor::sleep_for(10);
23
24   double speed = host->get_speed();
25   XBT_INFO("Done sleeping %.2fs; peak speed: %.0E flop/s; number of flops computed so far: %.0E (nothing should have "
26            "changed)",
27            sg4::Engine::get_clock() - start, host->get_speed(), sg_host_get_computed_flops(host));
28
29   // Run an activity
30   start = sg4::Engine::get_clock();
31   XBT_INFO("Run an activity of %.0E flops at current speed of %.0E flop/s", 200E6, host->get_speed());
32   sg4::this_actor::execute(200E6);
33
34   XBT_INFO(
35       "Done working on my activity; this took %.2fs; current peak speed: %.0E flop/s (when I started the computation, "
36       "the speed was set to %.0E flop/s); number of flops computed so "
37       "far: %.2E, average load as reported by the HostLoad plugin: %.5f (should be %.5f)",
38       sg4::Engine::get_clock() - start, host->get_speed(), speed, sg_host_get_computed_flops(host),
39       sg_host_get_avg_load(host),
40       200E6 / (10.5 * speed * host->get_core_count() +
41                (sg4::Engine::get_clock() - start - 0.5) * host->get_speed() * host->get_core_count()));
42
43   // ========= Change power peak =========
44   int pstate = 1;
45   host->set_pstate(pstate);
46   XBT_INFO(
47       "========= Requesting pstate %d (speed should be of %.0E flop/s and is of %.0E flop/s, average load is %.5f)",
48       pstate, host->get_pstate_speed(pstate), host->get_speed(), sg_host_get_avg_load(host));
49
50   // Run a second activity
51   start = sg4::Engine::get_clock();
52   XBT_INFO("Run an activity of %.0E flops", 100E6);
53   sg4::this_actor::execute(100E6);
54   XBT_INFO("Done working on my activity; this took %.2fs; current peak speed: %.0E flop/s; number of flops computed so "
55            "far: %.2E",
56            sg4::Engine::get_clock() - start, host->get_speed(), sg_host_get_computed_flops(host));
57
58   start = sg4::Engine::get_clock();
59   XBT_INFO("========= Requesting a reset of the computation and load counters");
60   sg_host_load_reset(host);
61   XBT_INFO("After reset: %.0E flops computed; load is %.5f", sg_host_get_computed_flops(host),
62            sg_host_get_avg_load(host));
63   XBT_INFO("Sleep for 4 seconds");
64   sg4::this_actor::sleep_for(4);
65   XBT_INFO("Done sleeping %.2f s; peak speed: %.0E flop/s; number of flops computed so far: %.0E",
66            sg4::Engine::get_clock() - start, host->get_speed(), sg_host_get_computed_flops(host));
67
68   // =========== Turn the other host off ==========
69   s4u_Host* host2 = sg4::Host::by_name("MyHost2");
70   XBT_INFO("Turning MyHost2 off, and sleeping another 10 seconds. MyHost2 computed %.0f flops so far and has an "
71            "average load of %.5f.",
72            sg_host_get_computed_flops(host2), sg_host_get_avg_load(host2));
73   host2->turn_off();
74   start = sg4::Engine::get_clock();
75   sg4::this_actor::sleep_for(10);
76   XBT_INFO("Done sleeping %.2f s; peak speed: %.0E flop/s; number of flops computed so far: %.0E",
77            sg4::Engine::get_clock() - start, host->get_speed(), sg_host_get_computed_flops(host));
78 }
79
80 static void change_speed()
81 {
82   s4u_Host* host = sg4::Host::by_name("MyHost1");
83   sg4::this_actor::sleep_for(10.5);
84   XBT_INFO("I slept until now, but now I'll change the speed of this host "
85            "while the other actor is still computing! This should slow the computation down.");
86   host->set_pstate(2);
87 }
88
89 int main(int argc, char* argv[])
90 {
91   sg_host_load_plugin_init();
92   sg4::Engine e(&argc, argv);
93
94   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s ../platforms/energy_platform.xml\n", argv[0], argv[0]);
95   e.load_platform(argv[1]);
96
97   sg4::Actor::create("load_test", e.host_by_name("MyHost1"), execute_load_test);
98   sg4::Actor::create("change_speed", e.host_by_name("MyHost1"), change_speed);
99
100   e.run();
101
102   XBT_INFO("Total simulation time: %.2f", sg4::Engine::get_clock());
103
104   return 0;
105 }