Logo AND Algorithmique Numérique Distribuée

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