Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Extend the python bindings and cosmetics
[simgrid.git] / src / bindings / python / simgrid_python.cpp
index 7b5efdb..c423bf7 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2018-2021. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2018-2022. 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. */
@@ -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>
@@ -86,7 +87,9 @@ PYBIND11_MODULE(simgrid, m)
   static py::object pyForcefulKillEx(py::register_exception<simgrid::ForcefulKillException>(m, "ActorKilled"));
 
   /* this_actor namespace */
-  m.def_submodule("this_actor", "Bindings of the s4u::this_actor namespace.")
+  m.def_submodule("this_actor", "Bindings of the s4u::this_actor namespace. See the C++ documentation for details.")
+      .def(
+          "debug", [](const char* s) { XBT_DEBUG("%s", s); }, "Display a logging message of 'debug' priority.")
       .def(
           "info", [](const char* s) { XBT_INFO("%s", s); }, "Display a logging message of 'info' priority.")
       .def(
@@ -123,23 +126,41 @@ PYBIND11_MODULE(simgrid, m)
               }
             });
           },
-          py::call_guard<py::gil_scoped_release>(), "");
+          py::call_guard<py::gil_scoped_release>(), "")
+      .def("get_pid", &simgrid::s4u::this_actor::get_pid, "Retrieves PID of the current actor")
+      .def("get_ppid", &simgrid::s4u::this_actor::get_ppid,
+           "Retrieves PPID of the current actor (i.e., the PID of its parent).");
 
   /* Class Engine */
   py::class_<Engine>(m, "Engine", "Simulation Engine")
       .def(py::init([](std::vector<std::string> args) {
-        auto argc           = static_cast<int>(args.size());
-        std::vector<char*> argv(args.size() + 1); // argv[argc] is nullptr
-        std::transform(begin(args), end(args), begin(argv), [](std::string& s) { return &s.front(); });
-        // Currently this can be dangling, we should wrap this somehow.
-        return new simgrid::s4u::Engine(&argc, argv.data());
-      }))
+             auto argc = static_cast<int>(args.size());
+             std::vector<char*> argv(args.size() + 1); // argv[argc] is nullptr
+             std::transform(begin(args), end(args), begin(argv), [](std::string& s) { return &s.front(); });
+             // Currently this can be dangling, we should wrap this somehow.
+             return new simgrid::s4u::Engine(&argc, argv.data());
+           }),
+           "The constructor should take the parameters from the command line, as is ")
       .def_static("get_clock", &Engine::get_clock,
                   "The simulation time, ie the amount of simulated seconds since the simulation start.")
+      .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")
+      .def("run", &Engine::run, py::call_guard<py::gil_scoped_release>(), "Run the simulation until its end")
+      .def("run_until", py::overload_cast<double>(&Engine::run_until, py::const_),
+           py::call_guard<py::gil_scoped_release>(), "Run the simulation until the given date",
+           py::arg("max_date") = -1)
       .def(
           "register_actor",
           [](Engine* e, const std::string& name, py::object fun_or_class) {
@@ -167,8 +188,8 @@ PYBIND11_MODULE(simgrid, m)
           "Registers the main function of an actor");
 
   /* Class Netzone */
-  py::class_<simgrid::s4u::NetZone, std::unique_ptr<simgrid::s4u::NetZone, py::nodelete>> netzone(m, "NetZone",
-                                                                                                  "Networking Zones");
+  py::class_<simgrid::s4u::NetZone, std::unique_ptr<simgrid::s4u::NetZone, py::nodelete>> netzone(
+      m, "NetZone", "Networking Zones. See the C++ documentation for details.");
   netzone.def_static("create_full_zone", &simgrid::s4u::create_full_zone, "Creates a zone of type FullZone")
       .def_static("create_torus_zone", &simgrid::s4u::create_torus_zone, "Creates a cluster of type Torus")
       .def_static("create_fatTree_zone", &simgrid::s4u::create_fatTree_zone, "Creates a cluster of type Fat-Tree")
@@ -237,8 +258,22 @@ PYBIND11_MODULE(simgrid, m)
                     const std::pair<unsigned int, unsigned int>&, unsigned int>());
 
   /* Class Host */
-  py::class_<simgrid::s4u::Host, std::unique_ptr<Host, py::nodelete>> host(m, "Host", "Simulated host");
+  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",
+          [](simgrid::s4u::Host* h, simgrid::s4u::Host* to) {
+            auto* list = new std::vector<simgrid::s4u::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));
+           })
       .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")
@@ -270,22 +305,29 @@ 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>`).");
   py::enum_<simgrid::s4u::Host::SharingPolicy>(host, "SharingPolicy")
       .value("NONLINEAR", simgrid::s4u::Host::SharingPolicy::NONLINEAR)
       .value("LINEAR", simgrid::s4u::Host::SharingPolicy::LINEAR)
       .export_values();
 
   /* Class Disk */
-  py::class_<simgrid::s4u::Disk, std::unique_ptr<simgrid::s4u::Disk, py::nodelete>> disk(m, "Disk", "Simulated disk");
-  disk.def("read", &simgrid::s4u::Disk::read, py::call_guard<py::gil_scoped_release>(), "Read data from disk")
-      .def("write", &simgrid::s4u::Disk::write, py::call_guard<py::gil_scoped_release>(), "Write data in disk")
+  py::class_<simgrid::s4u::Disk, std::unique_ptr<simgrid::s4u::Disk, py::nodelete>> disk(
+      m, "Disk", "Simulated disk. See the C++ documentation for details.");
+  disk.def("read", py::overload_cast<sg_size_t, double>(&simgrid::s4u::Disk::read, py::const_),
+           py::call_guard<py::gil_scoped_release>(), "Read data from disk", py::arg("size"), py::arg("priority") = 1)
+      .def("write", py::overload_cast<sg_size_t, double>(&simgrid::s4u::Disk::write, py::const_),
+           py::call_guard<py::gil_scoped_release>(), "Write data in disk", py::arg("size"), py::arg("priority") = 1)
       .def("read_async", &simgrid::s4u::Disk::read_async, py::call_guard<py::gil_scoped_release>(),
            "Non-blocking read data from disk")
       .def("write_async", &simgrid::s4u::Disk::write_async, py::call_guard<py::gil_scoped_release>(),
@@ -311,24 +353,38 @@ 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");
+  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")
+           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>(&simgrid::s4u::Link::set_latency),
-           py::call_guard<py::gil_scoped_release>(), "Set the latency")
+           py::call_guard<py::gil_scoped_release>(), "Set the latency as a float (in seconds).")
+      .def("set_bandwidth", &simgrid::s4u::Link::set_bandwidth, py::call_guard<py::gil_scoped_release>(),
+           "Set the bandwidth (in byte per second).")
+
+      .def("turn_on", &simgrid::s4u::Link::turn_on, py::call_guard<py::gil_scoped_release>(), "Turns the link on.")
+      .def("turn_off", &simgrid::s4u::Link::turn_off, py::call_guard<py::gil_scoped_release>(), "Turns the link off.")
+      .def("is_on", &simgrid::s4u::Link::is_on, "Check whether the link is on.")
+
       .def("set_sharing_policy", &simgrid::s4u::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>(),
            "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_property_readonly(
           "name",
           [](const simgrid::s4u::Link* self) {
             return std::string(self->get_name().c_str()); // Convert from xbt::string because of MC
           },
-          "The name of this link");
+          "The name of this link")
+      .def_property_readonly("bandwidth", &simgrid::s4u::Link::get_bandwidth, "The bandwidth (in bytes per second)")
+      .def_property_readonly("latency", &simgrid::s4u::Link::get_latency, "The latency (in seconds)");
+
   py::enum_<simgrid::s4u::Link::SharingPolicy>(link, "SharingPolicy")
       .value("NONLINEAR", simgrid::s4u::Link::SharingPolicy::NONLINEAR)
       .value("WIFI", simgrid::s4u::Link::SharingPolicy::WIFI)
@@ -355,7 +411,8 @@ PYBIND11_MODULE(simgrid, m)
       .def("get_link_down", &simgrid::s4u::SplitDuplexLink::get_link_down, "Get link direction down");
 
   /* Class Mailbox */
-  py::class_<simgrid::s4u::Mailbox, std::unique_ptr<Mailbox, py::nodelete>>(m, "Mailbox", "Mailbox")
+  py::class_<simgrid::s4u::Mailbox, std::unique_ptr<Mailbox, py::nodelete>>(
+      m, "Mailbox", "Mailbox. See the C++ documentation for details.")
       .def(
           "__str__", [](const Mailbox* self) { return std::string("Mailbox(") + self->get_cname() + ")"; },
           "Textual representation of the Mailbox`")
@@ -409,7 +466,8 @@ PYBIND11_MODULE(simgrid, m)
           "Get python object after async communication in receiver side");
 
   /* Class Comm */
-  py::class_<simgrid::s4u::Comm, simgrid::s4u::CommPtr>(m, "Comm", "Communication")
+  py::class_<simgrid::s4u::Comm, simgrid::s4u::CommPtr>(m, "Comm",
+                                                        "Communication. See the C++ documentation for details.")
       .def("test", &simgrid::s4u::Comm::test, py::call_guard<py::gil_scoped_release>(),
            "Test whether the communication is terminated.")
       .def("wait", &simgrid::s4u::Comm::wait, py::call_guard<py::gil_scoped_release>(),
@@ -424,7 +482,7 @@ PYBIND11_MODULE(simgrid, m)
           "Block until the completion of any communication in the list and return the index of the terminated one.");
 
   /* Class Io */
-  py::class_<simgrid::s4u::Io, simgrid::s4u::IoPtr>(m, "Io", "I/O activities")
+  py::class_<simgrid::s4u::Io, simgrid::s4u::IoPtr>(m, "Io", "I/O activities. See the C++ documentation for details.")
       .def("test", &simgrid::s4u::Io::test, py::call_guard<py::gil_scoped_release>(),
            "Test whether the I/O is terminated.")
       .def("wait", &simgrid::s4u::Io::wait, py::call_guard<py::gil_scoped_release>(),
@@ -436,7 +494,7 @@ PYBIND11_MODULE(simgrid, m)
                   "Block until the completion of any I/O in the list and return the index of the terminated one.");
 
   /* Class Exec */
-  py::class_<simgrid::s4u::Exec, simgrid::s4u::ExecPtr>(m, "Exec", "Execution")
+  py::class_<simgrid::s4u::Exec, simgrid::s4u::ExecPtr>(m, "Exec", "Execution. See the C++ documentation for details.")
       .def_property_readonly(
           "remaining",
           [](simgrid::s4u::ExecPtr self) {
@@ -464,7 +522,7 @@ PYBIND11_MODULE(simgrid, m)
   /* Class Actor */
   py::class_<simgrid::s4u::Actor, ActorPtr>(m, "Actor",
                                             "An actor is an independent stream of execution in your distributed "
-                                            "application")
+                                            "application. See the C++ documentation for details.")
       .def(
           "create",
           [](py::str name, Host* h, py::object fun, py::args args) {
@@ -483,7 +541,7 @@ 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) {