6 #include <pybind11/functional.h>
7 #include <pybind11/pybind11.h>
8 #include <pybind11/stl.h>
10 #include <simgrid/config.h>
12 #include <xbt/string.hpp>
14 #include <simgrid/s4u/Actor.hpp>
15 #include <simgrid/s4u/Engine.hpp>
16 #include <simgrid/s4u/Host.hpp>
18 #include <boost/intrusive_ptr.hpp>
20 namespace py = pybind11;
21 using simgrid::s4u::Actor;
22 using simgrid::s4u::ActorPtr;
23 using simgrid::s4u::Engine;
24 using simgrid::s4u::Host;
26 XBT_LOG_NEW_DEFAULT_CATEGORY(python, "python");
28 PYBIND11_DECLARE_HOLDER_TYPE(T, boost::intrusive_ptr<T>);
32 static std::string get_simgrid_version()
34 int major, minor, patch;
35 sg_version_get(&major, &minor, &patch);
36 return simgrid::xbt::string_printf("%i.%i.%i", major, minor, patch);
39 static std::string simgrid_version = get_simgrid_version();
43 PYBIND11_MODULE(simgrid, m)
46 m.doc() = "SimGrid userspace API";
48 m.attr("simgrid_version") = simgrid_version;
50 m.def("info", [](char* s) { XBT_INFO("%s", s); }, "Display a logging message of default priority.");
52 /* this_actor namespace */
53 m.def("execute", py::overload_cast<double>(&simgrid::s4u::this_actor::execute),
54 "Block the current actor, computing the given amount of flops, see :cpp:func:`void simgrid::s4u::this_actor::execute(double)`");
55 m.def("execute", py::overload_cast<double, double>(&simgrid::s4u::this_actor::execute),
56 "Block the current actor, computing the given amount of flops at the given priority, see :cpp:func:`void simgrid::s4u::this_actor::execute(double, double)`");
57 m.def("yield_", &simgrid::s4u::this_actor::yield, "Yield the actor, see :cpp:func:`void simgrid::s4u::this_actor::yield()`");
60 py::class_<Engine>(m, "Engine", "Simulation Engine, see :ref:`class s4u::Engine <API_s4u_Engine>`")
61 .def(py::init([](std::vector<std::string> args) -> simgrid::s4u::Engine* {
62 static char noarg[] = {'\0'};
63 int argc = args.size();
64 std::unique_ptr<char* []> argv(new char*[argc + 1]);
65 for (int i = 0; i != argc; ++i)
66 argv[i] = args[i].empty() ? noarg : &args[i].front();
68 // Currently this can be dangling, we should wrap this somehow.
69 return new simgrid::s4u::Engine(&argc, argv.get());
71 .def("load_platform", &Engine::load_platform,
72 "Load a platform file describing the environment, see :cpp:func:`simgrid::s4u::Engine::load_platform()`")
73 .def("load_deployment", &Engine::load_deployment,
74 "Load a deployment file and launch the actors that it contains, see :cpp:func:`simgrid::s4u::Engine::load_deployment()`")
75 .def("run", &Engine::run, "Run the simulation")
76 .def("register_function", [](Engine*, std::string name, std::function<void(std::vector<std::string>)> f) {
77 simgrid::simix::register_function(name,
78 [f](std::vector<std::string> args) -> simgrid::simix::ActorCode {
79 return [args, f]() { f(args); };
81 }, "Registers the main function of an actor that will be launched from the deployment file, , see :cpp:func:`simgrid::s4u::Engine::register_function()`");
84 py::class_<simgrid::s4u::Host, std::unique_ptr<Host, py::nodelete>>(m, "Host", "Simulation Engine, see :ref:`class s4u::Host <API_s4u_Host>`").def(
85 "by_name", &Host::by_name, "Retrieve a host from its name, or die");
88 // Select the right template instantiation
89 simgrid::s4u::ActorPtr (*create_actor)(std::string, Host*, std::function<void()>) = &Actor::create;
91 py::class_<simgrid::s4u::Actor, ActorPtr>(m, "Actor", ""
92 "An actor is an independent stream of execution in your distributed application, see :ref:`class s4u::Actor <API_s4u_Actor>`")
93 .def("create", create_actor, "Create an actor from a function, see :cpp:func:`simgrid::s4u::Actor::create()`")
94 .def("create", [](std::string name, Host* host) -> std::function<ActorPtr(std::function<void()>)> {
95 return [name, host](std::function<void()> f) -> ActorPtr {
96 return simgrid::s4u::Actor::create(name, host, std::move(f));
98 }, "Create an actor from a functor");