Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4a9fd8759ef4de59b10830687998e7deb89dff9e
[simgrid.git] / src / bindings / python / simgrid_python.cpp
1 #include <functional>
2 #include <memory>
3 #include <string>
4 #include <vector>
5
6 #include <pybind11/functional.h>
7 #include <pybind11/pybind11.h>
8 #include <pybind11/stl.h>
9
10 #include <simgrid/config.h>
11 #include <xbt/log.h>
12 #include <xbt/string.hpp>
13
14 #include <simgrid/s4u/Actor.hpp>
15 #include <simgrid/s4u/Engine.hpp>
16 #include <simgrid/s4u/Host.hpp>
17
18 #include <boost/intrusive_ptr.hpp>
19
20 namespace py = pybind11;
21 using simgrid::s4u::Actor;
22 using simgrid::s4u::ActorPtr;
23 using simgrid::s4u::Engine;
24 using simgrid::s4u::Host;
25
26 XBT_LOG_NEW_DEFAULT_CATEGORY(python, "python");
27
28 PYBIND11_DECLARE_HOLDER_TYPE(T, boost::intrusive_ptr<T>);
29
30 namespace {
31
32 static std::string get_simgrid_version()
33 {
34   int major, minor, patch;
35   sg_version_get(&major, &minor, &patch);
36   return simgrid::xbt::string_printf("%i.%i.%i", major, minor, patch);
37 }
38
39 static std::string simgrid_version = get_simgrid_version();
40
41 } // namespace
42
43 PYBIND11_MODULE(simgrid, m)
44 {
45
46   m.doc() = "SimGrid userspace API";
47
48   m.attr("simgrid_version") = simgrid_version;
49
50   m.def("info", [](char* s) { XBT_INFO("%s", s); }, "Display a logging message of default priority.");
51
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("execute", py::overload_cast<double, double>(&simgrid::s4u::this_actor::execute),
56         "Block the actor, computing the given amount of flops at the given priority");
57   m.def("yield_", &simgrid::s4u::this_actor::yield, "Yield the actor");
58
59   /* Class Engine */
60   py::class_<Engine>(m, "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();
67         argv[argc] = nullptr;
68         // Currently this can be dangling, we should wrap this somehow.
69         return new simgrid::s4u::Engine(&argc, argv.get());
70       }))
71       .def("load_platform", &Engine::load_platform, "Load a platform file describing the environment")
72       .def("load_deployment", &Engine::load_deployment, "Load a deployment file and launch the actors that it contains")
73       .def("run", &Engine::run, "Run the simulation")
74       .def("register_function", [](Engine*, std::string name, std::function<void(std::vector<std::string>)> f) {
75         simgrid::simix::register_function(name,
76             [f](std::vector<std::string> args) -> simgrid::simix::ActorCode {
77           return [args, f]() { f(args); };
78         });
79       }, "Registers the main function of an actor that will be launched from the deployment file");
80
81   // Currently, Host lead to segfault:
82   py::class_<simgrid::s4u::Host, std::unique_ptr<Host, py::nodelete>>(m, "Host")
83       .def("by_name", &Host::by_name, "Retrieve an host from its name, or die");
84
85   py::class_<simgrid::s4u::Actor, ActorPtr>(m, "Actor", "An actor is an independent stream of execution in your distributed application");
86
87   // Select the right template instantiation
88   simgrid::s4u::ActorPtr (*create_actor)(std::string, Host*, std::function<void()>) = &Actor::create;
89
90   m.def("create_actor", create_actor, "Create an actor");
91   m.def("create_actor", [](std::string name, Host* host) -> std::function<ActorPtr(std::function<void()>)> {
92     return [name, host](std::function<void()> f) -> ActorPtr {
93       return simgrid::s4u::Actor::create(name, host, std::move(f));
94     };
95   }, "Create an actor");
96 }