Logo AND Algorithmique Numérique Distribuée

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