From 064ce2ef6f2c3218cf06db0c11b82dc82863f746 Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Sun, 22 Apr 2018 22:28:53 +0200 Subject: [PATCH] rewrite the actor-create example --- .../s4u/actor-create/s4u-actor-create.cpp | 122 ++++++++++-------- .../s4u/actor-create/s4u-actor-create.tesh | 17 ++- .../s4u/actor-create/s4u-actor-create_d.xml | 10 +- 3 files changed, 84 insertions(+), 65 deletions(-) diff --git a/examples/s4u/actor-create/s4u-actor-create.cpp b/examples/s4u/actor-create/s4u-actor-create.cpp index 5b5c71bdde..8a51c0ef32 100644 --- a/examples/s4u/actor-create/s4u-actor-create.cpp +++ b/examples/s4u/actor-create/s4u-actor-create.cpp @@ -23,67 +23,69 @@ // This declares a logging channel so that XBT_INFO can be used later XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor_create, "The logging channel used in this example"); -/* Declares a first class of actors which sends a message to the mailbox 'mb42'. +/* Our first class of actors is simply implemented with a function, that takes a single string as parameter. + * + * Later, this actor class is instantiated within the simulation. + */ +static void receiver(std::string mailbox_name) +{ + simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(mailbox_name); + + XBT_INFO("Hello s4u, I'm ready to get any message you'd want on %s", mailbox->get_cname()); + + std::string* msg1 = static_cast(mailbox->get()); + std::string* msg2 = static_cast(mailbox->get()); + std::string* msg3 = static_cast(mailbox->get()); + XBT_INFO("I received '%s', '%s' and '%s'", msg1->c_str(), msg2->c_str(), msg3->c_str()); + delete msg1; + delete msg2; + delete msg3; + XBT_INFO("I'm done. See you."); +} + +/* Our second class of actors is also a function */ +static int forwarder(int argc, char** argv) +{ + xbt_assert(argc >= 3, "Actor forwarder requires 2 parameters, but got only %d", argc - 1); + simgrid::s4u::MailboxPtr in = simgrid::s4u::Mailbox::byName(argv[1]); + simgrid::s4u::MailboxPtr out = simgrid::s4u::Mailbox::byName(argv[2]); + std::string* msg = static_cast(in->get()); + XBT_INFO("Forward '%s'.", msg->c_str()); + out->put(msg, msg->size()); + return 0; +} + +/* Declares a third 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: + std::string mbox = "mb42"; std::string msg = "GaBuZoMeu"; - explicit Sender() = default; + explicit Sender() = default; /* Sending the default message */ + explicit Sender(std::string arg) { /* Sending the specified message */ msg= arg; } explicit Sender(std::vector args) { - /* This constructor is used when we pass parameters to the actor */ - if (args.size() > 0) - msg = args[0]; + /* This constructor is used when we start the actor from the deployment file */ + /* args[0] is the actor's name, so the first parameter is args[1] */ + + xbt_assert(args.size() >= 3, "The sender is expecting 2 parameters from the deployment file but got %zu", + args.size() - 1); + msg = args[1]; + mbox = args[2]; } - void operator()() + void operator()() /* This is the main code of the actor */ { XBT_INFO("Hello s4u, I have something to send"); - simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName("mb42"); + simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(mbox); mailbox->put(new std::string(msg), msg.size()); 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"); - - explicit Receiver() = default; - explicit 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] */ - - /* FIXME: this is a bug as this does not happen when starting the process directly - * We should fix it by not adding the process name as argv[0] from the deployment file, - * which is useless anyway since it's always the function name in this setting. - * But this will break MSG... - */ - if (args.size() > 1) - mailbox = simgrid::s4u::Mailbox::byName(args[1]); - } - void operator()() - { - XBT_INFO("Hello s4u, I'm ready to get any message you'd want on %s", mailbox->get_cname()); - - std::string* msg1 = static_cast(mailbox->get()); - std::string* msg2 = static_cast(mailbox->get()); - XBT_INFO("I received '%s' and '%s'", msg1->c_str(), msg2->c_str()); - delete msg1; - delete msg2; - XBT_INFO("I'm done. See you."); - } -}; - /* Here comes the main function of your program */ int main(int argc, char** argv) { @@ -95,21 +97,31 @@ int main(int argc, char** argv) /* 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. + * The easiest way to do so is to implement the behavior of your actor in a single function, + * as we do here for the receiver actors. This function can take any kind of parameters, as + * long as the last parameters of Actor::create() match what your function expects. */ - simgrid::s4u::Actor::create("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::create("receiver", simgrid::s4u::Host::by_name("Fafard"), &receiver, "mb42"); - simgrid::s4u::Actor::create("sender2", simgrid::s4u::Host::by_name("Jupiter"), "sender", args); + /* If your actor is getting more complex, you probably want to implement it as a class instead, + * as we do here for the sender actors. The main behavior goes into operator()() of the class. + * + * You can then directly start your actor, as follows: */ + simgrid::s4u::Actor::create("sender1", simgrid::s4u::Host::by_name("Tremblay"), Sender()); + /* If you want to pass parameters to your class, that's very easy: just use your constructors */ + simgrid::s4u::Actor::create("sender2", simgrid::s4u::Host::by_name("Jupiter"), Sender("GloubiBoulga")); - /* 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.load_deployment("s4u-actor-create_d.xml"); // And then, you load the deployment file + /* But starting actors directly is considered as a bad experimental habit, since it ties the code + * you want to test with the experimental scenario. Starting your actors from an external deployment + * file in XML ensures that you can test your code in several scenarios without changing the code itself. + * + * For that, you first need to register your function or your actor as follows. + * Actor classes must have a (std::vector) constructor, + * and actor functions must be of type int(*)(int argc, char**argv). */ + e.register_actor("sender"); // The sender class is passed as a template parameter here + e.register_function("forwarder", &forwarder); + /* Once actors and functions are registered, just load the deployment file */ + e.load_deployment("s4u-actor-create_d.xml"); /* Once every actors are started in the engine, the simulation can start */ e.run(); diff --git a/examples/s4u/actor-create/s4u-actor-create.tesh b/examples/s4u/actor-create/s4u-actor-create.tesh index 67c1549414..58a2574242 100644 --- a/examples/s4u/actor-create/s4u-actor-create.tesh +++ b/examples/s4u/actor-create/s4u-actor-create.tesh @@ -1,10 +1,13 @@ #!/usr/bin/env tesh $ $SG_TEST_EXENV ${bindir:=.}/s4u-actor-create -> [Tremblay:sender1:(1) 0.000000] [s4u_actor_create/INFO] Hello s4u, I have something to send -> [Jupiter:sender2:(2) 0.000000] [s4u_actor_create/INFO] Hello s4u, I have something to send -> [Fafard:receiver:(3) 0.000000] [s4u_actor_create/INFO] Hello s4u, I'm ready to get any message you'd want on mb42 -> [Tremblay:sender1:(1) 0.025709] [s4u_actor_create/INFO] I'm done. See you. -> [Jupiter:sender2:(2) 0.070434] [s4u_actor_create/INFO] I'm done. See you. -> [Fafard:receiver:(3) 0.070434] [s4u_actor_create/INFO] I received 'GaBuZoMeu' and 'GloubiBoulga' -> [Fafard:receiver:(3) 0.070434] [s4u_actor_create/INFO] I'm done. See you. +> [Tremblay:sender1:(2) 0.000000] [s4u_actor_create/INFO] Hello s4u, I have something to send +> [Jupiter:sender2:(3) 0.000000] [s4u_actor_create/INFO] Hello s4u, I have something to send +> [Fafard:sender:(4) 0.000000] [s4u_actor_create/INFO] Hello s4u, I have something to send +> [Fafard:receiver:(1) 0.000000] [s4u_actor_create/INFO] Hello s4u, I'm ready to get any message you'd want on mb42 +> [Fafard:sender:(4) 0.016392] [s4u_actor_create/INFO] I'm done. See you. +> [Ginette:forwarder:(5) 0.016392] [s4u_actor_create/INFO] Forward 'PopPop!'. +> [Tremblay:sender1:(2) 0.025709] [s4u_actor_create/INFO] I'm done. See you. +> [Jupiter:sender2:(3) 0.070434] [s4u_actor_create/INFO] I'm done. See you. +> [Fafard:receiver:(1) 0.086825] [s4u_actor_create/INFO] I received 'GaBuZoMeu', 'GloubiBoulga' and 'PopPop!' +> [Fafard:receiver:(1) 0.086825] [s4u_actor_create/INFO] I'm done. See you. diff --git a/examples/s4u/actor-create/s4u-actor-create_d.xml b/examples/s4u/actor-create/s4u-actor-create_d.xml index 11e719b75b..43f3b33016 100644 --- a/examples/s4u/actor-create/s4u-actor-create_d.xml +++ b/examples/s4u/actor-create/s4u-actor-create_d.xml @@ -8,8 +8,12 @@ - Instead, you want to start all your actors from the deployment file. --> - - - + + + + + + + -- 2.20.1