Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / examples / cpp / battery-energy / s4u-battery-energy.cpp
1 /* Copyright (c) 2003-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/plugins/battery.hpp"
7 #include "simgrid/plugins/energy.h"
8 #include "simgrid/s4u.hpp"
9 #include <simgrid/s4u/Actor.hpp>
10 #include <simgrid/s4u/Engine.hpp>
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(battery_energy, "Messages specific for this s4u example");
13
14 static void manager()
15 {
16   auto battery = simgrid::plugins::Battery::init("Battery", 0.8, -300, 300, 0.9, 0.9, 10, 1000);
17
18   auto* host1 = simgrid::s4u::Engine::get_instance()->host_by_name("MyHost1");
19   auto* host2 = simgrid::s4u::Engine::get_instance()->host_by_name("MyHost2");
20   auto* host3 = simgrid::s4u::Engine::get_instance()->host_by_name("MyHost3");
21
22   battery->schedule_handler(
23       0.2, simgrid::plugins::Battery::DISCHARGE, simgrid::plugins::Battery::Handler::PERSISTANT,
24       [&battery, &host1, &host2, &host3]() {
25         XBT_INFO("Handler -> Battery low: SoC: %f SoH: %f Energy stored: %fJ Energy provided: %fJ Energy consumed %fJ",
26                  battery->get_state_of_charge(), battery->get_state_of_health(), battery->get_energy_stored(),
27                  battery->get_energy_provided(), battery->get_energy_consumed());
28         XBT_INFO("Disconnecting hosts %s and %s", host1->get_cname(), host2->get_cname());
29         battery->connect_host(host1, false);
30         battery->connect_host(host2, false);
31         XBT_INFO("Energy consumed this far by: %s: %fJ, %s: %fJ, %s: %fJ", host1->get_cname(),
32                  sg_host_get_consumed_energy(host1), host2->get_cname(), sg_host_get_consumed_energy(host2),
33                  host3->get_cname(), sg_host_get_consumed_energy(host3));
34       });
35
36   XBT_INFO("Battery state: SoC: %f SoH: %f Energy stored: %fJ Energy provided: %fJ Energy consumed %fJ",
37            battery->get_state_of_charge(), battery->get_state_of_health(), battery->get_energy_stored(),
38            battery->get_energy_provided(), battery->get_energy_consumed());
39   XBT_INFO("Connecting hosts %s and %s to the battery", host1->get_cname(), host2->get_cname());
40   battery->connect_host(host1);
41   battery->connect_host(host2);
42
43   double flops = 1e9;
44   XBT_INFO("Host %s will now execute %f flops", host1->get_cname(), flops);
45   host2->execute(flops);
46
47   simgrid::s4u::this_actor::sleep_until(200);
48   XBT_INFO("Battery state: SoC: %f SoH: %f Energy stored: %fJ Energy provided: %fJ Energy consumed %fJ",
49            battery->get_state_of_charge(), battery->get_state_of_health(), battery->get_energy_stored(),
50            battery->get_energy_provided(), battery->get_energy_consumed());
51 }
52
53 int main(int argc, char* argv[])
54 {
55   simgrid::s4u::Engine e(&argc, argv);
56   // if you plan to use Battery::connect_host you have to init the energy plugin at start.
57   sg_host_energy_plugin_init();
58   e.load_platform(argv[1]);
59
60   simgrid::s4u::Actor::create("manager", e.host_by_name("MyHost1"), manager);
61
62   e.run();
63   return 0;
64 }