Logo AND Algorithmique Numérique Distribuée

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