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(s4u_python, "S4U 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 actor, computing the given amount of flops");
55 m.def("yield_", &simgrid::s4u::this_actor::yield, "Yield the actor");
58 py::class_<Engine>(m, "Engine")
59 .def(py::init([](std::vector<std::string> args) -> simgrid::s4u::Engine* {
60 static char noarg[] = {'\0'};
61 int argc = args.size();
62 std::unique_ptr<char* []> argv(new char*[argc + 1]);
63 for (int i = 0; i != argc; ++i)
64 argv[i] = args[i].empty() ? noarg : &args[i].front();
66 // Currently this can be dangling, we should wrap this somehow.
67 return new simgrid::s4u::Engine(&argc, argv.get());
69 .def("load_platform", &Engine::load_platform, "Load a platform file describing the environment")
70 .def("load_deployment", &Engine::load_deployment, "Load a deployment file and launch the actors that it contains")
71 .def("run", &Engine::run, "Run the simulation")
72 .def("register_function", [](Engine*, std::string name, std::function<void(std::vector<std::string>)> f) {
73 simgrid::simix::register_function(name,
74 [f](std::vector<std::string> args) -> simgrid::simix::ActorCode {
75 return [args, f]() { f(args); };
77 }, "Registers the main function of an actor that will be launched from the deployment file");
79 // Currently, Host lead to segfault:
80 py::class_<simgrid::s4u::Host, std::unique_ptr<Host, py::nodelete>>(m, "Host")
81 .def("by_name", &Host::by_name, "Retrieve an host from its name, or die");
83 py::class_<simgrid::s4u::Actor, ActorPtr>(m, "Actor", "An actor is an independent stream of execution in your distributed application");
85 // Select the right template instantiation
86 simgrid::s4u::ActorPtr (*create_actor)(std::string, Host*, std::function<void()>) = &Actor::create;
88 m.def("create_actor", create_actor, "Create an actor");
89 m.def("create_actor", [](std::string name, Host* host) -> std::function<ActorPtr(std::function<void()>)> {
90 return [name, host](std::function<void()> f) -> ActorPtr {
91 return simgrid::s4u::Actor::create(name, host, std::move(f));
93 }, "Create an actor");