Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Many tiny documentation improvements
[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/routing/NetPoint.hpp"
26 #include "src/kernel/context/Context.hpp"
27 #include <simgrid/Exception.hpp>
28 #include <simgrid/s4u/Actor.hpp>
29 #include <simgrid/s4u/Comm.hpp>
30 #include <simgrid/s4u/Disk.hpp>
31 #include <simgrid/s4u/Engine.hpp>
32 #include <simgrid/s4u/Exec.hpp>
33 #include <simgrid/s4u/Host.hpp>
34 #include <simgrid/s4u/Link.hpp>
35 #include <simgrid/s4u/Mailbox.hpp>
36 #include <simgrid/s4u/NetZone.hpp>
37 #include <simgrid/version.h>
38
39 #include <algorithm>
40 #include <memory>
41 #include <string>
42 #include <vector>
43
44 namespace py = pybind11;
45 using simgrid::s4u::Actor;
46 using simgrid::s4u::ActorPtr;
47 using simgrid::s4u::Engine;
48 using simgrid::s4u::Host;
49 using simgrid::s4u::Mailbox;
50
51 XBT_LOG_NEW_DEFAULT_CATEGORY(python, "python");
52
53 namespace {
54
55 std::string get_simgrid_version()
56 {
57   int major;
58   int minor;
59   int patch;
60   sg_version_get(&major, &minor, &patch);
61   return simgrid::xbt::string_printf("%i.%i.%i", major, minor, patch);
62 }
63
64 /** @brief Wrap for mailbox::get_async */
65 class PyGetAsync {
66   std::unique_ptr<PyObject*> data = std::make_unique<PyObject*>();
67
68 public:
69   PyObject** get() const { return data.get(); }
70 };
71
72 } // namespace
73
74 PYBIND11_DECLARE_HOLDER_TYPE(T, boost::intrusive_ptr<T>)
75
76 PYBIND11_MODULE(simgrid, m)
77 {
78   m.doc() = "SimGrid userspace API";
79
80   m.attr("simgrid_version") = get_simgrid_version();
81
82   // Swapped contexts are broken, starting from pybind11 v2.8.0.  Use thread contexts by default.
83   simgrid::s4u::Engine::set_config("contexts/factory:thread");
84
85   // Internal exception used to kill actors and sweep the RAII chimney (free objects living on the stack)
86   static py::object pyForcefulKillEx(py::register_exception<simgrid::ForcefulKillException>(m, "ActorKilled"));
87
88   /* this_actor namespace */
89   m.def_submodule("this_actor", "Bindings of the s4u::this_actor namespace. See the C++ documentation for details.")
90       .def(
91           "debug", [](const char* s) { XBT_DEBUG("%s", s); }, "Display a logging message of 'debug' priority.")
92       .def(
93           "info", [](const char* s) { XBT_INFO("%s", s); }, "Display a logging message of 'info' priority.")
94       .def(
95           "error", [](const char* s) { XBT_ERROR("%s", s); }, "Display a logging message of 'error' priority.")
96       .def("execute", py::overload_cast<double, double>(&simgrid::s4u::this_actor::execute),
97            py::call_guard<py::gil_scoped_release>(),
98            "Block the current actor, computing the given amount of flops at the given priority.", py::arg("flops"),
99            py::arg("priority") = 1)
100       .def("exec_init", py::overload_cast<double>(&simgrid::s4u::this_actor::exec_init),
101            py::call_guard<py::gil_scoped_release>())
102       .def("get_host", &simgrid::s4u::this_actor::get_host, "Retrieves host on which the current actor is located")
103       .def("set_host", &simgrid::s4u::this_actor::set_host, py::call_guard<py::gil_scoped_release>(),
104            "Moves the current actor to another host.", py::arg("dest"))
105       .def("sleep_for", static_cast<void (*)(double)>(&simgrid::s4u::this_actor::sleep_for),
106            py::call_guard<py::gil_scoped_release>(), "Block the actor sleeping for that amount of seconds.",
107            py::arg("duration"))
108       .def("sleep_until", static_cast<void (*)(double)>(&simgrid::s4u::this_actor::sleep_until),
109            py::call_guard<py::gil_scoped_release>(), "Block the actor sleeping until the specified timestamp.",
110            py::arg("duration"))
111       .def("suspend", &simgrid::s4u::this_actor::suspend, py::call_guard<py::gil_scoped_release>(),
112            "Suspend the current actor, that is blocked until resume()ed by another actor.")
113       .def("yield_", &simgrid::s4u::this_actor::yield, py::call_guard<py::gil_scoped_release>(), "Yield the actor")
114       .def("exit", &simgrid::s4u::this_actor::exit, py::call_guard<py::gil_scoped_release>(), "kill the current actor")
115       .def(
116           "on_exit",
117           [](py::object fun) {
118             fun.inc_ref(); // FIXME: why is this needed for tests like actor-kill and actor-lifetime?
119             simgrid::s4u::this_actor::on_exit([fun](bool /*failed*/) {
120               try {
121                 py::gil_scoped_acquire py_context; // need a new context for callback
122                 fun();
123               } catch (const py::error_already_set& e) {
124                 xbt_die("Error while executing the on_exit lambda: %s", e.what());
125               }
126             });
127           },
128           py::call_guard<py::gil_scoped_release>(), "")
129       .def("get_pid", &simgrid::s4u::this_actor::get_pid, "Retrieves PID of the current actor")
130       .def("get_ppid", &simgrid::s4u::this_actor::get_ppid,
131            "Retrieves PPID of the current actor (i.e., the PID of its parent).");
132
133   /* Class Engine */
134   py::class_<Engine>(m, "Engine", "Simulation Engine")
135       .def(py::init([](std::vector<std::string> args) {
136              auto argc = static_cast<int>(args.size());
137              std::vector<char*> argv(args.size() + 1); // argv[argc] is nullptr
138              std::transform(begin(args), end(args), begin(argv), [](std::string& s) { return &s.front(); });
139              // Currently this can be dangling, we should wrap this somehow.
140              return new simgrid::s4u::Engine(&argc, argv.data());
141            }),
142            "The constructor should take the parameters from the command line, as is ")
143       .def_static("get_clock", &Engine::get_clock,
144                   "The simulation time, ie the amount of simulated seconds since the simulation start.")
145       .def_static(
146           "instance", []() { return Engine::get_instance(); }, "Retrieve the simulation engine")
147       .def("get_all_hosts", &Engine::get_all_hosts, "Returns the list of all hosts found in the platform")
148       .def("load_platform", &Engine::load_platform, "Load a platform file describing the environment")
149       .def("load_deployment", &Engine::load_deployment, "Load a deployment file and launch the actors that it contains")
150       .def("run", &Engine::run, py::call_guard<py::gil_scoped_release>(), "Run the simulation until its end")
151       .def("run_until", py::overload_cast<double>(&Engine::run_until, py::const_),
152            py::call_guard<py::gil_scoped_release>(), "Run the simulation until the given date",
153            py::arg("max_date") = -1)
154       .def(
155           "register_actor",
156           [](Engine* e, const std::string& name, py::object fun_or_class) {
157             e->register_actor(name, [fun_or_class](std::vector<std::string> args) {
158               try {
159                 py::gil_scoped_acquire py_context;
160                 /* Convert the std::vector into a py::tuple */
161                 py::tuple params(args.size() - 1);
162                 for (size_t i = 1; i < args.size(); i++)
163                   params[i - 1] = py::cast(args[i]);
164
165                 py::object res = fun_or_class(*params);
166                 /* If I was passed a class, I just built an instance, so I need to call it now */
167                 if (py::isinstance<py::function>(res))
168                   res();
169               } catch (const py::error_already_set& ex) {
170                 if (ex.matches(pyForcefulKillEx)) {
171                   XBT_VERB("Actor killed");
172                   simgrid::ForcefulKillException::do_throw(); // Forward that ForcefulKill exception
173                 }
174                 throw;
175               }
176             });
177           },
178           "Registers the main function of an actor");
179
180   /* Class Netzone */
181   py::class_<simgrid::s4u::NetZone, std::unique_ptr<simgrid::s4u::NetZone, py::nodelete>> netzone(
182       m, "NetZone", "Networking Zones. See the C++ documentation for details.");
183   netzone.def_static("create_full_zone", &simgrid::s4u::create_full_zone, "Creates a zone of type FullZone")
184       .def_static("create_torus_zone", &simgrid::s4u::create_torus_zone, "Creates a cluster of type Torus")
185       .def_static("create_fatTree_zone", &simgrid::s4u::create_fatTree_zone, "Creates a cluster of type Fat-Tree")
186       .def_static("create_dragonfly_zone", &simgrid::s4u::create_dragonfly_zone, "Creates a cluster of type Dragonfly")
187       .def_static("create_star_zone", &simgrid::s4u::create_star_zone, "Creates a zone of type Star")
188       .def_static("create_floyd_zone", &simgrid::s4u::create_floyd_zone, "Creates a zone of type Floyd")
189       .def_static("create_dijkstra_zone", &simgrid::s4u::create_floyd_zone, "Creates a zone of type Dijkstra")
190       .def_static("create_vivaldi_zone", &simgrid::s4u::create_vivaldi_zone, "Creates a zone of type Vivaldi")
191       .def_static("create_empty_zone", &simgrid::s4u::create_empty_zone, "Creates a zone of type Empty")
192       .def_static("create_wifi_zone", &simgrid::s4u::create_wifi_zone, "Creates a zone of type Wi-Fi")
193       .def("add_route",
194            py::overload_cast<simgrid::kernel::routing::NetPoint*, simgrid::kernel::routing::NetPoint*,
195                              simgrid::kernel::routing::NetPoint*, simgrid::kernel::routing::NetPoint*,
196                              const std::vector<simgrid::s4u::LinkInRoute>&, bool>(&simgrid::s4u::NetZone::add_route),
197            "Add a route between 2 netpoints")
198       .def("create_host", py::overload_cast<const std::string&, double>(&simgrid::s4u::NetZone::create_host),
199            "Creates a host")
200       .def("create_host",
201            py::overload_cast<const std::string&, const std::string&>(&simgrid::s4u::NetZone::create_host),
202            "Creates a host")
203       .def("create_host",
204            py::overload_cast<const std::string&, const std::vector<double>&>(&simgrid::s4u::NetZone::create_host),
205            "Creates a host")
206       .def("create_host",
207            py::overload_cast<const std::string&, const std::vector<std::string>&>(&simgrid::s4u::NetZone::create_host),
208            "Creates a host")
209       .def("create_link", py::overload_cast<const std::string&, double>(&simgrid::s4u::NetZone::create_link),
210            "Creates a network link")
211       .def("create_link",
212            py::overload_cast<const std::string&, const std::string&>(&simgrid::s4u::NetZone::create_link),
213            "Creates a network link")
214       .def("create_link",
215            py::overload_cast<const std::string&, const std::vector<double>&>(&simgrid::s4u::NetZone::create_link),
216            "Creates a network link")
217       .def("create_link",
218            py::overload_cast<const std::string&, const std::vector<std::string>&>(&simgrid::s4u::NetZone::create_link),
219            "Creates a network link")
220       .def("create_split_duplex_link",
221            py::overload_cast<const std::string&, double>(&simgrid::s4u::NetZone::create_split_duplex_link),
222            "Creates a split-duplex link")
223       .def("create_split_duplex_link",
224            py::overload_cast<const std::string&, const std::string&>(&simgrid::s4u::NetZone::create_split_duplex_link),
225            "Creates a split-duplex link")
226       .def("create_router", &simgrid::s4u::NetZone::create_router, "Create a router")
227       .def("set_parent", &simgrid::s4u::NetZone::set_parent, "Set the parent of this zone")
228       .def("set_property", &simgrid::s4u::NetZone::set_property, "Add a property to this zone")
229       .def("get_netpoint", &simgrid::s4u::NetZone::get_netpoint, "Retrieve the netpoint associated to this zone")
230       .def("seal", &simgrid::s4u::NetZone::seal, "Seal this NetZone")
231       .def_property_readonly(
232           "name", [](const simgrid::s4u::NetZone* self) { return self->get_name(); }, "The name of this network zone");
233
234   /* Class ClusterCallbacks */
235   py::class_<simgrid::s4u::ClusterCallbacks>(m, "ClusterCallbacks", "Callbacks used to create cluster zones")
236       .def(py::init<const std::function<simgrid::s4u::ClusterCallbacks::ClusterNetPointCb>&,
237                     const std::function<simgrid::s4u::ClusterCallbacks::ClusterLinkCb>&,
238                     const std::function<simgrid::s4u::ClusterCallbacks::ClusterLinkCb>&>());
239
240   /* Class FatTreeParams */
241   py::class_<simgrid::s4u::FatTreeParams>(m, "FatTreeParams", "Parameters to create a Fat-Tree zone")
242       .def(py::init<unsigned int, const std::vector<unsigned int>&, const std::vector<unsigned int>&,
243                     const std::vector<unsigned int>&>());
244
245   /* Class DragonflyParams */
246   py::class_<simgrid::s4u::DragonflyParams>(m, "DragonflyParams", "Parameters to create a Dragonfly zone")
247       .def(py::init<const std::pair<unsigned int, unsigned int>&, const std::pair<unsigned int, unsigned int>&,
248                     const std::pair<unsigned int, unsigned int>&, unsigned int>());
249
250   /* Class Host */
251   py::class_<simgrid::s4u::Host, std::unique_ptr<Host, py::nodelete>> host(
252       m, "Host", "Simulated host. See the C++ documentation for details.");
253   host.def("by_name", &Host::by_name, "Retrieves a host from its name, or die")
254       .def("get_pstate_count", &Host::get_pstate_count, "Retrieve the count of defined pstate levels")
255       .def("get_pstate_speed", &Host::get_pstate_speed, "Retrieve the maximal speed at the given pstate")
256       .def("get_netpoint", &Host::get_netpoint, "Retrieve the netpoint associated to this host")
257       .def("get_disks", &Host::get_disks, "Retrieve the list of disks in this host")
258       .def("set_core_count", &Host::set_core_count, py::call_guard<py::gil_scoped_release>(),
259            "Set the number of cores in the CPU")
260       .def("set_coordinates", &Host::set_coordinates, py::call_guard<py::gil_scoped_release>(),
261            "Set the coordinates of this host")
262       .def("set_sharing_policy", &simgrid::s4u::Host::set_sharing_policy, py::call_guard<py::gil_scoped_release>(),
263            "Describe how the CPU is shared", py::arg("policy"), py::arg("cb") = simgrid::s4u::NonLinearResourceCb())
264       .def("create_disk", py::overload_cast<const std::string&, double, double>(&Host::create_disk),
265            py::call_guard<py::gil_scoped_release>(), "Create a disk")
266       .def("create_disk",
267            py::overload_cast<const std::string&, const std::string&, const std::string&>(&Host::create_disk),
268            py::call_guard<py::gil_scoped_release>(), "Create a disk")
269       .def("seal", &Host::seal, py::call_guard<py::gil_scoped_release>(), "Seal this host")
270       .def_property(
271           "pstate", &Host::get_pstate,
272           [](Host* h, int i) {
273             py::gil_scoped_release gil_guard;
274             h->set_pstate(i);
275           },
276           "The current pstate")
277       .def("current", &Host::current, py::call_guard<py::gil_scoped_release>(),
278            "Retrieves the host on which the running actor is located.")
279       .def_property_readonly(
280           "name",
281           [](const Host* self) {
282             return std::string(self->get_name().c_str()); // Convert from xbt::string because of MC
283           },
284           "The name of this host")
285       .def_property_readonly(
286           "load", &Host::get_load,
287           "Returns the current computation load (in flops per second). This is the currently achieved speed.")
288       .def_property_readonly(
289           "speed", &Host::get_speed,
290           "The peak computing speed in flops/s at the current pstate, taking the external load into account. "
291           "This is the max potential speed.");
292   py::enum_<simgrid::s4u::Host::SharingPolicy>(host, "SharingPolicy")
293       .value("NONLINEAR", simgrid::s4u::Host::SharingPolicy::NONLINEAR)
294       .value("LINEAR", simgrid::s4u::Host::SharingPolicy::LINEAR)
295       .export_values();
296
297   /* Class Disk */
298   py::class_<simgrid::s4u::Disk, std::unique_ptr<simgrid::s4u::Disk, py::nodelete>> disk(
299       m, "Disk", "Simulated disk. See the C++ documentation for details.");
300   disk.def("read", py::overload_cast<sg_size_t, double>(&simgrid::s4u::Disk::read, py::const_),
301            py::call_guard<py::gil_scoped_release>(), "Read data from disk", py::arg("size"), py::arg("priority") = 1)
302       .def("write", py::overload_cast<sg_size_t, double>(&simgrid::s4u::Disk::write, py::const_),
303            py::call_guard<py::gil_scoped_release>(), "Write data in disk", py::arg("size"), py::arg("priority") = 1)
304       .def("read_async", &simgrid::s4u::Disk::read_async, py::call_guard<py::gil_scoped_release>(),
305            "Non-blocking read data from disk")
306       .def("write_async", &simgrid::s4u::Disk::write_async, py::call_guard<py::gil_scoped_release>(),
307            "Non-blocking write data in disk")
308       .def("set_sharing_policy", &simgrid::s4u::Disk::set_sharing_policy, py::call_guard<py::gil_scoped_release>(),
309            "Set sharing policy for this disk", py::arg("op"), py::arg("policy"),
310            py::arg("cb") = simgrid::s4u::NonLinearResourceCb())
311       .def("seal", &simgrid::s4u::Disk::seal, py::call_guard<py::gil_scoped_release>(), "Seal this disk")
312       .def_property_readonly(
313           "name", [](const simgrid::s4u::Disk* self) { return self->get_name(); }, "The name of this disk");
314   py::enum_<simgrid::s4u::Disk::SharingPolicy>(disk, "SharingPolicy")
315       .value("NONLINEAR", simgrid::s4u::Disk::SharingPolicy::NONLINEAR)
316       .value("LINEAR", simgrid::s4u::Disk::SharingPolicy::LINEAR)
317       .export_values();
318   py::enum_<simgrid::s4u::Disk::Operation>(disk, "Operation")
319       .value("READ", simgrid::s4u::Disk::Operation::READ)
320       .value("WRITE", simgrid::s4u::Disk::Operation::WRITE)
321       .value("READWRITE", simgrid::s4u::Disk::Operation::READWRITE)
322       .export_values();
323
324   /* Class NetPoint */
325   py::class_<simgrid::kernel::routing::NetPoint, std::unique_ptr<simgrid::kernel::routing::NetPoint, py::nodelete>>
326       netpoint(m, "NetPoint", "NetPoint object");
327
328   /* Class Link */
329   py::class_<simgrid::s4u::Link, std::unique_ptr<simgrid::s4u::Link, py::nodelete>> link(
330       m, "Link", "Network link. See the C++ documentation for details.");
331   link.def("set_latency", py::overload_cast<const std::string&>(&simgrid::s4u::Link::set_latency),
332            py::call_guard<py::gil_scoped_release>(), "Set the latency")
333       .def("set_latency", py::overload_cast<double>(&simgrid::s4u::Link::set_latency),
334            py::call_guard<py::gil_scoped_release>(), "Set the latency")
335       .def("set_sharing_policy", &simgrid::s4u::Link::set_sharing_policy, py::call_guard<py::gil_scoped_release>(),
336            "Set sharing policy for this link")
337       .def("set_concurrency_limit", &simgrid::s4u::Link::set_concurrency_limit,
338            py::call_guard<py::gil_scoped_release>(), "Set concurrency limit for this link")
339       .def("set_host_wifi_rate", &simgrid::s4u::Link::set_host_wifi_rate, py::call_guard<py::gil_scoped_release>(),
340            "Set level of communication speed of given host on this Wi-Fi link")
341       .def("by_name", &simgrid::s4u::Link::by_name, "Retrieves a Link from its name, or dies")
342       .def("seal", &simgrid::s4u::Link::seal, py::call_guard<py::gil_scoped_release>(), "Seal this link")
343       .def_property_readonly(
344           "name",
345           [](const simgrid::s4u::Link* self) {
346             return std::string(self->get_name().c_str()); // Convert from xbt::string because of MC
347           },
348           "The name of this link");
349   py::enum_<simgrid::s4u::Link::SharingPolicy>(link, "SharingPolicy")
350       .value("NONLINEAR", simgrid::s4u::Link::SharingPolicy::NONLINEAR)
351       .value("WIFI", simgrid::s4u::Link::SharingPolicy::WIFI)
352       .value("SPLITDUPLEX", simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX)
353       .value("SHARED", simgrid::s4u::Link::SharingPolicy::SHARED)
354       .value("FATPIPE", simgrid::s4u::Link::SharingPolicy::FATPIPE)
355       .export_values();
356
357   /* Class LinkInRoute */
358   py::class_<simgrid::s4u::LinkInRoute> linkinroute(m, "LinkInRoute", "Abstraction to add link in routes");
359   linkinroute.def(py::init<const simgrid::s4u::Link*>());
360   linkinroute.def(py::init<const simgrid::s4u::Link*, simgrid::s4u::LinkInRoute::Direction>());
361   py::enum_<simgrid::s4u::LinkInRoute::Direction>(linkinroute, "Direction")
362       .value("UP", simgrid::s4u::LinkInRoute::Direction::UP)
363       .value("DOWN", simgrid::s4u::LinkInRoute::Direction::DOWN)
364       .value("NONE", simgrid::s4u::LinkInRoute::Direction::NONE)
365       .export_values();
366
367   /* Class Split-Duplex Link */
368   py::class_<simgrid::s4u::SplitDuplexLink, simgrid::s4u::Link,
369              std::unique_ptr<simgrid::s4u::SplitDuplexLink, py::nodelete>>(m, "SplitDuplexLink",
370                                                                            "Network split-duplex link")
371       .def("get_link_up", &simgrid::s4u::SplitDuplexLink::get_link_up, "Get link direction up")
372       .def("get_link_down", &simgrid::s4u::SplitDuplexLink::get_link_down, "Get link direction down");
373
374   /* Class Mailbox */
375   py::class_<simgrid::s4u::Mailbox, std::unique_ptr<Mailbox, py::nodelete>>(
376       m, "Mailbox", "Mailbox. See the C++ documentation for details.")
377       .def(
378           "__str__", [](const Mailbox* self) { return std::string("Mailbox(") + self->get_cname() + ")"; },
379           "Textual representation of the Mailbox`")
380       .def("by_name", &Mailbox::by_name, py::call_guard<py::gil_scoped_release>(), "Retrieve a Mailbox from its name")
381       .def_property_readonly(
382           "name",
383           [](const Mailbox* self) {
384             return std::string(self->get_name().c_str()); // Convert from xbt::string because of MC
385           },
386           "The name of that mailbox")
387       .def(
388           "put",
389           [](Mailbox* self, py::object data, int size) {
390             data.inc_ref();
391             self->put(data.ptr(), size);
392           },
393           py::call_guard<py::gil_scoped_release>(), "Blocking data transmission")
394       .def(
395           "put_async",
396           [](Mailbox* self, py::object data, int size) {
397             data.inc_ref();
398             return self->put_async(data.ptr(), size);
399           },
400           py::call_guard<py::gil_scoped_release>(), "Non-blocking data transmission")
401       .def(
402           "get",
403           [](Mailbox* self) {
404             py::object data = pybind11::reinterpret_steal<py::object>(self->get<PyObject>());
405             // data.dec_ref(); // FIXME: why does it break python-actor-create?
406             return data;
407           },
408           py::call_guard<py::gil_scoped_release>(), "Blocking data reception")
409       .def(
410           "get_async",
411           [](Mailbox* self) -> std::tuple<simgrid::s4u::CommPtr, PyGetAsync> {
412             PyGetAsync wrap;
413             auto comm = self->get_async(wrap.get());
414             return std::make_tuple(std::move(comm), std::move(wrap));
415           },
416           py::call_guard<py::gil_scoped_release>(),
417           "Non-blocking data reception. Use data.get() to get the python object after the communication has finished")
418       .def(
419           "set_receiver", [](Mailbox* self, ActorPtr actor) { self->set_receiver(actor); },
420           py::call_guard<py::gil_scoped_release>(), "Sets the actor as permanent receiver");
421
422   /* Class PyGetAsync */
423   py::class_<PyGetAsync>(m, "PyGetAsync", "Wrapper for async get communications")
424       .def(py::init<>())
425       .def(
426           "get", [](const PyGetAsync* self) { return py::reinterpret_steal<py::object>(*(self->get())); },
427           "Get python object after async communication in receiver side");
428
429   /* Class Comm */
430   py::class_<simgrid::s4u::Comm, simgrid::s4u::CommPtr>(m, "Comm",
431                                                         "Communication. See the C++ documentation for details.")
432       .def("test", &simgrid::s4u::Comm::test, py::call_guard<py::gil_scoped_release>(),
433            "Test whether the communication is terminated.")
434       .def("wait", &simgrid::s4u::Comm::wait, py::call_guard<py::gil_scoped_release>(),
435            "Block until the completion of that communication.")
436       // use py::overload_cast for wait_all/wait_any, until the overload marked XBT_ATTRIB_DEPRECATED_v332 is removed
437       .def_static(
438           "wait_all", py::overload_cast<const std::vector<simgrid::s4u::CommPtr>&>(&simgrid::s4u::Comm::wait_all),
439           py::call_guard<py::gil_scoped_release>(), "Block until the completion of all communications in the list.")
440       .def_static(
441           "wait_any", py::overload_cast<const std::vector<simgrid::s4u::CommPtr>&>(&simgrid::s4u::Comm::wait_any),
442           py::call_guard<py::gil_scoped_release>(),
443           "Block until the completion of any communication in the list and return the index of the terminated one.");
444
445   /* Class Io */
446   py::class_<simgrid::s4u::Io, simgrid::s4u::IoPtr>(m, "Io", "I/O activities. See the C++ documentation for details.")
447       .def("test", &simgrid::s4u::Io::test, py::call_guard<py::gil_scoped_release>(),
448            "Test whether the I/O is terminated.")
449       .def("wait", &simgrid::s4u::Io::wait, py::call_guard<py::gil_scoped_release>(),
450            "Block until the completion of that I/O operation")
451       .def_static(
452           "wait_any_for", &simgrid::s4u::Io::wait_any_for, py::call_guard<py::gil_scoped_release>(),
453           "Block until the completion of any I/O in the list (or timeout) and return the index of the terminated one.")
454       .def_static("wait_any", &simgrid::s4u::Io::wait_any, py::call_guard<py::gil_scoped_release>(),
455                   "Block until the completion of any I/O in the list and return the index of the terminated one.");
456
457   /* Class Exec */
458   py::class_<simgrid::s4u::Exec, simgrid::s4u::ExecPtr>(m, "Exec", "Execution. See the C++ documentation for details.")
459       .def_property_readonly(
460           "remaining",
461           [](simgrid::s4u::ExecPtr self) {
462             py::gil_scoped_release gil_guard;
463             return self->get_remaining();
464           },
465           "Amount of flops that remain to be computed until completion.")
466       .def_property_readonly(
467           "remaining_ratio",
468           [](simgrid::s4u::ExecPtr self) {
469             py::gil_scoped_release gil_guard;
470             return self->get_remaining_ratio();
471           },
472           "Amount of work remaining until completion from 0 (completely done) to 1 (nothing done "
473           "yet).")
474       .def_property("host", &simgrid::s4u::Exec::get_host, &simgrid::s4u::Exec::set_host,
475                     "Host on which this execution runs. Only the first host is returned for parallel executions.")
476       .def("test", &simgrid::s4u::Exec::test, py::call_guard<py::gil_scoped_release>(),
477            "Test whether the execution is terminated.")
478       .def("cancel", &simgrid::s4u::Exec::cancel, py::call_guard<py::gil_scoped_release>(), "Cancel that execution.")
479       .def("start", &simgrid::s4u::Exec::start, py::call_guard<py::gil_scoped_release>(), "Start that execution.")
480       .def("wait", &simgrid::s4u::Exec::wait, py::call_guard<py::gil_scoped_release>(),
481            "Block until the completion of that execution.");
482
483   /* Class Actor */
484   py::class_<simgrid::s4u::Actor, ActorPtr>(m, "Actor",
485                                             "An actor is an independent stream of execution in your distributed "
486                                             "application. See the C++ documentation for details.")
487       .def(
488           "create",
489           [](py::str name, Host* h, py::object fun, py::args args) {
490             fun.inc_ref();  // FIXME: why is this needed for tests like exec-async, exec-dvfs and exec-remote?
491             args.inc_ref(); // FIXME: why is this needed for tests like actor-migrate?
492             return simgrid::s4u::Actor::create(name, h, [fun, args]() {
493               try {
494                 py::gil_scoped_acquire py_context;
495                 fun(*args);
496               } catch (const py::error_already_set& ex) {
497                 if (ex.matches(pyForcefulKillEx)) {
498                   XBT_VERB("Actor killed");
499                   simgrid::ForcefulKillException::do_throw(); // Forward that ForcefulKill exception
500                 }
501                 throw;
502               }
503             });
504           },
505           py::call_guard<py::gil_scoped_release>(), "Create an actor from a function or an object. See the :ref:`example <s4u_ex_actors_create>`.")
506       .def_property(
507           "host", &Actor::get_host,
508           [](Actor* a, Host* h) {
509             py::gil_scoped_release gil_guard;
510             a->set_host(h);
511           },
512           "The host on which this actor is located")
513       .def_property_readonly("name", &Actor::get_cname, "The name of this actor.")
514       .def_property_readonly("pid", &Actor::get_pid, "The PID (unique identifier) of this actor.")
515       .def_property_readonly("ppid", &Actor::get_ppid,
516                              "The PID (unique identifier) of the actor that created this one.")
517       .def("by_pid", &Actor::by_pid, "Retrieve an actor by its PID")
518       .def("daemonize", &Actor::daemonize, py::call_guard<py::gil_scoped_release>(),
519            "This actor will be automatically terminated when the last non-daemon actor finishes (more info in the C++ "
520            "documentation).")
521       .def("is_daemon", &Actor::is_daemon,
522            "Returns True if that actor is a daemon and will be terminated automatically when the last non-daemon actor "
523            "terminates.")
524       .def("join", py::overload_cast<double>(&Actor::join, py::const_), py::call_guard<py::gil_scoped_release>(),
525            "Wait for the actor to finish (more info in the C++ documentation).", py::arg("timeout"))
526       .def("kill", &Actor::kill, py::call_guard<py::gil_scoped_release>(), "Kill that actor")
527       .def("kill_all", &Actor::kill_all, py::call_guard<py::gil_scoped_release>(), "Kill all actors but the caller.")
528       .def("self", &Actor::self, "Retrieves the current actor.")
529       .def("is_suspended", &Actor::is_suspended, "Returns True if that actor is currently suspended.")
530       .def("suspend", &Actor::suspend, py::call_guard<py::gil_scoped_release>(),
531            "Suspend that actor, that is blocked until resume()ed by another actor.")
532       .def("resume", &Actor::resume, py::call_guard<py::gil_scoped_release>(),
533            "Resume that actor, that was previously suspend()ed.");
534 }