Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Remove commented out code.
[simgrid.git] / examples / s4u / energy-exec-ptask / s4u-energy-exec-ptask.cpp
1 /* Copyright (c) 2007-2019. 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/energy.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
10
11 static void runner()
12 {
13   simgrid::s4u::Host* host1 = simgrid::s4u::Host::by_name("MyHost1");
14   simgrid::s4u::Host* host2 = simgrid::s4u::Host::by_name("MyHost2");
15   std::vector<simgrid::s4u::Host*> hosts{host1, host2};
16
17   double old_energy_host1 = sg_host_get_consumed_energy(host1);
18   double old_energy_host2 = sg_host_get_consumed_energy(host2);
19
20   XBT_INFO("[%s] Energetic profile: %s", host1->get_cname(), host1->get_property("wattage_per_state"));
21   XBT_INFO("[%s] Initial peak speed=%.0E flop/s; Total energy dissipated =%.0E J", host1->get_cname(), host1->get_speed(),
22            old_energy_host1);
23   XBT_INFO("[%s] Energetic profile: %s", host2->get_cname(), host2->get_property("wattage_per_state"));
24   XBT_INFO("[%s] Initial peak speed=%.0E flop/s; Total energy dissipated =%.0E J", host2->get_cname(), host2->get_speed(),
25            old_energy_host2);
26
27   double start = simgrid::s4u::Engine::get_clock();
28   XBT_INFO("Sleep for 10 seconds");
29   simgrid::s4u::this_actor::sleep_for(10);
30
31   double new_energy_host1 = sg_host_get_consumed_energy(host1);
32   double new_energy_host2 = sg_host_get_consumed_energy(host2);
33   XBT_INFO("Done sleeping (duration: %.2f s).\n"
34            "[%s] Current peak speed=%.0E; Energy dissipated during this step=%.2f J; Total energy dissipated=%.2f J\n"
35            "[%s] Current peak speed=%.0E; Energy dissipated during this step=%.2f J; Total energy dissipated=%.2f J\n",
36            simgrid::s4u::Engine::get_clock() - start,
37            host1->get_cname(), host1->get_speed(), (new_energy_host1 - old_energy_host1), sg_host_get_consumed_energy(host1),
38            host2->get_cname(), host2->get_speed(), (new_energy_host2 - old_energy_host2), sg_host_get_consumed_energy(host2));
39
40   old_energy_host1 = new_energy_host1;
41   old_energy_host2 = new_energy_host2;
42
43
44   // ========= Execute something =========
45   start             = simgrid::s4u::Engine::get_clock();
46   double flopAmount = 1E9;
47   std::vector<double> cpu_amounts{flopAmount, flopAmount};
48   std::vector<double> com_amounts{0, 0, 0, 0};
49   XBT_INFO("Run a task of %.0E flops on two hosts", flopAmount);
50   simgrid::s4u::this_actor::parallel_execute(hosts, cpu_amounts, com_amounts);
51
52   new_energy_host1 = sg_host_get_consumed_energy(host1);
53   new_energy_host2 = sg_host_get_consumed_energy(host2);
54   XBT_INFO("Task done (duration: %.2f s).\n"
55            "[%s] Current peak speed=%.0E flop/s; Energy dissipated during this step=%.2f J; Total energy dissipated=%.0f J\n"
56            "[%s] Current peak speed=%.0E flop/s; Energy dissipated during this step=%.2f J; Total energy dissipated=%.0f J\n",
57            simgrid::s4u::Engine::get_clock() - start,
58            host1->get_cname(), host1->get_speed(), (new_energy_host1 - old_energy_host1), sg_host_get_consumed_energy(host1),
59            host2->get_cname(), host2->get_speed(), (new_energy_host2 - old_energy_host2), sg_host_get_consumed_energy(host2));
60
61   old_energy_host1 = new_energy_host1;
62   old_energy_host2 = new_energy_host2;
63
64
65   // ========= Change power peak =========
66   int pstate = 2;
67   host1->set_pstate(pstate);
68   host2->set_pstate(pstate);
69   XBT_INFO("========= Requesting pstate %d for both hosts (speed should be of %.0E flop/s and is of %.0E flop/s)", pstate,
70            host1->get_pstate_speed(pstate), host1->get_speed());
71
72
73   // ========= Run another ptask =========
74   start             = simgrid::s4u::Engine::get_clock();
75   std::vector<double> cpu_amounts2{flopAmount, flopAmount};
76   std::vector<double> com_amounts2{0, 0, 0, 0};
77   XBT_INFO("Run a task of %.0E flops on %s and %.0E flops on %s.", flopAmount, host1->get_cname(), flopAmount, host2->get_cname());
78   simgrid::s4u::this_actor::parallel_execute(hosts, cpu_amounts2, com_amounts2);
79
80   new_energy_host1 = sg_host_get_consumed_energy(host1);
81   new_energy_host2 = sg_host_get_consumed_energy(host2);
82   XBT_INFO("Task done (duration: %.2f s).\n"
83            "[%s] Current peak speed=%.0E flop/s; Energy dissipated during this step=%.2f J; Total energy dissipated=%.0f J\n"
84            "[%s] Current peak speed=%.0E flop/s; Energy dissipated during this step=%.2f J; Total energy dissipated=%.0f J\n",
85            simgrid::s4u::Engine::get_clock() - start,
86            host1->get_cname(), host1->get_speed(), (new_energy_host1 - old_energy_host1), sg_host_get_consumed_energy(host1),
87            host2->get_cname(), host2->get_speed(), (new_energy_host2 - old_energy_host2), sg_host_get_consumed_energy(host2));
88
89   old_energy_host1 = new_energy_host1;
90   old_energy_host2 = new_energy_host2;
91
92
93   // ========= A new ptask with computation and communication =========
94   start             = simgrid::s4u::Engine::get_clock();
95   double comAmount = 1E7;
96   std::vector<double> cpu_amounts3{flopAmount, flopAmount};
97   std::vector<double> com_amounts3{0, comAmount, comAmount, 0};
98   XBT_INFO("Run a task with computation and communication on two hosts.");
99   simgrid::s4u::this_actor::parallel_execute(hosts, cpu_amounts3, com_amounts3);
100
101   new_energy_host1 = sg_host_get_consumed_energy(host1);
102   new_energy_host2 = sg_host_get_consumed_energy(host2);
103   XBT_INFO("Task done (duration: %.2f s).\n"
104            "[%s] Current peak speed=%.0E flop/s; Energy dissipated during this step=%.2f J; Total energy dissipated=%.0f J\n"
105            "[%s] Current peak speed=%.0E flop/s; Energy dissipated during this step=%.2f J; Total energy dissipated=%.0f J\n",
106            simgrid::s4u::Engine::get_clock() - start,
107            host1->get_cname(), host1->get_speed(), (new_energy_host1 - old_energy_host1), sg_host_get_consumed_energy(host1),
108            host2->get_cname(), host2->get_speed(), (new_energy_host2 - old_energy_host2), sg_host_get_consumed_energy(host2));
109
110   old_energy_host1 = new_energy_host1;
111   old_energy_host2 = new_energy_host2;
112
113
114   // ========= A new ptask with communication only =========
115   start             = simgrid::s4u::Engine::get_clock();
116   std::vector<double> cpu_amounts4{0, 0};
117   std::vector<double> com_amounts4{0, comAmount, comAmount, 0};
118   XBT_INFO("Run a task with only communication on two hosts.");
119   simgrid::s4u::this_actor::parallel_execute(hosts, cpu_amounts4, com_amounts4);
120
121   new_energy_host1 = sg_host_get_consumed_energy(host1);
122   new_energy_host2 = sg_host_get_consumed_energy(host2);
123   XBT_INFO("Task done (duration: %.2f s).\n"
124            "[%s] Current peak speed=%.0E flop/s; Energy dissipated during this step=%.2f J; Total energy dissipated=%.0f J\n"
125            "[%s] Current peak speed=%.0E flop/s; Energy dissipated during this step=%.2f J; Total energy dissipated=%.0f J\n",
126            simgrid::s4u::Engine::get_clock() - start,
127            host1->get_cname(), host1->get_speed(), (new_energy_host1 - old_energy_host1), sg_host_get_consumed_energy(host1),
128            host2->get_cname(), host2->get_speed(), (new_energy_host2 - old_energy_host2), sg_host_get_consumed_energy(host2));
129
130   XBT_INFO("Now is time to quit!");
131 }
132
133 int main(int argc, char* argv[])
134 {
135   sg_host_energy_plugin_init();
136   simgrid::s4u::Engine e(&argc, argv);
137   e.set_config("host/model:ptask_L07");
138
139   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s ../platforms/energy_platform.xml\n", argv[0], argv[0]);
140
141   e.load_platform(argv[1]);
142   simgrid::s4u::Actor::create("energy_ptask_test", simgrid::s4u::Host::by_name("MyHost1"), runner);
143
144   e.run();
145   XBT_INFO("End of simulation.");
146   return 0;
147 }