From: Adrien Date: Fri, 31 Mar 2023 11:54:07 +0000 (+0000) Subject: Add battery plugin and fix DAG doc X-Git-Tag: v3.34~247^2 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/c7118dec5b48156de26672adc4edefd129463718 Add battery plugin and fix DAG doc --- diff --git a/MANIFEST.in b/MANIFEST.in index 9ae7ec1439..1edd9e23de 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -162,6 +162,13 @@ include examples/cpp/app-masterworkers/s4u-app-masterworkers.tesh include examples/cpp/app-masterworkers/s4u-app-masterworkers_d.xml include examples/cpp/app-token-ring/s4u-app-token-ring.cpp include examples/cpp/app-token-ring/s4u-app-token-ring.tesh +include examples/cpp/battery-degradation/plot_battery_degradation.py +include examples/cpp/battery-degradation/s4u-battery-degradation.cpp +include examples/cpp/battery-degradation/s4u-battery-degradation.tesh +include examples/cpp/battery-energy/s4u-battery-energy.cpp +include examples/cpp/battery-energy/s4u-battery-energy.tesh +include examples/cpp/battery-simple/s4u-battery-simple.cpp +include examples/cpp/battery-simple/s4u-battery-simple.tesh include examples/cpp/cloud-capping/s4u-cloud-capping.cpp include examples/cpp/cloud-capping/s4u-cloud-capping.tesh include examples/cpp/cloud-migration/s4u-cloud-migration.cpp @@ -199,14 +206,23 @@ include examples/cpp/dag-comm/s4u-dag-comm.cpp include examples/cpp/dag-comm/s4u-dag-comm.tesh include examples/cpp/dag-failure/s4u-dag-failure.cpp include examples/cpp/dag-failure/s4u-dag-failure.tesh +include examples/cpp/dag-from-dax-simple/dag.xml +include examples/cpp/dag-from-dax-simple/s4u-dag-from-dax-simple.cpp +include examples/cpp/dag-from-dax-simple/s4u-dag-from-dax-simple.tesh include examples/cpp/dag-from-dax/s4u-dag-from-dax.cpp include examples/cpp/dag-from-dax/s4u-dag-from-dax.tesh include examples/cpp/dag-from-dax/simple_dax_with_cycle.xml include examples/cpp/dag-from-dax/smalldax.xml +include examples/cpp/dag-from-dot-simple/dag.dot +include examples/cpp/dag-from-dot-simple/s4u-dag-from-dot-simple.cpp +include examples/cpp/dag-from-dot-simple/s4u-dag-from-dot-simple.tesh include examples/cpp/dag-from-dot/dag.dot include examples/cpp/dag-from-dot/dag_with_cycle.dot include examples/cpp/dag-from-dot/s4u-dag-from-dot.cpp include examples/cpp/dag-from-dot/s4u-dag-from-dot.tesh +include examples/cpp/dag-from-json-simple/dag.json +include examples/cpp/dag-from-json-simple/s4u-dag-from-json-simple.cpp +include examples/cpp/dag-from-json-simple/s4u-dag-from-json-simple.tesh include examples/cpp/dag-io/s4u-dag-io.cpp include examples/cpp/dag-io/s4u-dag-io.tesh include examples/cpp/dag-scheduling/Montage_25.xml @@ -214,6 +230,8 @@ include examples/cpp/dag-scheduling/s4u-dag-scheduling.cpp include examples/cpp/dag-scheduling/s4u-dag-scheduling.tesh include examples/cpp/dag-simple/s4u-dag-simple.cpp include examples/cpp/dag-simple/s4u-dag-simple.tesh +include examples/cpp/dag-tuto/s4u-dag-tuto.cpp +include examples/cpp/dag-tuto/s4u-dag-tuto.tesh include examples/cpp/dht-chord/s4u-dht-chord-node.cpp include examples/cpp/dht-chord/s4u-dht-chord.cpp include examples/cpp/dht-chord/s4u-dht-chord.hpp @@ -1678,6 +1696,10 @@ include docs/source/app_smpi.rst include docs/source/application.rst include docs/source/community.rst include docs/source/conf.py +include docs/source/img/battery_degradation.svg +include docs/source/img/dag.svg +include docs/source/img/dag1.svg +include docs/source/img/dag2.svg include docs/source/img/design-scheduling-parallel.svg include docs/source/img/design-scheduling-simulatedtime.svg include docs/source/img/design-scheduling-wallclock.svg @@ -1699,17 +1721,6 @@ include docs/source/img/zoom_comm.drawio include docs/source/img/zoom_comm.svg include docs/source/index.rst include docs/source/intl.rst -include docs/source/tuto_dag/dag_lab1.cpp -include docs/source/tuto_dag/dag_lab2-1.cpp -include docs/source/tuto_dag/dag_lab2-2.cpp -include docs/source/tuto_dag/dag_lab2-3.cpp -include docs/source/tuto_dag/img/dag.svg -include docs/source/tuto_dag/img/dag1.svg -include docs/source/tuto_dag/img/dag2.svg -include docs/source/tuto_dag/simple_dax.xml -include docs/source/tuto_dag/simple_dot.dot -include docs/source/tuto_dag/simple_json.json -include docs/source/tuto_dag/small_platform.xml include docs/source/tuto_disk/CMakeLists.txt include docs/source/tuto_disk/Dockerfile include docs/source/tuto_disk/analysis.irst @@ -1776,6 +1787,7 @@ include examples/README.rst include examples/c/CMakeLists.txt include examples/cpp/CMakeLists.txt include examples/platforms/CMakeLists.txt +include examples/platforms/battery_platform.xml include examples/platforms/bypassRoute.xml include examples/platforms/bypassZoneRoute.xml include examples/platforms/cloud.xml @@ -1904,6 +1916,7 @@ include include/simgrid/mailbox.h include include/simgrid/modelchecker.h include include/simgrid/mutex.h include include/simgrid/plugins/ProducerConsumer.hpp +include include/simgrid/plugins/battery.hpp include include/simgrid/plugins/dvfs.h include include/simgrid/plugins/energy.h include include/simgrid/plugins/file_system.h @@ -2254,6 +2267,7 @@ include src/mc/transition/TransitionRandom.hpp include src/mc/transition/TransitionSynchro.cpp include src/mc/transition/TransitionSynchro.hpp include src/plugins/ProducerConsumer.cpp +include src/plugins/battery.cpp include src/plugins/chaos_monkey.cpp include src/plugins/file_system/s4u_FileSystem.cpp include src/plugins/host_dvfs.cpp diff --git a/docs/source/Plugins.rst b/docs/source/Plugins.rst index 0594693ee8..45112232bc 100644 --- a/docs/source/Plugins.rst +++ b/docs/source/Plugins.rst @@ -159,5 +159,13 @@ File System .. doxygengroup:: plugin_filesystem +.. _plugin_battery: + +Battery +=========== + +.. doxygengroup:: plugin_battery + + .. LocalWords: SimGrid diff --git a/docs/source/Tutorial_DAG.rst b/docs/source/Tutorial_DAG.rst index e807cc4f29..621eaf27fa 100644 --- a/docs/source/Tutorial_DAG.rst +++ b/docs/source/Tutorial_DAG.rst @@ -26,7 +26,7 @@ Set of edges representing precedence constraints between :ref:`Activities ` and load the platform. -.. code-block:: cpp - - simgrid::s4u::Engine e(&argc, argv); - e.load_platform(argv[1]); +.. literalinclude:: ../../examples/cpp/dag-tuto/s4u-dag-tuto.cpp + :language: cpp + :lines: 12-13 Retrieve pointers to some hosts. -.. code-block:: cpp - - simgrid::s4u::Host* tremblay = e.host_by_name("Tremblay"); - simgrid::s4u::Host* jupiter = e.host_by_name("Jupiter"); +.. literalinclude:: ../../examples/cpp/dag-tuto/s4u-dag-tuto.cpp + :language: cpp + :lines: 15-16 Initiate the activities. -.. code-block:: cpp - - simgrid::s4u::ExecPtr c1 = simgrid::s4u::Exec::init(); - simgrid::s4u::ExecPtr c2 = simgrid::s4u::Exec::init(); - simgrid::s4u::ExecPtr c3 = simgrid::s4u::Exec::init(); - simgrid::s4u::CommPtr t1 = simgrid::s4u::Comm::sendto_init(); +.. literalinclude:: ../../examples/cpp/dag-tuto/s4u-dag-tuto.cpp + :language: cpp + :lines: 18-21 Give names to thoses activities. -.. code-block:: cpp - - c1->set_name("c1"); - c2->set_name("c2"); - c3->set_name("c3"); - t1->set_name("t1"); +.. literalinclude:: ../../examples/cpp/dag-tuto/s4u-dag-tuto.cpp + :language: cpp + :lines: 23-26 Set the amount of work for each activity. -.. code-block:: cpp - - c1->set_flops_amount(1e9); - c2->set_flops_amount(5e9); - c3->set_flops_amount(2e9); - t1->set_payload_size(5e8); +.. literalinclude:: ../../examples/cpp/dag-tuto/s4u-dag-tuto.cpp + :language: cpp + :lines: 28-31 Define the dependencies between the activities. -.. code-block:: cpp - - c1->add_successor(t1); - t1->add_successor(c3); - c2->add_successor(c3); +.. literalinclude:: ../../examples/cpp/dag-tuto/s4u-dag-tuto.cpp + :language: cpp + :lines: 33-35 Set the location of each Exec activity and source and destination for the Comm activity. -.. code-block:: cpp - - c1->set_host(tremblay); - c2->set_host(jupiter); - c3->set_host(jupiter); - t1->set_source(tremblay); - t1->set_destination(jupiter); +.. literalinclude:: ../../examples/cpp/dag-tuto/s4u-dag-tuto.cpp + :language: cpp + :lines: 37-41 Start the executions of Activities without dependencies. -.. code-block:: cpp - - c1->start(); - c2->start(); +.. literalinclude:: ../../examples/cpp/dag-tuto/s4u-dag-tuto.cpp + :language: cpp + :lines: 43-44 Add a callback to monitor the activities. -.. code-block:: cpp - - Activity::on_completion_cb([](simgrid::s4u::Activity const& activity) { - XBT_INFO("Activity '%s' is complete (start time: %f, finish time: %f)", activity.get_cname(), activity.get_start_time(), - activity.get_finish_time()); - }); +.. literalinclude:: ../../examples/cpp/dag-tuto/s4u-dag-tuto.cpp + :language: cpp + :lines: 46-49 Finally, run the simulation. -.. code-block:: cpp - - e.run(); +.. literalinclude:: ../../examples/cpp/dag-tuto/s4u-dag-tuto.cpp + :language: cpp + :lines: 51 The execution of this code should give you the following output: -.. code-block:: bash - - [10.194200] [main/INFO] Activity 'c1' is complete (start time: 0.000000, finish time: 10.194200) - [65.534235] [main/INFO] Activity 'c2' is complete (start time: 0.000000, finish time: 65.534235) - [85.283378] [main/INFO] Activity 't1' is complete (start time: 10.194200, finish time: 85.283378) - [111.497072] [main/INFO] Activity 'c3' is complete (start time: 85.283378, finish time: 111.497072) - +.. literalinclude:: ../../examples/cpp/dag-tuto/s4u-dag-tuto.tesh + :language: none + :lines: 4- Lab 2: Import a DAG from a file ---------------- +------------------------------- In this lab we present how to import a DAG into you Simgrid simulation, either using a DOT file, a JSON file, or a DAX file. The files presented in this lab describe the following DAG: -.. image:: /tuto_dag/img/dag2.svg +.. image:: /img/dag2.svg :align: center From a DOT file @@ -192,74 +165,19 @@ A DOT file describes a workflow in accordance with the graphviz format. The following DOT file describes the workflow presented at the beginning of this lab: -.. code-block:: xml - - digraph G { - c1 [size="1e9"]; - c2 [size="5e9"]; - c3 [size="2e9"]; +.. literalinclude:: ../../examples/cpp/dag-from-dot-simple/dag.dot + :language: dot - root->c1 [size="2e8"]; - root->c2 [size="1e8"]; - c1->c3 [size="5e8"]; - c2->c3 [size="-1"]; - c3->end [size="2e8"]; - } +It can be imported as a vector of Activities into Simgrid using :cpp:func:`simgrid::s4u::create_DAG_from_DOT`. Then, you have to assign hosts to your Activities. -It can be imported as a vector of Activities into Simgrid using :cpp:func:`create_DAG_from_DOT(const std::string& filename)`. Then, you have to assign hosts to your Activities. - -.. code-block:: cpp - - #include "simgrid/s4u.hpp" - - XBT_LOG_NEW_DEFAULT_CATEGORY(main, "Messages specific for this s4u example"); - - int main(int argc, char* argv[]) { - simgrid::s4u::Engine e(&argc, argv); - e.load_platform(argv[1]); - - std::vector dag = simgrid::s4u::create_DAG_from_dot(argv[2]); - - simgrid::s4u::Host* tremblay = e.host_by_name("Tremblay"); - simgrid::s4u::Host* jupiter = e.host_by_name("Jupiter"); - simgrid::s4u::Host* fafard = e.host_by_name("Fafard"); - - dynamic_cast(dag[0].get())->set_host(fafard); - dynamic_cast(dag[1].get())->set_host(tremblay); - dynamic_cast(dag[2].get())->set_host(jupiter); - dynamic_cast(dag[3].get())->set_host(jupiter); - dynamic_cast(dag[8].get())->set_host(jupiter); - - for (const auto& a : dag) { - if (auto* comm = dynamic_cast(a.get())) { - auto pred = dynamic_cast((*comm->get_dependencies().begin()).get()); - auto succ = dynamic_cast(comm->get_successors().front().get()); - comm->set_source(pred->get_host())->set_destination(succ->get_host()); - } - } - - simgrid::s4u::Activity::on_completion_cb([](simgrid::s4u::Activity const& activity) { - XBT_INFO("Activity '%s' is complete (start time: %f, finish time: %f)", activity.get_cname(), activity.get_start_time(), - activity.get_finish_time()); - }); - - e.run(); - return 0; - } +.. literalinclude:: ../../examples/cpp/dag-from-dot-simple/s4u-dag-from-dot-simple.cpp + :language: cpp The execution of this code should give you the following output: -.. code-block:: bash - - [0.000000] [main/INFO] Activity 'root' is complete (start time: 0.000000, finish time: 0.000000) - [33.394394] [main/INFO] Activity 'root->c2' is complete (start time: 0.000000, finish time: 33.394394) - [39.832311] [main/INFO] Activity 'root->c1' is complete (start time: 0.000000, finish time: 39.832311) - [50.026511] [main/INFO] Activity 'c1' is complete (start time: 39.832311, finish time: 50.026511) - [98.928629] [main/INFO] Activity 'c2' is complete (start time: 33.394394, finish time: 98.928629) - [125.115689] [main/INFO] Activity 'c1->c3' is complete (start time: 50.026511, finish time: 125.115689) - [151.329383] [main/INFO] Activity 'c3' is complete (start time: 125.115689, finish time: 151.329383) - [151.743605] [main/INFO] Activity 'c3->end' is complete (start time: 151.329383, finish time: 151.743605) - [151.743605] [main/INFO] Activity 'end' is complete (start time: 151.743605, finish time: 151.743605) +.. literalinclude:: ../../examples/cpp/dag-from-dot-simple/s4u-dag-from-dot-simple.tesh + :language: none + :lines: 4- From a JSON file ................ @@ -268,82 +186,19 @@ A JSON file describes a workflow in accordance with the `wfformat dag = simgrid::s4u::create_DAG_from_json(argv[2]); - - simgrid::s4u::Activity::on_completion_cb([](simgrid::s4u::Activity const& activity) { - XBT_INFO("Activity '%s' is complete (start time: %f, finish time: %f)", activity.get_cname(), activity.get_start_time(), - activity.get_finish_time()); - }); - - e.run(); - return 0; - } +.. literalinclude:: ../../examples/cpp/dag-from-json-simple/s4u-dag-from-json-simple.cpp + :language: cpp The execution of this code should give you the following output: -.. code-block:: bash - - [10.194200] [main/INFO] Activity 'c1' is complete (start time: 0.000000, finish time: 10.194200) - [65.534235] [main/INFO] Activity 'c2' is complete (start time: 0.000000, finish time: 65.534235) - [85.283378] [main/INFO] Activity 't1' is complete (start time: 10.194200, finish time: 85.283378) - [111.497072] [main/INFO] Activity 'c3' is complete (start time: 85.283378, finish time: 111.497072) +.. literalinclude:: ../../examples/cpp/dag-from-json-simple/s4u-dag-from-json-simple.tesh + :language: none + :lines: 4- From a DAX file [deprecated] ............................ @@ -352,67 +207,10 @@ A DAX file describes a workflow in accordance with the `Pegasus - - - - - - - - - - - - - - - - - - -It can be imported as a vector of Activities into Simgrid using :cpp:func:`create_DAG_from_DAX(std::string)`. - -.. code-block:: cpp - - #include "simgrid/s4u.hpp" - - XBT_LOG_NEW_DEFAULT_CATEGORY(main, "Messages specific for this s4u example"); - - int main(int argc, char* argv[]) { - simgrid::s4u::Engine e(&argc, argv); - e.load_platform(argv[1]); - - std::vector dag = simgrid::s4u::create_DAG_from_DAX(argv[2]); - - simgrid::s4u::Host* tremblay = e.host_by_name("Tremblay"); - simgrid::s4u::Host* jupiter = e.host_by_name("Jupiter"); - simgrid::s4u::Host* fafard = e.host_by_name("Fafard"); - - dynamic_cast(dag[0].get())->set_host(fafard); - dynamic_cast(dag[1].get())->set_host(tremblay); - dynamic_cast(dag[2].get())->set_host(jupiter); - dynamic_cast(dag[3].get())->set_host(jupiter); - dynamic_cast(dag[8].get())->set_host(jupiter); - - for (const auto& a : dag) { - if (auto* comm = dynamic_cast(a.get())) { - auto pred = dynamic_cast((*comm->get_dependencies().begin()).get()); - auto succ = dynamic_cast(comm->get_successors().front().get()); - comm->set_source(pred->get_host())->set_destination(succ->get_host()); - } - } - - simgrid::s4u::Activity::on_completion_cb([](simgrid::s4u::Activity const& activity) { - XBT_INFO("Activity '%s' is complete (start time: %f, finish time: %f)", activity.get_cname(), activity.get_start_time(), - activity.get_finish_time()); - }); +.. literalinclude:: ../../examples/cpp/dag-from-dax-simple/dag.xml + :language: xml - e.run(); - return 0; - } +It can be imported as a vector of Activities into Simgrid using :cpp:func:`simgrid::s4u::create_DAG_from_DAX`. +.. literalinclude:: ../../examples/cpp/dag-from-dax-simple/s4u-dag-from-dax-simple.cpp + :language: cpp diff --git a/docs/source/img/battery_degradation.svg b/docs/source/img/battery_degradation.svg new file mode 100644 index 0000000000..630744ba83 --- /dev/null +++ b/docs/source/img/battery_degradation.svg @@ -0,0 +1,1314 @@ + + + + + + + + 2023-03-30T14:42:09.493393 + image/svg+xml + + + Matplotlib v3.7.1, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/source/tuto_dag/img/dag.svg b/docs/source/img/dag.svg similarity index 100% rename from docs/source/tuto_dag/img/dag.svg rename to docs/source/img/dag.svg diff --git a/docs/source/tuto_dag/img/dag1.svg b/docs/source/img/dag1.svg similarity index 100% rename from docs/source/tuto_dag/img/dag1.svg rename to docs/source/img/dag1.svg diff --git a/docs/source/tuto_dag/img/dag2.svg b/docs/source/img/dag2.svg similarity index 100% rename from docs/source/tuto_dag/img/dag2.svg rename to docs/source/img/dag2.svg diff --git a/docs/source/tuto_dag/dag_lab1.cpp b/docs/source/tuto_dag/dag_lab1.cpp deleted file mode 100644 index 1242ffa068..0000000000 --- a/docs/source/tuto_dag/dag_lab1.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include "simgrid/s4u.hpp" - -XBT_LOG_NEW_DEFAULT_CATEGORY(main, "Messages specific for this s4u tutorial"); - -int main(int argc, char* argv[]) { - simgrid::s4u::Engine e(&argc, argv); - e.load_platform(argv[1]); - - simgrid::s4u::Host* tremblay = e.host_by_name("Tremblay"); - simgrid::s4u::Host* jupiter = e.host_by_name("Jupiter"); - - simgrid::s4u::ExecPtr c1 = simgrid::s4u::Exec::init(); - simgrid::s4u::ExecPtr c2 = simgrid::s4u::Exec::init(); - simgrid::s4u::ExecPtr c3 = simgrid::s4u::Exec::init(); - simgrid::s4u::CommPtr t1 = simgrid::s4u::Comm::sendto_init(); - - c1->set_name("c1"); - c2->set_name("c2"); - c3->set_name("c3"); - t1->set_name("t1"); - - c1->set_flops_amount(1e9); - c2->set_flops_amount(5e9); - c3->set_flops_amount(2e9); - t1->set_payload_size(5e8); - - c1->add_successor(t1); - t1->add_successor(c3); - c2->add_successor(c3); - - c1->set_host(tremblay); - c2->set_host(jupiter); - c3->set_host(jupiter); - t1->set_source(tremblay); - t1->set_destination(jupiter); - - c1->start(); - c2->start(); - - simgrid::s4u::Activity::on_completion_cb([](simgrid::s4u::Activity const& activity) { - XBT_INFO("Activity '%s' is complete (start time: %f, finish time: %f)", activity.get_cname(), activity.get_start_time(), - activity.get_finish_time()); - }); - - e.run(); - return 0; -} - diff --git a/docs/source/tuto_dag/dag_lab2-1.cpp b/docs/source/tuto_dag/dag_lab2-1.cpp deleted file mode 100644 index 9ee5baa4b0..0000000000 --- a/docs/source/tuto_dag/dag_lab2-1.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "simgrid/s4u.hpp" - -XBT_LOG_NEW_DEFAULT_CATEGORY(main, "Messages specific for this s4u example"); - -int main(int argc, char* argv[]) { - simgrid::s4u::Engine e(&argc, argv); - e.load_platform(argv[1]); - - std::vector dag = simgrid::s4u::create_DAG_from_dot(argv[2]); - - simgrid::s4u::Host* tremblay = e.host_by_name("Tremblay"); - simgrid::s4u::Host* jupiter = e.host_by_name("Jupiter"); - simgrid::s4u::Host* fafard = e.host_by_name("Fafard"); - - dynamic_cast(dag[0].get())->set_host(fafard); - dynamic_cast(dag[1].get())->set_host(tremblay); - dynamic_cast(dag[2].get())->set_host(jupiter); - dynamic_cast(dag[3].get())->set_host(jupiter); - dynamic_cast(dag[8].get())->set_host(jupiter); - - for (const auto& a : dag) { - if (auto* comm = dynamic_cast(a.get())) { - auto pred = dynamic_cast((*comm->get_dependencies().begin()).get()); - auto succ = dynamic_cast(comm->get_successors().front().get()); - comm->set_source(pred->get_host())->set_destination(succ->get_host()); - } - } - - simgrid::s4u::Activity::on_completion_cb([](simgrid::s4u::Activity const& activity) { - XBT_INFO("Activity '%s' is complete (start time: %f, finish time: %f)", activity.get_cname(), activity.get_start_time(), - activity.get_finish_time()); - }); - - e.run(); - return 0; -} - diff --git a/docs/source/tuto_dag/dag_lab2-2.cpp b/docs/source/tuto_dag/dag_lab2-2.cpp deleted file mode 100644 index f392ecb99a..0000000000 --- a/docs/source/tuto_dag/dag_lab2-2.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include "simgrid/s4u.hpp" - -XBT_LOG_NEW_DEFAULT_CATEGORY(main, "Messages specific for this s4u example"); - -int main(int argc, char* argv[]) { - simgrid::s4u::Engine e(&argc, argv); - e.load_platform(argv[1]); - - std::vector dag = simgrid::s4u::create_DAG_from_json(argv[2]); - - simgrid::s4u::Activity::on_completion_cb([](simgrid::s4u::Activity const& activity) { - XBT_INFO("Activity '%s' is complete (start time: %f, finish time: %f)", activity.get_cname(), activity.get_start_time(), - activity.get_finish_time()); - }); - - e.run(); - return 0; -} - diff --git a/docs/source/tuto_dag/dag_lab2-3.cpp b/docs/source/tuto_dag/dag_lab2-3.cpp deleted file mode 100644 index e3db543c3c..0000000000 --- a/docs/source/tuto_dag/dag_lab2-3.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "simgrid/s4u.hpp" - -XBT_LOG_NEW_DEFAULT_CATEGORY(main, "Messages specific for this s4u example"); - -int main(int argc, char* argv[]) { - simgrid::s4u::Engine e(&argc, argv); - e.load_platform(argv[1]); - - std::vector dag = simgrid::s4u::create_DAG_from_DAX(argv[2]); - - simgrid::s4u::Host* tremblay = e.host_by_name("Tremblay"); - simgrid::s4u::Host* jupiter = e.host_by_name("Jupiter"); - simgrid::s4u::Host* fafard = e.host_by_name("Fafard"); - - dynamic_cast(dag[0].get())->set_host(fafard); - dynamic_cast(dag[1].get())->set_host(tremblay); - dynamic_cast(dag[2].get())->set_host(jupiter); - dynamic_cast(dag[3].get())->set_host(jupiter); - dynamic_cast(dag[8].get())->set_host(jupiter); - - for (const auto& a : dag) { - if (auto* comm = dynamic_cast(a.get())) { - auto pred = dynamic_cast((*comm->get_dependencies().begin()).get()); - auto succ = dynamic_cast(comm->get_successors().front().get()); - comm->set_source(pred->get_host())->set_destination(succ->get_host()); - } - } - - simgrid::s4u::Activity::on_completion_cb([](simgrid::s4u::Activity const& activity) { - XBT_INFO("Activity '%s' is complete (start time: %f, finish time: %f)", activity.get_cname(), activity.get_start_time(), - activity.get_finish_time()); - }); - - e.run(); - return 0; -} - diff --git a/docs/source/tuto_dag/small_platform.xml b/docs/source/tuto_dag/small_platform.xml deleted file mode 100644 index 69bdcae955..0000000000 --- a/docs/source/tuto_dag/small_platform.xml +++ /dev/null @@ -1,194 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/examples/cpp/CMakeLists.txt b/examples/cpp/CMakeLists.txt index cea4755a76..10fd24ae3b 100644 --- a/examples/cpp/CMakeLists.txt +++ b/examples/cpp/CMakeLists.txt @@ -99,9 +99,13 @@ if (SIMGRID_HAVE_MC) endforeach() endif() - if(NOT HAVE_GRAPHVIZ) set(_dag-from-dot_disable 1) + set(_dag-from-dot-simple_disable 1) +endif() + +if (NOT SIMGRID_HAVE_JSON) + set(_dag-from-json-simple_disable 1) endif() if(NOT SIMGRID_HAVE_NS3) @@ -116,10 +120,11 @@ foreach (example activity-testany activity-waitany actor-create actor-daemon actor-exiting actor-join actor-kill actor-lifetime actor-migrate actor-suspend actor-yield actor-stacksize app-bittorrent app-chainsend app-token-ring + battery-degradation battery-simple battery-energy comm-pingpong comm-ready comm-suspend comm-testany comm-wait comm-waitany comm-waitall comm-waituntil comm-dependent comm-host2host comm-failure comm-throttling cloud-capping cloud-migration cloud-simple - dag-comm dag-from-dax dag-from-dot dag-failure dag-io dag-scheduling dag-simple + dag-comm dag-from-json-simple dag-from-dax-simple dag-from-dax dag-from-dot-simple dag-from-dot dag-failure dag-io dag-scheduling dag-simple dag-tuto dht-chord dht-kademlia energy-exec energy-boot energy-link energy-vm energy-exec-ptask energy-wifi engine-filtering engine-run-partial @@ -286,14 +291,18 @@ set(xml_files ${xml_files} ${CMAKE_CURRENT_SOURCE_DIR}/actor-create/s4u-a ${CMAKE_CURRENT_SOURCE_DIR}/network-ns3/dogbone_d.xml ${CMAKE_CURRENT_SOURCE_DIR}/network-ns3/onelink_d.xml ${CMAKE_CURRENT_SOURCE_DIR}/network-ns3/one_cluster_d.xml PARENT_SCOPE) -set(bin_files ${bin_files} ${CMAKE_CURRENT_SOURCE_DIR}/dht-kademlia/generate.py +set(bin_files ${bin_files} ${CMAKE_CURRENT_SOURCE_DIR}/battery-degradation/plot_battery_degradation.py + ${CMAKE_CURRENT_SOURCE_DIR}/dht-kademlia/generate.py ${CMAKE_CURRENT_SOURCE_DIR}/mc-bugged1-liveness/s4u-mc-bugged1-liveness-stack-cleaner ${CMAKE_CURRENT_SOURCE_DIR}/mc-bugged1-liveness/promela_bugged1_liveness ${CMAKE_CURRENT_SOURCE_DIR}/mc-bugged2-liveness/promela_bugged2_liveness PARENT_SCOPE) set(txt_files ${txt_files} ${CMAKE_CURRENT_SOURCE_DIR}/dag-from-dax/simple_dax_with_cycle.xml ${CMAKE_CURRENT_SOURCE_DIR}/dag-from-dax/smalldax.xml + ${CMAKE_CURRENT_SOURCE_DIR}/dag-from-dax-simple/dag.xml ${CMAKE_CURRENT_SOURCE_DIR}/dag-from-dot/dag.dot + ${CMAKE_CURRENT_SOURCE_DIR}/dag-from-dot-simple/dag.dot ${CMAKE_CURRENT_SOURCE_DIR}/dag-from-dot/dag_with_cycle.dot + ${CMAKE_CURRENT_SOURCE_DIR}/dag-from-json-simple/dag.json ${CMAKE_CURRENT_SOURCE_DIR}/replay-comm/s4u-replay-comm-split-p0.txt ${CMAKE_CURRENT_SOURCE_DIR}/replay-comm/s4u-replay-comm-split-p1.txt ${CMAKE_CURRENT_SOURCE_DIR}/replay-comm/s4u-replay-comm.txt diff --git a/examples/cpp/battery-degradation/plot_battery_degradation.py b/examples/cpp/battery-degradation/plot_battery_degradation.py new file mode 100644 index 0000000000..6863832a64 --- /dev/null +++ b/examples/cpp/battery-degradation/plot_battery_degradation.py @@ -0,0 +1,16 @@ +# Copyright (c) 2003-2023. The SimGrid Team. +# All rights reserved. + +# This program is free software; you can redistribute it and/or modify it +# under the terms of the license (GNU LGPL) which comes with this package. + +import sys +import pandas as pd +import seaborn as sns + +df = pd.read_csv(sys.argv[1], names=['Time','Value', 'SoC - SoH']) +df['Time'] = df['Time'].apply(lambda x: float(x.split(" ")[-1])) +sns.set_theme() +sns.set(rc={'figure.figsize':(16,8)}) +g = sns.lineplot(data=df, x='Time', y='Value', hue='SoC - SoH') +g.get_figure().savefig('battery_degradation.svg') \ No newline at end of file diff --git a/examples/cpp/battery-degradation/s4u-battery-degradation.cpp b/examples/cpp/battery-degradation/s4u-battery-degradation.cpp new file mode 100644 index 0000000000..0aad5a1e56 --- /dev/null +++ b/examples/cpp/battery-degradation/s4u-battery-degradation.cpp @@ -0,0 +1,34 @@ +/* Copyright (c) 2003-2023. The SimGrid Team. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "simgrid/plugins/battery.hpp" +#include "simgrid/s4u.hpp" + +XBT_LOG_NEW_DEFAULT_CATEGORY(battery_degradation, "Messages specific for this s4u example"); + +static void manager() +{ + auto battery = simgrid::s4u::this_actor::get_host(); + double power = 100; + while (sg_battery_get_state_of_health(battery) > 0) { + XBT_INFO("%f,%f,SoC", simgrid::s4u::Engine::get_clock(), sg_battery_get_state_of_charge(battery)); + XBT_INFO("%f,%f,SoH", simgrid::s4u::Engine::get_clock(), sg_battery_get_state_of_health(battery)); + sg_battery_set_power(battery, power); + simgrid::s4u::this_actor::sleep_until(sg_battery_get_next_event_date(battery)); + power *= -1; + } +} + +int main(int argc, char* argv[]) +{ + simgrid::s4u::Engine e(&argc, argv); + e.load_platform(argv[1]); + + sg_battery_plugin_init(); + + simgrid::s4u::Actor::create("manager", e.get_all_hosts()[0], manager); + e.run(); + return 0; +} diff --git a/examples/cpp/battery-degradation/s4u-battery-degradation.tesh b/examples/cpp/battery-degradation/s4u-battery-degradation.tesh new file mode 100644 index 0000000000..876bd7a923 --- /dev/null +++ b/examples/cpp/battery-degradation/s4u-battery-degradation.tesh @@ -0,0 +1,5 @@ +#!/usr/bin/env tesh + +! output ignore + +$ ${bindir:=.}/s4u-battery-degradation ${platfdir}/battery_platform.xml diff --git a/examples/cpp/battery-energy/s4u-battery-energy.cpp b/examples/cpp/battery-energy/s4u-battery-energy.cpp new file mode 100644 index 0000000000..55bfc3c297 --- /dev/null +++ b/examples/cpp/battery-energy/s4u-battery-energy.cpp @@ -0,0 +1,73 @@ +/* Copyright (c) 2003-2023. The SimGrid Team. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "simgrid/plugins/battery.hpp" +#include "simgrid/plugins/energy.h" +#include "simgrid/s4u.hpp" + +#include + +XBT_LOG_NEW_DEFAULT_CATEGORY(battery_energy, "Messages specific for this s4u example"); + +static void manager() +{ + auto battery = simgrid::s4u::Engine::get_instance()->host_by_name("battery"); + auto host1 = simgrid::s4u::Engine::get_instance()->host_by_name("host1"); + auto host2 = simgrid::s4u::Engine::get_instance()->host_by_name("host2"); + + XBT_INFO("Initial Battery: SoC: %f SoH: %f Capacity (Total): %fWh Capacity (Usable): %fWh P: %fW", + sg_battery_get_state_of_charge(battery), sg_battery_get_state_of_health(battery), + sg_battery_get_capacity(battery), + sg_battery_get_capacity(battery) * + (sg_battery_get_state_of_charge_max(battery) - sg_battery_get_state_of_charge_min(battery)), + sg_battery_get_power(battery)); + + // Start execs on each host + simgrid::s4u::ExecPtr exec1 = simgrid::s4u::Exec::init(); + exec1->set_flops_amount(1e10); + exec1->set_host(host1); + exec1->start(); + simgrid::s4u::ExecPtr exec2 = simgrid::s4u::Exec::init(); + exec2->set_flops_amount(1e10); + exec2->set_host(host2); + exec2->start(); + // Set power generation from the battery + double total_power_w = + sg_host_get_wattmax_at(host1, host1->get_pstate()) + sg_host_get_wattmax_at(host2, host2->get_pstate()); + sg_battery_set_power(battery, total_power_w); + XBT_INFO("Battery power set to: %fW (host1) + %fW (host2)", sg_host_get_wattmax_at(host1, host1->get_pstate()), + sg_host_get_wattmax_at(host2, host2->get_pstate())); + double end = sg_battery_get_next_event_date(battery); + XBT_INFO("The battery will be depleted at: %f", end); + XBT_INFO("Exec1 will be finished in: %f", exec1->get_remaining() / host1->get_speed()); + XBT_INFO("Exec2 will be finished in: %f", exec2->get_remaining() / host2->get_speed()); + simgrid::s4u::this_actor::sleep_until(end); + + // Battery depleted + XBT_INFO("Battery depleted: SoC: %f SoH: %f P: %fW", sg_battery_get_state_of_charge(battery), + sg_battery_get_state_of_health(battery), sg_battery_get_power(battery)); + double energy_battery = sg_host_get_consumed_energy(host1) + sg_host_get_consumed_energy(host2); + XBT_INFO("Pursuing with power from the grid until both execs are finished"); + simgrid::s4u::this_actor::sleep_for( + std::max(exec1->get_remaining() / host1->get_speed(), exec2->get_remaining() / host2->get_speed())); + + // Execs finished + double energy_grid = sg_host_get_consumed_energy(host1) + sg_host_get_consumed_energy(host2) - energy_battery; + XBT_INFO("Energy consumed: Battery: %fJ (%fWh) Grid: %fJ (%fWh)", energy_battery, energy_battery / 3600, energy_grid, + energy_grid / 3600); +} + +int main(int argc, char* argv[]) +{ + simgrid::s4u::Engine e(&argc, argv); + e.load_platform(argv[1]); + + sg_host_energy_plugin_init(); + sg_battery_plugin_init(); + + simgrid::s4u::Actor::create("manager", e.host_by_name("host1"), manager); + e.run(); + return 0; +} diff --git a/examples/cpp/battery-energy/s4u-battery-energy.tesh b/examples/cpp/battery-energy/s4u-battery-energy.tesh new file mode 100644 index 0000000000..e0ecf5533a --- /dev/null +++ b/examples/cpp/battery-energy/s4u-battery-energy.tesh @@ -0,0 +1,15 @@ +#!/usr/bin/env tesh + +$ ${bindir:=.}/s4u-battery-energy ${platfdir}/battery_platform.xml +> [host1:manager:(1) 0.000000] [battery_energy/INFO] Initial Battery: SoC: 0.800000 SoH: 1.000000 Capacity (Total): 10.000000Wh Capacity (Usable): 6.000000Wh P: 0.000000W +> [host1:manager:(1) 0.000000] [battery_energy/INFO] Battery power set to: 200.000000W (host1) + 170.000000W (host2) +> [host1:manager:(1) 0.000000] [battery_energy/INFO] The battery will be depleted at: 58.378378 +> [host1:manager:(1) 0.000000] [battery_energy/INFO] Exec1 will be finished in: 100.000000 +> [host1:manager:(1) 0.000000] [battery_energy/INFO] Exec2 will be finished in: 200.000000 +> [host1:manager:(1) 58.378378] [battery_energy/INFO] Battery depleted: SoC: 0.200000 SoH: 0.995833 P: 0.000000W +> [host1:manager:(1) 58.378378] [battery_energy/INFO] Pursuing with power from the grid until both execs are finished +> [host1:manager:(1) 200.000000] [battery_energy/INFO] Energy consumed: Battery: 21600.000000J (6.000000Wh) Grid: 42400.000000J (11.777778Wh) +> [200.000000] [host_energy/INFO] Total energy consumption: 64000.000000 Joules (used hosts: 64000.000000 Joules; unused/idle hosts: 0.000000) +> [200.000000] [host_energy/INFO] Energy consumption of host battery: 0.000000 Joules +> [200.000000] [host_energy/INFO] Energy consumption of host host1: 30000.000000 Joules +> [200.000000] [host_energy/INFO] Energy consumption of host host2: 34000.000000 Joules diff --git a/examples/cpp/battery-simple/s4u-battery-simple.cpp b/examples/cpp/battery-simple/s4u-battery-simple.cpp new file mode 100644 index 0000000000..435c402a21 --- /dev/null +++ b/examples/cpp/battery-simple/s4u-battery-simple.cpp @@ -0,0 +1,41 @@ +/* Copyright (c) 2003-2023. The SimGrid Team. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "simgrid/plugins/battery.hpp" +#include "simgrid/s4u.hpp" + +XBT_LOG_NEW_DEFAULT_CATEGORY(battery_simple, "Messages specific for this s4u example"); + +static void manager() +{ + auto battery = simgrid::s4u::Engine::get_instance()->host_by_name("battery"); + double consumption_w = 200; + + XBT_INFO("Initial Battery: SoC: %f SoH: %f Capacity (Total): %fWh Capacity (Usable): %fWh P: %f", + sg_battery_get_state_of_charge(battery), sg_battery_get_state_of_health(battery), + sg_battery_get_capacity(battery), + sg_battery_get_capacity(battery) * + (sg_battery_get_state_of_charge_max(battery) - sg_battery_get_state_of_charge_min(battery)), + sg_battery_get_power(battery)); + double start = simgrid::s4u::Engine::get_clock(); + sg_battery_set_power(battery, consumption_w); + XBT_INFO("Battery power set to: %fW", consumption_w); + double end = sg_battery_get_next_event_date(battery); + XBT_INFO("The battery will be depleted at: %f", end); + simgrid::s4u::this_actor::sleep_until(end); + XBT_INFO("Energy consumed : %fJ (%fWh)", (end - start) * consumption_w, (end - start) * consumption_w / 3600); +} + +int main(int argc, char* argv[]) +{ + simgrid::s4u::Engine e(&argc, argv); + e.load_platform(argv[1]); + + sg_battery_plugin_init(); + + simgrid::s4u::Actor::create("manager", e.host_by_name("host1"), manager); + e.run(); + return 0; +} diff --git a/examples/cpp/battery-simple/s4u-battery-simple.tesh b/examples/cpp/battery-simple/s4u-battery-simple.tesh new file mode 100644 index 0000000000..b2691454e9 --- /dev/null +++ b/examples/cpp/battery-simple/s4u-battery-simple.tesh @@ -0,0 +1,7 @@ +#!/usr/bin/env tesh + +$ ${bindir:=.}/s4u-battery-simple ${platfdir}/battery_platform.xml +> [host1:manager:(1) 0.000000] [battery_simple/INFO] Initial Battery: SoC: 0.800000 SoH: 1.000000 Capacity (Total): 10.000000Wh Capacity (Usable): 6.000000Wh P: 0.000000 +> [host1:manager:(1) 0.000000] [battery_simple/INFO] Battery power set to: 200.000000W +> [host1:manager:(1) 0.000000] [battery_simple/INFO] The battery will be depleted at: 108.000000 +> [host1:manager:(1) 108.000000] [battery_simple/INFO] Energy consumed : 21600.000000J (6.000000Wh) diff --git a/docs/source/tuto_dag/simple_dax.xml b/examples/cpp/dag-from-dax-simple/dag.xml similarity index 100% rename from docs/source/tuto_dag/simple_dax.xml rename to examples/cpp/dag-from-dax-simple/dag.xml diff --git a/examples/cpp/dag-from-dax-simple/s4u-dag-from-dax-simple.cpp b/examples/cpp/dag-from-dax-simple/s4u-dag-from-dax-simple.cpp new file mode 100644 index 0000000000..d55ec54f0a --- /dev/null +++ b/examples/cpp/dag-from-dax-simple/s4u-dag-from-dax-simple.cpp @@ -0,0 +1,42 @@ +/* Copyright (c) 2003-2023. The SimGrid Team. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "simgrid/s4u.hpp" + +XBT_LOG_NEW_DEFAULT_CATEGORY(dag_from_dax_simple, "Messages specific for this s4u example"); + +int main(int argc, char* argv[]) +{ + simgrid::s4u::Engine e(&argc, argv); + e.load_platform(argv[1]); + + std::vector dag = simgrid::s4u::create_DAG_from_DAX(argv[2]); + + simgrid::s4u::Host* tremblay = e.host_by_name("Tremblay"); + simgrid::s4u::Host* jupiter = e.host_by_name("Jupiter"); + simgrid::s4u::Host* fafard = e.host_by_name("Fafard"); + + dynamic_cast(dag[0].get())->set_host(fafard); + dynamic_cast(dag[1].get())->set_host(tremblay); + dynamic_cast(dag[2].get())->set_host(jupiter); + dynamic_cast(dag[3].get())->set_host(jupiter); + dynamic_cast(dag[8].get())->set_host(jupiter); + + for (const auto& a : dag) { + if (auto* comm = dynamic_cast(a.get())) { + auto pred = dynamic_cast((*comm->get_dependencies().begin()).get()); + auto succ = dynamic_cast(comm->get_successors().front().get()); + comm->set_source(pred->get_host())->set_destination(succ->get_host()); + } + } + + simgrid::s4u::Activity::on_completion_cb([](simgrid::s4u::Activity const& activity) { + XBT_INFO("Activity '%s' is complete (start time: %f, finish time: %f)", activity.get_cname(), + activity.get_start_time(), activity.get_finish_time()); + }); + + e.run(); + return 0; +} diff --git a/examples/cpp/dag-from-dax-simple/s4u-dag-from-dax-simple.tesh b/examples/cpp/dag-from-dax-simple/s4u-dag-from-dax-simple.tesh new file mode 100644 index 0000000000..7e7819474b --- /dev/null +++ b/examples/cpp/dag-from-dax-simple/s4u-dag-from-dax-simple.tesh @@ -0,0 +1,12 @@ +#!/usr/bin/env tesh + +$ ${bindir:=.}/s4u-dag-from-dax-simple --log=no_loc ${platfdir}/small_platform.xml ${srcdir:=.}/dag.xml +> [0.000000] [dag_from_dax_simple/INFO] Activity 'root' is complete (start time: 0.000000, finish time: 0.000000) +> [33.394394] [dag_from_dax_simple/INFO] Activity 'root_i2_2@c2' is complete (start time: 0.000000, finish time: 33.394394) +> [39.832311] [dag_from_dax_simple/INFO] Activity 'root_i1_1@c1' is complete (start time: 0.000000, finish time: 39.832311) +> [467.988690] [dag_from_dax_simple/INFO] Activity '1@c1' is complete (start time: 39.832311, finish time: 467.988690) +> [543.077868] [dag_from_dax_simple/INFO] Activity '1@c1_o1_3@c3' is complete (start time: 467.988690, finish time: 543.077868) +> [2785.832267] [dag_from_dax_simple/INFO] Activity '2@c2' is complete (start time: 33.394394, finish time: 2785.832267) +> [3886.807417] [dag_from_dax_simple/INFO] Activity '3@c3' is complete (start time: 2785.832267, finish time: 3886.807417) +> [3887.221639] [dag_from_dax_simple/INFO] Activity '3@c3_o3_end' is complete (start time: 3886.807417, finish time: 3887.221639) +> [3887.221639] [dag_from_dax_simple/INFO] Activity 'end' is complete (start time: 3887.221639, finish time: 3887.221639) \ No newline at end of file diff --git a/docs/source/tuto_dag/simple_dot.dot b/examples/cpp/dag-from-dot-simple/dag.dot similarity index 100% rename from docs/source/tuto_dag/simple_dot.dot rename to examples/cpp/dag-from-dot-simple/dag.dot diff --git a/examples/cpp/dag-from-dot-simple/s4u-dag-from-dot-simple.cpp b/examples/cpp/dag-from-dot-simple/s4u-dag-from-dot-simple.cpp new file mode 100644 index 0000000000..5fbcf2cc74 --- /dev/null +++ b/examples/cpp/dag-from-dot-simple/s4u-dag-from-dot-simple.cpp @@ -0,0 +1,42 @@ +/* Copyright (c) 2003-2023. The SimGrid Team. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "simgrid/s4u.hpp" + +XBT_LOG_NEW_DEFAULT_CATEGORY(dag_from_dot_simple, "Messages specific for this s4u example"); + +int main(int argc, char* argv[]) +{ + simgrid::s4u::Engine e(&argc, argv); + e.load_platform(argv[1]); + + std::vector dag = simgrid::s4u::create_DAG_from_dot(argv[2]); + + simgrid::s4u::Host* tremblay = e.host_by_name("Tremblay"); + simgrid::s4u::Host* jupiter = e.host_by_name("Jupiter"); + simgrid::s4u::Host* fafard = e.host_by_name("Fafard"); + + dynamic_cast(dag[0].get())->set_host(fafard); + dynamic_cast(dag[1].get())->set_host(tremblay); + dynamic_cast(dag[2].get())->set_host(jupiter); + dynamic_cast(dag[3].get())->set_host(jupiter); + dynamic_cast(dag[8].get())->set_host(jupiter); + + for (const auto& a : dag) { + if (auto* comm = dynamic_cast(a.get())) { + auto pred = dynamic_cast((*comm->get_dependencies().begin()).get()); + auto succ = dynamic_cast(comm->get_successors().front().get()); + comm->set_source(pred->get_host())->set_destination(succ->get_host()); + } + } + + simgrid::s4u::Activity::on_completion_cb([](simgrid::s4u::Activity const& activity) { + XBT_INFO("Activity '%s' is complete (start time: %f, finish time: %f)", activity.get_cname(), + activity.get_start_time(), activity.get_finish_time()); + }); + + e.run(); + return 0; +} diff --git a/examples/cpp/dag-from-dot-simple/s4u-dag-from-dot-simple.tesh b/examples/cpp/dag-from-dot-simple/s4u-dag-from-dot-simple.tesh new file mode 100644 index 0000000000..d6a88d5a26 --- /dev/null +++ b/examples/cpp/dag-from-dot-simple/s4u-dag-from-dot-simple.tesh @@ -0,0 +1,12 @@ +#!/usr/bin/env tesh + +$ ${bindir:=.}/s4u-dag-from-dot-simple --log=no_loc ${platfdir}/small_platform.xml ${srcdir:=.}/dag.dot +> [0.000000] [dag_from_dot_simple/INFO] Activity 'root' is complete (start time: 0.000000, finish time: 0.000000) +> [33.394394] [dag_from_dot_simple/INFO] Activity 'root->c2' is complete (start time: 0.000000, finish time: 33.394394) +> [39.832311] [dag_from_dot_simple/INFO] Activity 'root->c1' is complete (start time: 0.000000, finish time: 39.832311) +> [50.026511] [dag_from_dot_simple/INFO] Activity 'c1' is complete (start time: 39.832311, finish time: 50.026511) +> [98.928629] [dag_from_dot_simple/INFO] Activity 'c2' is complete (start time: 33.394394, finish time: 98.928629) +> [125.115689] [dag_from_dot_simple/INFO] Activity 'c1->c3' is complete (start time: 50.026511, finish time: 125.115689) +> [151.329383] [dag_from_dot_simple/INFO] Activity 'c3' is complete (start time: 125.115689, finish time: 151.329383) +> [151.743605] [dag_from_dot_simple/INFO] Activity 'c3->end' is complete (start time: 151.329383, finish time: 151.743605) +> [151.743605] [dag_from_dot_simple/INFO] Activity 'end' is complete (start time: 151.743605, finish time: 151.743605) \ No newline at end of file diff --git a/docs/source/tuto_dag/simple_json.json b/examples/cpp/dag-from-json-simple/dag.json similarity index 100% rename from docs/source/tuto_dag/simple_json.json rename to examples/cpp/dag-from-json-simple/dag.json diff --git a/examples/cpp/dag-from-json-simple/s4u-dag-from-json-simple.cpp b/examples/cpp/dag-from-json-simple/s4u-dag-from-json-simple.cpp new file mode 100644 index 0000000000..e6246b1796 --- /dev/null +++ b/examples/cpp/dag-from-json-simple/s4u-dag-from-json-simple.cpp @@ -0,0 +1,24 @@ +/* Copyright (c) 2003-2023. The SimGrid Team. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "simgrid/s4u.hpp" + +XBT_LOG_NEW_DEFAULT_CATEGORY(dag_from_json_simple, "Messages specific for this s4u example"); + +int main(int argc, char* argv[]) +{ + simgrid::s4u::Engine e(&argc, argv); + e.load_platform(argv[1]); + + std::vector dag = simgrid::s4u::create_DAG_from_json(argv[2]); + + simgrid::s4u::Activity::on_completion_cb([](simgrid::s4u::Activity const& activity) { + XBT_INFO("Activity '%s' is complete (start time: %f, finish time: %f)", activity.get_cname(), + activity.get_start_time(), activity.get_finish_time()); + }); + + e.run(); + return 0; +} diff --git a/examples/cpp/dag-from-json-simple/s4u-dag-from-json-simple.tesh b/examples/cpp/dag-from-json-simple/s4u-dag-from-json-simple.tesh new file mode 100644 index 0000000000..1d37a65809 --- /dev/null +++ b/examples/cpp/dag-from-json-simple/s4u-dag-from-json-simple.tesh @@ -0,0 +1,7 @@ +#!/usr/bin/env tesh + +$ ${bindir:=.}/s4u-dag-from-json-simple --log=no_loc ${platfdir}/small_platform.xml ${srcdir:=.}/dag.json +> [10.194200] [dag_from_json_simple/INFO] Activity 'c1' is complete (start time: 0.000000, finish time: 10.194200) +> [65.534235] [dag_from_json_simple/INFO] Activity 'c2' is complete (start time: 0.000000, finish time: 65.534235) +> [85.283378] [dag_from_json_simple/INFO] Activity 't1' is complete (start time: 10.194200, finish time: 85.283378) +> [111.497072] [dag_from_json_simple/INFO] Activity 'c3' is complete (start time: 85.283378, finish time: 111.497072) \ No newline at end of file diff --git a/examples/cpp/dag-tuto/s4u-dag-tuto.cpp b/examples/cpp/dag-tuto/s4u-dag-tuto.cpp new file mode 100644 index 0000000000..39b56d6c52 --- /dev/null +++ b/examples/cpp/dag-tuto/s4u-dag-tuto.cpp @@ -0,0 +1,53 @@ +/* Copyright (c) 2003-2023. The SimGrid Team. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "simgrid/s4u.hpp" + +XBT_LOG_NEW_DEFAULT_CATEGORY(dag_tuto, "Messages specific for this s4u tutorial"); + +int main(int argc, char* argv[]) +{ + simgrid::s4u::Engine e(&argc, argv); + e.load_platform(argv[1]); + + simgrid::s4u::Host* tremblay = e.host_by_name("Tremblay"); + simgrid::s4u::Host* jupiter = e.host_by_name("Jupiter"); + + simgrid::s4u::ExecPtr c1 = simgrid::s4u::Exec::init(); + simgrid::s4u::ExecPtr c2 = simgrid::s4u::Exec::init(); + simgrid::s4u::ExecPtr c3 = simgrid::s4u::Exec::init(); + simgrid::s4u::CommPtr t1 = simgrid::s4u::Comm::sendto_init(); + + c1->set_name("c1"); + c2->set_name("c2"); + c3->set_name("c3"); + t1->set_name("t1"); + + c1->set_flops_amount(1e9); + c2->set_flops_amount(5e9); + c3->set_flops_amount(2e9); + t1->set_payload_size(5e8); + + c1->add_successor(t1); + t1->add_successor(c3); + c2->add_successor(c3); + + c1->set_host(tremblay); + c2->set_host(jupiter); + c3->set_host(jupiter); + t1->set_source(tremblay); + t1->set_destination(jupiter); + + c1->start(); + c2->start(); + + simgrid::s4u::Activity::on_completion_cb([](simgrid::s4u::Activity const& activity) { + XBT_INFO("Activity '%s' is complete (start time: %f, finish time: %f)", activity.get_cname(), + activity.get_start_time(), activity.get_finish_time()); + }); + + e.run(); + return 0; +} diff --git a/examples/cpp/dag-tuto/s4u-dag-tuto.tesh b/examples/cpp/dag-tuto/s4u-dag-tuto.tesh new file mode 100644 index 0000000000..56e448fbdb --- /dev/null +++ b/examples/cpp/dag-tuto/s4u-dag-tuto.tesh @@ -0,0 +1,7 @@ +#!/usr/bin/env tesh + +$ ${bindir:=.}/s4u-dag-tuto --log=no_loc ${platfdir}/small_platform.xml +> [10.194200] [dag_tuto/INFO] Activity 'c1' is complete (start time: 0.000000, finish time: 10.194200) +> [65.534235] [dag_tuto/INFO] Activity 'c2' is complete (start time: 0.000000, finish time: 65.534235) +> [85.283378] [dag_tuto/INFO] Activity 't1' is complete (start time: 10.194200, finish time: 85.283378) +> [111.497072] [dag_tuto/INFO] Activity 'c3' is complete (start time: 85.283378, finish time: 111.497072) \ No newline at end of file diff --git a/examples/platforms/battery_platform.xml b/examples/platforms/battery_platform.xml new file mode 100644 index 0000000000..210b4cb1c4 --- /dev/null +++ b/examples/platforms/battery_platform.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/include/simgrid/plugins/battery.hpp b/include/simgrid/plugins/battery.hpp new file mode 100644 index 0000000000..3899ff0cf2 --- /dev/null +++ b/include/simgrid/plugins/battery.hpp @@ -0,0 +1,27 @@ +#ifndef SIMGRID_PLUGINS_BATTERY_H_ +#define SIMGRID_PLUGINS_BATTERY_H_ + +#include +#include +#include + +SG_BEGIN_DECL + +XBT_PUBLIC void sg_battery_plugin_init(); + +XBT_PUBLIC void sg_battery_set_state(const_sg_host_t host, bool state); +XBT_PUBLIC void sg_battery_set_power(const_sg_host_t host, double power); + +XBT_PUBLIC bool sg_battery_is_active(const_sg_host_t host); +XBT_PUBLIC double sg_battery_get_power(const_sg_host_t host); +XBT_PUBLIC double sg_battery_get_state_of_charge(const_sg_host_t host); +XBT_PUBLIC double sg_battery_get_state_of_charge_min(const_sg_host_t host); +XBT_PUBLIC double sg_battery_get_state_of_charge_max(const_sg_host_t host); +XBT_PUBLIC double sg_battery_get_state_of_health(const_sg_host_t host); +XBT_PUBLIC double sg_battery_get_capacity(const_sg_host_t host); +XBT_PUBLIC double sg_battery_get_cumulative_cost(const_sg_host_t host); +XBT_PUBLIC double sg_battery_get_next_event_date(const_sg_host_t host); + +SG_END_DECL + +#endif \ No newline at end of file diff --git a/src/plugins/battery.cpp b/src/plugins/battery.cpp new file mode 100644 index 0000000000..262809584d --- /dev/null +++ b/src/plugins/battery.cpp @@ -0,0 +1,598 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "src/kernel/resource/CpuImpl.hpp" +#include "src/simgrid/module.hpp" + +#include +#include + +SIMGRID_REGISTER_PLUGIN(battery, "Battery management", &sg_battery_plugin_init) + +/** @defgroup plugin_battery plugin_battery Plugin Battery + + @beginrst + +This is the battery plugin, enabling management of batteries on hosts. +To activate this plugin, first call :cpp:func:`sg_battery_plugin_init()`. + +We consider a constant energy exchange model. + +Properties of batteries such as State of Charge and State of Health are lazily updated, ie., when reading values and +when the power is modified. + +State of Charge (SoC) +..................... + +If the power of a battery is set to a negative value then the battery will act as a load and fill over time until it +reaches its maximal SoC. Conversely, if the power of a battery is set to a positive value then the battery will act as a +generator and deplete over time until it reaches its minimal SoC. When reaching either its maximal or minimal SoC it +will set its power to 0. + +The natural depletion of batteries over time is not taken into account. + +State of Health (SoH) +..................... + +A battery starts with an energy budget :math:`E` such as: + +.. math:: + + E = C \times U \times D \times N \times 2 + +Where :math:`C` is the initial capacity, :math:`U` is the ratio of usable capacity, :math:`D` is the depth of discharge +and :math:`N` is the number of cycles of the battery. + +The SoH represents the consumption of this energy budget during the lifetime of the battery. +Use the battery reduces its SoH and its capacity in consequence. +When the SoH reaches 0, the battery becomes unusable. + +.. warning:: + + Due to the decrease of the battery capacity with the SoH, a large usable capacity leads to very tiny battery capacity + when reaching low SoH. This may results in charge and discharge cycles too short to be evaluated by the simulator. To + avoid this situation you should not try to reach a SoH of 0 with a usable capacity set to 1. + +Plotting the output of the example "battery-degradation" highlights the linear decrease of the SoH due to a continuous +use of the battery and the decreasing cycle duration as its capacity reduces: + +.. image:: /img/battery_degradation.svg + :align: center + +Batteries properties +.................... + +Properties of the batteries are defined as properties of hosts in the platform XML file. + +Here is an example of XML declaration: + +.. code-block:: xml + + + + + + + + +The different properties are: + +- ``battery_active``: Set the battery as active if set to 1 (default=0) +- ``battery_power``: Set the initial power of the battery in W (default=0). A negative value indicates that the battery act as a load and charge. A positive value indicates that the battery act as a generator and discharge +- ``battery_state_of_charge``: Set the initial state of charge of the battery (default=0) +- ``battery_state_of_charge_min``: Set the minimal state of charge of the battery (default=0.2) +- ``battery_state_of_charge_max``: Set the maximal state of charge of the battery (default=0.8) +- ``battery_capacity``: Set the initial capacity of the battery in Wh (default=0) +- ``battery_usable_capacity``: Set the ratio of usable capacity of the battery (default=0.8) +- ``battery_depth_of_discharge``: Set the depth of discharge of the battery (default=0.9) +- ``battery_charge_efficiency``: Set the charge efficiency of the battery (default=1) +- ``battery_discharge_efficiency``: Set the charge efficiency of the battery (default=1) +- ``battery_cycles``: Set the number of cycle of the battery (default=1) +- ``battery_depth_of_discharge``: Set the number of cycle of the battery (default=1) +- ``battery_eval_cost``: Evaluate the cost of the battery during the simulation if set to 1 (defaulf=0) +- ``battery_investment_cost``: Set the investment cost of the battery (default=0) +- ``battery_static_maintenance_cost``: Set the static maintenance cost of the battery (default=0) +- ``battery_variable_maintenance_cost``: Set the variable maintenance cost of the battery (default=0) + + @endrst + */ +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(battery, kernel, "Logging specific to the battery plugin"); + +namespace simgrid::plugin { +class Battery { +private: + simgrid::s4u::Host* host_ = nullptr; + + double state_of_charge_min_ = 0.2; + double state_of_charge_max_ = 0.8; + double charge_efficiency_ = 1; + double discharge_efficiency_ = 1; + double initial_capacity_wh_ = 0; + double cycles_ = 1; // total complete cycles (charge + discharge) the battery can do before complete depletion. + double depth_of_discharge_ = 0.9; + double usable_capacity_ = 0.8; + double energy_budget_j_ = 0; + + bool active_ = false; + double power_w_ = 0; // NEGATIVE when charging (consumes power) POSITIVE when discharging (generates power) + double state_of_charge_ = 0; + double capacity_wh_ = 0; + double next_event_ = -1; + double energy_exchanged_j_ = 0; + double last_updated_ = 0; + + // Calculation of costs from Bei Li thesis (link :https://tel.archives-ouvertes.fr/tel-02077668/document) (chapter 4) + bool eval_cost_ = false; + double cumulative_cost_ = 0; + double investment_cost_per_wh_ = 0; + double static_maintenance_cost_per_wh_times_h_ = 0; + double variable_maintenance_cost_per_wh_ = 0; + + void init_battery_params(); + void init_cost_params(); + void update(); + + void set_state_of_charge(double soc); + void set_state_of_charge_min(double soc); + void set_state_of_charge_max(double soc); + void set_capacity(double c); + void set_initial_capacity(double c); + void set_cycles(int c); + void set_depth_of_discharge(double d); + void set_usable_capacity(double c); + void set_charge_efficiency(double e); + void set_discharge_efficiency(double e); + void set_eval_cost(bool eval); + void set_investment_cost(double c); + void set_static_maintenance_cost(double c); + void set_variable_maintenance_cost(double c); + + bool is_charging(); + +public: + static simgrid::xbt::Extension EXTENSION_ID; + + explicit Battery(simgrid::s4u::Host* host); + ~Battery(); + + void set_state(bool state); + void set_power(const double power); + + bool is_active() const; + double get_power(); + double get_state_of_charge(); + double get_state_of_charge_min(); + double get_state_of_charge_max(); + double get_state_of_health(); + double get_capacity(); + double get_cumulative_cost(); + double get_next_event_date(); +}; + +simgrid::xbt::Extension Battery::EXTENSION_ID; + +void Battery::set_power(const double p) +{ + update(); + xbt_assert(p == 0 or (p > 0 and state_of_charge_ > state_of_charge_min_) or + (p < 0 and state_of_charge_ < state_of_charge_max_), + "Incoherent power and state of charge. A battery cannot charge(discharge) past its maximal(minimal) state " + "of charge."); + xbt_assert(p == 0 or energy_exchanged_j_ < energy_budget_j_, "Cannot set power of a fully used battery."); + simgrid::kernel::actor::simcall_answered([this, p] { + power_w_ = p; + if (power_w_ == 0) { + next_event_ = -1; + return; + } + double soc_shutdown; + double soh_shutdown; + if (power_w_ > 0) { + soc_shutdown = capacity_wh_ * 3600 * (state_of_charge_ - state_of_charge_min_) / (power_w_ * charge_efficiency_); + soh_shutdown = (energy_budget_j_ - energy_exchanged_j_) / (power_w_ * charge_efficiency_); + } else if (power_w_ < 0) { + soc_shutdown = + capacity_wh_ * 3600 * (state_of_charge_max_ - state_of_charge_) / (abs(power_w_) / discharge_efficiency_); + soh_shutdown = (energy_budget_j_ - energy_exchanged_j_) / (abs(power_w_) / discharge_efficiency_); + } + if (soh_shutdown <= 0) + next_event_ = simgrid::s4u::Engine::get_clock() + soc_shutdown; + else + next_event_ = simgrid::s4u::Engine::get_clock() + std::min(soc_shutdown, soh_shutdown); + }); +} + +void Battery::set_state(bool state) +{ + update(); + simgrid::kernel::actor::simcall_answered([this, state] { active_ = state; }); +} + +void Battery::set_state_of_charge(double soc) +{ + xbt_assert(soc > 0 and soc <= 1, " : state of charge should be in ]0,1] (provided: %f)", soc); + simgrid::kernel::actor::simcall_answered([this, soc] { state_of_charge_ = soc; }); +} + +void Battery::set_state_of_charge_min(double soc) +{ + xbt_assert(soc > 0 and soc <= 1 and soc < state_of_charge_max_, + " : state of charge min should be in ]0,1] and below state of charge max (provided: %f)", soc); + simgrid::kernel::actor::simcall_answered([this, soc] { state_of_charge_min_ = soc; }); +} + +void Battery::set_state_of_charge_max(double soc) +{ + xbt_assert(soc > 0 and soc <= 1 and soc > state_of_charge_min_, + " : state of charge max should be in ]0,1] and above state of charge min (provided: %f)", soc); + simgrid::kernel::actor::simcall_answered([this, soc] { state_of_charge_max_ = soc; }); +} + +void Battery::set_initial_capacity(double c) +{ + xbt_assert(c > 0, " : capacity should be > 0 (provided: %f)", c); + simgrid::kernel::actor::simcall_answered([this, c] { initial_capacity_wh_ = c; }); +} + +void Battery::set_capacity(double c) +{ + xbt_assert(c > 0, " : capacity should be > 0 (provided: %f)", c); + simgrid::kernel::actor::simcall_answered([this, c] { capacity_wh_ = c; }); +} + +void Battery::set_cycles(int c) +{ + xbt_assert(c > 0, " : cycles should be > 0 (provided: %d)", c); + simgrid::kernel::actor::simcall_answered([this, c] { cycles_ = c; }); +} + +void Battery::set_depth_of_discharge(double d) +{ + xbt_assert(d > 0 and d <= 1, " : depth of discharge should be in ]0, 1] (provided: %f)", d); + simgrid::kernel::actor::simcall_answered([this, d] { depth_of_discharge_ = d; }); +} + +void Battery::set_usable_capacity(double c) +{ + xbt_assert(c > 0 and c <= 1, " : usable capacity should be in ]0, 1] (provided: %f)", c); + simgrid::kernel::actor::simcall_answered([this, c] { usable_capacity_ = c; }); +} + +void Battery::set_charge_efficiency(double e) +{ + xbt_assert(e > 0 and e <= 1, " : charge efficiency should be in [0,1] (provided: %f)", e); + simgrid::kernel::actor::simcall_answered([this, e] { charge_efficiency_ = e; }); +} + +void Battery::set_discharge_efficiency(double e) +{ + xbt_assert(e > 0 and e <= 1, " : discharge efficiency should be in [0,1] (provided: %f)", e); + simgrid::kernel::actor::simcall_answered([this, e] { discharge_efficiency_ = e; }); +} + +void Battery::set_eval_cost(bool eval) +{ + simgrid::kernel::actor::simcall_answered([this, eval] { eval_cost_ = eval; }); +} + +void Battery::set_investment_cost(double c) +{ + xbt_assert(c >= 0, " : investment cost should be >= 0 (provided: %f)", c); + simgrid::kernel::actor::simcall_answered([this, c] { investment_cost_per_wh_ = c; }); +} + +void Battery::set_static_maintenance_cost(double c) +{ + xbt_assert(c >= 0, " : static maintenance cost should be >= 0 (provided: %f)", c); + simgrid::kernel::actor::simcall_answered([this, c] { static_maintenance_cost_per_wh_times_h_ = c; }); +} + +void Battery::set_variable_maintenance_cost(double c) +{ + xbt_assert(c >= 0, " : variable maintenance cost should be >= 0 (provided: %f)", c); + simgrid::kernel::actor::simcall_answered([this, c] { variable_maintenance_cost_per_wh_ = c; }); +} + +bool Battery::is_charging() +{ + update(); + return power_w_ < 0; +} + +bool Battery::is_active() const +{ + return active_; +} + +double Battery::get_power() +{ + update(); + return power_w_; +} + +double Battery::get_state_of_charge() +{ + update(); + return state_of_charge_; +} + +double Battery::get_state_of_charge_min() +{ + return state_of_charge_min_; +} + +double Battery::get_state_of_charge_max() +{ + return state_of_charge_max_; +} + +double Battery::get_state_of_health() +{ + update(); + return 1 - (energy_exchanged_j_ / energy_budget_j_); +} + +double Battery::get_capacity() +{ + update(); + return capacity_wh_; +} + +double Battery::get_cumulative_cost() +{ + update(); + return cumulative_cost_; +} + +double Battery::get_next_event_date() +{ + update(); + return next_event_; +} + +void Battery::init_battery_params() +{ + const char* prop_chars; + prop_chars = host_->get_property("battery_capacity"); + if (prop_chars) { + set_capacity(xbt_str_parse_double(prop_chars, ("cannot parse double: " + std::string(prop_chars)).c_str())); + set_initial_capacity(xbt_str_parse_double(prop_chars, ("cannot parse double: " + std::string(prop_chars)).c_str())); + } + prop_chars = host_->get_property("battery_usable_capacity"); + if (prop_chars) + set_usable_capacity(xbt_str_parse_double(prop_chars, ("cannot parse double: " + std::string(prop_chars)).c_str())); + prop_chars = host_->get_property("battery_depth_of_discharge"); + if (prop_chars) + set_depth_of_discharge( + xbt_str_parse_double(prop_chars, ("cannot parse double: " + std::string(prop_chars)).c_str())); + prop_chars = host_->get_property("battery_cycles"); + if (prop_chars) + set_cycles(xbt_str_parse_int(prop_chars, ("cannot parse int: " + std::string(prop_chars)).c_str())); + simgrid::kernel::actor::simcall_answered([this] { + energy_budget_j_ = (initial_capacity_wh_ * usable_capacity_ * depth_of_discharge_ * 3600 * cycles_ * 2); + }); + prop_chars = host_->get_property("battery_active"); + if (prop_chars) + set_state(xbt_str_parse_int(prop_chars, ("cannot parse int: " + std::string(prop_chars)).c_str())); + prop_chars = host_->get_property("battery_state_of_charge_min"); + if (prop_chars) + set_state_of_charge_min( + xbt_str_parse_double(prop_chars, ("cannot parse double: " + std::string(prop_chars)).c_str())); + prop_chars = host_->get_property("battery_state_of_charge_max"); + if (prop_chars) + set_state_of_charge_max( + xbt_str_parse_double(prop_chars, ("cannot parse double: " + std::string(prop_chars)).c_str())); + prop_chars = host_->get_property("battery_charge_efficiency"); + if (prop_chars) + set_charge_efficiency( + xbt_str_parse_double(prop_chars, ("cannot parse double: " + std::string(prop_chars)).c_str())); + prop_chars = host_->get_property("battery_discharge_efficiency"); + if (prop_chars) + set_discharge_efficiency( + xbt_str_parse_double(prop_chars, ("cannot parse double: " + std::string(prop_chars)).c_str())); + prop_chars = host_->get_property("battery_state_of_charge"); + if (prop_chars) + set_state_of_charge(xbt_str_parse_double(prop_chars, ("cannot parse double: " + std::string(prop_chars)).c_str())); + prop_chars = host_->get_property("battery_eval_cost"); + if (prop_chars) + set_eval_cost(xbt_str_parse_int(prop_chars, ("cannot parse int: " + std::string(prop_chars)).c_str())); + prop_chars = host_->get_property("battery_investment_cost"); + if (prop_chars) + set_investment_cost(xbt_str_parse_int(prop_chars, ("cannot parse double: " + std::string(prop_chars)).c_str())); + prop_chars = host_->get_property("battery_static_maintenance_cost"); + if (prop_chars) + set_static_maintenance_cost( + xbt_str_parse_int(prop_chars, ("cannot parse double: " + std::string(prop_chars)).c_str())); + prop_chars = host_->get_property("battery_variable_maintenance_cost"); + if (prop_chars) + set_variable_maintenance_cost( + xbt_str_parse_int(prop_chars, ("cannot parse double: " + std::string(prop_chars)).c_str())); + prop_chars = host_->get_property("battery_power"); + if (prop_chars) + set_power(xbt_str_parse_double(prop_chars, ("cannot parse double: " + std::string(prop_chars)).c_str())); + simgrid::kernel::actor::simcall_answered([this] { last_updated_ = simgrid::s4u::Engine::get_clock(); }); +} + +void Battery::update() +{ + simgrid::kernel::actor::simcall_answered([this] { + double now = simgrid::s4u::Engine::get_clock(); + double time_delta_real = now - last_updated_; + if (time_delta_real <= 0 or not is_active()) + return; + double time_delta_until_event = next_event_ - last_updated_; + bool event = next_event_ != -1 and time_delta_until_event <= time_delta_real; + double power_real_w = power_w_ < 0 ? power_w_ * charge_efficiency_ : power_w_ / discharge_efficiency_; + state_of_charge_ -= power_real_w * (event ? time_delta_until_event : time_delta_real) / (3600 * capacity_wh_); + energy_exchanged_j_ += (event ? time_delta_until_event : time_delta_real) * abs(power_real_w); + capacity_wh_ = initial_capacity_wh_ * usable_capacity_ * (1 - (energy_exchanged_j_ / energy_budget_j_)) + + initial_capacity_wh_ * (1 - usable_capacity_); + capacity_wh_ = std::max(capacity_wh_, 0.0); + if (eval_cost_) { + double usage_cost_per_wh = + (investment_cost_per_wh_ / (depth_of_discharge_ * cycles_ * 2) + variable_maintenance_cost_per_wh_); + double usage_cost = + usage_cost_per_wh * abs(power_real_w) * (event ? time_delta_until_event : time_delta_real) / 3600; + double static_maintenance_cost = + static_maintenance_cost_per_wh_times_h_ * initial_capacity_wh_ * time_delta_real / 3600; + cumulative_cost_ += usage_cost + static_maintenance_cost; + } + if (event) { + power_w_ = 0; + next_event_ = -1; + } + last_updated_ = now; + }); +} + +Battery::Battery(simgrid::s4u::Host* host) : host_(host) +{ + init_battery_params(); +} + +Battery::~Battery() = default; +} // namespace simgrid::plugin + +using simgrid::plugin::Battery; + +/* **************************** events callback *************************** */ + +static void on_creation(simgrid::s4u::Host& host) +{ + if (dynamic_cast(&host)) // Ignore virtual machines + return; + host.extension_set(new Battery(&host)); +} + +/* **************************** Public interface *************************** */ + +static void ensure_plugin_inited() +{ + if (not Battery::EXTENSION_ID.valid()) + throw simgrid::xbt::InitializationError("The Battery plugin is not active. Please call sg_battery_plugin_init() " + "before calling any function related to that plugin."); +} + +/** @ingroup plugin_battery + * @brief Enable battery plugin. + */ +void sg_battery_plugin_init() +{ + if (Battery::EXTENSION_ID.valid()) + return; + Battery::EXTENSION_ID = simgrid::s4u::Host::extension_create(); + simgrid::s4u::Host::on_creation_cb(&on_creation); +} + +/** @ingroup plugin_battery + * @param state The state to set. + * @brief Set the state of the battery. + * A battery set to inactive (false) will neither update its state of charge nor its state of health. + */ +void sg_battery_set_state(const_sg_host_t host, bool state) +{ + ensure_plugin_inited(); + host->extension()->set_state(state); +} + +/** @ingroup plugin_battery + * @param power The power to set in W. + * @brief Set the power of the battery. + * @note A negative value makes the battery act as a load and charge. + * A positive value makes the battery act as a generator and discharge. + */ +void sg_battery_set_power(const_sg_host_t host, double power) +{ + ensure_plugin_inited(); + host->extension()->set_power(power); +} + +/** @ingroup plugin_battery + * @brief Return true if the battery is active. + */ +bool sg_battery_is_active(const_sg_host_t host) +{ + ensure_plugin_inited(); + return host->extension()->is_active(); +} + +/** @ingroup plugin_battery + * @brief Return the power of the battery in W. + * @note A negative value indicates that the battery act as a load and charge. + * A positive value indicates that the battery act as a generator and discharge. + */ +double sg_battery_get_power(const_sg_host_t host) +{ + ensure_plugin_inited(); + return host->extension()->get_power(); +} + +/** @ingroup plugin_battery + * @brief Return the state of charge of the battery. + */ +double sg_battery_get_state_of_charge(const_sg_host_t host) +{ + ensure_plugin_inited(); + return host->extension()->get_state_of_charge(); +} + +/** @ingroup plugin_battery + * @brief Return the minimal state of charge of the battery. + */ +double sg_battery_get_state_of_charge_min(const_sg_host_t host) +{ + ensure_plugin_inited(); + return host->extension()->get_state_of_charge_min(); +} + +/** @ingroup plugin_battery + * @brief Return the maximal state of charge of the battery. + */ +double sg_battery_get_state_of_charge_max(const_sg_host_t host) +{ + ensure_plugin_inited(); + return host->extension()->get_state_of_charge_max(); +} + +/** @ingroup plugin_battery + * @brief Return the state of health of the battery. + */ +double sg_battery_get_state_of_health(const_sg_host_t host) +{ + ensure_plugin_inited(); + return host->extension()->get_state_of_health(); +} + +/** @ingroup plugin_battery + * @brief Return the capacity of the battery in Wh. + * @note capacity is reduced by the state of health of the battery. + */ +double sg_battery_get_capacity(const_sg_host_t host) +{ + ensure_plugin_inited(); + return host->extension()->get_capacity(); +} + +/** @ingroup plugin_battery + * @brief Return the cumulative cost of the battery. + */ +double sg_battery_get_cumulative_cost(const_sg_host_t host) +{ + ensure_plugin_inited(); + return host->extension()->get_cumulative_cost(); +} + +/** @ingroup plugin_battery + * @brief Return the date of the next event, i.e., when the battery will be empty or full. + * @note If power is null then return -1. + */ +double sg_battery_get_next_event_date(const_sg_host_t host) +{ + ensure_plugin_inited(); + return host->extension()->get_next_event_date(); +} diff --git a/tools/cmake/DefinePackages.cmake b/tools/cmake/DefinePackages.cmake index 6cc2d4c244..7f458ab88f 100644 --- a/tools/cmake/DefinePackages.cmake +++ b/tools/cmake/DefinePackages.cmake @@ -453,6 +453,7 @@ set(PLUGINS_SRC src/plugins/vm/VmLiveMigration.cpp src/plugins/vm/VmLiveMigration.hpp src/plugins/vm/dirty_page_tracking.cpp + src/plugins/battery.cpp ) @@ -631,6 +632,7 @@ set(headers_to_install include/simgrid/chrono.hpp include/simgrid/plugins/dvfs.h include/simgrid/plugins/energy.h + include/simgrid/plugins/battery.hpp include/simgrid/plugins/file_system.h include/simgrid/plugins/live_migration.h include/simgrid/plugins/load.h @@ -874,17 +876,10 @@ set(DOC_SOURCES docs/source/tuto_s4u/master-workers-lab4.cpp docs/source/Tutorial_DAG.rst - docs/source/tuto_dag/dag_lab1.cpp - docs/source/tuto_dag/dag_lab2-1.cpp - docs/source/tuto_dag/dag_lab2-2.cpp - docs/source/tuto_dag/dag_lab2-3.cpp - docs/source/tuto_dag/img/dag1.svg - docs/source/tuto_dag/img/dag2.svg - docs/source/tuto_dag/img/dag.svg - docs/source/tuto_dag/simple_dax.xml - docs/source/tuto_dag/simple_dot.dot - docs/source/tuto_dag/simple_json.json - docs/source/tuto_dag/small_platform.xml + docs/source/img/battery_degradation.svg + docs/source/img/dag1.svg + docs/source/img/dag2.svg + docs/source/img/dag.svg docs/source/Tutorial_MPI_Applications.rst docs/source/tuto_smpi/3hosts.png @@ -1076,6 +1071,7 @@ set(CMAKE_SOURCE_FILES ) set(PLATFORMS_EXAMPLES + examples/platforms/battery_platform.xml examples/platforms/bypassRoute.xml examples/platforms/bypassZoneRoute.xml examples/platforms/cloud.xml