[SimGrid-user] Complex arguments for s4u::Actor::createActor

Martin Quinson martin.quinson at ens-rennes.fr
Fri Jul 14 00:03:10 CEST 2017


Hello,

I don't see not trivial solution right now and your proposal is so
awful that my mailer refused to display it up to the end :) 

Could you please open an issue on github so that we don't forget?

Thanks & sorry,
Mt.

On Thu, Jul 13, 2017 at 04:36:02PM +0200, Pierre-Fran├žois Dutot wrote:
> Dear all,
> 
> 
> I am trying to move from MSG to s4u, and encounter the following question. I
> used to pass complex structures to MSG_process_create, and I am now limited
> to std::vector<std::string> types of arguments for actors created with s4u.
> 
> 
> I found an ugly hack (described in the attached file), but I wonder if there
> isn't a more elegant way to pass my complex structures to the new Actor ?
> 
> 
> As you can see, the attached example is directly inspired from
> simgrid/examples/s4u/actor-create/s4u_actor-create.cpp
> 
> 
> Any thoughts ?
> 
> 
> Thanks,
> 
> Pierre-Fran├žois.
> 
> 

> /* 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. */
> 
> /* 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 <simgrid/s4u.hpp>
> 
> // 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");
> 
> struct MyComplexStruct {
>   std::string msg;
>   // Assume that there are plenty of fields here
> };
> 
> std::map<std::string, MyComplexStruct> HorribleGlobalVariable;
> 
> /* 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:
>   explicit Sender(std::vector<std::string> args)
>   {
>     local_struct.msg = ((HorribleGlobalVariable.at(args[0])).msg);
>   }
>   void operator()()
>   {
>     XBT_INFO("Hello s4u, I have something to send");
>     simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName("mb42");
> 
>     mailbox->put(xbt_strdup(local_struct.msg.c_str()), local_struct.msg.size());
>     XBT_INFO("I'm done. See you.");
>   }
> 
> private:
>   MyComplexStruct local_struct;
> };
> 
> /* 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<std::string> 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->getName());
> 
>     char* msg1 = static_cast<char*>(mailbox->get());
>     XBT_INFO("I received '%s'", msg1);
>     xbt_free(msg1);
>     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);
> 
>   /* Then you should load a platform file, describing your simulated platform */
>   e->loadPlatform("../../platforms/small_platform.xml");
> 
>   MyComplexStruct example_struct;
>   // initialization will obviously be more complex than that
>   example_struct.msg = "Testing the struct";
> 
>   HorribleGlobalVariable["GloubiBoulga"] = example_struct;
>   
>   /* The second way is to first register your function, and then retrieve it */
>   e->registerFunction<Sender>("sender"); // The sender is passed as a template parameter here
>   std::vector<std::string> 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>("receiver");   // You first have to register the actor as with the second approach
>   e->loadDeployment("s4u_actor-create_d.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 */
>   delete e;
> 
>   return 0;
> }
> 
> 

> _______________________________________________
> Simgrid-user mailing list
> Simgrid-user at lists.gforge.inria.fr
> https://lists.gforge.inria.fr/mailman/listinfo/simgrid-user


-- 
Don't ask me to add a feature. Send me a patch.  -- Neil Spring
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://lists.gforge.inria.fr/pipermail/simgrid-user/attachments/20170714/3250b395/attachment.sig>


More information about the Simgrid-user mailing list