Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Pointer-to-const again.
[simgrid.git] / src / bindings / python / simgrid_python.cpp
index 0bf8598..cb20aaa 100644 (file)
@@ -22,6 +22,7 @@
 #pragma GCC diagnostic pop
 #endif
 
+#include "simgrid/kernel/ProfileBuilder.hpp"
 #include "simgrid/kernel/routing/NetPoint.hpp"
 #include "src/kernel/context/Context.hpp"
 #include <simgrid/Exception.hpp>
@@ -46,6 +47,7 @@ using simgrid::s4u::Actor;
 using simgrid::s4u::ActorPtr;
 using simgrid::s4u::Engine;
 using simgrid::s4u::Host;
+using simgrid::s4u::Link;
 using simgrid::s4u::Mailbox;
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(python, "python");
@@ -85,6 +87,9 @@ PYBIND11_MODULE(simgrid, m)
   // Internal exception used to kill actors and sweep the RAII chimney (free objects living on the stack)
   static py::object pyForcefulKillEx(py::register_exception<simgrid::ForcefulKillException>(m, "ActorKilled"));
 
+  py::register_exception<simgrid::NetworkFailureException>(m, "NetworkFailureException");
+  py::register_exception<simgrid::TimeoutException>(m, "TimeoutException");
+
   /* this_actor namespace */
   m.def_submodule("this_actor", "Bindings of the s4u::this_actor namespace. See the C++ documentation for details.")
       .def(
@@ -114,7 +119,8 @@ PYBIND11_MODULE(simgrid, m)
       .def("exit", &simgrid::s4u::this_actor::exit, py::call_guard<py::gil_scoped_release>(), "kill the current actor")
       .def(
           "on_exit",
-          [](py::object fun) {
+          [](py::object cb) {
+            py::function fun = py::reinterpret_borrow<py::function>(cb);
             fun.inc_ref(); // FIXME: why is this needed for tests like actor-kill and actor-lifetime?
             simgrid::s4u::this_actor::on_exit([fun](bool /*failed*/) {
               try {
@@ -145,6 +151,15 @@ PYBIND11_MODULE(simgrid, m)
       .def_static(
           "instance", []() { return Engine::get_instance(); }, "Retrieve the simulation engine")
       .def("get_all_hosts", &Engine::get_all_hosts, "Returns the list of all hosts found in the platform")
+      .def("get_all_links", &Engine::get_all_links, "Returns the list of all links found in the platform")
+
+      .def("get_netzone_root", &Engine::get_netzone_root, "Retrieve the root netzone, containing all others.")
+      .def("get_all_netpoints", &Engine::get_all_netpoints)
+      .def("get_netzone_root", &Engine::get_netzone_root)
+      .def("netpoint_by_name", &Engine::netpoint_by_name_or_null)
+      .def("netzone_by_name", &Engine::netzone_by_name_or_null)
+      .def("set_netzone_root", &Engine::set_netzone_root)
+
       .def("load_platform", &Engine::load_platform, "Load a platform file describing the environment")
       .def("load_deployment", &Engine::load_deployment, "Load a deployment file and launch the actors that it contains")
       .def("run", &Engine::run, py::call_guard<py::gil_scoped_release>(), "Run the simulation until its end")
@@ -251,6 +266,51 @@ PYBIND11_MODULE(simgrid, m)
   py::class_<simgrid::s4u::Host, std::unique_ptr<Host, py::nodelete>> host(
       m, "Host", "Simulated host. See the C++ documentation for details.");
   host.def("by_name", &Host::by_name, "Retrieves a host from its name, or die")
+      .def(
+          "route_to",
+          [](const simgrid::s4u::Host* h, const simgrid::s4u::Host* to) {
+            auto* list = new std::vector<Link*>();
+            double bw  = 0;
+            h->route_to(to, *list, &bw);
+            return make_tuple(list, bw);
+          },
+          "Retrieves the list of links and the bandwidth between two hosts.")
+      .def(
+          "set_speed_profile",
+          [](Host* h, const std::string& profile, double period) {
+            h->set_speed_profile(simgrid::kernel::profile::ProfileBuilder::from_string("", profile, period));
+          },
+          py::call_guard<py::gil_scoped_release>(),
+          "Specify a profile modeling the external load according to an exhaustive list. "
+          "Each line of the profile describes timed events as ``date ratio``. "
+          "For example, the following content describes an host which computational speed is initially 1 (i.e, 100%) "
+          "and then halved after 42 seconds:\n\n"
+          ".. code-block:: python\n\n"
+          "   \"\"\"\n"
+          "   0 1.0\n"
+          "   42 0.5\n"
+          "   \"\"\"\n\n"
+          ".. warning:: Don't get fooled: bandwidth and latency profiles of links contain absolute values,"
+          " while speed profiles of hosts contain ratios.\n\n"
+          "The second function parameter is the periodicity: the time to wait after the last event to start again over "
+          "the list. Set it to -1 to not loop over.")
+      .def(
+          "set_state_profile",
+          [](Host* h, const std::string& profile, double period) {
+            h->set_state_profile(simgrid::kernel::profile::ProfileBuilder::from_string("", profile, period));
+          },
+          py::call_guard<py::gil_scoped_release>(),
+          "Specify a profile modeling the churn. "
+          "Each line of the profile describes timed events as ``date boolean``, where the boolean (0 or 1) tells "
+          "whether the host is on. "
+          "For example, the following content describes a link which turns off at t=1 and back on at t=2:\n\n"
+          ".. code-block:: python\n\n"
+          "   \"\"\"\n"
+          "   1.0 0\n"
+          "   2.0 1\n"
+          "   \"\"\"\n\n"
+          "The second function parameter is the periodicity: the time to wait after the last event to start again over "
+          "the list. Set it to -1 to not loop over.")
       .def("get_pstate_count", &Host::get_pstate_count, "Retrieve the count of defined pstate levels")
       .def("get_pstate_speed", &Host::get_pstate_speed, "Retrieve the maximal speed at the given pstate")
       .def("get_netpoint", &Host::get_netpoint, "Retrieve the netpoint associated to this host")
@@ -282,13 +342,32 @@ PYBIND11_MODULE(simgrid, m)
             return std::string(self->get_name().c_str()); // Convert from xbt::string because of MC
           },
           "The name of this host")
-      .def_property_readonly(
-          "load", &Host::get_load,
-          "Returns the current computation load (in flops per second). This is the currently achieved speed.")
+      .def_property_readonly("load", &Host::get_load,
+                             "Returns the current computation load (in flops per second), NOT taking the external load "
+                             "into account. This is the currently achieved speed.")
       .def_property_readonly(
           "speed", &Host::get_speed,
-          "The peak computing speed in flops/s at the current pstate, taking the external load into account. "
-          "This is the max potential speed.");
+          "The peak computing speed in flops/s at the current pstate, NOT taking the external load into account. "
+          "This is the max potential speed.")
+      .def_property_readonly(
+          "available_speed", &Host::get_available_speed,
+          "Get the available speed ratio, between 0 and 1.\n"
+          "This accounts for external load (see :py:func:`set_speed_profile() <simgrid.Host.set_speed_profile>`).")
+      .def(
+          "on_creation_cb",
+          [](py::object cb) {
+            Host::on_creation_cb([cb](Host& h) {
+              py::function fun = py::reinterpret_borrow<py::function>(cb);
+              try {
+                py::gil_scoped_acquire py_context; // need a new context for callback
+                fun(&h);
+              } catch (const py::error_already_set& e) {
+                xbt_die("Error while executing the on_creation lambda : %s", e.what());
+              }
+            });
+          },
+          py::call_guard<py::gil_scoped_release>(), "");
+
   py::enum_<simgrid::s4u::Host::SharingPolicy>(host, "SharingPolicy")
       .value("NONLINEAR", simgrid::s4u::Host::SharingPolicy::NONLINEAR)
       .value("LINEAR", simgrid::s4u::Host::SharingPolicy::LINEAR)
@@ -326,38 +405,104 @@ PYBIND11_MODULE(simgrid, m)
       netpoint(m, "NetPoint", "NetPoint object");
 
   /* Class Link */
-  py::class_<simgrid::s4u::Link, std::unique_ptr<simgrid::s4u::Link, py::nodelete>> link(
-      m, "Link", "Network link. See the C++ documentation for details.");
-  link.def("set_latency", py::overload_cast<const std::string&>(&simgrid::s4u::Link::set_latency),
-           py::call_guard<py::gil_scoped_release>(), "Set the latency")
-      .def("set_latency", py::overload_cast<double>(&simgrid::s4u::Link::set_latency),
-           py::call_guard<py::gil_scoped_release>(), "Set the latency")
-      .def("set_sharing_policy", &simgrid::s4u::Link::set_sharing_policy, py::call_guard<py::gil_scoped_release>(),
+  py::class_<Link, std::unique_ptr<Link, py::nodelete>> link(m, "Link",
+                                                             "Network link. See the C++ documentation for details.");
+  link.def("set_latency", py::overload_cast<const std::string&>(&Link::set_latency),
+           py::call_guard<py::gil_scoped_release>(),
+           "Set the latency as a string. Accepts values with units, such as â€˜1s’ or â€˜7ms’.\nFull list of accepted "
+           "units: w (week), d (day), h, s, ms, us, ns, ps.")
+      .def("set_latency", py::overload_cast<double>(&Link::set_latency), py::call_guard<py::gil_scoped_release>(),
+           "Set the latency as a float (in seconds).")
+      .def("set_bandwidth", &Link::set_bandwidth, py::call_guard<py::gil_scoped_release>(),
+           "Set the bandwidth (in byte per second).")
+      .def(
+          "set_bandwidth_profile",
+          [](Link* l, const std::string& profile, double period) {
+            l->set_bandwidth_profile(simgrid::kernel::profile::ProfileBuilder::from_string("", profile, period));
+          },
+          py::call_guard<py::gil_scoped_release>(),
+          "Specify a profile modeling the external load according to an exhaustive list. "
+          "Each line of the profile describes timed events as ``date bandwidth`` (in bytes per second). "
+          "For example, the following content describes a link which bandwidth changes to 40 Mb/s at t=4, and to 6 "
+          "Mb/s at t=8:\n\n"
+          ".. code-block:: python\n\n"
+          "   \"\"\"\n"
+          "   4.0 40000000\n"
+          "   8.0 60000000\n"
+          "   \"\"\"\n\n"
+          ".. warning:: Don't get fooled: bandwidth and latency profiles of links contain absolute values,"
+          " while speed profiles of hosts contain ratios.\n\n"
+          "The second function parameter is the periodicity: the time to wait after the last event to start again over "
+          "the list. Set it to -1 to not loop over.")
+      .def(
+          "set_latency_profile",
+          [](Link* l, const std::string& profile, double period) {
+            l->set_latency_profile(simgrid::kernel::profile::ProfileBuilder::from_string("", profile, period));
+          },
+          py::call_guard<py::gil_scoped_release>(),
+          "Specify a profile modeling the external load according to an exhaustive list. "
+          "Each line of the profile describes timed events as ``date latency`` (in seconds). "
+          "For example, the following content describes a link which latency changes to 1ms (0.001s) at t=1, and to 2s "
+          "at t=2:\n\n"
+          ".. code-block:: python\n\n"
+          "   \"\"\"\n"
+          "   1.0 0.001\n"
+          "   2.0 2\n"
+          "   \"\"\"\n\n"
+          ".. warning:: Don't get fooled: bandwidth and latency profiles of links contain absolute values,"
+          " while speed profiles of hosts contain ratios.\n\n"
+          "The second function parameter is the periodicity: the time to wait after the last event to start again over "
+          "the list. Set it to -1 to not loop over.")
+      .def(
+          "set_state_profile",
+          [](Link* l, const std::string& profile, double period) {
+            l->set_state_profile(simgrid::kernel::profile::ProfileBuilder::from_string("", profile, period));
+          },
+          "Specify a profile modeling the churn. "
+          "Each line of the profile describes timed events as ``date boolean``, where the boolean (0 or 1) tells "
+          "whether the link is on. "
+          "For example, the following content describes a link which turns off at t=1 and back on at t=2:\n\n"
+          ".. code-block:: python\n\n"
+          "   \"\"\"\n"
+          "   1.0 0\n"
+          "   2.0 1\n"
+          "   \"\"\"\n\n"
+          "The second function parameter is the periodicity: the time to wait after the last event to start again over "
+          "the list. Set it to -1 to not loop over.")
+
+      .def("turn_on", &Link::turn_on, py::call_guard<py::gil_scoped_release>(), "Turns the link on.")
+      .def("turn_off", &Link::turn_off, py::call_guard<py::gil_scoped_release>(), "Turns the link off.")
+      .def("is_on", &Link::is_on, "Check whether the link is on.")
+
+      .def("set_sharing_policy", &Link::set_sharing_policy, py::call_guard<py::gil_scoped_release>(),
            "Set sharing policy for this link")
-      .def("set_concurrency_limit", &simgrid::s4u::Link::set_concurrency_limit,
-           py::call_guard<py::gil_scoped_release>(), "Set concurrency limit for this link")
-      .def("set_host_wifi_rate", &simgrid::s4u::Link::set_host_wifi_rate, py::call_guard<py::gil_scoped_release>(),
+      .def("set_concurrency_limit", &Link::set_concurrency_limit, py::call_guard<py::gil_scoped_release>(),
+           "Set concurrency limit for this link")
+      .def("set_host_wifi_rate", &Link::set_host_wifi_rate, py::call_guard<py::gil_scoped_release>(),
            "Set level of communication speed of given host on this Wi-Fi link")
-      .def("by_name", &simgrid::s4u::Link::by_name, "Retrieves a Link from its name, or dies")
-      .def("seal", &simgrid::s4u::Link::seal, py::call_guard<py::gil_scoped_release>(), "Seal this link")
+      .def("by_name", &Link::by_name, "Retrieves a Link from its name, or dies")
+      .def("seal", &Link::seal, py::call_guard<py::gil_scoped_release>(), "Seal this link")
       .def_property_readonly(
           "name",
-          [](const simgrid::s4u::Link* self) {
+          [](const Link* self) {
             return std::string(self->get_name().c_str()); // Convert from xbt::string because of MC
           },
-          "The name of this link");
-  py::enum_<simgrid::s4u::Link::SharingPolicy>(link, "SharingPolicy")
-      .value("NONLINEAR", simgrid::s4u::Link::SharingPolicy::NONLINEAR)
-      .value("WIFI", simgrid::s4u::Link::SharingPolicy::WIFI)
-      .value("SPLITDUPLEX", simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX)
-      .value("SHARED", simgrid::s4u::Link::SharingPolicy::SHARED)
-      .value("FATPIPE", simgrid::s4u::Link::SharingPolicy::FATPIPE)
+          "The name of this link")
+      .def_property_readonly("bandwidth", &Link::get_bandwidth, "The bandwidth (in bytes per second)")
+      .def_property_readonly("latency", &Link::get_latency, "The latency (in seconds)");
+
+  py::enum_<Link::SharingPolicy>(link, "SharingPolicy")
+      .value("NONLINEAR", Link::SharingPolicy::NONLINEAR)
+      .value("WIFI", Link::SharingPolicy::WIFI)
+      .value("SPLITDUPLEX", Link::SharingPolicy::SPLITDUPLEX)
+      .value("SHARED", Link::SharingPolicy::SHARED)
+      .value("FATPIPE", Link::SharingPolicy::FATPIPE)
       .export_values();
 
   /* Class LinkInRoute */
   py::class_<simgrid::s4u::LinkInRoute> linkinroute(m, "LinkInRoute", "Abstraction to add link in routes");
-  linkinroute.def(py::init<const simgrid::s4u::Link*>());
-  linkinroute.def(py::init<const simgrid::s4u::Link*, simgrid::s4u::LinkInRoute::Direction>());
+  linkinroute.def(py::init<const Link*>());
+  linkinroute.def(py::init<const Link*, simgrid::s4u::LinkInRoute::Direction>());
   py::enum_<simgrid::s4u::LinkInRoute::Direction>(linkinroute, "Direction")
       .value("UP", simgrid::s4u::LinkInRoute::Direction::UP)
       .value("DOWN", simgrid::s4u::LinkInRoute::Direction::DOWN)
@@ -365,9 +510,8 @@ PYBIND11_MODULE(simgrid, m)
       .export_values();
 
   /* Class Split-Duplex Link */
-  py::class_<simgrid::s4u::SplitDuplexLink, simgrid::s4u::Link,
-             std::unique_ptr<simgrid::s4u::SplitDuplexLink, py::nodelete>>(m, "SplitDuplexLink",
-                                                                           "Network split-duplex link")
+  py::class_<simgrid::s4u::SplitDuplexLink, Link, std::unique_ptr<simgrid::s4u::SplitDuplexLink, py::nodelete>>(
+      m, "SplitDuplexLink", "Network split-duplex link")
       .def("get_link_up", &simgrid::s4u::SplitDuplexLink::get_link_up, "Get link direction up")
       .def("get_link_down", &simgrid::s4u::SplitDuplexLink::get_link_down, "Get link direction down");
 
@@ -384,6 +528,13 @@ PYBIND11_MODULE(simgrid, m)
             return std::string(self->get_name().c_str()); // Convert from xbt::string because of MC
           },
           "The name of that mailbox")
+      .def(
+          "put",
+          [](Mailbox* self, py::object data, int size, double timeout) {
+            data.inc_ref();
+            self->put(data.ptr(), size, timeout);
+          },
+          py::call_guard<py::gil_scoped_release>(), "Blocking data transmission with a timeout")
       .def(
           "put",
           [](Mailbox* self, py::object data, int size) {
@@ -502,7 +653,8 @@ PYBIND11_MODULE(simgrid, m)
               }
             });
           },
-          py::call_guard<py::gil_scoped_release>(), "Create an actor from a function or an object.")
+          py::call_guard<py::gil_scoped_release>(),
+          "Create an actor from a function or an object. See the :ref:`example <s4u_ex_actors_create>`.")
       .def_property(
           "host", &Actor::get_host,
           [](Actor* a, Host* h) {
@@ -515,6 +667,8 @@ PYBIND11_MODULE(simgrid, m)
       .def_property_readonly("ppid", &Actor::get_ppid,
                              "The PID (unique identifier) of the actor that created this one.")
       .def("by_pid", &Actor::by_pid, "Retrieve an actor by its PID")
+      .def("set_auto_restart", &Actor::set_auto_restart, py::call_guard<py::gil_scoped_release>(),
+           "Specify whether the actor shall restart when its host reboots.")
       .def("daemonize", &Actor::daemonize, py::call_guard<py::gil_scoped_release>(),
            "This actor will be automatically terminated when the last non-daemon actor finishes (more info in the C++ "
            "documentation).")