Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
reduce the amount of includes to speed up recompilations
[simgrid.git] / examples / msg / actions / actions.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 <stdio.h>
8 #include <stdlib.h>
9 #include "simgrid/msg.h"        /* Yeah! If you want to use msg, you need to include simgrid/msg.h */
10 #include "simgrid/simix.h"      /* semaphores for the barrier */
11 #include <xbt/replay.h>
12
13 /** @addtogroup MSG_examples
14  *
15  *  @section MSG_ex_actions Trace driven simulations
16  * 
17  *  The <b>actions/actions.c</b> example demonstrates how to run trace-driven simulations. It
18  *  is very handy when you want to test an algorithm or protocol that
19  *  does nothing unless it receives some events from outside. For
20  *  example, a P2P protocol reacts to requests from the user, but
21  *  does nothing if there is no such event. 
22  * 
23  *  In such situations, SimGrid allows to write your protocol in your
24  *  C file, and the events to react to in a separate text file.
25  *  Declare a function handling each of the events that you want to
26  *  accept in your trace files, register them using \ref
27  *  xbt_replay_action_register in your main, and then use \ref
28  *  MSG_action_trace_run to launch the simulation. You can either
29  *  have one trace file containing all your events, or a file per
30  *  simulated process. Check the tesh files in the example directory
31  *  for details on how to do it.
32  *
33  *  This example uses this approach to replay MPI-like traces. It
34  *  comes with a set of event handlers reproducing MPI events. This
35  *  is somehow similar to SMPI, yet differently implemented. This
36  *  code should probably be changed to use SMPI internals instead,
37  *  but wasn't, so far.
38  * 
39  */
40
41 XBT_LOG_NEW_DEFAULT_CATEGORY(actions, "Messages specific for this msg example");
42 int communicator_size = 0;
43
44 static void action_Isend(const char *const *action);
45
46 typedef struct {
47   int last_Irecv_sender_id;
48   int bcast_counter;
49   int reduce_counter;
50   int allReduce_counter;
51   xbt_dynar_t isends;           /* of msg_comm_t */
52   /* Used to implement irecv+wait */
53   xbt_dynar_t irecvs;           /* of msg_comm_t */
54   xbt_dynar_t tasks;            /* of msg_task_t */
55 } s_process_globals_t, *process_globals_t;
56
57 /* Helper function */
58 static double parse_double(const char *string)
59 {
60   double value;
61   char *endptr;
62
63   value = strtod(string, &endptr);
64   if (*endptr != '\0')
65     THROWF(unknown_error, 0, "%s is not a double", string);
66   return value;
67 }
68
69 #define ACT_DEBUG(...) \
70   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) {  \
71     char *NAME = xbt_str_join_array(action, " ");              \
72     XBT_DEBUG(__VA_ARGS__);                                    \
73     xbt_free(NAME);                                            \
74   } else ((void)0)
75
76 static void log_action(const char *const *action, double date)
77 {
78   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) {
79     char *name = xbt_str_join_array(action, " ");
80     XBT_VERB("%s %f", name, date);
81     xbt_free(name);
82   }
83 }
84
85 static void asynchronous_cleanup(void)
86 {
87   process_globals_t globals =
88       (process_globals_t) MSG_process_get_data(MSG_process_self());
89
90   /* Destroy any isend which correspond to completed communications */
91   int found;
92   msg_comm_t comm;
93   while ((found = MSG_comm_testany(globals->isends)) != -1) {
94     xbt_dynar_remove_at(globals->isends, found, &comm);
95     MSG_comm_destroy(comm);
96   }
97 }
98
99 /* My actions */
100 static void action_send(const char *const *action)
101 {
102   char to[250];
103   const char *size_str = action[3];
104   double size = parse_double(size_str);
105   double clock = MSG_get_clock();       /* this "call" is free thanks to inlining */
106
107   sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
108
109   ACT_DEBUG("Entering Send: %s (size: %g)", NAME, size);
110   if (size < 65536) {
111     action_Isend(action);
112   } else {
113     MSG_task_send(MSG_task_create(to, 0, size, NULL), to);
114   }
115
116   log_action(action, MSG_get_clock() - clock);
117   asynchronous_cleanup();
118 }
119
120 static void action_Isend(const char *const *action)
121 {
122   char to[250];
123   const char *size = action[3];
124   double clock = MSG_get_clock();
125   process_globals_t globals =
126       (process_globals_t) MSG_process_get_data(MSG_process_self());
127
128   sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
129   msg_comm_t comm =
130       MSG_task_isend(MSG_task_create(to, 0, parse_double(size), NULL), to);
131   xbt_dynar_push(globals->isends, &comm);
132
133   XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
134   log_action(action, MSG_get_clock() - clock);
135   asynchronous_cleanup();
136 }
137
138
139 static void action_recv(const char *const *action)
140 {
141   char mailbox_name[250];
142   msg_task_t task = NULL;
143   double clock = MSG_get_clock();
144
145   sprintf(mailbox_name, "%s_%s", action[2],
146           MSG_process_get_name(MSG_process_self()));
147
148   ACT_DEBUG("Receiving: %s", NAME);
149   msg_error_t res = MSG_task_receive(&task, mailbox_name);
150   log_action(action, MSG_get_clock() - clock);
151
152   if (res == MSG_OK) {
153     MSG_task_destroy(task);
154   }
155   asynchronous_cleanup();
156 }
157
158 static void action_Irecv(const char *const *action)
159 {
160   char mailbox[250];
161   double clock = MSG_get_clock();
162   process_globals_t globals =
163       (process_globals_t) MSG_process_get_data(MSG_process_self());
164
165   XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
166
167   sprintf(mailbox, "%s_%s", action[2],
168           MSG_process_get_name(MSG_process_self()));
169   msg_task_t t = NULL;
170   xbt_dynar_push(globals->tasks, &t);
171   msg_comm_t c =
172       MSG_task_irecv(xbt_dynar_get_ptr
173                      (globals->tasks, xbt_dynar_length(globals->tasks) - 1),
174                      mailbox);
175   xbt_dynar_push(globals->irecvs, &c);
176
177   log_action(action, MSG_get_clock() - clock);
178   asynchronous_cleanup();
179 }
180
181
182 static void action_wait(const char *const *action)
183 {
184   msg_task_t task = NULL;
185   msg_comm_t comm;
186   double clock = MSG_get_clock();
187   process_globals_t globals =
188       (process_globals_t) MSG_process_get_data(MSG_process_self());
189
190   xbt_assert(xbt_dynar_length(globals->irecvs),
191              "action wait not preceded by any irecv: %s",
192              xbt_str_join_array(action, " "));
193
194   ACT_DEBUG("Entering %s", NAME);
195   comm = xbt_dynar_pop_as(globals->irecvs, msg_comm_t);
196   MSG_comm_wait(comm, -1);
197   task = xbt_dynar_pop_as(globals->tasks, msg_task_t);
198   MSG_comm_destroy(comm);
199   MSG_task_destroy(task);
200
201   log_action(action, MSG_get_clock() - clock);
202 }
203
204 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
205 static void action_barrier(const char *const *action)
206 {
207   static smx_mutex_t mutex = NULL;
208   static smx_cond_t cond = NULL;
209   static int processes_arrived_sofar = 0;
210
211   if (mutex == NULL) {          // first arriving on the barrier
212     mutex = simcall_mutex_init();
213     cond = simcall_cond_init();
214     processes_arrived_sofar = 0;
215   }
216   ACT_DEBUG("Entering barrier: %s (%d already there)", NAME,
217             processes_arrived_sofar);
218
219   simcall_mutex_lock(mutex);
220   if (++processes_arrived_sofar == communicator_size) {
221     simcall_cond_broadcast(cond);
222     simcall_mutex_unlock(mutex);
223   } else {
224     simcall_cond_wait(cond, mutex);
225     simcall_mutex_unlock(mutex);
226   }
227
228   ACT_DEBUG("Exiting barrier: %s", NAME);
229
230   processes_arrived_sofar--;
231   if (!processes_arrived_sofar) {
232     SIMIX_cond_destroy(cond);
233     SIMIX_mutex_destroy(mutex);
234     mutex = NULL;
235   }
236 }
237
238 static void action_reduce(const char *const *action)
239 {
240   int i;
241   char *reduce_identifier;
242   char mailbox[80];
243   double comm_size = parse_double(action[2]);
244   double comp_size = parse_double(action[3]);
245   msg_task_t comp_task = NULL;
246   const char *process_name;
247   double clock = MSG_get_clock();
248
249   process_globals_t counters =
250       (process_globals_t) MSG_process_get_data(MSG_process_self());
251
252   xbt_assert(communicator_size, "Size of Communicator is not defined, "
253              "can't use collective operations");
254
255   process_name = MSG_process_get_name(MSG_process_self());
256
257   reduce_identifier = bprintf("reduce_%d", counters->reduce_counter++);
258
259   if (!strcmp(process_name, "p0")) {
260     XBT_DEBUG("%s: %s is the Root", reduce_identifier, process_name);
261
262     msg_comm_t *comms = xbt_new0(msg_comm_t, communicator_size - 1);
263     msg_task_t *tasks = xbt_new0(msg_task_t, communicator_size - 1);
264     for (i = 1; i < communicator_size; i++) {
265       sprintf(mailbox, "%s_p%d_p0", reduce_identifier, i);
266       comms[i - 1] = MSG_task_irecv(&(tasks[i - 1]), mailbox);
267     }
268     MSG_comm_waitall(comms, communicator_size - 1, -1);
269     for (i = 1; i < communicator_size; i++) {
270       MSG_comm_destroy(comms[i - 1]);
271       MSG_task_destroy(tasks[i - 1]);
272     }
273     xbt_free(comms);
274     xbt_free(tasks);
275
276     comp_task = MSG_task_create("reduce_comp", comp_size, 0, NULL);
277     XBT_DEBUG("%s: computing 'reduce_comp'", reduce_identifier);
278     MSG_task_execute(comp_task);
279     MSG_task_destroy(comp_task);
280     XBT_DEBUG("%s: computed", reduce_identifier);
281
282   } else {
283     XBT_DEBUG("%s: %s sends", reduce_identifier, process_name);
284     sprintf(mailbox, "%s_%s_p0", reduce_identifier, process_name);
285     XBT_DEBUG("put on %s", mailbox);
286     MSG_task_send(MSG_task_create(reduce_identifier, 0, comm_size, NULL),
287                   mailbox);
288   }
289
290   log_action(action, MSG_get_clock() - clock);
291   xbt_free(reduce_identifier);
292 }
293
294 static void action_bcast(const char *const *action)
295 {
296   int i;
297   char *bcast_identifier;
298   char mailbox[80];
299   double comm_size = parse_double(action[2]);
300   msg_task_t task = NULL;
301   const char *process_name;
302   double clock = MSG_get_clock();
303
304   process_globals_t counters =
305       (process_globals_t) MSG_process_get_data(MSG_process_self());
306
307   xbt_assert(communicator_size, "Size of Communicator is not defined, "
308              "can't use collective operations");
309
310   process_name = MSG_process_get_name(MSG_process_self());
311
312   bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
313
314   if (!strcmp(process_name, "p0")) {
315     XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
316
317     msg_comm_t *comms = xbt_new0(msg_comm_t, communicator_size - 1);
318
319     for (i = 1; i < communicator_size; i++) {
320       sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
321       comms[i - 1] =
322           MSG_task_isend(MSG_task_create(mailbox, 0, comm_size, NULL), mailbox);
323     }
324     MSG_comm_waitall(comms, communicator_size - 1, -1);
325     for (i = 1; i < communicator_size; i++)
326       MSG_comm_destroy(comms[i - 1]);
327     xbt_free(comms);
328
329     XBT_DEBUG("%s: all messages sent by %s have been received",
330               bcast_identifier, process_name);
331
332   } else {
333     sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
334     MSG_task_receive(&task, mailbox);
335     MSG_task_destroy(task);
336     XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
337   }
338
339   log_action(action, MSG_get_clock() - clock);
340   xbt_free(bcast_identifier);
341 }
342
343
344 static void action_sleep(const char *const *action)
345 {
346   const char *duration = action[2];
347   double clock = MSG_get_clock();
348
349   ACT_DEBUG("Entering %s", NAME);
350   MSG_process_sleep(parse_double(duration));
351   log_action(action, MSG_get_clock() - clock);
352 }
353
354 static void action_allReduce(const char *const *action)
355 {
356   int i;
357   char *allreduce_identifier;
358   char mailbox[80];
359   double comm_size = parse_double(action[2]);
360   double comp_size = parse_double(action[3]);
361   msg_task_t task = NULL, comp_task = NULL;
362   const char *process_name;
363   double clock = MSG_get_clock();
364
365   process_globals_t counters =
366       (process_globals_t) MSG_process_get_data(MSG_process_self());
367
368   xbt_assert(communicator_size, "Size of Communicator is not defined, "
369              "can't use collective operations");
370
371   process_name = MSG_process_get_name(MSG_process_self());
372
373   allreduce_identifier = bprintf("allReduce_%d", counters->allReduce_counter++);
374
375   if (!strcmp(process_name, "p0")) {
376     XBT_DEBUG("%s: %s is the Root", allreduce_identifier, process_name);
377
378     msg_comm_t *comms = xbt_new0(msg_comm_t, communicator_size - 1);
379     msg_task_t *tasks = xbt_new0(msg_task_t, communicator_size - 1);
380     for (i = 1; i < communicator_size; i++) {
381       sprintf(mailbox, "%s_p%d_p0", allreduce_identifier, i);
382       comms[i - 1] = MSG_task_irecv(&(tasks[i - 1]), mailbox);
383     }
384     MSG_comm_waitall(comms, communicator_size - 1, -1);
385     for (i = 1; i < communicator_size; i++) {
386       MSG_comm_destroy(comms[i - 1]);
387       MSG_task_destroy(tasks[i - 1]);
388     }
389     xbt_free(tasks);
390
391     comp_task = MSG_task_create("allReduce_comp", comp_size, 0, NULL);
392     XBT_DEBUG("%s: computing 'reduce_comp'", allreduce_identifier);
393     MSG_task_execute(comp_task);
394     MSG_task_destroy(comp_task);
395     XBT_DEBUG("%s: computed", allreduce_identifier);
396
397     for (i = 1; i < communicator_size; i++) {
398       sprintf(mailbox, "%s_p0_p%d", allreduce_identifier, i);
399       comms[i - 1] =
400           MSG_task_isend(MSG_task_create(mailbox, 0, comm_size, NULL), mailbox);
401     }
402     MSG_comm_waitall(comms, communicator_size - 1, -1);
403     for (i = 1; i < communicator_size; i++)
404       MSG_comm_destroy(comms[i - 1]);
405     xbt_free(comms);
406
407     XBT_DEBUG("%s: all messages sent by %s have been received",
408               allreduce_identifier, process_name);
409
410   } else {
411     XBT_DEBUG("%s: %s sends", allreduce_identifier, process_name);
412     sprintf(mailbox, "%s_%s_p0", allreduce_identifier, process_name);
413     XBT_DEBUG("put on %s", mailbox);
414     MSG_task_send(MSG_task_create(allreduce_identifier, 0, comm_size, NULL),
415                   mailbox);
416
417     sprintf(mailbox, "%s_p0_%s", allreduce_identifier, process_name);
418     MSG_task_receive(&task, mailbox);
419     MSG_task_destroy(task);
420     XBT_DEBUG("%s: %s has received", allreduce_identifier, process_name);
421   }
422
423   log_action(action, MSG_get_clock() - clock);
424   xbt_free(allreduce_identifier);
425 }
426
427 static void action_comm_size(const char *const *action)
428 {
429   const char *size = action[2];
430   double clock = MSG_get_clock();
431
432   communicator_size = parse_double(size);
433   log_action(action, MSG_get_clock() - clock);
434 }
435
436 static void action_compute(const char *const *action)
437 {
438   const char *amount = action[2];
439   msg_task_t task = MSG_task_create("task", parse_double(amount), 0, NULL);
440   double clock = MSG_get_clock();
441
442   ACT_DEBUG("Entering %s", NAME);
443   MSG_task_execute(task);
444   MSG_task_destroy(task);
445   log_action(action, MSG_get_clock() - clock);
446 }
447
448 static void action_init(const char *const *action)
449 {
450   XBT_DEBUG("Initialize the counters");
451   process_globals_t globals =
452       (process_globals_t) calloc(1, sizeof(s_process_globals_t));
453   globals->isends = xbt_dynar_new(sizeof(msg_comm_t), NULL);
454   globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t), NULL);
455   globals->tasks = xbt_dynar_new(sizeof(msg_task_t), NULL);
456   MSG_process_set_data(MSG_process_self(), globals);
457
458 }
459
460 static void action_finalize(const char *const *action)
461 {
462   process_globals_t globals =
463       (process_globals_t) MSG_process_get_data(MSG_process_self());
464   if (globals) {
465     asynchronous_cleanup();
466     xbt_dynar_free_container(&(globals->isends));
467     xbt_dynar_free_container(&(globals->irecvs));
468     xbt_dynar_free_container(&(globals->tasks));
469     xbt_free(globals);
470   }
471 }
472
473 /** Main function */
474 int main(int argc, char *argv[])
475 {
476   msg_error_t res = MSG_OK;
477
478   /* Check the given arguments */
479   MSG_init(&argc, argv);
480   /* Explicit initialization of the action module is required now*/
481   MSG_action_init();
482
483   xbt_assert(argc > 2,
484        "Usage: %s platform_file deployment_file [action_files]\n"
485        "\t# if all actions are in the same file\n"
486        "\tExample: %s msg_platform.xml msg_deployment.xml actions\n"
487        "\t# if actions are in separate files, specified in deployment\n"
488        "\tExample: %s msg_platform.xml msg_deployment.xml ",
489        argv[0],argv[0],argv[0]);
490
491   printf("WARNING: THIS BINARY IS KINDA DEPRECATED\n"
492    "This example is still relevant if you want to learn about MSG-based trace replay, "
493    "but if you want to simulate MPI-like traces, you should use the newer version "
494    "that is in the examples/smpi/replay directory instead.\n");
495    
496   /*  Simulation setting */
497   MSG_create_environment(argv[1]);
498
499   /* No need to register functions as in classical MSG programs: the actions get started anyway */
500   MSG_launch_application(argv[2]);
501
502   /*   Action registration */
503   xbt_replay_action_register("init", action_init);
504   xbt_replay_action_register("finalize", action_finalize);
505   xbt_replay_action_register("comm_size", action_comm_size);
506   xbt_replay_action_register("send", action_send);
507   xbt_replay_action_register("Isend", action_Isend);
508   xbt_replay_action_register("recv", action_recv);
509   xbt_replay_action_register("Irecv", action_Irecv);
510   xbt_replay_action_register("wait", action_wait);
511   xbt_replay_action_register("barrier", action_barrier);
512   xbt_replay_action_register("bcast", action_bcast);
513   xbt_replay_action_register("reduce", action_reduce);
514   xbt_replay_action_register("allReduce", action_allReduce);
515   xbt_replay_action_register("sleep", action_sleep);
516   xbt_replay_action_register("compute", action_compute);
517
518
519   /* Actually do the simulation using MSG_action_trace_run */
520   res = MSG_action_trace_run(argv[3]);  // it's ok to pass a NULL argument here
521
522   XBT_INFO("Simulation time %g", MSG_get_clock());
523
524   /* Explicit finalization of the action module is required now*/
525   MSG_action_exit();
526
527   return res != MSG_OK;
528 }                               /* end_of_main */