Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[python] cleanup Engine getters/setters
[simgrid.git] / src / bindings / python / simgrid_python.cpp
1 /* Copyright (c) 2018-2022. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifdef _WIN32
7 #warning Try to work around https://bugs.python.org/issue11566
8 #define _hypot hypot
9 #endif
10
11 #if defined(__GNUG__)
12 #pragma GCC diagnostic push
13 #pragma GCC diagnostic ignored "-Wunused-value"
14 #endif
15
16 #include <pybind11/pybind11.h> // Must come before our own stuff
17
18 #include <pybind11/functional.h>
19 #include <pybind11/stl.h>
20
21 #if defined(__GNUG__)
22 #pragma GCC diagnostic pop
23 #endif
24
25 #include "simgrid/kernel/ProfileBuilder.hpp"
26 #include "simgrid/kernel/routing/NetPoint.hpp"
27 #include "src/kernel/context/Context.hpp"
28 #include <simgrid/Exception.hpp>
29 #include <simgrid/s4u/Actor.hpp>
30 #include <simgrid/s4u/Comm.hpp>
31 #include <simgrid/s4u/Disk.hpp>
32 #include <simgrid/s4u/Engine.hpp>
33 #include <simgrid/s4u/Exec.hpp>
34 #include <simgrid/s4u/Host.hpp>
35 #include <simgrid/s4u/Link.hpp>
36 #include <simgrid/s4u/Mailbox.hpp>
37 #include <simgrid/s4u/NetZone.hpp>
38 #include <simgrid/version.h>
39
40 #include <algorithm>
41 #include <memory>
42 #include <string>
43 #include <vector>
44
45 namespace py = pybind11;
46 using simgrid::s4u::Actor;
47 using simgrid::s4u::ActorPtr;
48 using simgrid::s4u::Engine;
49 using simgrid::s4u::Host;
50 using simgrid::s4u::Link;
51 using simgrid::s4u::Mailbox;
52
53 XBT_LOG_NEW_DEFAULT_CATEGORY(python, "python");
54
55 namespace {
56
57 std::string get_simgrid_version()
58 {
59   int major;
60   int minor;
61   int patch;
62   sg_version_get(&major, &minor, &patch);
63   return simgrid::xbt::string_printf("%i.%i.%i", major, minor, patch);
64 }
65
66 /** @brief Wrap for mailbox::get_async */
67 class PyGetAsync {
68   std::unique_ptr<PyObject*> data = std::make_unique<PyObject*>();
69
70 public:
71   PyObject** get() const { return data.get(); }
72 };
73
74 } // namespace
75
76 PYBIND11_DECLARE_HOLDER_TYPE(T, boost::intrusive_ptr<T>)
77
78 PYBIND11_MODULE(simgrid, m)
79 {
80   m.doc() = "SimGrid userspace API";
81
82   m.attr("simgrid_version") = get_simgrid_version();
83
84   // Swapped contexts are broken, starting from pybind11 v2.8.0.  Use thread contexts by default.
85   simgrid::s4u::Engine::set_config("contexts/factory:thread");
86
87   // Internal exception used to kill actors and sweep the RAII chimney (free objects living on the stack)
88   static py::object pyForcefulKillEx(py::register_exception<simgrid::ForcefulKillException>(m, "ActorKilled"));
89
90   py::register_exception<simgrid::NetworkFailureException>(m, "NetworkFailureException");
91   py::register_exception<simgrid::TimeoutException>(m, "TimeoutException");
92
93   /* this_actor namespace */
94   m.def_submodule("this_actor", "Bindings of the s4u::this_actor namespace. See the C++ documentation for details.")
95       .def(
96           "debug", [](const char* s) { XBT_DEBUG("%s", s); }, "Display a logging message of 'debug' priority.")
97       .def(
98           "info", [](const char* s) { XBT_INFO("%s", s); }, "Display a logging message of 'info' priority.")
99       .def(
100           "error", [](const char* s) { XBT_ERROR("%s", s); }, "Display a logging message of 'error' priority.")
101       .def("execute", py::overload_cast<double, double>(&simgrid::s4u::this_actor::execute),
102            py::call_guard<py::gil_scoped_release>(),
103            "Block the current actor, computing the given amount of flops at the given priority.", py::arg("flops"),
104            py::arg("priority") = 1)
105       .def("exec_init", py::overload_cast<double>(&simgrid::s4u::this_actor::exec_init),
106            py::call_guard<py::gil_scoped_release>())
107       .def("get_host", &simgrid::s4u::this_actor::get_host, "Retrieves host on which the current actor is located")
108       .def("set_host", &simgrid::s4u::this_actor::set_host, py::call_guard<py::gil_scoped_release>(),
109            "Moves the current actor to another host.", py::arg("dest"))
110       .def("sleep_for", static_cast<void (*)(double)>(&simgrid::s4u::this_actor::sleep_for),
111            py::call_guard<py::gil_scoped_release>(), "Block the actor sleeping for that amount of seconds.",
112            py::arg("duration"))
113       .def("sleep_until", static_cast<void (*)(double)>(&simgrid::s4u::this_actor::sleep_until),
114            py::call_guard<py::gil_scoped_release>(), "Block the actor sleeping until the specified timestamp.",
115            py::arg("duration"))
116       .def("suspend", &simgrid::s4u::this_actor::suspend, py::call_guard<py::gil_scoped_release>(),
117            "Suspend the current actor, that is blocked until resume()ed by another actor.")
118       .def("yield_", &simgrid::s4u::this_actor::yield, py::call_guard<py::gil_scoped_release>(), "Yield the actor")
119       .def("exit", &simgrid::s4u::this_actor::exit, py::call_guard<py::gil_scoped_release>(), "kill the current actor")
120       .def(
121           "on_exit",
122           [](py::object cb) {
123             py::function fun = py::reinterpret_borrow<py::function>(cb);
124             fun.inc_ref(); // FIXME: why is this needed for tests like actor-kill and actor-lifetime?
125             simgrid::s4u::this_actor::on_exit([fun](bool /*failed*/) {
126               try {
127                 py::gil_scoped_acquire py_context; // need a new context for callback
128                 fun();
129               } catch (const py::error_already_set& e) {
130                 xbt_die("Error while executing the on_exit lambda: %s", e.what());
131               }
132             });
133           },
134           py::call_guard<py::gil_scoped_release>(), "")
135       .def("get_pid", &simgrid::s4u::this_actor::get_pid, "Retrieves PID of the current actor")
136       .def("get_ppid", &simgrid::s4u::this_actor::get_ppid,
137            "Retrieves PPID of the current actor (i.e., the PID of its parent).");
138
139   /* Class Engine */
140   py::class_<Engine>(m, "Engine", "Simulation Engine")
141       .def(py::init([](std::vector<std::string> args) {
142              auto argc = static_cast<int>(args.size());
143              std::vector<char*> argv(args.size() + 1); // argv[argc] is nullptr
144              std::transform(begin(args), end(args), begin(argv), [](std::string& s) { return &s.front(); });
145              // Currently this can be dangling, we should wrap this somehow.
146              return new simgrid::s4u::Engine(&argc, argv.data());
147            }),
148            "The constructor should take the parameters from the command line, as is ")
149       .def_static("get_clock",
150                   [](pybind11::object& self) // XBT_ATTRIB_DEPRECATED_v333
151                   {
152                     PyErr_WarnEx(PyExc_DeprecationWarning,
153                                  "get_clock() is deprecated and  will be dropped after v3.32, use clock() instead.", 1);
154                     return self.attr("clock");
155                   })
156       .def_static("clock", &Engine::get_clock,
157                   "The simulation time, ie the amount of simulated seconds since the simulation start.")
158       .def_static(
159           "instance", []() { return Engine::get_instance(); }, "Retrieve the simulation engine")
160       .def("get_all_hosts",
161            [](pybind11::object& self) // XBT_ATTRIB_DEPRECATED_v333
162            {
163              PyErr_WarnEx(PyExc_DeprecationWarning,
164                           "get_all_hosts() is deprecated and  will be dropped after v3.32, use all_host() instead.", 1);
165              return self.attr("all_hosts");
166            })
167       .def_property_readonly("all_hosts", &Engine::get_all_hosts, "Returns the list of all hosts found in the platform")
168       .def("get_all_links",
169            [](pybind11::object& self) // XBT_ATTRIB_DEPRECATED_v333
170            {
171              PyErr_WarnEx(PyExc_DeprecationWarning,
172                           "get_all_links() is deprecated and  will be dropped after v3.32, use all_links() instead.",
173                           1);
174              return self.attr("all_links");
175            })
176       .def_property_readonly("all_links", &Engine::get_all_links, "Returns the list of all links found in the platform")
177       .def("get_all_netpoints",
178            [](pybind11::object& self) // XBT_ATTRIB_DEPRECATED_v333
179            {
180              PyErr_WarnEx(
181                  PyExc_DeprecationWarning,
182                  "get_all_netpoints() is deprecated and  will be dropped after v3.32, use all_netpoints() instead.", 1);
183              return self.attr("all_netpoints");
184            })
185       .def_property_readonly("all_netpoints", &Engine::get_all_netpoints)
186       .def("get_netzone_root",
187            [](pybind11::object& self) // XBT_ATTRIB_DEPRECATED_v333
188            {
189              PyErr_WarnEx(
190                  PyExc_DeprecationWarning,
191                  "get_netzone_root() is deprecated and  will be dropped after v3.32, use netzone_root() instead.", 1);
192              return self.attr("netzone_root");
193            })
194       .def("set_netzone_root",
195            [](pybind11::object& self) // XBT_ATTRIB_DEPRECATED_v333
196            {
197              PyErr_WarnEx(PyExc_DeprecationWarning,
198                           "set_netzone_root() is deprecated and  will be dropped after v3.32.", 1);
199            })
200       .def_property_readonly("netzone_root", &Engine::get_netzone_root,
201                              "Retrieve the root netzone, containing all others.")
202       .def("netpoint_by_name", &Engine::netpoint_by_name_or_null)
203       .def("netzone_by_name", &Engine::netzone_by_name_or_null)
204       .def("load_platform", &Engine::load_platform, "Load a platform file describing the environment")
205       .def("load_deployment", &Engine::load_deployment, "Load a deployment file and launch the actors that it contains")
206       .def("run", &Engine::run, py::call_guard<py::gil_scoped_release>(), "Run the simulation until its end")
207       .def("run_until", py::overload_cast<double>(&Engine::run_until, py::const_),
208            py::call_guard<py::gil_scoped_release>(), "Run the simulation until the given date",
209            py::arg("max_date") = -1)
210       .def(
211           "register_actor",
212           [](Engine* e, const std::string& name, py::object fun_or_class) {
213             e->register_actor(name, [fun_or_class](std::vector<std::string> args) {
214               try {
215                 py::gil_scoped_acquire py_context;
216                 /* Convert the std::vector into a py::tuple */
217                 py::tuple params(args.size() - 1);
218                 for (size_t i = 1; i < args.size(); i++)
219                   params[i - 1] = py::cast(args[i]);
220
221                 py::object res = fun_or_class(*params);
222                 /* If I was passed a class, I just built an instance, so I need to call it now */
223                 if (py::isinstance<py::function>(res))
224                   res();
225               } catch (const py::error_already_set& ex) {
226                 if (ex.matches(pyForcefulKillEx)) {
227                   XBT_VERB("Actor killed");
228                   simgrid::ForcefulKillException::do_throw(); // Forward that ForcefulKill exception
229                 }
230                 throw;
231               }
232             });
233           },
234           "Registers the main function of an actor");
235
236   /* Class Netzone */
237   py::class_<simgrid::s4u::NetZone, std::unique_ptr<simgrid::s4u::NetZone, py::nodelete>> netzone(
238       m, "NetZone", "Networking Zones. See the C++ documentation for details.");
239   netzone.def_static("create_full_zone", &simgrid::s4u::create_full_zone, "Creates a zone of type FullZone")
240       .def_static("create_torus_zone", &simgrid::s4u::create_torus_zone, "Creates a cluster of type Torus")
241       .def_static("create_fatTree_zone", &simgrid::s4u::create_fatTree_zone, "Creates a cluster of type Fat-Tree")
242       .def_static("create_dragonfly_zone", &simgrid::s4u::create_dragonfly_zone, "Creates a cluster of type Dragonfly")
243       .def_static("create_star_zone", &simgrid::s4u::create_star_zone, "Creates a zone of type Star")
244       .def_static("create_floyd_zone", &simgrid::s4u::create_floyd_zone, "Creates a zone of type Floyd")
245       .def_static("create_dijkstra_zone", &simgrid::s4u::create_floyd_zone, "Creates a zone of type Dijkstra")
246       .def_static("create_vivaldi_zone", &simgrid::s4u::create_vivaldi_zone, "Creates a zone of type Vivaldi")
247       .def_static("create_empty_zone", &simgrid::s4u::create_empty_zone, "Creates a zone of type Empty")
248       .def_static("create_wifi_zone", &simgrid::s4u::create_wifi_zone, "Creates a zone of type Wi-Fi")
249       .def("add_route",
250            py::overload_cast<simgrid::kernel::routing::NetPoint*, simgrid::kernel::routing::NetPoint*,
251                              simgrid::kernel::routing::NetPoint*, simgrid::kernel::routing::NetPoint*,
252                              const std::vector<simgrid::s4u::LinkInRoute>&, bool>(&simgrid::s4u::NetZone::add_route),
253            "Add a route between 2 netpoints")
254       .def("create_host", py::overload_cast<const std::string&, double>(&simgrid::s4u::NetZone::create_host),
255            "Creates a host")
256       .def("create_host",
257            py::overload_cast<const std::string&, const std::string&>(&simgrid::s4u::NetZone::create_host),
258            "Creates a host")
259       .def("create_host",
260            py::overload_cast<const std::string&, const std::vector<double>&>(&simgrid::s4u::NetZone::create_host),
261            "Creates a host")
262       .def("create_host",
263            py::overload_cast<const std::string&, const std::vector<std::string>&>(&simgrid::s4u::NetZone::create_host),
264            "Creates a host")
265       .def("create_link", py::overload_cast<const std::string&, double>(&simgrid::s4u::NetZone::create_link),
266            "Creates a network link")
267       .def("create_link",
268            py::overload_cast<const std::string&, const std::string&>(&simgrid::s4u::NetZone::create_link),
269            "Creates a network link")
270       .def("create_link",
271            py::overload_cast<const std::string&, const std::vector<double>&>(&simgrid::s4u::NetZone::create_link),
272            "Creates a network link")
273       .def("create_link",
274            py::overload_cast<const std::string&, const std::vector<std::string>&>(&simgrid::s4u::NetZone::create_link),
275            "Creates a network link")
276       .def("create_split_duplex_link",
277            py::overload_cast<const std::string&, double>(&simgrid::s4u::NetZone::create_split_duplex_link),
278            "Creates a split-duplex link")
279       .def("create_split_duplex_link",
280            py::overload_cast<const std::string&, const std::string&>(&simgrid::s4u::NetZone::create_split_duplex_link),
281            "Creates a split-duplex link")
282       .def("create_router", &simgrid::s4u::NetZone::create_router, "Create a router")
283       .def("set_parent", &simgrid::s4u::NetZone::set_parent, "Set the parent of this zone")
284       .def("set_property", &simgrid::s4u::NetZone::set_property, "Add a property to this zone")
285       .def("get_netpoint", &simgrid::s4u::NetZone::get_netpoint, "Retrieve the netpoint associated to this zone")
286       .def("seal", &simgrid::s4u::NetZone::seal, "Seal this NetZone")
287       .def_property_readonly(
288           "name", [](const simgrid::s4u::NetZone* self) { return self->get_name(); },
289           "The name of this network zone (read-only property).");
290
291   /* Class ClusterCallbacks */
292   py::class_<simgrid::s4u::ClusterCallbacks>(m, "ClusterCallbacks", "Callbacks used to create cluster zones")
293       .def(py::init<const std::function<simgrid::s4u::ClusterCallbacks::ClusterNetPointCb>&,
294                     const std::function<simgrid::s4u::ClusterCallbacks::ClusterLinkCb>&,
295                     const std::function<simgrid::s4u::ClusterCallbacks::ClusterLinkCb>&>());
296
297   /* Class FatTreeParams */
298   py::class_<simgrid::s4u::FatTreeParams>(m, "FatTreeParams", "Parameters to create a Fat-Tree zone")
299       .def(py::init<unsigned int, const std::vector<unsigned int>&, const std::vector<unsigned int>&,
300                     const std::vector<unsigned int>&>());
301
302   /* Class DragonflyParams */
303   py::class_<simgrid::s4u::DragonflyParams>(m, "DragonflyParams", "Parameters to create a Dragonfly zone")
304       .def(py::init<const std::pair<unsigned int, unsigned int>&, const std::pair<unsigned int, unsigned int>&,
305                     const std::pair<unsigned int, unsigned int>&, unsigned int>());
306
307   /* Class Host */
308   py::class_<simgrid::s4u::Host, std::unique_ptr<Host, py::nodelete>> host(
309       m, "Host", "Simulated host. See the C++ documentation for details.");
310   host.def("by_name", &Host::by_name, "Retrieves a host from its name, or die")
311       .def(
312           "route_to",
313           [](const simgrid::s4u::Host* h, const simgrid::s4u::Host* to) {
314             auto* list = new std::vector<Link*>();
315             double bw  = 0;
316             h->route_to(to, *list, &bw);
317             return make_tuple(list, bw);
318           },
319           "Retrieves the list of links and the bandwidth between two hosts.")
320       .def(
321           "set_speed_profile",
322           [](Host* h, const std::string& profile, double period) {
323             h->set_speed_profile(simgrid::kernel::profile::ProfileBuilder::from_string("", profile, period));
324           },
325           py::call_guard<py::gil_scoped_release>(),
326           "Specify a profile modeling the external load according to an exhaustive list. "
327           "Each line of the profile describes timed events as ``date ratio``. "
328           "For example, the following content describes an host which computational speed is initially 1 (i.e, 100%) "
329           "and then halved after 42 seconds:\n\n"
330           ".. code-block:: python\n\n"
331           "   \"\"\"\n"
332           "   0 1.0\n"
333           "   42 0.5\n"
334           "   \"\"\"\n\n"
335           ".. warning:: Don't get fooled: bandwidth and latency profiles of links contain absolute values,"
336           " while speed profiles of hosts contain ratios.\n\n"
337           "The second function parameter is the periodicity: the time to wait after the last event to start again over "
338           "the list. Set it to -1 to not loop over.")
339       .def(
340           "set_state_profile",
341           [](Host* h, const std::string& profile, double period) {
342             h->set_state_profile(simgrid::kernel::profile::ProfileBuilder::from_string("", profile, period));
343           },
344           py::call_guard<py::gil_scoped_release>(),
345           "Specify a profile modeling the churn. "
346           "Each line of the profile describes timed events as ``date boolean``, where the boolean (0 or 1) tells "
347           "whether the host is on. "
348           "For example, the following content describes a link which turns off at t=1 and back on at t=2:\n\n"
349           ".. code-block:: python\n\n"
350           "   \"\"\"\n"
351           "   1.0 0\n"
352           "   2.0 1\n"
353           "   \"\"\"\n\n"
354           "The second function parameter is the periodicity: the time to wait after the last event to start again over "
355           "the list. Set it to -1 to not loop over.")
356       .def("get_pstate_count", &Host::get_pstate_count, "Retrieve the count of defined pstate levels")
357       .def("get_pstate_speed", &Host::get_pstate_speed, "Retrieve the maximal speed at the given pstate")
358       .def("get_netpoint", &Host::get_netpoint, "Retrieve the netpoint associated to this host")
359       .def("get_disks", &Host::get_disks, "Retrieve the list of disks in this host")
360       .def("set_core_count", &Host::set_core_count, py::call_guard<py::gil_scoped_release>(),
361            "Set the number of cores in the CPU")
362       .def("set_coordinates", &Host::set_coordinates, py::call_guard<py::gil_scoped_release>(),
363            "Set the coordinates of this host")
364       .def("set_sharing_policy", &simgrid::s4u::Host::set_sharing_policy, py::call_guard<py::gil_scoped_release>(),
365            "Describe how the CPU is shared", py::arg("policy"), py::arg("cb") = simgrid::s4u::NonLinearResourceCb())
366       .def("create_disk", py::overload_cast<const std::string&, double, double>(&Host::create_disk),
367            py::call_guard<py::gil_scoped_release>(), "Create a disk")
368       .def("create_disk",
369            py::overload_cast<const std::string&, const std::string&, const std::string&>(&Host::create_disk),
370            py::call_guard<py::gil_scoped_release>(), "Create a disk")
371       .def("seal", &Host::seal, py::call_guard<py::gil_scoped_release>(), "Seal this host")
372       .def_property(
373           "pstate", &Host::get_pstate,
374           [](Host* h, int i) {
375             py::gil_scoped_release gil_guard;
376             h->set_pstate(i);
377           },
378           "The current pstate (read/write property).")
379       .def("current", &Host::current, py::call_guard<py::gil_scoped_release>(),
380            "Retrieves the host on which the running actor is located.")
381       .def_property_readonly(
382           "name",
383           [](const Host* self) {
384             return std::string(self->get_name().c_str()); // Convert from xbt::string because of MC
385           },
386           "The name of this host (read-only property).")
387       .def_property_readonly("load", &Host::get_load,
388                              "Returns the current computation load (in flops per second), NOT taking the external load "
389                              "into account. This is the currently achieved speed (read-only property).")
390       .def_property_readonly(
391           "speed", &Host::get_speed,
392           "The peak computing speed in flops/s at the current pstate, NOT taking the external load into account. "
393           "This is the max potential speed (read-only property).")
394       .def_property_readonly("available_speed", &Host::get_available_speed,
395                              "Get the available speed ratio, between 0 and 1.\n"
396                              "This accounts for external load (see :py:func:`set_speed_profile() "
397                              "<simgrid.Host.set_speed_profile>`) (read-only property).")
398       .def(
399           "on_creation_cb",
400           [](py::object cb) {
401             Host::on_creation_cb([cb](Host& h) {
402               py::function fun = py::reinterpret_borrow<py::function>(cb);
403               try {
404                 py::gil_scoped_acquire py_context; // need a new context for callback
405                 fun(&h);
406               } catch (const py::error_already_set& e) {
407                 xbt_die("Error while executing the on_creation lambda : %s", e.what());
408               }
409             });
410           },
411           py::call_guard<py::gil_scoped_release>(), "");
412
413   py::enum_<simgrid::s4u::Host::SharingPolicy>(host, "SharingPolicy")
414       .value("NONLINEAR", simgrid::s4u::Host::SharingPolicy::NONLINEAR)
415       .value("LINEAR", simgrid::s4u::Host::SharingPolicy::LINEAR)
416       .export_values();
417
418   /* Class Disk */
419   py::class_<simgrid::s4u::Disk, std::unique_ptr<simgrid::s4u::Disk, py::nodelete>> disk(
420       m, "Disk", "Simulated disk. See the C++ documentation for details.");
421   disk.def("read", py::overload_cast<sg_size_t, double>(&simgrid::s4u::Disk::read, py::const_),
422            py::call_guard<py::gil_scoped_release>(), "Read data from disk", py::arg("size"), py::arg("priority") = 1)
423       .def("write", py::overload_cast<sg_size_t, double>(&simgrid::s4u::Disk::write, py::const_),
424            py::call_guard<py::gil_scoped_release>(), "Write data in disk", py::arg("size"), py::arg("priority") = 1)
425       .def("read_async", &simgrid::s4u::Disk::read_async, py::call_guard<py::gil_scoped_release>(),
426            "Non-blocking read data from disk")
427       .def("write_async", &simgrid::s4u::Disk::write_async, py::call_guard<py::gil_scoped_release>(),
428            "Non-blocking write data in disk")
429       .def("set_sharing_policy", &simgrid::s4u::Disk::set_sharing_policy, py::call_guard<py::gil_scoped_release>(),
430            "Set sharing policy for this disk", py::arg("op"), py::arg("policy"),
431            py::arg("cb") = simgrid::s4u::NonLinearResourceCb())
432       .def("seal", &simgrid::s4u::Disk::seal, py::call_guard<py::gil_scoped_release>(), "Seal this disk")
433       .def_property_readonly(
434           "name", [](const simgrid::s4u::Disk* self) { return self->get_name(); },
435           "The name of this disk (read-only property).");
436   py::enum_<simgrid::s4u::Disk::SharingPolicy>(disk, "SharingPolicy")
437       .value("NONLINEAR", simgrid::s4u::Disk::SharingPolicy::NONLINEAR)
438       .value("LINEAR", simgrid::s4u::Disk::SharingPolicy::LINEAR)
439       .export_values();
440   py::enum_<simgrid::s4u::Disk::Operation>(disk, "Operation")
441       .value("READ", simgrid::s4u::Disk::Operation::READ)
442       .value("WRITE", simgrid::s4u::Disk::Operation::WRITE)
443       .value("READWRITE", simgrid::s4u::Disk::Operation::READWRITE)
444       .export_values();
445
446   /* Class NetPoint */
447   py::class_<simgrid::kernel::routing::NetPoint, std::unique_ptr<simgrid::kernel::routing::NetPoint, py::nodelete>>
448       netpoint(m, "NetPoint", "NetPoint object");
449
450   /* Class Link */
451   py::class_<Link, std::unique_ptr<Link, py::nodelete>> link(m, "Link",
452                                                              "Network link. See the C++ documentation for details.");
453   link.def("set_latency", py::overload_cast<const std::string&>(&Link::set_latency),
454            py::call_guard<py::gil_scoped_release>(),
455            "Set the latency as a string. Accepts values with units, such as â€˜1s’ or â€˜7ms’.\nFull list of accepted "
456            "units: w (week), d (day), h, s, ms, us, ns, ps.")
457       .def("set_latency", py::overload_cast<double>(&Link::set_latency), py::call_guard<py::gil_scoped_release>(),
458            "Set the latency as a float (in seconds).")
459       .def("set_bandwidth", &Link::set_bandwidth, py::call_guard<py::gil_scoped_release>(),
460            "Set the bandwidth (in byte per second).")
461       .def(
462           "set_bandwidth_profile",
463           [](Link* l, const std::string& profile, double period) {
464             l->set_bandwidth_profile(simgrid::kernel::profile::ProfileBuilder::from_string("", profile, period));
465           },
466           py::call_guard<py::gil_scoped_release>(),
467           "Specify a profile modeling the external load according to an exhaustive list. "
468           "Each line of the profile describes timed events as ``date bandwidth`` (in bytes per second). "
469           "For example, the following content describes a link which bandwidth changes to 40 Mb/s at t=4, and to 6 "
470           "Mb/s at t=8:\n\n"
471           ".. code-block:: python\n\n"
472           "   \"\"\"\n"
473           "   4.0 40000000\n"
474           "   8.0 60000000\n"
475           "   \"\"\"\n\n"
476           ".. warning:: Don't get fooled: bandwidth and latency profiles of links contain absolute values,"
477           " while speed profiles of hosts contain ratios.\n\n"
478           "The second function parameter is the periodicity: the time to wait after the last event to start again over "
479           "the list. Set it to -1 to not loop over.")
480       .def(
481           "set_latency_profile",
482           [](Link* l, const std::string& profile, double period) {
483             l->set_latency_profile(simgrid::kernel::profile::ProfileBuilder::from_string("", profile, period));
484           },
485           py::call_guard<py::gil_scoped_release>(),
486           "Specify a profile modeling the external load according to an exhaustive list. "
487           "Each line of the profile describes timed events as ``date latency`` (in seconds). "
488           "For example, the following content describes a link which latency changes to 1ms (0.001s) at t=1, and to 2s "
489           "at t=2:\n\n"
490           ".. code-block:: python\n\n"
491           "   \"\"\"\n"
492           "   1.0 0.001\n"
493           "   2.0 2\n"
494           "   \"\"\"\n\n"
495           ".. warning:: Don't get fooled: bandwidth and latency profiles of links contain absolute values,"
496           " while speed profiles of hosts contain ratios.\n\n"
497           "The second function parameter is the periodicity: the time to wait after the last event to start again over "
498           "the list. Set it to -1 to not loop over.")
499       .def(
500           "set_state_profile",
501           [](Link* l, const std::string& profile, double period) {
502             l->set_state_profile(simgrid::kernel::profile::ProfileBuilder::from_string("", profile, period));
503           },
504           "Specify a profile modeling the churn. "
505           "Each line of the profile describes timed events as ``date boolean``, where the boolean (0 or 1) tells "
506           "whether the link is on. "
507           "For example, the following content describes a link which turns off at t=1 and back on at t=2:\n\n"
508           ".. code-block:: python\n\n"
509           "   \"\"\"\n"
510           "   1.0 0\n"
511           "   2.0 1\n"
512           "   \"\"\"\n\n"
513           "The second function parameter is the periodicity: the time to wait after the last event to start again over "
514           "the list. Set it to -1 to not loop over.")
515
516       .def("turn_on", &Link::turn_on, py::call_guard<py::gil_scoped_release>(), "Turns the link on.")
517       .def("turn_off", &Link::turn_off, py::call_guard<py::gil_scoped_release>(), "Turns the link off.")
518       .def("is_on", &Link::is_on, "Check whether the link is on.")
519
520       .def("set_sharing_policy", &Link::set_sharing_policy, py::call_guard<py::gil_scoped_release>(),
521            "Set sharing policy for this link")
522       .def("set_concurrency_limit", &Link::set_concurrency_limit, py::call_guard<py::gil_scoped_release>(),
523            "Set concurrency limit for this link")
524       .def("set_host_wifi_rate", &Link::set_host_wifi_rate, py::call_guard<py::gil_scoped_release>(),
525            "Set level of communication speed of given host on this Wi-Fi link")
526       .def_static("by_name", &Link::by_name, "Retrieves a Link from its name, or dies")
527       .def("seal", &Link::seal, py::call_guard<py::gil_scoped_release>(), "Seal this link")
528       .def_property_readonly(
529           "name",
530           [](const Link* self) {
531             return std::string(self->get_name().c_str()); // Convert from xbt::string because of MC
532           },
533           "The name of this link")
534       .def_property_readonly("bandwidth", &Link::get_bandwidth,
535                              "The bandwidth (in bytes per second) (read-only property).")
536       .def_property_readonly("latency", &Link::get_latency, "The latency (in seconds) (read-only property).");
537
538   py::enum_<Link::SharingPolicy>(link, "SharingPolicy")
539       .value("NONLINEAR", Link::SharingPolicy::NONLINEAR)
540       .value("WIFI", Link::SharingPolicy::WIFI)
541       .value("SPLITDUPLEX", Link::SharingPolicy::SPLITDUPLEX)
542       .value("SHARED", Link::SharingPolicy::SHARED)
543       .value("FATPIPE", Link::SharingPolicy::FATPIPE)
544       .export_values();
545
546   /* Class LinkInRoute */
547   py::class_<simgrid::s4u::LinkInRoute> linkinroute(m, "LinkInRoute", "Abstraction to add link in routes");
548   linkinroute.def(py::init<const Link*>());
549   linkinroute.def(py::init<const Link*, simgrid::s4u::LinkInRoute::Direction>());
550   py::enum_<simgrid::s4u::LinkInRoute::Direction>(linkinroute, "Direction")
551       .value("UP", simgrid::s4u::LinkInRoute::Direction::UP)
552       .value("DOWN", simgrid::s4u::LinkInRoute::Direction::DOWN)
553       .value("NONE", simgrid::s4u::LinkInRoute::Direction::NONE)
554       .export_values();
555
556   /* Class Split-Duplex Link */
557   py::class_<simgrid::s4u::SplitDuplexLink, Link, std::unique_ptr<simgrid::s4u::SplitDuplexLink, py::nodelete>>(
558       m, "SplitDuplexLink", "Network split-duplex link")
559       .def("get_link_up", &simgrid::s4u::SplitDuplexLink::get_link_up, "Get link direction up")
560       .def("get_link_down", &simgrid::s4u::SplitDuplexLink::get_link_down, "Get link direction down");
561
562   /* Class Mailbox */
563   py::class_<simgrid::s4u::Mailbox, std::unique_ptr<Mailbox, py::nodelete>>(
564       m, "Mailbox", "Mailbox. See the C++ documentation for details.")
565       .def(
566           "__str__", [](const Mailbox* self) { return std::string("Mailbox(") + self->get_cname() + ")"; },
567           "Textual representation of the Mailbox`")
568       .def("by_name", &Mailbox::by_name, py::call_guard<py::gil_scoped_release>(), "Retrieve a Mailbox from its name")
569       .def_property_readonly(
570           "name",
571           [](const Mailbox* self) {
572             return std::string(self->get_name().c_str()); // Convert from xbt::string because of MC
573           },
574           "The name of that mailbox (read-only property).")
575       .def(
576           "put",
577           [](Mailbox* self, py::object data, int size, double timeout) {
578             data.inc_ref();
579             self->put(data.ptr(), size, timeout);
580           },
581           py::call_guard<py::gil_scoped_release>(), "Blocking data transmission with a timeout")
582       .def(
583           "put",
584           [](Mailbox* self, py::object data, int size) {
585             data.inc_ref();
586             self->put(data.ptr(), size);
587           },
588           py::call_guard<py::gil_scoped_release>(), "Blocking data transmission")
589       .def(
590           "put_async",
591           [](Mailbox* self, py::object data, int size) {
592             data.inc_ref();
593             return self->put_async(data.ptr(), size);
594           },
595           py::call_guard<py::gil_scoped_release>(), "Non-blocking data transmission")
596       .def(
597           "get",
598           [](Mailbox* self) {
599             py::object data = pybind11::reinterpret_steal<py::object>(self->get<PyObject>());
600             // data.dec_ref(); // FIXME: why does it break python-actor-create?
601             return data;
602           },
603           py::call_guard<py::gil_scoped_release>(), "Blocking data reception")
604       .def(
605           "get_async",
606           [](Mailbox* self) -> std::tuple<simgrid::s4u::CommPtr, PyGetAsync> {
607             PyGetAsync wrap;
608             auto comm = self->get_async(wrap.get());
609             return std::make_tuple(std::move(comm), std::move(wrap));
610           },
611           py::call_guard<py::gil_scoped_release>(),
612           "Non-blocking data reception. Use data.get() to get the python object after the communication has finished")
613       .def(
614           "set_receiver", [](Mailbox* self, ActorPtr actor) { self->set_receiver(actor); },
615           py::call_guard<py::gil_scoped_release>(), "Sets the actor as permanent receiver");
616
617   /* Class PyGetAsync */
618   py::class_<PyGetAsync>(m, "PyGetAsync", "Wrapper for async get communications")
619       .def(py::init<>())
620       .def(
621           "get", [](const PyGetAsync* self) { return py::reinterpret_steal<py::object>(*(self->get())); },
622           "Get python object after async communication in receiver side");
623
624   /* Class Comm */
625   py::class_<simgrid::s4u::Comm, simgrid::s4u::CommPtr>(m, "Comm",
626                                                         "Communication. See the C++ documentation for details.")
627       .def("test", &simgrid::s4u::Comm::test, py::call_guard<py::gil_scoped_release>(),
628            "Test whether the communication is terminated.")
629       .def("wait", &simgrid::s4u::Comm::wait, py::call_guard<py::gil_scoped_release>(),
630            "Block until the completion of that communication.")
631       // use py::overload_cast for wait_all/wait_any, until the overload marked XBT_ATTRIB_DEPRECATED_v332 is removed
632       .def_static(
633           "wait_all", py::overload_cast<const std::vector<simgrid::s4u::CommPtr>&>(&simgrid::s4u::Comm::wait_all),
634           py::call_guard<py::gil_scoped_release>(), "Block until the completion of all communications in the list.")
635       .def_static(
636           "wait_any", py::overload_cast<const std::vector<simgrid::s4u::CommPtr>&>(&simgrid::s4u::Comm::wait_any),
637           py::call_guard<py::gil_scoped_release>(),
638           "Block until the completion of any communication in the list and return the index of the terminated one.");
639
640   /* Class Io */
641   py::class_<simgrid::s4u::Io, simgrid::s4u::IoPtr>(m, "Io", "I/O activities. See the C++ documentation for details.")
642       .def("test", &simgrid::s4u::Io::test, py::call_guard<py::gil_scoped_release>(),
643            "Test whether the I/O is terminated.")
644       .def("wait", &simgrid::s4u::Io::wait, py::call_guard<py::gil_scoped_release>(),
645            "Block until the completion of that I/O operation")
646       .def_static(
647           "wait_any_for", &simgrid::s4u::Io::wait_any_for, py::call_guard<py::gil_scoped_release>(),
648           "Block until the completion of any I/O in the list (or timeout) and return the index of the terminated one.")
649       .def_static("wait_any", &simgrid::s4u::Io::wait_any, py::call_guard<py::gil_scoped_release>(),
650                   "Block until the completion of any I/O in the list and return the index of the terminated one.");
651
652   /* Class Exec */
653   py::class_<simgrid::s4u::Exec, simgrid::s4u::ExecPtr>(m, "Exec", "Execution. See the C++ documentation for details.")
654       .def_property_readonly(
655           "remaining",
656           [](simgrid::s4u::ExecPtr self) {
657             py::gil_scoped_release gil_guard;
658             return self->get_remaining();
659           },
660           "Amount of flops that remain to be computed until completion (read-only property).")
661       .def_property_readonly(
662           "remaining_ratio",
663           [](simgrid::s4u::ExecPtr self) {
664             py::gil_scoped_release gil_guard;
665             return self->get_remaining_ratio();
666           },
667           "Amount of work remaining until completion from 0 (completely done) to 1 (nothing done "
668           "yet) (read-only property).")
669       .def_property("host", &simgrid::s4u::Exec::get_host, &simgrid::s4u::Exec::set_host,
670                     "Host on which this execution runs. Only the first host is returned for parallel executions. "
671                     "Changing this value migrates the execution.")
672       .def("test", &simgrid::s4u::Exec::test, py::call_guard<py::gil_scoped_release>(),
673            "Test whether the execution is terminated.")
674       .def("cancel", &simgrid::s4u::Exec::cancel, py::call_guard<py::gil_scoped_release>(), "Cancel that execution.")
675       .def("start", &simgrid::s4u::Exec::start, py::call_guard<py::gil_scoped_release>(), "Start that execution.")
676       .def("wait", &simgrid::s4u::Exec::wait, py::call_guard<py::gil_scoped_release>(),
677            "Block until the completion of that execution.");
678
679   /* Class Actor */
680   py::class_<simgrid::s4u::Actor, ActorPtr>(m, "Actor",
681                                             "An actor is an independent stream of execution in your distributed "
682                                             "application. See the C++ documentation for details.")
683       .def(
684           "create",
685           [](py::str name, Host* h, py::object fun, py::args args) {
686             fun.inc_ref();  // FIXME: why is this needed for tests like exec-async, exec-dvfs and exec-remote?
687             args.inc_ref(); // FIXME: why is this needed for tests like actor-migrate?
688             return simgrid::s4u::Actor::create(name, h, [fun, args]() {
689               try {
690                 py::gil_scoped_acquire py_context;
691                 fun(*args);
692               } catch (const py::error_already_set& ex) {
693                 if (ex.matches(pyForcefulKillEx)) {
694                   XBT_VERB("Actor killed");
695                   simgrid::ForcefulKillException::do_throw(); // Forward that ForcefulKill exception
696                 }
697                 throw;
698               }
699             });
700           },
701           py::call_guard<py::gil_scoped_release>(),
702           "Create an actor from a function or an object. See the :ref:`example <s4u_ex_actors_create>`.")
703       .def_property(
704           "host", &Actor::get_host,
705           [](Actor* a, Host* h) {
706             py::gil_scoped_release gil_guard;
707             a->set_host(h);
708           },
709           "The host on which this actor is located. Changing this value migrates the actor.\n\n"
710           "If the actor is currently blocked on an execution activity, the activity is also migrated to the new host. "
711           "If it’s blocked on another kind of activity, an error is raised as the mandated code is not written yet. "
712           "Please report that bug if you need it.\n\n"
713           "Asynchronous activities started by the actor are not migrated automatically, so you have to take care of "
714           "this yourself (only you knows which ones should be migrated). ")
715       .def_property_readonly("name", &Actor::get_cname, "The name of this actor (read-only property).")
716       .def_property_readonly("pid", &Actor::get_pid, "The PID (unique identifier) of this actor (read-only property).")
717       .def_property_readonly("ppid", &Actor::get_ppid,
718                              "The PID (unique identifier) of the actor that created this one (read-only property).")
719       .def("by_pid", &Actor::by_pid, "Retrieve an actor by its PID")
720       .def("set_auto_restart", &Actor::set_auto_restart, py::call_guard<py::gil_scoped_release>(),
721            "Specify whether the actor shall restart when its host reboots.")
722       .def("daemonize", &Actor::daemonize, py::call_guard<py::gil_scoped_release>(),
723            "This actor will be automatically terminated when the last non-daemon actor finishes (more info in the C++ "
724            "documentation).")
725       .def("is_daemon", &Actor::is_daemon,
726            "Returns True if that actor is a daemon and will be terminated automatically when the last non-daemon actor "
727            "terminates.")
728       .def("join", py::overload_cast<double>(&Actor::join, py::const_), py::call_guard<py::gil_scoped_release>(),
729            "Wait for the actor to finish (more info in the C++ documentation).", py::arg("timeout"))
730       .def("kill", &Actor::kill, py::call_guard<py::gil_scoped_release>(), "Kill that actor")
731       .def("kill_all", &Actor::kill_all, py::call_guard<py::gil_scoped_release>(), "Kill all actors but the caller.")
732       .def("self", &Actor::self, "Retrieves the current actor.")
733       .def("is_suspended", &Actor::is_suspended, "Returns True if that actor is currently suspended.")
734       .def("suspend", &Actor::suspend, py::call_guard<py::gil_scoped_release>(),
735            "Suspend that actor, that is blocked until resume()ed by another actor.")
736       .def("resume", &Actor::resume, py::call_guard<py::gil_scoped_release>(),
737            "Resume that actor, that was previously suspend()ed.");
738 }