Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
renaming properties to platform-properties
[simgrid.git] / examples / msg / actions-comm / actions-comm.c
1 /* Copyright (c) 2009-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "simgrid/msg.h"
8 #include "simgrid/simix.h"      /* semaphores for the barrier */
9 #include <xbt/replay.h>
10
11 /** @addtogroup MSG_examples
12  *
13  *  @section msg_ex_actions Trace driven simulations
14  * 
15  *  This section details how to run trace-driven simulations. It is very handy when you  want to test an algorithm or
16  *  protocol that does nothing unless it receives some events from outside. For example, a P2P protocol reacts to
17  *  requests from the user, but does nothing if there is no such event.
18  * 
19  *  In such situations, SimGrid allows you to write your protocol in a C file, and the events to react to in a separate
20  *  text file. Declare a function handling each of the events that you want to accept in your trace files, register
21  *  them using \ref xbt_replay_action_register in your main, and then use \ref MSG_action_trace_run to launch the
22  *  simulation. You can either have one trace file containing all your events, or a file per simulated process. Check
23  *  the tesh files in the example directories for details on how to do it.
24  *
25  *  - <b>Communication: actions-comm/actions-comm.c</b>. This example comes with a set of event handlers reproducing
26  *  some classical communication primitives (synchronous and asynchronous send/receive, broadcast, barrier, ...).
27  */
28
29 XBT_LOG_NEW_DEFAULT_CATEGORY(actions, "Messages specific for this msg example");
30 int communicator_size = 0;
31
32 static void action_Isend(const char *const *action);
33
34 typedef struct {
35   int last_Irecv_sender_id;
36   int bcast_counter;
37   xbt_dynar_t isends;           /* of msg_comm_t */
38   /* Used to implement irecv+wait */
39   xbt_dynar_t irecvs;           /* of msg_comm_t */
40   xbt_dynar_t tasks;            /* of msg_task_t */
41 } s_process_globals_t, *process_globals_t;
42
43 /* Helper function */
44 static double parse_double(const char *string)
45 {
46   double value;
47   char *endptr;
48
49   value = strtod(string, &endptr);
50   if (*endptr != '\0')
51     THROWF(unknown_error, 0, "%s is not a double", string);
52   return value;
53 }
54
55 #define ACT_DEBUG(...) \
56   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) {  \
57     char *NAME = xbt_str_join_array(action, " ");              \
58     XBT_DEBUG(__VA_ARGS__);                                    \
59     xbt_free(NAME);                                            \
60   } else ((void)0)
61
62 static void log_action(const char *const *action, double date)
63 {
64   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) {
65     char *name = xbt_str_join_array(action, " ");
66     XBT_VERB("%s %f", name, date);
67     xbt_free(name);
68   }
69 }
70
71 static void asynchronous_cleanup(void)
72 {
73   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
74
75   /* Destroy any isend which correspond to completed communications */
76   int found;
77   msg_comm_t comm;
78   while ((found = MSG_comm_testany(globals->isends)) != -1) {
79     xbt_dynar_remove_at(globals->isends, found, &comm);
80     MSG_comm_destroy(comm);
81   }
82 }
83
84 /* My actions */
85 static void action_send(const char *const *action)
86 {
87   char to[250];
88   const char *size_str = action[3];
89   double size = parse_double(size_str);
90   double clock = MSG_get_clock();
91
92   sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
93
94   ACT_DEBUG("Entering Send: %s (size: %g)", NAME, size);
95   if (size < 65536) {
96     action_Isend(action);
97   } else {
98     MSG_task_send(MSG_task_create(to, 0, size, NULL), to);
99   }
100
101   log_action(action, MSG_get_clock() - clock);
102   asynchronous_cleanup();
103 }
104
105 static void action_Isend(const char *const *action)
106 {
107   char to[250];
108   const char *size = action[3];
109   double clock = MSG_get_clock();
110   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
111
112   sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
113   msg_comm_t comm = MSG_task_isend(MSG_task_create(to, 0, parse_double(size), NULL), to);
114   xbt_dynar_push(globals->isends, &comm);
115
116   XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
117   log_action(action, MSG_get_clock() - clock);
118   asynchronous_cleanup();
119 }
120
121 static void action_recv(const char *const *action)
122 {
123   char mailbox_name[250];
124   msg_task_t task = NULL;
125   double clock = MSG_get_clock();
126
127   sprintf(mailbox_name, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
128
129   ACT_DEBUG("Receiving: %s", NAME);
130   msg_error_t res = MSG_task_receive(&task, mailbox_name);
131   log_action(action, MSG_get_clock() - clock);
132
133   if (res == MSG_OK) {
134     MSG_task_destroy(task);
135   }
136   asynchronous_cleanup();
137 }
138
139 static void action_Irecv(const char *const *action)
140 {
141   char mailbox[250];
142   double clock = MSG_get_clock();
143   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
144
145   XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
146
147   sprintf(mailbox, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
148   msg_task_t t = NULL;
149   xbt_dynar_push(globals->tasks, &t);
150   msg_comm_t c = MSG_task_irecv(xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks) - 1), mailbox);
151   xbt_dynar_push(globals->irecvs, &c);
152
153   log_action(action, MSG_get_clock() - clock);
154   asynchronous_cleanup();
155 }
156
157 static void action_wait(const char *const *action)
158 {
159   msg_task_t task = NULL;
160   msg_comm_t comm;
161   double clock = MSG_get_clock();
162   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
163
164   xbt_assert(xbt_dynar_length(globals->irecvs), "action wait not preceded by any irecv: %s",
165              xbt_str_join_array(action, " "));
166
167   ACT_DEBUG("Entering %s", NAME);
168   comm = xbt_dynar_pop_as(globals->irecvs, msg_comm_t);
169   MSG_comm_wait(comm, -1);
170   task = xbt_dynar_pop_as(globals->tasks, msg_task_t);
171   MSG_comm_destroy(comm);
172   MSG_task_destroy(task);
173
174   log_action(action, MSG_get_clock() - clock);
175 }
176
177 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
178 static void action_barrier(const char *const *action)
179 {
180   static smx_mutex_t mutex = NULL;
181   static smx_cond_t cond = NULL;
182   static int processes_arrived_sofar = 0;
183
184   if (mutex == NULL) {          // first arriving on the barrier
185     mutex = simcall_mutex_init();
186     cond = simcall_cond_init();
187     processes_arrived_sofar = 0;
188   }
189   ACT_DEBUG("Entering barrier: %s (%d already there)", NAME, processes_arrived_sofar);
190
191   simcall_mutex_lock(mutex);
192   if (++processes_arrived_sofar == communicator_size) {
193     simcall_cond_broadcast(cond);
194     simcall_mutex_unlock(mutex);
195   } else {
196     simcall_cond_wait(cond, mutex);
197     simcall_mutex_unlock(mutex);
198   }
199
200   ACT_DEBUG("Exiting barrier: %s", NAME);
201
202   processes_arrived_sofar--;
203   if (!processes_arrived_sofar) {
204     SIMIX_cond_destroy(cond);
205     SIMIX_mutex_destroy(mutex);
206     mutex = NULL;
207   }
208 }
209
210 static void action_bcast(const char *const *action)
211 {
212   int i;
213   char *bcast_identifier;
214   char mailbox[80];
215   double comm_size = parse_double(action[2]);
216   msg_task_t task = NULL;
217   const char *process_name;
218   double clock = MSG_get_clock();
219
220   process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
221
222   xbt_assert(communicator_size, "Size of Communicator is not defined, can't use collective operations");
223
224   process_name = MSG_process_get_name(MSG_process_self());
225
226   bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
227
228   if (!strcmp(process_name, "p0")) {
229     XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
230
231     msg_comm_t *comms = xbt_new0(msg_comm_t, communicator_size - 1);
232
233     for (i = 1; i < communicator_size; i++) {
234       sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
235       comms[i - 1] = MSG_task_isend(MSG_task_create(mailbox, 0, comm_size, NULL), mailbox);
236     }
237     MSG_comm_waitall(comms, communicator_size - 1, -1);
238     for (i = 1; i < communicator_size; i++)
239       MSG_comm_destroy(comms[i - 1]);
240     xbt_free(comms);
241
242     XBT_DEBUG("%s: all messages sent by %s have been received", bcast_identifier, process_name);
243   } else {
244     sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
245     MSG_task_receive(&task, mailbox);
246     MSG_task_destroy(task);
247     XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
248   }
249
250   log_action(action, MSG_get_clock() - clock);
251   xbt_free(bcast_identifier);
252 }
253
254 static void action_comm_size(const char *const *action)
255 {
256   const char *size = action[2];
257   double clock = MSG_get_clock();
258
259   communicator_size = parse_double(size);
260   log_action(action, MSG_get_clock() - clock);
261 }
262
263 static void action_compute(const char *const *action)
264 {
265   const char *amount = action[2];
266   msg_task_t task = MSG_task_create("task", parse_double(amount), 0, NULL);
267   double clock = MSG_get_clock();
268
269   ACT_DEBUG("Entering %s", NAME);
270   MSG_task_execute(task);
271   MSG_task_destroy(task);
272   log_action(action, MSG_get_clock() - clock);
273 }
274
275 static void action_init(const char *const *action)
276 {
277   XBT_DEBUG("Initialize the counters");
278   process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
279   globals->isends = xbt_dynar_new(sizeof(msg_comm_t), NULL);
280   globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t), NULL);
281   globals->tasks = xbt_dynar_new(sizeof(msg_task_t), NULL);
282   MSG_process_set_data(MSG_process_self(), globals);
283 }
284
285 static void action_finalize(const char *const *action)
286 {
287   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
288   if (globals) {
289     asynchronous_cleanup();
290     xbt_dynar_free_container(&(globals->isends));
291     xbt_dynar_free_container(&(globals->irecvs));
292     xbt_dynar_free_container(&(globals->tasks));
293     xbt_free(globals);
294   }
295 }
296
297 /** Main function */
298 int main(int argc, char *argv[])
299 {
300   msg_error_t res = MSG_OK;
301
302   /* Check the given arguments */
303   MSG_init(&argc, argv);
304   /* Explicit initialization of the action module is required now*/
305   MSG_action_init();
306
307   xbt_assert(argc > 2,
308        "Usage: %s platform_file deployment_file [action_files]\n"
309        "\t# if all actions are in the same file\n"
310        "\tExample: %s msg_platform.xml msg_deployment.xml actions\n"
311        "\t# if actions are in separate files, specified in deployment\n"
312        "\tExample: %s msg_platform.xml msg_deployment.xml ",
313        argv[0],argv[0],argv[0]);
314
315   MSG_create_environment(argv[1]);
316   MSG_launch_application(argv[2]);
317
318   /*   Action registration */
319   xbt_replay_action_register("init", action_init);
320   xbt_replay_action_register("finalize", action_finalize);
321   xbt_replay_action_register("comm_size", action_comm_size);
322   xbt_replay_action_register("send", action_send);
323   xbt_replay_action_register("Isend", action_Isend);
324   xbt_replay_action_register("recv", action_recv);
325   xbt_replay_action_register("Irecv", action_Irecv);
326   xbt_replay_action_register("wait", action_wait);
327   xbt_replay_action_register("barrier", action_barrier);
328   xbt_replay_action_register("bcast", action_bcast);
329   xbt_replay_action_register("compute", action_compute);
330
331   /* Actually do the simulation using MSG_action_trace_run */
332   res = MSG_action_trace_run(argv[3]);  // it's ok to pass a NULL argument here
333
334   XBT_INFO("Simulation time %g", MSG_get_clock());
335
336   /* Explicit finalization of the action module is required now*/
337   MSG_action_exit();
338
339   return res != MSG_OK;
340 }