Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 's4u_enforce' into 'master'
[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 <simgrid/Exception.hpp>
28 #include <simgrid/s4u/Actor.hpp>
29 #include <simgrid/s4u/Barrier.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/Mutex.hpp>
38 #include <simgrid/s4u/NetZone.hpp>
39 #include <simgrid/s4u/Semaphore.hpp>
40 #include <simgrid/version.h>
41
42 #include <algorithm>
43 #include <memory>
44 #include <string>
45 #include <vector>
46
47 namespace py = pybind11;
48 using simgrid::s4u::Actor;
49 using simgrid::s4u::ActorPtr;
50 using simgrid::s4u::Barrier;
51 using simgrid::s4u::BarrierPtr;
52 using simgrid::s4u::Comm;
53 using simgrid::s4u::CommPtr;
54 using simgrid::s4u::Engine;
55 using simgrid::s4u::Host;
56 using simgrid::s4u::Link;
57 using simgrid::s4u::Mailbox;
58 using simgrid::s4u::Mutex;
59 using simgrid::s4u::MutexPtr;
60 using simgrid::s4u::Semaphore;
61 using simgrid::s4u::SemaphorePtr;
62
63 XBT_LOG_NEW_DEFAULT_CATEGORY(python, "python");
64
65 namespace {
66
67 std::string get_simgrid_version()
68 {
69   int major;
70   int minor;
71   int patch;
72   sg_version_get(&major, &minor, &patch);
73   return simgrid::xbt::string_printf("%i.%i.%i", major, minor, patch);
74 }
75
76 /** @brief Wrap for mailbox::get_async */
77 class PyGetAsync {
78   std::unique_ptr<PyObject*> data = std::make_unique<PyObject*>();
79
80 public:
81   PyObject** get() const { return data.get(); }
82 };
83
84 } // namespace
85
86 PYBIND11_DECLARE_HOLDER_TYPE(T, boost::intrusive_ptr<T>)
87
88 PYBIND11_MODULE(simgrid, m)
89 {
90   m.doc() = "SimGrid userspace API";
91
92   m.attr("simgrid_version") = get_simgrid_version();
93
94   // Swapped contexts are broken, starting from pybind11 v2.8.0.  Use thread contexts by default.
95   simgrid::s4u::Engine::set_config("contexts/factory:thread");
96
97   // Internal exception used to kill actors and sweep the RAII chimney (free objects living on the stack)
98   static py::object pyForcefulKillEx(py::register_exception<simgrid::ForcefulKillException>(m, "ActorKilled"));
99
100   py::register_exception<simgrid::NetworkFailureException>(m, "NetworkFailureException");
101   py::register_exception<simgrid::TimeoutException>(m, "TimeoutException");
102   py::register_exception<simgrid::HostFailureException>(m, "HostFailureException");
103   py::register_exception<simgrid::StorageFailureException>(m, "StorageFailureException");
104   py::register_exception<simgrid::VmFailureException>(m, "VmFailureException");
105   py::register_exception<simgrid::CancelException>(m, "CancelException");
106   py::register_exception<simgrid::AssertionError>(m, "AssertionError");
107
108   /* this_actor namespace */
109   m.def_submodule("this_actor", "Bindings of the s4u::this_actor namespace. See the C++ documentation for details.")
110       .def(
111           "debug", [](const char* s) { XBT_DEBUG("%s", s); }, "Display a logging message of 'debug' priority.")
112       .def(
113           "info", [](const char* s) { XBT_INFO("%s", s); }, "Display a logging message of 'info' priority.")
114       .def(
115           "warning", [](const char* s) { XBT_WARN("%s", s); }, "Display a logging message of 'warning' priority.")
116       .def(
117           "error", [](const char* s) { XBT_ERROR("%s", s); }, "Display a logging message of 'error' priority.")
118       .def("execute", py::overload_cast<double, double>(&simgrid::s4u::this_actor::execute),
119            py::call_guard<py::gil_scoped_release>(),
120            "Block the current actor, computing the given amount of flops at the given priority.", py::arg("flops"),
121            py::arg("priority") = 1)
122       .def("exec_init", py::overload_cast<double>(&simgrid::s4u::this_actor::exec_init),
123            py::call_guard<py::gil_scoped_release>())
124       .def("exec_async", py::overload_cast<double>(&simgrid::s4u::this_actor::exec_async),
125            py::call_guard<py::gil_scoped_release>())
126       .def("get_host", &simgrid::s4u::this_actor::get_host, "Retrieves host on which the current actor is located")
127       .def("set_host", &simgrid::s4u::this_actor::set_host, py::call_guard<py::gil_scoped_release>(),
128            "Moves the current actor to another host.", py::arg("dest"))
129       .def("sleep_for", static_cast<void (*)(double)>(&simgrid::s4u::this_actor::sleep_for),
130            py::call_guard<py::gil_scoped_release>(), "Block the actor sleeping for that amount of seconds.",
131            py::arg("duration"))
132       .def("sleep_until", static_cast<void (*)(double)>(&simgrid::s4u::this_actor::sleep_until),
133            py::call_guard<py::gil_scoped_release>(), "Block the actor sleeping until the specified timestamp.",
134            py::arg("duration"))
135       .def("suspend", &simgrid::s4u::this_actor::suspend, py::call_guard<py::gil_scoped_release>(),
136            "Suspend the current actor, that is blocked until resume()ed by another actor.")
137       .def("yield_", &simgrid::s4u::this_actor::yield, py::call_guard<py::gil_scoped_release>(), "Yield the actor")
138       .def("exit", &simgrid::s4u::this_actor::exit, py::call_guard<py::gil_scoped_release>(), "kill the current actor")
139       .def(
140           "on_exit",
141           [](py::object cb) {
142             py::function fun = py::reinterpret_borrow<py::function>(cb);
143             fun.inc_ref(); // FIXME: why is this needed for tests like actor-kill and actor-lifetime?
144             simgrid::s4u::this_actor::on_exit([fun](bool failed) {
145               py::gil_scoped_acquire py_context; // need a new context for callback
146               try {
147                 fun(failed);
148               } catch (const py::error_already_set& e) {
149                 xbt_die("Error while executing the on_exit lambda: %s", e.what());
150               }
151             });
152           },
153           py::call_guard<py::gil_scoped_release>(),
154           "Define a lambda to be called when the actor ends. It takes a bool parameter indicating whether the actor "
155           "was killed. If False, the actor finished peacefully.")
156       .def("get_pid", &simgrid::s4u::this_actor::get_pid, "Retrieves PID of the current actor")
157       .def("get_ppid", &simgrid::s4u::this_actor::get_ppid,
158            "Retrieves PPID of the current actor (i.e., the PID of its parent).");
159
160   /* Class Engine */
161   py::class_<Engine>(m, "Engine", "Simulation Engine")
162       .def(py::init([](std::vector<std::string> args) {
163              auto argc = static_cast<int>(args.size());
164              std::vector<char*> argv(args.size() + 1); // argv[argc] is nullptr
165              std::transform(begin(args), end(args), begin(argv), [](std::string& s) { return &s.front(); });
166              // Currently this can be dangling, we should wrap this somehow.
167              return new simgrid::s4u::Engine(&argc, argv.data());
168            }),
169            "The constructor should take the parameters from the command line, as is ")
170       .def_static("get_clock",
171                   []() // XBT_ATTRIB_DEPRECATED_v334
172                   {
173                     PyErr_WarnEx(
174                         PyExc_DeprecationWarning,
175                         "get_clock() is deprecated and  will be dropped after v3.33, use `Engine.clock` instead.", 1);
176                     return Engine::get_clock();
177                   })
178       .def_property_readonly_static(
179           "clock", [](py::object /* self */) { return Engine::get_clock(); },
180           "The simulation time, ie the amount of simulated seconds since the simulation start.")
181       .def_property_readonly_static(
182           "instance", [](py::object /* self */) { return Engine::get_instance(); }, "Retrieve the simulation engine")
183       .def("get_all_hosts",
184            [](py::object self) // XBT_ATTRIB_DEPRECATED_v334
185            {
186              PyErr_WarnEx(PyExc_DeprecationWarning,
187                           "get_all_hosts() is deprecated and  will be dropped after v3.33, use all_hosts instead.", 1);
188              return self.attr("all_hosts");
189            })
190       .def("host_by_name", &Engine::host_by_name_or_null, py::call_guard<py::gil_scoped_release>(),
191            "Retrieve a host by its name, or None if it does not exist in the platform.")
192       .def_property_readonly("all_hosts", &Engine::get_all_hosts, "Returns the list of all hosts found in the platform")
193       .def("get_all_links",
194            [](py::object self) // XBT_ATTRIB_DEPRECATED_v334
195            {
196              PyErr_WarnEx(PyExc_DeprecationWarning,
197                           "get_all_links() is deprecated and  will be dropped after v3.33, use all_links instead.", 1);
198              return self.attr("all_links");
199            })
200       .def_property_readonly("all_links", &Engine::get_all_links, "Returns the list of all links found in the platform")
201       .def("get_all_netpoints",
202            [](py::object self) // XBT_ATTRIB_DEPRECATED_v334
203            {
204              PyErr_WarnEx(
205                  PyExc_DeprecationWarning,
206                  "get_all_netpoints() is deprecated and  will be dropped after v3.33, use all_netpoints instead.", 1);
207              return self.attr("all_netpoints");
208            })
209       .def_property_readonly("all_netpoints", &Engine::get_all_netpoints)
210       .def("get_netzone_root",
211            [](py::object self) // XBT_ATTRIB_DEPRECATED_v334
212            {
213              PyErr_WarnEx(PyExc_DeprecationWarning,
214                           "get_netzone_root() is deprecated and  will be dropped after v3.3, use netzone_root instead.",
215                           1);
216              return self.attr("netzone_root");
217            })
218       .def_property_readonly("netzone_root", &Engine::get_netzone_root,
219                              "Retrieve the root netzone, containing all others.")
220       .def("netpoint_by_name", &Engine::netpoint_by_name_or_null)
221       .def("netzone_by_name", &Engine::netzone_by_name_or_null)
222       .def("load_platform", &Engine::load_platform, "Load a platform file describing the environment")
223       .def("load_deployment", &Engine::load_deployment, "Load a deployment file and launch the actors that it contains")
224       .def("mailbox_by_name_or_create", &Engine::mailbox_by_name_or_create,
225            py::call_guard<py::gil_scoped_release>(),
226            py::arg("name"),
227            "Find a mailbox from its name or create one if it does not exist")
228       .def("run", &Engine::run, py::call_guard<py::gil_scoped_release>(), "Run the simulation until its end")
229       .def("run_until", py::overload_cast<double>(&Engine::run_until, py::const_),
230            py::call_guard<py::gil_scoped_release>(), "Run the simulation until the given date",
231            py::arg("max_date") = -1)
232       .def(
233           "register_actor",
234           [](Engine* e, const std::string& name, py::object fun_or_class) {
235             e->register_actor(name, [fun_or_class](std::vector<std::string> args) {
236               py::gil_scoped_acquire py_context;
237               try {
238                 /* Convert the std::vector into a py::tuple */
239                 py::tuple params(args.size() - 1);
240                 for (size_t i = 1; i < args.size(); i++)
241                   params[i - 1] = py::cast(args[i]);
242
243                 py::object res = fun_or_class(*params);
244                 /* If I was passed a class, I just built an instance, so I need to call it now */
245                 if (py::isinstance<py::function>(res))
246                   res();
247               } catch (const py::error_already_set& ex) {
248                 if (ex.matches(pyForcefulKillEx)) {
249                   XBT_VERB("Actor killed");
250                   simgrid::ForcefulKillException::do_throw(); // Forward that ForcefulKill exception
251                 }
252                 throw;
253               }
254             });
255           },
256           "Registers the main function of an actor");
257
258   /* Class Netzone */
259   py::class_<simgrid::s4u::NetZone, std::unique_ptr<simgrid::s4u::NetZone, py::nodelete>> netzone(
260       m, "NetZone", "Networking Zones. See the C++ documentation for details.");
261   netzone.def_static("create_full_zone", &simgrid::s4u::create_full_zone, "Creates a zone of type FullZone")
262       .def_static("create_torus_zone", &simgrid::s4u::create_torus_zone, "Creates a cluster of type Torus")
263       .def_static("create_fatTree_zone", &simgrid::s4u::create_fatTree_zone, "Creates a cluster of type Fat-Tree")
264       .def_static("create_dragonfly_zone", &simgrid::s4u::create_dragonfly_zone, "Creates a cluster of type Dragonfly")
265       .def_static("create_star_zone", &simgrid::s4u::create_star_zone, "Creates a zone of type Star")
266       .def_static("create_floyd_zone", &simgrid::s4u::create_floyd_zone, "Creates a zone of type Floyd")
267       .def_static("create_dijkstra_zone", &simgrid::s4u::create_floyd_zone, "Creates a zone of type Dijkstra")
268       .def_static("create_vivaldi_zone", &simgrid::s4u::create_vivaldi_zone, "Creates a zone of type Vivaldi")
269       .def_static("create_empty_zone", &simgrid::s4u::create_empty_zone, "Creates a zone of type Empty")
270       .def_static("create_wifi_zone", &simgrid::s4u::create_wifi_zone, "Creates a zone of type Wi-Fi")
271       .def("add_route",
272            py::overload_cast<simgrid::kernel::routing::NetPoint*, simgrid::kernel::routing::NetPoint*,
273                              simgrid::kernel::routing::NetPoint*, simgrid::kernel::routing::NetPoint*,
274                              const std::vector<simgrid::s4u::LinkInRoute>&, bool>(&simgrid::s4u::NetZone::add_route),
275            "Add a route between 2 netpoints")
276       .def("create_host", py::overload_cast<const std::string&, double>(&simgrid::s4u::NetZone::create_host),
277            "Creates a host")
278       .def("create_host",
279            py::overload_cast<const std::string&, const std::string&>(&simgrid::s4u::NetZone::create_host),
280            "Creates a host")
281       .def("create_host",
282            py::overload_cast<const std::string&, const std::vector<double>&>(&simgrid::s4u::NetZone::create_host),
283            "Creates a host")
284       .def("create_host",
285            py::overload_cast<const std::string&, const std::vector<std::string>&>(&simgrid::s4u::NetZone::create_host),
286            "Creates a host")
287       .def("create_link", py::overload_cast<const std::string&, double>(&simgrid::s4u::NetZone::create_link),
288            "Creates a network link")
289       .def("create_link",
290            py::overload_cast<const std::string&, const std::string&>(&simgrid::s4u::NetZone::create_link),
291            "Creates a network link")
292       .def("create_link",
293            py::overload_cast<const std::string&, const std::vector<double>&>(&simgrid::s4u::NetZone::create_link),
294            "Creates a network link")
295       .def("create_link",
296            py::overload_cast<const std::string&, const std::vector<std::string>&>(&simgrid::s4u::NetZone::create_link),
297            "Creates a network link")
298       .def("create_split_duplex_link",
299            py::overload_cast<const std::string&, double>(&simgrid::s4u::NetZone::create_split_duplex_link),
300            "Creates a split-duplex link")
301       .def("create_split_duplex_link",
302            py::overload_cast<const std::string&, const std::string&>(&simgrid::s4u::NetZone::create_split_duplex_link),
303            "Creates a split-duplex link")
304       .def("create_router", &simgrid::s4u::NetZone::create_router, "Create a router")
305       .def("set_parent", &simgrid::s4u::NetZone::set_parent, "Set the parent of this zone")
306       .def("set_property", &simgrid::s4u::NetZone::set_property, "Add a property to this zone")
307       .def("get_netpoint",
308            [](py::object self) // XBT_ATTRIB_DEPRECATED_v334
309            {
310              PyErr_WarnEx(PyExc_DeprecationWarning,
311                           "get_netpoint() is deprecated and  will be dropped after v3.33, use netpoint instead.", 1);
312              return self.attr("netpoint");
313            })
314       .def_property_readonly("netpoint", &simgrid::s4u::NetZone::get_netpoint,
315                              "Retrieve the netpoint associated to this zone")
316       .def("seal", &simgrid::s4u::NetZone::seal, "Seal this NetZone")
317       .def_property_readonly(
318           "name", [](const simgrid::s4u::NetZone* self) { return self->get_name(); },
319           "The name of this network zone (read-only property).");
320
321   /* Class ClusterCallbacks */
322   py::class_<simgrid::s4u::ClusterCallbacks>(m, "ClusterCallbacks", "Callbacks used to create cluster zones")
323       .def(py::init<const std::function<simgrid::s4u::ClusterCallbacks::ClusterNetPointCb>&,
324                     const std::function<simgrid::s4u::ClusterCallbacks::ClusterLinkCb>&,
325                     const std::function<simgrid::s4u::ClusterCallbacks::ClusterLinkCb>&>());
326
327   /* Class FatTreeParams */
328   py::class_<simgrid::s4u::FatTreeParams>(m, "FatTreeParams", "Parameters to create a Fat-Tree zone")
329       .def(py::init<unsigned int, const std::vector<unsigned int>&, const std::vector<unsigned int>&,
330                     const std::vector<unsigned int>&>());
331
332   /* Class DragonflyParams */
333   py::class_<simgrid::s4u::DragonflyParams>(m, "DragonflyParams", "Parameters to create a Dragonfly zone")
334       .def(py::init<const std::pair<unsigned int, unsigned int>&, const std::pair<unsigned int, unsigned int>&,
335                     const std::pair<unsigned int, unsigned int>&, unsigned int>());
336
337   /* Class Host */
338   py::class_<simgrid::s4u::Host, std::unique_ptr<Host, py::nodelete>> host(
339       m, "Host", "Simulated host. See the C++ documentation for details.");
340   host.def_static("by_name", &Host::by_name, py::arg("name"), "Retrieves a host from its name, or die")
341       .def(
342           "route_to",
343           [](const simgrid::s4u::Host* h, const simgrid::s4u::Host* to) {
344             auto* list = new std::vector<Link*>();
345             double bw  = 0;
346             h->route_to(to, *list, &bw);
347             return make_tuple(list, bw);
348           },
349           "Retrieves the list of links and the bandwidth between two hosts.")
350       .def(
351           "set_speed_profile",
352           [](Host* h, const std::string& profile, double period) {
353             h->set_speed_profile(simgrid::kernel::profile::ProfileBuilder::from_string("", profile, period));
354           },
355           py::call_guard<py::gil_scoped_release>(),
356           "Specify a profile modeling the external load according to an exhaustive list. "
357           "Each line of the profile describes timed events as ``date ratio``. "
358           "For example, the following content describes an host which computational speed is initially 1 (i.e, 100%) "
359           "and then halved after 42 seconds:\n\n"
360           ".. code-block:: python\n\n"
361           "   \"\"\"\n"
362           "   0 1.0\n"
363           "   42 0.5\n"
364           "   \"\"\"\n\n"
365           ".. warning:: Don't get fooled: bandwidth and latency profiles of links contain absolute values,"
366           " while speed profiles of hosts contain ratios.\n\n"
367           "The second function parameter is the periodicity: the time to wait after the last event to start again over "
368           "the list. Set it to -1 to not loop over.")
369       .def(
370           "set_state_profile",
371           [](Host* h, const std::string& profile, double period) {
372             h->set_state_profile(simgrid::kernel::profile::ProfileBuilder::from_string("", profile, period));
373           },
374           py::call_guard<py::gil_scoped_release>(),
375           "Specify a profile modeling the churn. "
376           "Each line of the profile describes timed events as ``date boolean``, where the boolean (0 or 1) tells "
377           "whether the host is on. "
378           "For example, the following content describes a link which turns off at t=1 and back on at t=2:\n\n"
379           ".. code-block:: python\n\n"
380           "   \"\"\"\n"
381           "   1.0 0\n"
382           "   2.0 1\n"
383           "   \"\"\"\n\n"
384           "The second function parameter is the periodicity: the time to wait after the last event to start again over "
385           "the list. Set it to -1 to not loop over.")
386       .def("get_pstate_count",
387            [](py::object self) // XBT_ATTRIB_DEPRECATED_v334
388            {
389              PyErr_WarnEx(
390                  PyExc_DeprecationWarning,
391                  "get_pstate_count() is deprecated and  will be dropped after v3.33, use pstate_count instead.", 1);
392              return self.attr("pstate_count");
393            })
394       .def_property_readonly("pstate_count", &Host::get_pstate_count, "Retrieve the count of defined pstate levels")
395       .def("get_pstate_speed",
396            [](py::object self, int state) // XBT_ATTRIB_DEPRECATED_v334
397            {
398              PyErr_WarnEx(
399                  PyExc_DeprecationWarning,
400                  "get_pstate_speed() is deprecated and  will be dropped after v3.33, use pstate_speed instead.", 1);
401              return self.attr("pstate_speed")(state);
402            })
403       .def("pstate_speed", &Host::get_pstate_speed, py::call_guard<py::gil_scoped_release>(),
404            "Retrieve the maximal speed at the given pstate")
405       .def("get_netpoint",
406            [](py::object self) // XBT_ATTRIB_DEPRECATED_v334
407            {
408              PyErr_WarnEx(PyExc_DeprecationWarning,
409                           "get_netpoint() is deprecated and  will be dropped after v3.33, use netpoint instead.", 1);
410              return self.attr("netpoint");
411            })
412       .def_property_readonly("netpoint", &Host::get_netpoint, "Retrieve the netpoint associated to this zone")
413       .def("get_disks", &Host::get_disks, "Retrieve the list of disks in this host")
414       .def("set_core_count",
415            [](py::object self, double count) // XBT_ATTRIB_DEPRECATED_v334
416            {
417              PyErr_WarnEx(PyExc_DeprecationWarning,
418                           "set_core_count() is deprecated and  will be dropped after v3.33, use core_count instead.",
419                           1);
420              self.attr("core_count")(count);
421            })
422       .def_property(
423           "core_count", &Host::get_core_count,
424           [](Host* h, int count) {
425             py::gil_scoped_release gil_guard;
426             return h->set_core_count(count);
427           },
428           "Manage the number of cores in the CPU")
429       .def("set_coordinates", &Host::set_coordinates, py::call_guard<py::gil_scoped_release>(),
430            "Set the coordinates of this host")
431       .def("set_sharing_policy", &simgrid::s4u::Host::set_sharing_policy, py::call_guard<py::gil_scoped_release>(),
432            "Describe how the CPU is shared", py::arg("policy"), py::arg("cb") = simgrid::s4u::NonLinearResourceCb())
433       .def("create_disk", py::overload_cast<const std::string&, double, double>(&Host::create_disk),
434            py::call_guard<py::gil_scoped_release>(), "Create a disk")
435       .def("create_disk",
436            py::overload_cast<const std::string&, const std::string&, const std::string&>(&Host::create_disk),
437            py::call_guard<py::gil_scoped_release>(), "Create a disk")
438       .def("seal", &Host::seal, py::call_guard<py::gil_scoped_release>(), "Seal this host")
439       .def_property(
440           "pstate", &Host::get_pstate,
441           [](Host* h, int i) {
442             py::gil_scoped_release gil_guard;
443             h->set_pstate(i);
444           },
445           "The current pstate (read/write property).")
446       .def_static("current", &Host::current, py::call_guard<py::gil_scoped_release>(),
447            "Retrieves the host on which the running actor is located.")
448       .def_property_readonly(
449           "name",
450           [](const Host* self) {
451             return std::string(self->get_name().c_str()); // Convert from xbt::string because of MC
452           },
453           "The name of this host (read-only property).")
454       .def_property_readonly("load", &Host::get_load,
455                              "Returns the current computation load (in flops per second), NOT taking the external load "
456                              "into account. This is the currently achieved speed (read-only property).")
457       .def_property_readonly(
458           "speed", &Host::get_speed,
459           "The peak computing speed in flops/s at the current pstate, NOT taking the external load into account. "
460           "This is the max potential speed (read-only property).")
461       .def_property_readonly("available_speed", &Host::get_available_speed,
462                              "Get the available speed ratio, between 0 and 1.\n"
463                              "This accounts for external load (see :py:func:`set_speed_profile() "
464                              "<simgrid.Host.set_speed_profile>`) (read-only property).")
465       .def_static(
466           "on_creation_cb",
467           [](py::object cb) {
468             Host::on_creation_cb([cb](Host& h) {
469               py::function fun = py::reinterpret_borrow<py::function>(cb);
470               py::gil_scoped_acquire py_context; // need a new context for callback
471               try {
472                 fun(&h);
473               } catch (const py::error_already_set& e) {
474                 xbt_die("Error while executing the on_creation lambda : %s", e.what());
475               }
476             });
477           },
478           py::call_guard<py::gil_scoped_release>(), "");
479
480   py::enum_<simgrid::s4u::Host::SharingPolicy>(host, "SharingPolicy")
481       .value("NONLINEAR", simgrid::s4u::Host::SharingPolicy::NONLINEAR)
482       .value("LINEAR", simgrid::s4u::Host::SharingPolicy::LINEAR)
483       .export_values();
484
485   /* Class Disk */
486   py::class_<simgrid::s4u::Disk, std::unique_ptr<simgrid::s4u::Disk, py::nodelete>> disk(
487       m, "Disk", "Simulated disk. See the C++ documentation for details.");
488   disk.def("read", py::overload_cast<sg_size_t, double>(&simgrid::s4u::Disk::read, py::const_),
489            py::call_guard<py::gil_scoped_release>(), "Read data from disk", py::arg("size"), py::arg("priority") = 1)
490       .def("write", py::overload_cast<sg_size_t, double>(&simgrid::s4u::Disk::write, py::const_),
491            py::call_guard<py::gil_scoped_release>(), "Write data in disk", py::arg("size"), py::arg("priority") = 1)
492       .def("read_async", &simgrid::s4u::Disk::read_async, py::call_guard<py::gil_scoped_release>(),
493            "Non-blocking read data from disk")
494       .def("write_async", &simgrid::s4u::Disk::write_async, py::call_guard<py::gil_scoped_release>(),
495            "Non-blocking write data in disk")
496       .def("set_sharing_policy", &simgrid::s4u::Disk::set_sharing_policy, py::call_guard<py::gil_scoped_release>(),
497            "Set sharing policy for this disk", py::arg("op"), py::arg("policy"),
498            py::arg("cb") = simgrid::s4u::NonLinearResourceCb())
499       .def("seal", &simgrid::s4u::Disk::seal, py::call_guard<py::gil_scoped_release>(), "Seal this disk")
500       .def_property_readonly(
501           "name", [](const simgrid::s4u::Disk* self) { return self->get_name(); },
502           "The name of this disk (read-only property).");
503   py::enum_<simgrid::s4u::Disk::SharingPolicy>(disk, "SharingPolicy")
504       .value("NONLINEAR", simgrid::s4u::Disk::SharingPolicy::NONLINEAR)
505       .value("LINEAR", simgrid::s4u::Disk::SharingPolicy::LINEAR)
506       .export_values();
507   py::enum_<simgrid::s4u::Disk::Operation>(disk, "Operation")
508       .value("READ", simgrid::s4u::Disk::Operation::READ)
509       .value("WRITE", simgrid::s4u::Disk::Operation::WRITE)
510       .value("READWRITE", simgrid::s4u::Disk::Operation::READWRITE)
511       .export_values();
512
513   /* Class NetPoint */
514   py::class_<simgrid::kernel::routing::NetPoint, std::unique_ptr<simgrid::kernel::routing::NetPoint, py::nodelete>>
515       netpoint(m, "NetPoint", "NetPoint object");
516
517   /* Class Link */
518   py::class_<Link, std::unique_ptr<Link, py::nodelete>> link(m, "Link",
519                                                              "Network link. See the C++ documentation for details.");
520   link.def("set_latency", py::overload_cast<const std::string&>(&Link::set_latency),
521            py::call_guard<py::gil_scoped_release>(),
522            "Set the latency as a string. Accepts values with units, such as â€˜1s’ or â€˜7ms’.\nFull list of accepted "
523            "units: w (week), d (day), h, s, ms, us, ns, ps.")
524       .def("set_latency", py::overload_cast<double>(&Link::set_latency), py::call_guard<py::gil_scoped_release>(),
525            "Set the latency as a float (in seconds).")
526       .def("set_bandwidth", &Link::set_bandwidth, py::call_guard<py::gil_scoped_release>(),
527            "Set the bandwidth (in byte per second).")
528       .def(
529           "set_bandwidth_profile",
530           [](Link* l, const std::string& profile, double period) {
531             l->set_bandwidth_profile(simgrid::kernel::profile::ProfileBuilder::from_string("", profile, period));
532           },
533           py::call_guard<py::gil_scoped_release>(),
534           "Specify a profile modeling the external load according to an exhaustive list. "
535           "Each line of the profile describes timed events as ``date bandwidth`` (in bytes per second). "
536           "For example, the following content describes a link which bandwidth changes to 40 Mb/s at t=4, and to 6 "
537           "Mb/s at t=8:\n\n"
538           ".. code-block:: python\n\n"
539           "   \"\"\"\n"
540           "   4.0 40000000\n"
541           "   8.0 60000000\n"
542           "   \"\"\"\n\n"
543           ".. warning:: Don't get fooled: bandwidth and latency profiles of links contain absolute values,"
544           " while speed profiles of hosts contain ratios.\n\n"
545           "The second function parameter is the periodicity: the time to wait after the last event to start again over "
546           "the list. Set it to -1 to not loop over.")
547       .def(
548           "set_latency_profile",
549           [](Link* l, const std::string& profile, double period) {
550             l->set_latency_profile(simgrid::kernel::profile::ProfileBuilder::from_string("", profile, period));
551           },
552           py::call_guard<py::gil_scoped_release>(),
553           "Specify a profile modeling the external load according to an exhaustive list. "
554           "Each line of the profile describes timed events as ``date latency`` (in seconds). "
555           "For example, the following content describes a link which latency changes to 1ms (0.001s) at t=1, and to 2s "
556           "at t=2:\n\n"
557           ".. code-block:: python\n\n"
558           "   \"\"\"\n"
559           "   1.0 0.001\n"
560           "   2.0 2\n"
561           "   \"\"\"\n\n"
562           ".. warning:: Don't get fooled: bandwidth and latency profiles of links contain absolute values,"
563           " while speed profiles of hosts contain ratios.\n\n"
564           "The second function parameter is the periodicity: the time to wait after the last event to start again over "
565           "the list. Set it to -1 to not loop over.")
566       .def(
567           "set_state_profile",
568           [](Link* l, const std::string& profile, double period) {
569             l->set_state_profile(simgrid::kernel::profile::ProfileBuilder::from_string("", profile, period));
570           },
571           "Specify a profile modeling the churn. "
572           "Each line of the profile describes timed events as ``date boolean``, where the boolean (0 or 1) tells "
573           "whether the link is on. "
574           "For example, the following content describes a link which turns off at t=1 and back on at t=2:\n\n"
575           ".. code-block:: python\n\n"
576           "   \"\"\"\n"
577           "   1.0 0\n"
578           "   2.0 1\n"
579           "   \"\"\"\n\n"
580           "The second function parameter is the periodicity: the time to wait after the last event to start again over "
581           "the list. Set it to -1 to not loop over.")
582
583       .def("turn_on", &Link::turn_on, py::call_guard<py::gil_scoped_release>(), "Turns the link on.")
584       .def("turn_off", &Link::turn_off, py::call_guard<py::gil_scoped_release>(), "Turns the link off.")
585       .def("is_on", &Link::is_on, "Check whether the link is on.")
586
587       .def("set_sharing_policy", &Link::set_sharing_policy, py::call_guard<py::gil_scoped_release>(),
588            "Set sharing policy for this link")
589       .def("set_concurrency_limit", &Link::set_concurrency_limit, py::call_guard<py::gil_scoped_release>(),
590            "Set concurrency limit for this link")
591       .def("set_host_wifi_rate", &Link::set_host_wifi_rate, py::call_guard<py::gil_scoped_release>(),
592            "Set level of communication speed of given host on this Wi-Fi link")
593       .def_static("by_name", &Link::by_name, "Retrieves a Link from its name, or dies")
594       .def("seal", &Link::seal, py::call_guard<py::gil_scoped_release>(), "Seal this link")
595       .def_property_readonly(
596           "name",
597           [](const Link* self) {
598             return std::string(self->get_name().c_str()); // Convert from xbt::string because of MC
599           },
600           "The name of this link")
601       .def_property_readonly("bandwidth", &Link::get_bandwidth,
602                              "The bandwidth (in bytes per second) (read-only property).")
603       .def_property_readonly("latency", &Link::get_latency, "The latency (in seconds) (read-only property).");
604
605   py::enum_<Link::SharingPolicy>(link, "SharingPolicy")
606       .value("NONLINEAR", Link::SharingPolicy::NONLINEAR)
607       .value("WIFI", Link::SharingPolicy::WIFI)
608       .value("SPLITDUPLEX", Link::SharingPolicy::SPLITDUPLEX)
609       .value("SHARED", Link::SharingPolicy::SHARED)
610       .value("FATPIPE", Link::SharingPolicy::FATPIPE)
611       .export_values();
612
613   /* Class LinkInRoute */
614   py::class_<simgrid::s4u::LinkInRoute> linkinroute(m, "LinkInRoute", "Abstraction to add link in routes");
615   linkinroute.def(py::init<const Link*>());
616   linkinroute.def(py::init<const Link*, simgrid::s4u::LinkInRoute::Direction>());
617   py::enum_<simgrid::s4u::LinkInRoute::Direction>(linkinroute, "Direction")
618       .value("UP", simgrid::s4u::LinkInRoute::Direction::UP)
619       .value("DOWN", simgrid::s4u::LinkInRoute::Direction::DOWN)
620       .value("NONE", simgrid::s4u::LinkInRoute::Direction::NONE)
621       .export_values();
622
623   /* Class Split-Duplex Link */
624   py::class_<simgrid::s4u::SplitDuplexLink, Link, std::unique_ptr<simgrid::s4u::SplitDuplexLink, py::nodelete>>(
625       m, "SplitDuplexLink", "Network split-duplex link")
626       .def("get_link_up",
627            [](py::object self) // XBT_ATTRIB_DEPRECATED_v334
628            {
629              PyErr_WarnEx(PyExc_DeprecationWarning,
630                           "get_link_up() is deprecated and  will be dropped after v3.33, use link_up instead.", 1);
631              return self.attr("link_up");
632            })
633       .def_property_readonly("link_up", &simgrid::s4u::SplitDuplexLink::get_link_up, "Get link direction up")
634       .def("get_link_down",
635            [](py::object self) // XBT_ATTRIB_DEPRECATED_v334
636            {
637              PyErr_WarnEx(PyExc_DeprecationWarning,
638                           "get_link_down() is deprecated and  will be dropped after v3.33, use link_down instead.", 1);
639              return self.attr("link_down");
640            })
641       .def_property_readonly("link_down", &simgrid::s4u::SplitDuplexLink::get_link_down, "Get link direction down");
642
643   /* Class Mailbox */
644   py::class_<simgrid::s4u::Mailbox, std::unique_ptr<Mailbox, py::nodelete>>(
645       m, "Mailbox", "Mailbox. See the C++ documentation for details.")
646       .def(
647           "__str__", [](const Mailbox* self) { return std::string("Mailbox(") + self->get_cname() + ")"; },
648           "Textual representation of the Mailbox`")
649       .def_static("by_name", &Mailbox::by_name,
650                   py::call_guard<py::gil_scoped_release>(),
651                   py::arg("name"),
652                   "Retrieve a Mailbox from its name")
653       .def_property_readonly(
654           "name",
655           [](const Mailbox* self) {
656             return std::string(self->get_name().c_str()); // Convert from xbt::string because of MC
657           },
658           "The name of that mailbox (read-only property).")
659       .def_property_readonly("ready", &Mailbox::ready, py::call_guard<py::gil_scoped_release>(),
660                              "Check if there is a communication ready to be consumed from a mailbox.")
661       .def(
662           "put",
663           [](Mailbox* self, py::object data, uint64_t size, double timeout) {
664             data.inc_ref();
665             self->put(data.ptr(), size, timeout);
666           },
667           py::call_guard<py::gil_scoped_release>(), "Blocking data transmission with a timeout")
668       .def(
669           "put",
670           [](Mailbox* self, py::object data, uint64_t size) {
671             data.inc_ref();
672             self->put(data.ptr(), size);
673           },
674           py::call_guard<py::gil_scoped_release>(), "Blocking data transmission")
675       .def(
676           "put_async",
677           [](Mailbox* self, py::object data, uint64_t size) {
678             data.inc_ref();
679             return self->put_async(data.ptr(), size);
680           },
681           py::call_guard<py::gil_scoped_release>(), "Non-blocking data transmission")
682       .def(
683           "put_init",
684           [](Mailbox* self, py::object data, uint64_t size) {
685             data.inc_ref();
686             return self->put_init(data.ptr(), size);
687           },
688           py::call_guard<py::gil_scoped_release>(),
689           "Creates (but don’t start) a data transmission to that mailbox.")
690       .def(
691           "get",
692           [](Mailbox* self) {
693             py::object data = py::reinterpret_steal<py::object>(self->get<PyObject>());
694             // data.dec_ref(); // FIXME: why does it break python-actor-create?
695             return data;
696           },
697           py::call_guard<py::gil_scoped_release>(), "Blocking data reception")
698       .def(
699           "get_async",
700           [](Mailbox* self) -> std::tuple<CommPtr, PyGetAsync> {
701             PyGetAsync wrap;
702             auto comm = self->get_async(wrap.get());
703             return std::make_tuple(std::move(comm), std::move(wrap));
704           },
705           py::call_guard<py::gil_scoped_release>(),
706           "Non-blocking data reception. Use data.get() to get the python object after the communication has finished")
707       .def(
708           "set_receiver", [](Mailbox* self, ActorPtr actor) { self->set_receiver(actor); },
709           py::call_guard<py::gil_scoped_release>(), "Sets the actor as permanent receiver");
710
711   /* Class PyGetAsync */
712   py::class_<PyGetAsync>(m, "PyGetAsync", "Wrapper for async get communications")
713       .def(py::init<>())
714       .def(
715           "get", [](const PyGetAsync* self) { return py::reinterpret_steal<py::object>(*(self->get())); },
716           "Get python object after async communication in receiver side");
717
718   /* Class Comm */
719   py::class_<Comm, CommPtr>(m, "Comm", "Communication. See the C++ documentation for details.")
720       .def_property_readonly("dst_data_size", &Comm::get_dst_data_size,
721                              py::call_guard<py::gil_scoped_release>(),
722                              "Retrieve the size of the received data.")
723       .def_property_readonly("mailbox", &Comm::get_mailbox,
724                              py::call_guard<py::gil_scoped_release>(),
725                              "Retrieve the mailbox on which this comm acts.")
726       .def_property_readonly("sender", &Comm::get_sender,
727                              py::call_guard<py::gil_scoped_release>())
728       .def_property_readonly("state_str", [](const Comm* self){ return std::string(self->get_state_str()); },
729                              py::call_guard<py::gil_scoped_release>(),
730                              "Retrieve the Comm state as string")
731       .def_property_readonly("remaining",  &Comm::get_remaining,
732                              py::call_guard<py::gil_scoped_release>(),
733                              "Remaining amount of work that this Comm entails")
734       .def_property_readonly("start_time",  &Comm::get_start_time,
735                              py::call_guard<py::gil_scoped_release>(),
736                              "Time at which this Comm started")
737       .def_property_readonly("finish_time",  &Comm::get_finish_time,
738                              py::call_guard<py::gil_scoped_release>(),
739                              "Time at which this Comm finished")
740       .def("set_payload_size", &Comm::set_payload_size, py::call_guard<py::gil_scoped_release>(),
741            py::arg("bytes"),
742            "Specify the amount of bytes which exchange should be simulated.")
743       .def("set_rate", &Comm::set_rate, py::call_guard<py::gil_scoped_release>(),
744            py::arg("rate"),
745            "Sets the maximal communication rate (in byte/sec). Must be done before start")
746       .def("cancel", [](Comm* self){ return self->cancel(); },
747            py::call_guard<py::gil_scoped_release>(), py::return_value_policy::reference_internal,
748            "Cancel the activity.")
749       .def("start", [](Comm* self){ return self->start(); },
750            py::call_guard<py::gil_scoped_release>(), py::return_value_policy::reference_internal,
751            "Starts a previously created activity. This function is optional: you can call wait() even if you didn't "
752            "call start()")
753       .def("suspend", [](Comm* self){ return self->suspend(); },
754           py::call_guard<py::gil_scoped_release>(), py::return_value_policy::reference_internal,
755           "Suspend the activity.")
756       .def("resume", [](Comm* self){ return self->resume(); },
757           py::call_guard<py::gil_scoped_release>(), py::return_value_policy::reference_internal,
758           "Resume the activity.")
759       .def("test", &Comm::test, py::call_guard<py::gil_scoped_release>(),
760            "Test whether the communication is terminated.")
761       .def("wait", &Comm::wait, py::call_guard<py::gil_scoped_release>(),
762            "Block until the completion of that communication.")
763       .def("wait_for", &Comm::wait_for, py::call_guard<py::gil_scoped_release>(),
764            py::arg("timeout"),
765            "Block until the completion of that communication, or raises TimeoutException after the specified timeout.")
766       .def("wait_until", &Comm::wait_until, py::call_guard<py::gil_scoped_release>(),
767            py::arg("time_limit"),
768            "Block until the completion of that communication, or raises TimeoutException after the specified time.")
769       .def("detach", [](Comm* self) { return self->detach(); },
770            py::return_value_policy::reference_internal,
771            py::call_guard<py::gil_scoped_release>(),
772            "Start the comm, and ignore its result. It can be completely forgotten after that.")
773       .def_static("sendto", &Comm::sendto, py::call_guard<py::gil_scoped_release>(),
774                   py::arg("from"), py::arg("to"), py::arg("simulated_size_in_bytes"),
775                   "Do a blocking communication between two arbitrary hosts.")
776       .def_static("sendto_init", py::overload_cast<Host*, Host*>(&Comm::sendto_init),
777                   py::call_guard<py::gil_scoped_release>(),
778                   py::arg("from"), py::arg("to"),
779                   "Creates a communication between the two given hosts, bypassing the mailbox mechanism.")
780       .def_static("sendto_async", &Comm::sendto_async, py::call_guard<py::gil_scoped_release>(),
781                   py::arg("from"), py::arg("to"), py::arg("simulated_size_in_bytes"),
782                   "Do a blocking communication between two arbitrary hosts.\n\nThis initializes a communication that "
783                   "completely bypass the mailbox and actors mechanism. There is really no limit on the hosts involved. "
784                   "In particular, the actor does not have to be on one of the involved hosts.")
785       .def_static("test_any", &Comm::test_any,
786                   py::call_guard<py::gil_scoped_release>(),
787                   py::arg("comms"),
788                   "take a vector s4u::CommPtr and return the rank of the first finished one (or -1 if none is done)")
789       .def_static("wait_all", &Comm::wait_all, py::call_guard<py::gil_scoped_release>(),
790                   py::arg("comms"),
791                   "Block until the completion of all communications in the list.")
792       .def_static("wait_all_for", &Comm::wait_all_for, py::call_guard<py::gil_scoped_release>(),
793                   py::arg("comms"), py::arg("timeout"),
794                   "Block until the completion of all communications in the list, or raises TimeoutException after "
795                   "the specified timeout.")
796       .def_static("wait_any", &Comm::wait_any,
797                   py::call_guard<py::gil_scoped_release>(),
798                   py::arg("comms"),
799                   "Block until the completion of any communication in the list and return the index of the "
800                   "terminated one.")
801       .def_static("wait_any_for", &Comm::wait_any_for,
802                   py::call_guard<py::gil_scoped_release>(),
803                   py::arg("comms"), py::arg("timeout"),
804                   "Block until the completion of any communication in the list and return the index of the terminated "
805                   "one, or -1 if a timeout occurred.");
806
807   /* Class Io */
808   py::class_<simgrid::s4u::Io, simgrid::s4u::IoPtr>(m, "Io", "I/O activities. See the C++ documentation for details.")
809       .def("test", &simgrid::s4u::Io::test, py::call_guard<py::gil_scoped_release>(),
810            "Test whether the I/O is terminated.")
811       .def("wait", &simgrid::s4u::Io::wait, py::call_guard<py::gil_scoped_release>(),
812            "Block until the completion of that I/O operation")
813       .def_static(
814           "wait_any_for", &simgrid::s4u::Io::wait_any_for, py::call_guard<py::gil_scoped_release>(),
815           "Block until the completion of any I/O in the list (or timeout) and return the index of the terminated one.")
816       .def_static("wait_any", &simgrid::s4u::Io::wait_any, py::call_guard<py::gil_scoped_release>(),
817                   "Block until the completion of any I/O in the list and return the index of the terminated one.");
818
819   /* Class Exec */
820   py::class_<simgrid::s4u::Exec, simgrid::s4u::ExecPtr>(m, "Exec", "Execution. See the C++ documentation for details.")
821       .def_property_readonly(
822           "remaining",
823           [](simgrid::s4u::ExecPtr self) {
824             py::gil_scoped_release gil_guard;
825             return self->get_remaining();
826           },
827           "Amount of flops that remain to be computed until completion (read-only property).")
828       .def_property_readonly(
829           "remaining_ratio",
830           [](simgrid::s4u::ExecPtr self) {
831             py::gil_scoped_release gil_guard;
832             return self->get_remaining_ratio();
833           },
834           "Amount of work remaining until completion from 0 (completely done) to 1 (nothing done "
835           "yet) (read-only property).")
836       .def_property("host", &simgrid::s4u::Exec::get_host, &simgrid::s4u::Exec::set_host,
837                     "Host on which this execution runs. Only the first host is returned for parallel executions. "
838                     "Changing this value migrates the execution.")
839       .def("test", &simgrid::s4u::Exec::test, py::call_guard<py::gil_scoped_release>(),
840            "Test whether the execution is terminated.")
841       .def("cancel", &simgrid::s4u::Exec::cancel, py::call_guard<py::gil_scoped_release>(), "Cancel that execution.")
842       .def("start", &simgrid::s4u::Exec::start, py::call_guard<py::gil_scoped_release>(), "Start that execution.")
843       .def("wait", &simgrid::s4u::Exec::wait, py::call_guard<py::gil_scoped_release>(),
844            "Block until the completion of that execution.");
845
846   /* Class Semaphore */
847   py::class_<Semaphore, SemaphorePtr>(m, "Semaphore",
848                                       "A classical semaphore, but blocking in the simulation world. See the C++ "
849                                       "documentation for details.")
850       .def(py::init<>(&Semaphore::create), py::call_guard<py::gil_scoped_release>(), py::arg("capacity"),
851            "Semaphore constructor.")
852       .def("acquire", &Semaphore::acquire, py::call_guard<py::gil_scoped_release>(),
853            "Acquire on the semaphore object with no timeout. Blocks until the semaphore is acquired.")
854       .def("acquire_timeout", &Semaphore::acquire_timeout, py::call_guard<py::gil_scoped_release>(), py::arg("timeout"),
855            "Acquire on the semaphore object with no timeout. Blocks until the semaphore is acquired or return "
856            "true if it has not been acquired after the specified timeout.")
857       .def("release", &Semaphore::release, py::call_guard<py::gil_scoped_release>(),
858            "Release the semaphore.")
859       .def_property_readonly("capacity", &Semaphore::get_capacity, py::call_guard<py::gil_scoped_release>(),
860                              "Get the semaphore capacity.")
861       .def_property_readonly("would_block", &Semaphore::would_block, py::call_guard<py::gil_scoped_release>(),
862                              "Check whether trying to acquire the semaphore would block (in other word, checks whether "
863                              "this semaphore has capacity).")
864       // Allow semaphores to be automatically acquired/released with a context manager: `with semaphore: ...`
865       .def("__enter__", [](Semaphore* self){ self->acquire(); }, py::call_guard<py::gil_scoped_release>())
866       .def("__exit__", [](Semaphore* self){ self->release(); }, py::call_guard<py::gil_scoped_release>());
867
868   /* Class Mutex */
869   py::class_<Mutex, MutexPtr>(m, "Mutex",
870                               "A classical mutex, but blocking in the simulation world."
871                               "See the C++ documentation for details.")
872       .def(py::init<>(&Mutex::create), py::call_guard<py::gil_scoped_release>(), "Mutex constructor.")
873       .def("lock", &Mutex::lock, py::call_guard<py::gil_scoped_release>(), "Block until the mutex is acquired.")
874       .def("try_lock", &Mutex::try_lock, py::call_guard<py::gil_scoped_release>(),
875            "Try to acquire the mutex. Return true if the mutex was acquired, false otherwise.")
876       .def("unlock", &Mutex::unlock, py::call_guard<py::gil_scoped_release>(), "Release the mutex.")
877       // Allow mutexes to be automatically acquired/released with a context manager: `with mutex: ...`
878       .def("__enter__", [](Mutex* self){ self->lock(); }, py::call_guard<py::gil_scoped_release>())
879       .def("__exit__", [](Mutex* self, const py::object&, const py::object&, const py::object&) { self->unlock(); },
880           py::call_guard<py::gil_scoped_release>());
881
882   /* Class Barrier */
883   py::class_<Barrier, BarrierPtr>(m, "Barrier",
884                                   "A classical barrier, but blocking in the simulation world.")
885       .def(py::init<>(&Barrier::create), py::call_guard<py::gil_scoped_release>(), py::arg("expected_actors"),
886            "Barrier constructor.")
887       .def("wait", &Barrier::wait, py::call_guard<py::gil_scoped_release>(),
888            "Blocks into the barrier. Every waiting actors will be unlocked once the expected amount of actors reaches "
889            "the barrier.");
890
891   /* Class Actor */
892   py::class_<simgrid::s4u::Actor, ActorPtr>(m, "Actor",
893                                             "An actor is an independent stream of execution in your distributed "
894                                             "application. See the C++ documentation for details.")
895       .def(
896           "create",
897           [](py::str name, Host* h, py::object fun, py::args args) {
898             fun.inc_ref();  // FIXME: why is this needed for tests like exec-async, exec-dvfs and exec-remote?
899             args.inc_ref(); // FIXME: why is this needed for tests like actor-migrate?
900             return simgrid::s4u::Actor::create(name, h, [fun, args]() {
901               py::gil_scoped_acquire py_context;
902               try {
903                 fun(*args);
904               } catch (const py::error_already_set& ex) {
905                 if (ex.matches(pyForcefulKillEx)) {
906                   XBT_VERB("Actor killed");
907                   simgrid::ForcefulKillException::do_throw(); // Forward that ForcefulKill exception
908                 }
909                 throw;
910               }
911             });
912           },
913           py::call_guard<py::gil_scoped_release>(),
914           "Create an actor from a function or an object. See the :ref:`example <s4u_ex_actors_create>`.")
915       .def_property(
916           "host", &Actor::get_host,
917           [](Actor* a, Host* h) {
918             py::gil_scoped_release gil_guard;
919             a->set_host(h);
920           },
921           "The host on which this actor is located. Changing this value migrates the actor.\n\n"
922           "If the actor is currently blocked on an execution activity, the activity is also migrated to the new host. "
923           "If it’s blocked on another kind of activity, an error is raised as the mandated code is not written yet. "
924           "Please report that bug if you need it.\n\n"
925           "Asynchronous activities started by the actor are not migrated automatically, so you have to take care of "
926           "this yourself (only you knows which ones should be migrated). ")
927       .def_property_readonly("name", &Actor::get_cname, "The name of this actor (read-only property).")
928       .def_property_readonly("pid", &Actor::get_pid, "The PID (unique identifier) of this actor (read-only property).")
929       .def_property_readonly("ppid", &Actor::get_ppid,
930                              "The PID (unique identifier) of the actor that created this one (read-only property).")
931       .def_static("by_pid", &Actor::by_pid, py::arg("pid"), "Retrieve an actor by its PID")
932       .def("set_auto_restart", &Actor::set_auto_restart, py::call_guard<py::gil_scoped_release>(),
933            "Specify whether the actor shall restart when its host reboots.")
934       .def("daemonize", &Actor::daemonize, py::call_guard<py::gil_scoped_release>(),
935            "This actor will be automatically terminated when the last non-daemon actor finishes (more info in the C++ "
936            "documentation).")
937       .def("is_daemon", &Actor::is_daemon,
938            "Returns True if that actor is a daemon and will be terminated automatically when the last non-daemon actor "
939            "terminates.")
940       .def("join", py::overload_cast<double>(&Actor::join, py::const_), py::call_guard<py::gil_scoped_release>(),
941            "Wait for the actor to finish (more info in the C++ documentation).", py::arg("timeout") = -1)
942       .def("kill", &Actor::kill, py::call_guard<py::gil_scoped_release>(), "Kill that actor")
943       .def("self", &Actor::self, "Retrieves the current actor.")
944       .def("is_suspended", &Actor::is_suspended, "Returns True if that actor is currently suspended.")
945       .def("suspend", &Actor::suspend, py::call_guard<py::gil_scoped_release>(),
946            "Suspend that actor, that is blocked until resume()ed by another actor.")
947       .def("resume", &Actor::resume, py::call_guard<py::gil_scoped_release>(),
948            "Resume that actor, that was previously suspend()ed.")
949       .def_static("kill_all", &Actor::kill_all, py::call_guard<py::gil_scoped_release>(), "Kill all actors but the caller.");
950 }