Logo AND Algorithmique Numérique Distribuée

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