Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
even more ignorable cruft
[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(s4u_python, "S4U 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("yield_", &simgrid::s4u::this_actor::yield, "Yield the actor");
56
57   /* Class Engine */
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();
65         argv[argc] = nullptr;
66         // Currently this can be dangling, we should wrap this somehow.
67         return new simgrid::s4u::Engine(&argc, argv.get());
68       }))
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); };
76         });
77       }, "Registers the main function of an actor that will be launched from the deployment file");
78
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");
82
83   py::class_<simgrid::s4u::Actor, ActorPtr>(m, "Actor", "An actor is an independent stream of execution in your distributed application");
84
85   // Select the right template instantiation
86   simgrid::s4u::ActorPtr (*create_actor)(std::string, Host*, std::function<void()>) = &Actor::create;
87
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));
92     };
93   }, "Create an actor");
94 }