Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3c9b931486a2b54599dec13e47e69f39dd8d0e47
[simgrid.git] / examples / s4u / launching / s4u_launching.cpp
1 /* Copyright (c) 2006-2016. 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
7 /* This example shows how to declare and start your actors.
8  *
9  * The first step is to declare the code of your actors (what they do exactly
10  * does not matter to this example) and then you ask SimGrid to start your
11  * actors. There is three ways of doing so:
12  *  - Directly, by instantiating your actor as paramter to Actor::create();
13  *  - By first registering your actors before instantiating it;
14  *  - Through the deployment file.
15  *
16  * This example shows all these solutions, even if you obviously should use
17  * only one of these solutions to start your actors. The most advised solution
18  * is to use a deployment file, as it creates a clear separation between your
19  * application and the settings to test it. This is a better scientific
20  * methodology. Actually, starting an actor with Actor::create() is mostly
21  * useful to start an actor from another actor.
22  *
23  */
24
25 #include <simgrid/s4u.hpp>
26
27 // This declares a logging channel so that XBT_INFO can be used later
28 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_launching_test, "The logging channel used in this example");
29
30
31 /* Declares a first class of actors which sends a message to the mailbox 'mb42'.
32  * The sent message is what was passed as parameter on creation (or 'GaBuZoMeu' by default)
33  *
34  * Later, this actor class is instantiated twice in the simulation.
35  */
36 class Sender {
37 public:
38   const char *msg = "GaBuZoMeu";
39   Sender() {
40     /* Constructor used when no parameter is passed to the actor */
41   };
42   Sender(std::vector<std::string> args) {
43     /* This constructor is used when we pass parameters to the actor */
44     if (args.size() > 0)
45       msg = args[0].c_str();
46   }
47   void operator()() {
48     XBT_INFO("Hello s4u, I have something to send");
49     simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName("mb42");
50
51     simgrid::s4u::this_actor::send(mailbox, xbt_strdup(msg), strlen(msg));
52     XBT_INFO("I'm done. See you.");
53   }
54 };
55
56
57 /* Declares a second class of actor which receive two messages on the mailbox which
58  * name is passed as parameter ('thingy' by default, ie the wrong one).
59  *
60  * Later, this actor class is instantiated once in the simulation.
61  */
62 class Receiver {
63 public:
64   simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName("thingy");
65
66   Receiver() {};
67   Receiver(std::vector<std::string> args) {
68     /* This constructor is used when we pass parameters to the actor */
69     /* as with argc/argv, args[0] is the actor's name, so the first parameter is args[1] */
70     if (args.size() > 1)
71       mailbox = simgrid::s4u::Mailbox::byName(args[1]);
72   }
73   void operator()() {
74     XBT_INFO("Hello s4u, I'm ready to get any message you'd want on %s", mailbox->getName());
75
76     char *msg1 = static_cast<char*>(simgrid::s4u::this_actor::recv(mailbox));
77     char *msg2 = static_cast<char*>(simgrid::s4u::this_actor::recv(mailbox));
78     XBT_INFO("I received '%s' and '%s'",msg1,msg2);
79     XBT_INFO("I'm done. See you.");
80   }
81 };
82
83
84 /* Here comes the main function of your program */
85 int main(int argc, char **argv) {
86   /* When your program starts, you have to first start a new simulation engine, as follows */
87   simgrid::s4u::Engine *e = new simgrid::s4u::Engine(&argc,argv);
88
89   /* Then you should load a platform file, describing your simulated platform */
90   e->loadPlatform("../../platforms/small_platform.xml");
91
92   /* And now you have to ask SimGrid to actually start your actors.
93    *
94    * You can first directly start your actor, as follows. Note the last parameter: 'Sender()',
95    * as if you would call the Sender function.
96    */
97   simgrid::s4u::Actor::createActor("sender1", simgrid::s4u::Host::by_name("Tremblay"), Sender());
98
99   /* The second way is to first register your function, and then retrieve it */
100   e->registerFunction<Sender>("sender"); // The sender is passed as a template parameter here
101   std::vector<std::string> args;  // Here we declare the parameter that the actor will get
102   args.push_back("GloubiBoulga"); // Add a parameter to the set (we could have done it in the first approach too)
103
104   simgrid::s4u::Actor::createActor("sender2", simgrid::s4u::Host::by_name("Jupiter"), "sender", args);
105
106   /* The third way to start your actors is to use a deployment file. */
107   e->registerFunction<Receiver>("receiver"); // You first have to register the actor as with the second approach
108   e->loadDeployment("deployment.xml");       // And then, you load the deployment file
109
110   /* Once every actors are started in the engine, the simulation can start */
111   e->run();
112
113   /* Once the simulation is done, the program is ended */
114   return 0;
115 }