From: Martin Quinson Date: Fri, 12 Aug 2016 20:53:41 +0000 (+0200) Subject: rewrite the s4u_launching example X-Git-Tag: v3_14~569 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/28bb7c8be1ed83295ff75f57c086493733a11228 rewrite the s4u_launching example --- diff --git a/examples/s4u/CMakeLists.txt b/examples/s4u/CMakeLists.txt index 1edddbe9a0..f484128424 100644 --- a/examples/s4u/CMakeLists.txt +++ b/examples/s4u/CMakeLists.txt @@ -7,20 +7,6 @@ foreach (example io launching mutex actions-comm) set(examples_src ${examples_src} ${CMAKE_CURRENT_SOURCE_DIR}/${example}/s4u_${example}.cpp) endforeach() -add_executable (s4u_launching_function launching/s4u_launching_function.cpp) -target_link_libraries(s4u_launching_function simgrid) -set_target_properties(s4u_launching_function PROPERTIES RUNTIME_OUTPUT_DIRECTORY - ${CMAKE_CURRENT_BINARY_DIR}/launching) - -add_executable (s4u_launching_deployment launching/s4u_launching_deployment.cpp) -target_link_libraries(s4u_launching_deployment simgrid) -set_target_properties(s4u_launching_deployment PROPERTIES RUNTIME_OUTPUT_DIRECTORY - ${CMAKE_CURRENT_BINARY_DIR}/launching) - -set(examples_src ${examples_src} ${CMAKE_CURRENT_SOURCE_DIR}/launching/s4u_launching_function.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/launching/s4u_launching_deployment.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/launching/s4u_launching.h) - set(examples_src ${examples_src} PARENT_SCOPE) set(tesh_files ${tesh_files} PARENT_SCOPE) set(xml_files ${xml_files} ${CMAKE_CURRENT_SOURCE_DIR}/launching/deployment.xml diff --git a/examples/s4u/README.doc b/examples/s4u/README.doc index e08626cc70..f80cb812a9 100644 --- a/examples/s4u/README.doc +++ b/examples/s4u/README.doc @@ -13,20 +13,27 @@ documentation, but it should remain readable directly. @ingroup s4u_api @brief Find the S4U example fitting your needs in the archive. - - @ref s4u_ex_launching + - @ref s4u_ex_basics - @ref s4u_ex_synchro -@section s4u_ex_launching Launching actors to populate your simulation +@section s4u_ex_basics Basics of SimGrid simulation + + - Launching actors + Shows how to start your actors to populate your simulation\n + @ref examples/s4u/launching/s4u_launching.cpp and @ref examples/s4u/launching/deployment.xml @section s4u_ex_synchro Inter-Actor Synchronization - - Mutex. - @ref examples/s4u/mutex/s4u_mutex.cpp\n - Shows how to use simgrid::s4u::Mutex synchronization objects + - Mutex. + Shows how to use simgrid::s4u::Mutex synchronization objects\n + @ref examples/s4u/mutex/s4u_mutex.cpp */ /** +@example examples/s4u/launching/s4u_launching.cpp +@example examples/s4u/launching/deployment.xml + @example examples/s4u/mutex/s4u_mutex.cpp */ \ No newline at end of file diff --git a/examples/s4u/launching/.gitignore b/examples/s4u/launching/.gitignore index f110ab6dfa..7cef20dc0e 100644 --- a/examples/s4u/launching/.gitignore +++ b/examples/s4u/launching/.gitignore @@ -1,3 +1 @@ s4u_launching -s4u_launching_deployment -s4u_launching_function diff --git a/examples/s4u/launching/deployment.xml b/examples/s4u/launching/deployment.xml index 400425360e..2c69f2161b 100644 --- a/examples/s4u/launching/deployment.xml +++ b/examples/s4u/launching/deployment.xml @@ -1,8 +1,15 @@ - - - + + + + + + diff --git a/examples/s4u/launching/s4u_launching.cpp b/examples/s4u/launching/s4u_launching.cpp index 85f512a80c..bad809870d 100644 --- a/examples/s4u/launching/s4u_launching.cpp +++ b/examples/s4u/launching/s4u_launching.cpp @@ -3,16 +3,113 @@ /* 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 + +/* This example shows how to declare and start your actors. + * + * The first step is to declare the code of your actors (what they do exactly + * does not matter to this example) and then you ask SimGrid to start your + * actors. There is three ways of doing so: + * - Directly, by instantiating your actor as paramter to Actor::create(); + * - By first registering your actors before instantiating it; + * - Through the deployment file. + * + * This example shows all these solutions, even if you obviously should use + * only one of these solutions to start your actors. The most advised solution + * is to use a deployment file, as it creates a clear separation between your + * application and the settings to test it. This is a better scientific + * methodology. Actually, starting an actor with Actor::create() is mostly + * useful to start an actor from another actor. + * + */ + #include -#include "s4u_launching.h" +// This declares a logging channel so that XBT_INFO can be used later +XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_launching_test, "The logging channel used in this example"); + + +/* Declares a first class of actors which sends a message to the mailbox 'mb42'. + * The sent message is what was passed as parameter on creation (or 'GaBuZoMeu' by default) + * + * Later, this actor class is instantiated twice in the simulation. + */ +class Sender { +public: + const char *msg = "GaBuZoMeu"; + Sender() { + /* Constructor used when no parameter is passed to the actor */ + }; + Sender(std::vector args) { + /* This constructor is used when we pass parameters to the actor */ + if (args.size() > 0) + msg = args[0].c_str(); + } + void operator()() { + XBT_INFO("Hello s4u, I have something to send"); + simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName("mb42"); + + simgrid::s4u::this_actor::send(mailbox, xbt_strdup(msg), strlen(msg)); + XBT_INFO("I'm done. See you."); + } +}; + + +/* Declares a second class of actor which receive two messages on the mailbox which + * name is passed as parameter ('thingy' by default, ie the wrong one). + * + * Later, this actor class is instantiated once in the simulation. + */ +class Receiver { +public: + simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName("thingy"); + + Receiver() {}; + Receiver(std::vector args) { + /* This constructor is used when we pass parameters to the actor */ + /* as with argc/argv, args[0] is the actor's name, so the first parameter is args[1] */ + if (args.size() > 1) + mailbox = simgrid::s4u::Mailbox::byName(args[1].c_str()); + } + void operator()() { + XBT_INFO("Hello s4u, I'm ready to get any message you'd want on %s", mailbox->getName()); + + char *msg1 = static_cast(simgrid::s4u::this_actor::recv(mailbox)); + char *msg2 = static_cast(simgrid::s4u::this_actor::recv(mailbox)); + XBT_INFO("I received '%s' and '%s'",msg1,msg2); + XBT_INFO("I'm done. See you."); + } +}; + +/* Here comes the main function of your program */ int main(int argc, char **argv) { + /* When your program starts, you have to first start a new simulation engine, as follows */ simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv); - e->loadPlatform("../../platforms/two_hosts.xml"); - simgrid::s4u::Actor::createActor("worker", simgrid::s4u::Host::by_name("Tremblay"), Worker()); - simgrid::s4u::Actor::createActor("master", simgrid::s4u::Host::by_name("Jupiter"), Master()); + + /* Then you should load a platform file, describing your simulated platform */ + e->loadPlatform("../../platforms/small_platform.xml"); + + /* And now you have to ask SimGrid to actually start your actors. + * + * You can first directly start your actor, as follows. Note the last parameter: 'Sender()', + * as if you would call the Sender function. + */ + simgrid::s4u::Actor::createActor("sender1", simgrid::s4u::Host::by_name("Tremblay"), Sender()); + + /* The second way is to first register your function, and then retrieve it */ + e->registerFunction("sender"); // The sender is passed as a template parameter here + std::vector args; // Here we declare the parameter that the actor will get + args.push_back("GloubiBoulga"); // Add a parameter to the set (we could have done it in the first approach too) + + simgrid::s4u::Actor::createActor("sender2", simgrid::s4u::Host::by_name("Jupiter"), "sender", args); + + /* The third way to start your actors is to use a deployment file. */ + e->registerFunction("receiver"); // You first have to register the actor as with the second approach + e->loadDeployment("deployment.xml"); // And then, you load the deployment file + + /* Once every actors are started in the engine, the simulation can start */ e->run(); + + /* Once the simulation is done, the program is ended */ return 0; } diff --git a/examples/s4u/launching/s4u_launching.h b/examples/s4u/launching/s4u_launching.h deleted file mode 100644 index 1cfdc83cf9..0000000000 --- a/examples/s4u/launching/s4u_launching.h +++ /dev/null @@ -1,35 +0,0 @@ -/* 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 - -XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category"); - -class Worker { -public: - Worker() {}; - Worker(std::vector 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(std::vector 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/launching/s4u_launching.tesh b/examples/s4u/launching/s4u_launching.tesh index 214dbea586..681772163d 100644 --- a/examples/s4u/launching/s4u_launching.tesh +++ b/examples/s4u/launching/s4u_launching.tesh @@ -1,22 +1,10 @@ #! ./tesh $ $SG_TEST_EXENV ${bindir:=.}/s4u_launching -> [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. - -$ $SG_TEST_EXENV ${bindir:=.}/s4u_launching_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. - -$ $SG_TEST_EXENV ${bindir:=.}/s4u_launching_deployment deployment.xml -> [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. +> [Tremblay:sender1:(0) 0.000000] [s4u_launching_test/INFO] Hello s4u, I have something to send +> [Jupiter:sender2:(0) 0.000000] [s4u_launching_test/INFO] Hello s4u, I have something to send +> [Fafard:receiver:(0) 0.000000] [s4u_launching_test/INFO] Hello s4u, I'm ready to get any message you'd want on mb42 +> [Tremblay:sender1:(0) 0.025709] [s4u_launching_test/INFO] I'm done. See you. +> [Jupiter:sender2:(0) 0.070434] [s4u_launching_test/INFO] I'm done. See you. +> [Fafard:receiver:(0) 0.070434] [s4u_launching_test/INFO] I received 'GaBuZoMeu' and 'GloubiBoulga' +> [Fafard:receiver:(0) 0.070434] [s4u_launching_test/INFO] I'm done. See you. diff --git a/examples/s4u/launching/s4u_launching_deployment.cpp b/examples/s4u/launching/s4u_launching_deployment.cpp deleted file mode 100644 index 115cf4a05b..0000000000 --- a/examples/s4u/launching/s4u_launching_deployment.cpp +++ /dev/null @@ -1,22 +0,0 @@ -/* 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 -#include - -#include "s4u_launching.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"); - e->loadDeployment(argv[1]); - e->run(); - return 0; -} diff --git a/examples/s4u/launching/s4u_launching_function.cpp b/examples/s4u/launching/s4u_launching_function.cpp deleted file mode 100644 index 713aa96276..0000000000 --- a/examples/s4u/launching/s4u_launching_function.cpp +++ /dev/null @@ -1,25 +0,0 @@ -/* 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 - -#include - -#include "s4u_launching.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"); - std::vector args; - simgrid::s4u::Actor::createActor("worker", simgrid::s4u::Host::by_name("Tremblay"), "worker", args); - simgrid::s4u::Actor::createActor("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 a858bdf078..89ea5f469a 100644 --- a/include/simgrid/s4u/Actor.hpp +++ b/include/simgrid/s4u/Actor.hpp @@ -25,6 +25,7 @@ #include #include #include +#include namespace simgrid { namespace s4u { @@ -168,19 +169,18 @@ public: xbt_assert(actor != nullptr); SIMIX_process_unref(actor->pimpl_); } - using Ptr = boost::intrusive_ptr; // ***** Actor creation ***** /** Retrieve a reference to myself */ - static Ptr self(); + static ActorPtr self(); /** Create an actor using a function * * If the actor is restarted, the actor has a fresh copy of the function. */ - static Ptr createActor(const char* name, s4u::Host *host, double killTime, std::function code); + static ActorPtr createActor(const char* name, s4u::Host *host, double killTime, std::function code); - static Ptr createActor(const char* name, s4u::Host *host, std::function code) + static ActorPtr createActor(const char* name, s4u::Host *host, std::function code) { return createActor(name, host, -1.0, std::move(code)); } @@ -196,17 +196,17 @@ public: // This constructor is enabled only if the call code(args...) is valid: typename = typename std::result_of::type > - static Ptr createActor(const char* name, s4u::Host *host, F code, Args... args) + static ActorPtr createActor(const char* name, s4u::Host *host, F code, Args... args) { return createActor(name, host, wrap_task(std::move(code), std::move(args)...)); } // Create actor from function name: - static Ptr createActor(const char* name, s4u::Host *host, double killTime, + static ActorPtr createActor(const char* name, s4u::Host *host, double killTime, const char* function, std::vector args); - static Ptr createActor(const char* name, s4u::Host *host, const char* function, + static ActorPtr createActor(const char* name, s4u::Host *host, const char* function, std::vector args) { return createActor(name, host, -1.0, function, std::move(args)); @@ -242,7 +242,7 @@ public: void kill(); static void kill(int pid); - static Ptr forPid(int pid); + static ActorPtr forPid(int pid); /** * Wait for the actor to finish. @@ -259,8 +259,6 @@ protected: smx_process_t getImpl(); }; -using ActorPtr = Actor::Ptr; - /** @ingroup s4u_api * @brief Static methods working on the current actor (see @ref s4u::Actor) */ namespace this_actor { @@ -295,13 +293,13 @@ namespace this_actor { * * See \ref Comm for the full communication API (including non blocking communications). */ - XBT_PUBLIC(void*) recv(Mailbox &chan); + XBT_PUBLIC(void*) recv(MailboxPtr chan); /** Block the actor until it delivers a message of the given simulated size to the given mailbox * * See \ref Comm for the full communication API (including non blocking communications). */ - XBT_PUBLIC(void) send(Mailbox &chan, void*payload, size_t simulatedSize); + XBT_PUBLIC(void) send(MailboxPtr chan, void*payload, size_t simulatedSize); /** * Return the PID of the current actor. diff --git a/include/simgrid/s4u/comm.hpp b/include/simgrid/s4u/comm.hpp index 1083fbe2ae..ff5ec4218f 100644 --- a/include/simgrid/s4u/comm.hpp +++ b/include/simgrid/s4u/comm.hpp @@ -78,13 +78,13 @@ public: return res; } /** Creates (but don't start) an async send to the mailbox @p dest */ - static Comm &send_init(Mailbox &dest); + static Comm &send_init(MailboxPtr dest); /** Creates and start an async send to the mailbox @p dest */ - static Comm &send_async(Mailbox &dest, void *data, int simulatedByteAmount); + static Comm &send_async(MailboxPtr dest, void *data, int simulatedByteAmount); /** Creates (but don't start) an async recv onto the mailbox @p from */ - static Comm &recv_init(Mailbox &from); + static Comm &recv_init(MailboxPtr from); /** Creates and start an async recv to the mailbox @p from */ - static Comm &recv_async(Mailbox &from, void **data); + static Comm &recv_async(MailboxPtr from, void **data); void start() override; void wait() override; @@ -125,7 +125,7 @@ private: smx_process_t sender_ = nullptr; smx_process_t receiver_ = nullptr; - Mailbox *mailbox_ = nullptr; + MailboxPtr mailbox_ = nullptr; }; }} // namespace simgrid::s4u diff --git a/include/simgrid/s4u/forward.hpp b/include/simgrid/s4u/forward.hpp index b8f6b4263d..5b864a8ce9 100644 --- a/include/simgrid/s4u/forward.hpp +++ b/include/simgrid/s4u/forward.hpp @@ -6,14 +6,21 @@ #ifndef SIMGRID_S4U_FORWARD_HPP #define SIMGRID_S4U_FORWARD_HPP +#include + namespace simgrid { namespace s4u { +class Actor; +using ActorPtr = boost::intrusive_ptr; + class Activity; class Comm; class Engine; class Host; class Mailbox; +using MailboxPtr = boost::intrusive_ptr; + class Storage; } diff --git a/include/simgrid/s4u/mailbox.hpp b/include/simgrid/s4u/mailbox.hpp index 99f65b49cb..6298ad37fa 100644 --- a/include/simgrid/s4u/mailbox.hpp +++ b/include/simgrid/s4u/mailbox.hpp @@ -8,8 +8,6 @@ #include -#include - #include #include @@ -40,13 +38,12 @@ public: // We don't have to manage the lifetime of mailboxes: friend void intrusive_ptr_add_ref(Mailbox*) {} friend void intrusive_ptr_release(Mailbox*) {} - using Ptr = boost::intrusive_ptr; /** Get the name of that mailbox */ const char *getName(); /** Retrieve the mailbox associated to the given string */ - static Ptr byName(const char *name); + static MailboxPtr byName(const char *name); /** Returns whether the mailbox contains queued communications */ bool empty(); @@ -65,8 +62,6 @@ public: ActorPtr receiver(); }; -using MailboxPtr = Mailbox::Ptr; - }} // namespace simgrid::s4u XBT_PUBLIC(sg_mbox_t) sg_mbox_by_name(const char*name); diff --git a/src/s4u/s4u_actor.cpp b/src/s4u/s4u_actor.cpp index 444fa6cbc9..803a979ebd 100644 --- a/src/s4u/s4u_actor.cpp +++ b/src/s4u/s4u_actor.cpp @@ -38,7 +38,7 @@ ActorPtr Actor::createActor(const char* name, s4u::Host *host, double killTime, smx_process_t process = simcall_process_create( name, std::move(code), nullptr, host->name().c_str(), killTime, nullptr, 0); - return Ptr(&process->getIface()); + return ActorPtr(&process->getIface()); } ActorPtr Actor::createActor(const char* name, s4u::Host *host, double killTime, @@ -142,7 +142,7 @@ e_smx_state_t execute(double flops) { return simcall_execution_wait(s); } -void* recv(Mailbox &chan) { +void* recv(MailboxPtr chan) { void *res = nullptr; Comm& c = Comm::recv_init(chan); c.setDstData(&res,sizeof(res)); @@ -150,7 +150,7 @@ void* recv(Mailbox &chan) { return res; } -void send(Mailbox &chan, void *payload, size_t simulatedSize) { +void send(MailboxPtr chan, void *payload, size_t simulatedSize) { Comm& c = Comm::send_init(chan); c.setRemains(simulatedSize); c.setSrcData(payload); diff --git a/src/s4u/s4u_comm.cpp b/src/s4u/s4u_comm.cpp index 7873e1d451..4fd7d4e122 100644 --- a/src/s4u/s4u_comm.cpp +++ b/src/s4u/s4u_comm.cpp @@ -20,17 +20,17 @@ Comm::~Comm() { -s4u::Comm &Comm::send_init(s4u::Mailbox &chan) { +s4u::Comm &Comm::send_init(s4u::MailboxPtr chan) { s4u::Comm *res = new s4u::Comm(); res->sender_ = SIMIX_process_self(); - res->mailbox_ = &chan; + res->mailbox_ = chan; return *res; } -s4u::Comm &Comm::recv_init(s4u::Mailbox &chan) { +s4u::Comm &Comm::recv_init(s4u::MailboxPtr chan) { s4u::Comm *res = new s4u::Comm(); res->receiver_ = SIMIX_process_self(); - res->mailbox_ = &chan; + res->mailbox_ = chan; return *res; } @@ -132,7 +132,7 @@ void Comm::wait(double timeout) { state_ = finished; } -s4u::Comm &Comm::send_async(Mailbox &dest, void *data, int simulatedSize) { +s4u::Comm &Comm::send_async(MailboxPtr dest, void *data, int simulatedSize) { s4u::Comm &res = s4u::Comm::send_init(dest); res.setRemains(simulatedSize); res.srcBuff_ = data; @@ -141,7 +141,7 @@ s4u::Comm &Comm::send_async(Mailbox &dest, void *data, int simulatedSize) { return res; } -s4u::Comm &Comm::recv_async(Mailbox &dest, void **data) { +s4u::Comm &Comm::recv_async(MailboxPtr dest, void **data) { s4u::Comm &res = s4u::Comm::recv_init(dest); res.setDstData(data); res.start();