Logo AND Algorithmique Numérique Distribuée

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