Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / examples / s4u / actor-create / s4u-actor-create.cpp
1 /* Copyright (c) 2006-2021. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 /* This example shows how to declare and start your actors.
7  *
8  * The first step is to declare the code of your actors (what they do exactly does not matter to this example) and then
9  * you ask SimGrid to start your actors. There is three ways of doing so:
10  *  - Directly, by instantiating your actor as parameter to Actor::create()
11  *  - By first registering your actors before instantiating it
12  *  - Through the deployment file.
13  *
14  * This example shows all these solutions, even if you obviously should use only one of these solutions to start your
15  * actors. The most advised solution is to use a deployment file, as it creates a clear separation between your
16  * application and the settings to test it. This is a better scientific methodology. Actually, starting an actor with
17  * Actor::create() is mostly useful to start an actor from another actor.
18  */
19
20 #include <simgrid/s4u.hpp>
21 #include <string>
22
23 // This declares a logging channel so that XBT_INFO can be used later
24 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor_create, "The logging channel used in this example");
25
26 /* Our first class of actors is simply implemented with a function, that takes a single string as parameter.
27  *
28  * Later, this actor class is instantiated within the simulation.
29  */
30 static void receiver(const std::string& mailbox_name)
31 {
32   simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name(mailbox_name);
33
34   XBT_INFO("Hello s4u, I'm ready to get any message you'd want on %s", mailbox->get_cname());
35
36   auto msg1 = mailbox->get_unique<std::string>();
37   auto msg2 = mailbox->get_unique<std::string>();
38   auto msg3 = mailbox->get_unique<std::string>();
39   XBT_INFO("I received '%s', '%s' and '%s'", msg1->c_str(), msg2->c_str(), msg3->c_str());
40   XBT_INFO("I'm done. See you.");
41 }
42
43 /* Our second class of actors is also a function */
44 static void forwarder(int argc, char** argv)
45 {
46   xbt_assert(argc >= 3, "Actor forwarder requires 2 parameters, but got only %d", argc - 1);
47   simgrid::s4u::Mailbox* in    = simgrid::s4u::Mailbox::by_name(argv[1]);
48   simgrid::s4u::Mailbox* out   = simgrid::s4u::Mailbox::by_name(argv[2]);
49   auto* msg                    = in->get<std::string>();
50   XBT_INFO("Forward '%s'.", msg->c_str());
51   out->put(msg, msg->size());
52 }
53
54 /* Declares a third class of actors which sends a message to the mailbox 'mb42'.
55  * The sent message is what was passed as parameter on creation (or 'GaBuZoMeu' by default)
56  *
57  * Later, this actor class is instantiated twice in the simulation.
58  */
59 class Sender {
60 public:
61   std::string mbox  = "mb42";
62   std::string msg = "GaBuZoMeu";
63   explicit Sender() = default; /* Sending the default message */
64   explicit Sender(const std::string& arg) : msg(arg) { /* Sending the specified message */}
65   explicit Sender(std::vector<std::string> args)
66   {
67     /* This constructor is used when we start the actor from the deployment file */
68     /* args[0] is the actor's name, so the first parameter is args[1] */
69
70     xbt_assert(args.size() >= 3, "The sender is expecting 2 parameters from the deployment file but got %zu",
71                args.size() - 1);
72     msg  = args[1];
73     mbox = args[2];
74   }
75   void operator()() const /* This is the main code of the actor */
76   {
77     XBT_INFO("Hello s4u, I have something to send");
78     simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name(mbox);
79
80     mailbox->put(new std::string(msg), msg.size());
81     XBT_INFO("I'm done. See you.");
82   }
83 };
84
85 /* Here comes the main function of your program */
86 int main(int argc, char** argv)
87 {
88   /* When your program starts, you have to first start a new simulation engine, as follows */
89   simgrid::s4u::Engine e(&argc, argv);
90
91   /* Then you should load a platform file, describing your simulated platform */
92   e.load_platform("../../platforms/small_platform.xml");
93
94   /* And now you have to ask SimGrid to actually start your actors.
95    *
96    * The easiest way to do so is to implement the behavior of your actor in a single function,
97    * as we do here for the receiver actors. This function can take any kind of parameters, as
98    * long as the last parameters of Actor::create() match what your function expects.
99    */
100   simgrid::s4u::Actor::create("receiver", simgrid::s4u::Host::by_name("Fafard"), &receiver, "mb42");
101
102   /* If your actor is getting more complex, you probably want to implement it as a class instead,
103    * as we do here for the sender actors. The main behavior goes into operator()() of the class.
104    *
105    * You can then directly start your actor, as follows: */
106   simgrid::s4u::Actor::create("sender1", simgrid::s4u::Host::by_name("Tremblay"), Sender());
107   /* If you want to pass parameters to your class, that's very easy: just use your constructors */
108   simgrid::s4u::Actor::create("sender2", simgrid::s4u::Host::by_name("Jupiter"), Sender("GloubiBoulga"));
109
110   /* But starting actors directly is considered as a bad experimental habit, since it ties the code
111    * you want to test with the experimental scenario. Starting your actors from an external deployment
112    * file in XML ensures that you can test your code in several scenarios without changing the code itself.
113    *
114    * For that, you first need to register your function or your actor as follows.
115    * Actor classes must have a (std::vector<std::string>) constructor,
116    * and actor functions must be of type int(*)(int argc, char**argv). */
117   e.register_actor<Sender>("sender"); // The sender class is passed as a template parameter here
118   e.register_function("forwarder", &forwarder);
119   /* Once actors and functions are registered, just load the deployment file */
120   e.load_deployment("s4u-actor-create_d.xml");
121
122   /* Once every actors are started in the engine, the simulation can start */
123   e.run();
124
125   /* Once the simulation is done, the program is ended */
126   return 0;
127 }