Logo AND Algorithmique Numérique Distribuée

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