Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
52387a63a68274afb4cc7752abdca5f64e88c26e
[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 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()`");
58
59   /* Class Engine */
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();
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,
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); };
80         });
81       }, "Registers the main function of an actor that will be launched from the deployment file, , see :cpp:func:`simgrid::s4u::Engine::register_function()`");
82
83   /* Class Host */
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");
86
87   /* Class Actor */
88   // Select the right template instantiation
89   simgrid::s4u::ActorPtr (*create_actor)(std::string, Host*, std::function<void()>) = &Actor::create;
90
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));
97       };
98     }, "Create an actor from a functor");
99
100
101 }