Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'pikachuyann/simgrid-xbt_random'
[simgrid.git] / examples / c / actor-create / actor-create.c
1 /* Copyright (c) 2006-2018. 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 paramter 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/actor.h>
21 #include <simgrid/engine.h>
22 #include <simgrid/host.h>
23 #include <simgrid/mailbox.h>
24 #include <xbt/asserts.h>
25 #include <xbt/log.h>
26
27 // This declares a logging channel so that XBT_INFO can be used later
28 XBT_LOG_NEW_DEFAULT_CATEGORY(actor_create, "The logging channel used in this example");
29
30 /* Our first class of actors is simply implemented with a function.
31  * As every CSG actors, its parameters are in a vector of string (argc/argv), and it does not return anything.
32  *
33  * One 'receiver' actor is instantiated within the simulation later in this file.
34  */
35 static void receiver(int argc, char** argv)
36 {
37   xbt_assert(argc == 2, "This actor expects a single argument: the mailbox on which to get messages");
38   sg_mailbox_t mailbox = sg_mailbox_by_name(argv[1]);
39
40   XBT_INFO("Hello, I'm ready to get any message you'd want on %s", argv[1]);
41
42   const char* msg1 = sg_mailbox_get(mailbox);
43   const char* msg2 = sg_mailbox_get(mailbox);
44   const char* msg3 = sg_mailbox_get(mailbox);
45   XBT_INFO("I received '%s', '%s' and '%s'", msg1, msg2, msg3);
46   XBT_INFO("I'm done. See you.");
47 }
48
49 /* Our second class of actors, in charge of sending stuff */
50 static void sender(int argc, char** argv)
51 {
52   xbt_assert(argc == 3, "Actor 'sender' requires 2 parameters (mailbox and data to send), but got only %d", argc - 1);
53   XBT_INFO("Hello, I have something to send");
54   char* sent_data      = argv[1];
55   sg_mailbox_t mailbox = sg_mailbox_by_name(argv[2]);
56
57   sg_mailbox_put(mailbox, xbt_strdup(sent_data), strlen(sent_data));
58   XBT_INFO("I'm done. See you.");
59 }
60
61 static void forwarder(int argc, char** argv)
62 {
63   xbt_assert(argc >= 3, "Actor forwarder requires 2 parameters, but got only %d", argc - 1);
64   sg_mailbox_t mailbox_in  = sg_mailbox_by_name(argv[1]);
65   sg_mailbox_t mailbox_out = sg_mailbox_by_name(argv[2]);
66   char* msg                = (char*)(sg_mailbox_get(mailbox_in));
67   XBT_INFO("Forward '%s'.", msg);
68   sg_mailbox_put(mailbox_out, msg, strlen(msg));
69 }
70
71 /* Here comes the main function of your program */
72 int main(int argc, char** argv)
73 {
74   /* When your program starts, you have to first start a new simulation engine, as follows */
75   simgrid_init(&argc, argv);
76
77   /* Then you should load a platform file, describing your simulated platform */
78   simgrid_load_platform("../../platforms/small_platform.xml");
79
80   /* And now you have to ask SimGrid to actually start your actors.
81    *
82    * The easiest way to do so is to implement the behavior of your actor in a single function,
83    * as we do here for the receiver actors. This function can take any kind of parameters, as
84    * long as the last parameters of Actor::create() match what your function expects.
85    */
86   int recv_argc           = 2;
87   const char* recv_argv[] = {"receiver", "mb42", NULL};
88   sg_actor_t recv         = sg_actor_init("receiver", sg_host_by_name("Fafard"));
89   sg_actor_start(recv, receiver, recv_argc, recv_argv);
90
91   int sender1_argc           = 3;
92   const char* sender1_argv[] = {"sender", "GaBuZoMeu", "mb42", NULL};
93   sg_actor_t sender1         = sg_actor_init("sender1", sg_host_by_name("Tremblay"));
94   sg_actor_start(sender1, sender, sender1_argc, sender1_argv);
95
96   int sender2_argc           = 3;
97   const char* sender2_argv[] = {"sender", "GloubiBoulga", "mb42", NULL};
98   sg_actor_t sender2         = sg_actor_init("sender2", sg_host_by_name("Jupiter"));
99   sg_actor_start(sender2, sender, sender2_argc, sender2_argv);
100
101   /* But starting actors directly is considered as a bad experimental habit, since it ties the code
102    * you want to test with the experimental scenario. Starting your actors from an external deployment
103    * file in XML ensures that you can test your code in several scenarios without changing the code itself.
104    *
105    * For that, you first need to register your function or your actor as follows.
106    * actor functions must be of type void(*)(int argc, char**argv). */
107   simgrid_register_function("sender", sender);
108   simgrid_register_function("forwarder", forwarder);
109   /* Once actors and functions are registered, just load the deployment file */
110   simgrid_load_deployment("actor-create_d.xml");
111
112   /* Once every actors are started in the engine, the simulation can start */
113   simgrid_run();
114
115   /* Once the simulation is done, the program is ended */
116   return 0;
117 }