1 /* Copyright (c) 2006-2018. The SimGrid Team. All rights reserved. */
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. */
6 /* This example shows how to declare and start your actors.
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.
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.
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>
27 // This declares a logging channel so that XBT_INFO can be used later
28 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_actor_create, "The logging channel used in this example");
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.
33 * One 'receiver' actor is instantiated within the simulation later in this file.
35 static int receiver(int argc, char** argv)
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]);
40 XBT_INFO("Hello s4u, I'm ready to get any message you'd want on %s", argv[1]);
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.");
50 /* Our second class of actors, in charge of sending stuff */
51 static int sender(int argc, char** argv)
53 xbt_assert(argc == 3, "Actor 'sender' requires 2 parameters (mailbox and data to send), but got only %d", argc - 1);
54 XBT_INFO("Hello s4u, I have something to send");
55 sg_mailbox_t mailbox = sg_mailbox_by_name(argv[1]);
56 char* sent_data = argv[2];
58 sg_mailbox_put(mailbox, xbt_strdup(sent_data), strlen(sent_data));
59 XBT_INFO("I'm done. See you.");
63 /* Here comes the main function of your program */
64 int main(int argc, char** argv)
66 /* When your program starts, you have to first start a new simulation engine, as follows */
67 simgrid_init(&argc, argv);
69 /* Then you should load a platform file, describing your simulated platform */
70 simgrid_load_platform("../../platforms/small_platform.xml");
72 /* And now you have to ask SimGrid to actually start your actors.
74 * The easiest way to do so is to implement the behavior of your actor in a single function,
75 * as we do here for the receiver actors. This function can take any kind of parameters, as
76 * long as the last parameters of Actor::create() match what your function expects.
79 const char* recv_argv[] = {"receiver", "mb42", NULL};
80 sg_actor_t recv = sg_actor_init("receiver", sg_host_by_name("Fafard"));
81 sg_actor_start(recv, receiver, recv_argc, recv_argv);
83 /* But starting actors directly is considered as a bad experimental habit, since it ties the code
84 * you want to test with the experimental scenario. Starting your actors from an external deployment
85 * file in XML ensures that you can test your code in several scenarios without changing the code itself.
87 * For that, you first need to register your function or your actor as follows.
88 * Actor classes must have a (std::vector<std::string>) constructor,
89 * and actor functions must be of type int(*)(int argc, char**argv). */
90 simgrid_register_function("sender", &sender);
91 /* Once actors and functions are registered, just load the deployment file */
92 simgrid_load_deployment("csg-actor-create_d.xml");
94 /* Once every actors are started in the engine, the simulation can start */
97 /* Once the simulation is done, the program is ended */