Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1c573c30fd3ef2329859c3b754ce581bc05c2a42
[simgrid.git] / examples / cpp / energy-exec / s4u-energy-exec.cpp
1 /* Copyright (c) 2007-2022. 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 dvfs()
13 {
14   sg4::Host* host1 = sg4::Host::by_name("MyHost1");
15   sg4::Host* host2 = sg4::Host::by_name("MyHost2");
16
17   XBT_INFO("Energetic profile: %s", host1->get_property("wattage_per_state"));
18   XBT_INFO("Initial peak speed=%.0E flop/s; Energy dissipated =%.0E J", host1->get_speed(),
19            sg_host_get_consumed_energy(host1));
20
21   double start = sg4::Engine::get_clock();
22   XBT_INFO("Sleep for 10 seconds");
23   sg4::this_actor::sleep_for(10);
24   XBT_INFO("Done sleeping (duration: %.2f s). Current peak speed=%.0E; Energy dissipated=%.2f J",
25            sg4::Engine::get_clock() - start, host1->get_speed(), sg_host_get_consumed_energy(host1));
26
27   // Execute something
28   start             = sg4::Engine::get_clock();
29   double flopAmount = 100E6;
30   XBT_INFO("Run a computation of %.0E flops", flopAmount);
31   sg4::this_actor::execute(flopAmount);
32   XBT_INFO(
33       "Computation done (duration: %.2f s). Current peak speed=%.0E flop/s; Current consumption: from %.0fW to %.0fW"
34       " depending on load; Energy dissipated=%.0f J",
35       sg4::Engine::get_clock() - start, host1->get_speed(), sg_host_get_wattmin_at(host1, host1->get_pstate()),
36       sg_host_get_wattmax_at(host1, host1->get_pstate()), sg_host_get_consumed_energy(host1));
37
38   // ========= Change power peak =========
39   int pstate = 2;
40   host1->set_pstate(pstate);
41   XBT_INFO("========= Requesting pstate %d (speed should be of %.0E flop/s and is of %.0E flop/s)", pstate,
42            host1->get_pstate_speed(pstate), host1->get_speed());
43
44   // Run another computation
45   start = sg4::Engine::get_clock();
46   XBT_INFO("Run a computation of %.0E flops", flopAmount);
47   sg4::this_actor::execute(flopAmount);
48   XBT_INFO("Computation done (duration: %.2f s). Current peak speed=%.0E flop/s; Energy dissipated=%.0f J",
49            sg4::Engine::get_clock() - start, host1->get_speed(), sg_host_get_consumed_energy(host1));
50
51   start = sg4::Engine::get_clock();
52   XBT_INFO("Sleep for 4 seconds");
53   sg4::this_actor::sleep_for(4);
54   XBT_INFO("Done sleeping (duration: %.2f s). Current peak speed=%.0E flop/s; Energy dissipated=%.0f J",
55            sg4::Engine::get_clock() - start, host1->get_speed(), sg_host_get_consumed_energy(host1));
56
57   // =========== Turn the other host off ==========
58   XBT_INFO("Turning MyHost2 off, and sleeping another 10 seconds. MyHost2 dissipated %.0f J so far.",
59            sg_host_get_consumed_energy(host2));
60   host2->turn_off();
61   start = sg4::Engine::get_clock();
62   sg4::this_actor::sleep_for(10);
63   XBT_INFO("Done sleeping (duration: %.2f s). Current peak speed=%.0E flop/s; Energy dissipated=%.0f J",
64            sg4::Engine::get_clock() - start, host1->get_speed(), sg_host_get_consumed_energy(host1));
65 }
66
67 int main(int argc, char* argv[])
68 {
69   sg_host_energy_plugin_init();
70   sg4::Engine e(&argc, argv);
71
72   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s ../platforms/energy_platform.xml\n", argv[0], argv[0]);
73
74   e.load_platform(argv[1]);
75   sg4::Actor::create("dvfs_test", e.host_by_name("MyHost1"), dvfs);
76
77   e.run();
78
79   XBT_INFO("End of simulation.");
80
81   return 0;
82 }