Logo AND Algorithmique Numérique Distribuée

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