From 3e978b7ee3fb648f19de9aeb4450a98e30dd437b Mon Sep 17 00:00:00 2001 From: Gabriel Corona Date: Wed, 22 Jun 2016 12:03:54 +0200 Subject: [PATCH] [s4u] Register function names --- examples/s4u/CMakeLists.txt | 4 +++ examples/s4u/basic/s4u_basic.cpp | 26 ++--------------- examples/s4u/basic/s4u_basic.h | 35 +++++++++++++++++++++++ examples/s4u/basic/s4u_basic.tesh | 7 +++++ examples/s4u/basic/s4u_basic_function.cpp | 22 ++++++++++++++ include/simgrid/s4u/actor.hpp | 7 +++++ include/simgrid/s4u/engine.hpp | 24 ++++++++++++++++ include/simgrid/simix.hpp | 9 ++++++ src/s4u/s4u_actor.cpp | 11 +++++++ src/simix/smx_deployment.cpp | 11 +++++++ src/simix/smx_private.h | 7 +---- 11 files changed, 133 insertions(+), 30 deletions(-) create mode 100644 examples/s4u/basic/s4u_basic.h create mode 100644 examples/s4u/basic/s4u_basic_function.cpp diff --git a/examples/s4u/CMakeLists.txt b/examples/s4u/CMakeLists.txt index a3a2a04226..f649ba1c86 100644 --- a/examples/s4u/CMakeLists.txt +++ b/examples/s4u/CMakeLists.txt @@ -7,6 +7,10 @@ foreach (example basic io mutex actions-comm) set(examples_src ${examples_src} ${CMAKE_CURRENT_SOURCE_DIR}/${example}/s4u_${example}.cpp) endforeach() +add_executable (s4u_basic_function basic/s4u_basic_function.cpp) +target_link_libraries(s4u_basic_function simgrid) +set_target_properties(s4u_basic_function PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/basic) + set(examples_src ${examples_src} PARENT_SCOPE) set(tesh_files ${tesh_files} PARENT_SCOPE) set(xml_files ${xml_files} ${CMAKE_CURRENT_SOURCE_DIR}/actions-comm/s4u_actions-comm_split_d.xml diff --git a/examples/s4u/basic/s4u_basic.cpp b/examples/s4u/basic/s4u_basic.cpp index 12156f2547..73cf6137b3 100644 --- a/examples/s4u/basic/s4u_basic.cpp +++ b/examples/s4u/basic/s4u_basic.cpp @@ -5,31 +5,9 @@ #include -#include "simgrid/s4u.h" - -XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category"); - -class Worker { -public: - void operator()() { - XBT_INFO("Hello s4u, I'm ready to serve"); - char *msg = static_cast(simgrid::s4u::this_actor::recv( - *simgrid::s4u::Mailbox::byName("worker"))); - XBT_INFO("I received '%s'",msg); - XBT_INFO("I'm done. See you."); - } -}; - -class Master { -public: - void operator()() { - const char *msg = "GaBuZoMeu"; - XBT_INFO("Hello s4u, I have something to send"); - simgrid::s4u::this_actor::send(*simgrid::s4u::Mailbox::byName("worker"), xbt_strdup(msg), strlen(msg)); - XBT_INFO("I'm done. See you."); - } -}; +#include +#include "s4u_basic.h" int main(int argc, char **argv) { simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv); diff --git a/examples/s4u/basic/s4u_basic.h b/examples/s4u/basic/s4u_basic.h new file mode 100644 index 0000000000..7a3f9c6bed --- /dev/null +++ b/examples/s4u/basic/s4u_basic.h @@ -0,0 +1,35 @@ +/* Copyright (c) 2006-2016. 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 "simgrid/s4u.h" + +XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category"); + +class Worker { +public: + Worker() {}; + Worker(simgrid::xbt::args args) {} + void operator()() { + XBT_INFO("Hello s4u, I'm ready to serve"); + char *msg = static_cast(simgrid::s4u::this_actor::recv( + *simgrid::s4u::Mailbox::byName("worker"))); + XBT_INFO("I received '%s'",msg); + XBT_INFO("I'm done. See you."); + } +}; + +class Master { +public: + Master() {}; + Master(simgrid::xbt::args args) {} + void operator()() { + const char *msg = "GaBuZoMeu"; + XBT_INFO("Hello s4u, I have something to send"); + simgrid::s4u::this_actor::send(*simgrid::s4u::Mailbox::byName("worker"), xbt_strdup(msg), strlen(msg)); + XBT_INFO("I'm done. See you."); + } +}; diff --git a/examples/s4u/basic/s4u_basic.tesh b/examples/s4u/basic/s4u_basic.tesh index 111fea7d58..627fb9d4cd 100644 --- a/examples/s4u/basic/s4u_basic.tesh +++ b/examples/s4u/basic/s4u_basic.tesh @@ -6,3 +6,10 @@ $ $SG_TEST_EXENV ${bindir:=.}/s4u_basic > [Tremblay:worker:(0) 0.001301] [s4u_test/INFO] I received 'GaBuZoMeu' > [Tremblay:worker:(0) 0.001301] [s4u_test/INFO] I'm done. See you. > [Jupiter:master:(0) 0.001301] [s4u_test/INFO] I'm done. See you. + +$ $SG_TEST_EXENV ${bindir:=.}/s4u_basic_function +> [Tremblay:worker:(0) 0.000000] [s4u_test/INFO] Hello s4u, I'm ready to serve +> [Jupiter:master:(0) 0.000000] [s4u_test/INFO] Hello s4u, I have something to send +> [Tremblay:worker:(0) 0.001301] [s4u_test/INFO] I received 'GaBuZoMeu' +> [Tremblay:worker:(0) 0.001301] [s4u_test/INFO] I'm done. See you. +> [Jupiter:master:(0) 0.001301] [s4u_test/INFO] I'm done. See you. diff --git a/examples/s4u/basic/s4u_basic_function.cpp b/examples/s4u/basic/s4u_basic_function.cpp new file mode 100644 index 0000000000..dd39a93778 --- /dev/null +++ b/examples/s4u/basic/s4u_basic_function.cpp @@ -0,0 +1,22 @@ +/* Copyright (c) 2006-2016. 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 "s4u_basic.h" + +int main(int argc, char **argv) { + simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv); + e->loadPlatform("../../platforms/two_hosts.xml"); + e->registerFunction("worker"); + e->registerFunction("master"); + simgrid::xbt::args args; + simgrid::s4u::Actor("worker", simgrid::s4u::Host::by_name("Tremblay"), "worker", args); + simgrid::s4u::Actor("master", simgrid::s4u::Host::by_name("Jupiter"), "master", args); + e->run(); + return 0; +} diff --git a/include/simgrid/s4u/actor.hpp b/include/simgrid/s4u/actor.hpp index 7f8016f4fc..052ad862e5 100644 --- a/include/simgrid/s4u/actor.hpp +++ b/include/simgrid/s4u/actor.hpp @@ -206,6 +206,13 @@ public: Actor(name, host, wrap_task(std::move(code), std::move(args)...)) {} + // Create actor from function name: + + Actor(const char* name, s4u::Host *host, double killTime, const char* function, simgrid::xbt::args args); + + Actor(const char* name, s4u::Host *host, const char* function, simgrid::xbt::args args) : + Actor(name, host, -1.0, function, std::move(args)) {} + /** Retrieves the actor that have the given PID (or NULL if not existing) */ //static Actor *byPid(int pid); not implemented diff --git a/include/simgrid/s4u/engine.hpp b/include/simgrid/s4u/engine.hpp index bee4afa5c0..07863e17e8 100644 --- a/include/simgrid/s4u/engine.hpp +++ b/include/simgrid/s4u/engine.hpp @@ -7,6 +7,9 @@ #define SIMGRID_S4U_ENGINE_HPP #include +#include + +#include #include @@ -58,6 +61,27 @@ public: /** @brief Retrieve the AS of the given name (or nullptr if not found) */ simgrid::s4u::As *asByNameOrNull(const char *name); + template + void registerFunction(const char* name) + { + simgrid::simix::registerFunction(name, [](simgrid::xbt::args args){ + return simgrid::simix::ActorCode([args] { + F code(std::move(args)); + code(); + }); + }); + } + + template + void registerFunction(const char* name, F code) + { + simgrid::simix::registerFunction(name, [code](simgrid::xbt::args args){ + return simgrid::simix::ActorCode([code,args] { + code(std::move(args)); + }); + }); + } + private: static s4u::Engine *instance_; }; diff --git a/include/simgrid/simix.hpp b/include/simgrid/simix.hpp index 99d241054f..eb92f4262b 100644 --- a/include/simgrid/simix.hpp +++ b/include/simgrid/simix.hpp @@ -17,6 +17,7 @@ #include #include +#include #include @@ -171,6 +172,14 @@ public: XBT_PUBLIC(void) set_maestro(std::function code); XBT_PUBLIC(void) create_maestro(std::function code); +// What's executed as SIMIX actor code: +typedef std::function ActorCode; + +// Create ActorCode based on argv: +typedef std::function ActorCodeFactory; + +XBT_PUBLIC(void) registerFunction(const char* name, ActorCodeFactory factory); + } } diff --git a/src/s4u/s4u_actor.cpp b/src/s4u/s4u_actor.cpp index 5cec45afd9..ca6cfec3d0 100644 --- a/src/s4u/s4u_actor.cpp +++ b/src/s4u/s4u_actor.cpp @@ -12,6 +12,8 @@ #include "simgrid/s4u/host.hpp" #include "simgrid/s4u/mailbox.hpp" +#include "src/simix/smx_private.h" + XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor,"S4U actors"); using namespace simgrid; @@ -25,6 +27,15 @@ s4u::Actor::Actor(const char* name, s4u::Host *host, double killTime, std::funct killTime, nullptr, 0)); } +s4u::Actor::Actor(const char* name, s4u::Host *host, double killTime, const char* function, simgrid::xbt::args args) +{ + simgrid::simix::ActorCodeFactory& factory = SIMIX_get_actor_code_factory(function); + simgrid::simix::ActorCode code = factory(std::move(args)); + this->pimpl_ = SIMIX_process_ref(simcall_process_create( + name, std::move(code), nullptr, host->name().c_str(), + killTime, nullptr, 0)); +} + void s4u::Actor::join() { simcall_process_join(pimpl_, -1); } diff --git a/src/simix/smx_deployment.cpp b/src/simix/smx_deployment.cpp index b5e84b1669..a740981cf3 100644 --- a/src/simix/smx_deployment.cpp +++ b/src/simix/smx_deployment.cpp @@ -151,3 +151,14 @@ void SIMIX_process_set_function(const char *process_host, process.on_failure = SURF_PROCESS_ON_FAILURE_DIE; sg_platf_new_process(&process); } + +namespace simgrid { +namespace simix { + +void registerFunction(const char* name, ActorCodeFactory factory) +{ + simix_global->registered_functions[name] = std::move(factory); +} + +} +} \ No newline at end of file diff --git a/src/simix/smx_private.h b/src/simix/smx_private.h index 6906a08b6d..bb735b8e84 100644 --- a/src/simix/smx_private.h +++ b/src/simix/smx_private.h @@ -13,6 +13,7 @@ #include + #include "src/internal_config.h" #include "simgrid/simix.h" #include "surf/surf.h" @@ -67,12 +68,6 @@ typedef struct s_smx_context_factory *smx_context_factory_t; namespace simgrid { namespace simix { -// What's executed as SIMIX actor code: -typedef std::function ActorCode; - -// Create ActorCode based on argv: -typedef std::function ActorCodeFactory; - class Global { public: smx_context_factory_t context_factory = nullptr; -- 2.20.1