Logo AND Algorithmique Numérique Distribuée

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