From 3a8a41d353f154422a95e78218a2496ab90ef471 Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Mon, 31 Dec 2018 01:33:57 +0100 Subject: [PATCH] python: extend the bindings WORKING: - Mailbox::put and Mailbox::get - simgrid.Actor.create("name", Fafard, callable) (with callable being either a function or an object implementing __call__(self) - simgrid.Actor.create("name", Fafard, callable, param1, param2) (with callable taking 2 parameters) - e.register_function("name", fun) (with fun being a function taking a tuple parameter) NOT WORKING - e.register_function("name", class) (with class being a class name implementing __init__(self, *args)) --- docs/source/app_s4u.rst | 9 ++++- src/bindings/python/simgrid_python.cpp | 55 +++++++++++++++++++++----- 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/docs/source/app_s4u.rst b/docs/source/app_s4u.rst index f170e383e2..ffe1151119 100644 --- a/docs/source/app_s4u.rst +++ b/docs/source/app_s4u.rst @@ -586,7 +586,7 @@ Functions .. automodule:: simgrid :members: - :exclude-members: Actor, Host, Engine + :exclude-members: Actor, Host, Engine, Mailbox =========== Class Actor @@ -608,3 +608,10 @@ Class Host .. autoclass:: simgrid.Host :members: + +============= +Class Mailbox +============= + +.. autoclass:: simgrid.Mailbox + :members: diff --git a/src/bindings/python/simgrid_python.cpp b/src/bindings/python/simgrid_python.cpp index 52387a63a6..a9f013559b 100644 --- a/src/bindings/python/simgrid_python.cpp +++ b/src/bindings/python/simgrid_python.cpp @@ -1,3 +1,8 @@ +/* Copyright (c) 2018. The SimGrid Team. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + #include #include #include @@ -14,6 +19,7 @@ #include #include #include +#include #include @@ -22,6 +28,7 @@ using simgrid::s4u::Actor; using simgrid::s4u::ActorPtr; using simgrid::s4u::Engine; using simgrid::s4u::Host; +using simgrid::s4u::Mailbox; XBT_LOG_NEW_DEFAULT_CATEGORY(python, "python"); @@ -78,24 +85,52 @@ PYBIND11_MODULE(simgrid, m) [f](std::vector args) -> simgrid::simix::ActorCode { return [args, f]() { f(args); }; }); - }, "Registers the main function of an actor that will be launched from the deployment file, , see :cpp:func:`simgrid::s4u::Engine::register_function()`"); + }, "Registers the main function of an actor, see :cpp:func:`simgrid::s4u::Engine::register_function()`") + ; /* Class Host */ py::class_>(m, "Host", "Simulation Engine, see :ref:`class s4u::Host `").def( "by_name", &Host::by_name, "Retrieve a host from its name, or die"); - /* Class Actor */ - // Select the right template instantiation - simgrid::s4u::ActorPtr (*create_actor)(std::string, Host*, std::function) = &Actor::create; + /* Class Mailbox */ + py::class_>(m, "Mailbox", "Mailbox, see :ref:`class s4u::Mailbox `") + .def("by_name", &Mailbox::by_name, "Retrieve a Mailbox from its name, see :cpp:func:`simgrid::s4u::Mailbox::by_name()`") + .def("get_name", &Mailbox::get_name, "Retrieves the name of that host, see :cpp:func:`simgrid::s4u::Mailbox::get_name()`") + .def("put", [](Mailbox self, py::object data, int size) { + data.inc_ref(); + self.put(data.ptr(), size); + }, "Blocking data transmission, see :cpp:func:`void simgrid::s4u::Mailbox::put(void*, uint64_t)`") + .def("get", [](Mailbox self) -> py::object { + py::object data = pybind11::reinterpret_steal(pybind11::handle(static_cast(self.get()))); + data.dec_ref(); + return data; + }, "Blocking data reception, see :cpp:func:`void* simgrid::s4u::Mailbox::get()`"); + /* Class Actor */ py::class_(m, "Actor", "" "An actor is an independent stream of execution in your distributed application, see :ref:`class s4u::Actor `") - .def("create", create_actor, "Create an actor from a function, see :cpp:func:`simgrid::s4u::Actor::create()`") - .def("create", [](std::string name, Host* host) -> std::function)> { - return [name, host](std::function f) -> ActorPtr { - return simgrid::s4u::Actor::create(name, host, std::move(f)); - }; - }, "Create an actor from a functor"); + + .def("create", [](py::args args, py::kwargs kwargs) { + xbt_assert(args.size()>2, "Creating an actor takes at least 3 parameters: name, host, and main function."); + return simgrid::s4u::Actor::create(args[0].cast(), args[1].cast(), [args]() { + py::tuple funargs(args.size()-3); + for (size_t i=3; i ActorPtr { + xbt_assert(pybind11::hasattr(obj, "__call__"), "Your object does not implement the __call__() method"); + + return simgrid::s4u::Actor::create(name, host, [obj](){ + obj.attr("__call__")(); + }); + }, "Create an actor from a python object") + */; } -- 2.20.1