Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move module tracing into corresponding modules
[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,
44                              "Messages specific for this msg example");
45 int communicator_size = 0;
46
47 static void action_Isend(const char *const *action);
48
49 typedef struct  {
50   int last_Irecv_sender_id;
51   int bcast_counter;
52   int reduce_counter;
53   int allReduce_counter;
54   xbt_dynar_t isends; /* of msg_comm_t */
55   /* Used to implement irecv+wait */
56   xbt_dynar_t irecvs; /* of msg_comm_t */
57   xbt_dynar_t tasks; /* of msg_task_t */
58 } s_process_globals_t, *process_globals_t;
59
60 /* Helper function */
61 static double parse_double(const char *string)
62 {
63   double value;
64   char *endptr;
65
66   value = strtod(string, &endptr);
67   if (*endptr != '\0')
68     THROWF(unknown_error, 0, "%s is not a double", string);
69   return value;
70 }
71
72 static int get_rank (const char *process_name)
73 {
74   return atoi(&(process_name[1]));
75
76
77 static void asynchronous_cleanup(void) {
78   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
79
80   /* Destroy any isend which correspond to completed communications */
81   int found;
82   msg_comm_t comm;
83   while ((found = MSG_comm_testany(globals->isends)) != -1) {
84     xbt_dynar_remove_at(globals->isends,found,&comm);
85     MSG_comm_destroy(comm);
86   }
87 }
88
89 /* My actions */
90 static void action_send(const char *const *action)
91 {
92   char *name = NULL;
93   char to[250];
94   const char *size_str = action[3];
95   double size=parse_double(size_str);
96   double clock = MSG_get_clock(); /* this "call" is free thanks to inlining */
97
98   sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),action[2]);
99
100   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
101     name = xbt_str_join_array(action, " ");
102
103   XBT_DEBUG("Entering Send: %s (size: %lg)", name, size);
104    if (size<65536) {
105      action_Isend(action);
106    } else {
107      MSG_task_send(MSG_task_create(name, 0, size, NULL), to);
108    }
109    
110    XBT_VERB("%s %f", name, MSG_get_clock() - clock);
111
112   free(name);
113   asynchronous_cleanup();
114 }
115
116 static void action_Isend(const char *const *action)
117 {
118   char to[250];
119   const char *size = action[3];
120   double clock = MSG_get_clock();
121   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
122
123
124   sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),action[2]);
125   msg_comm_t comm =
126       MSG_task_isend( MSG_task_create(to,0,parse_double(size),NULL), to);
127   xbt_dynar_push(globals->isends,&comm);
128
129   XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
130   XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
131
132   asynchronous_cleanup();
133 }
134
135
136 static void action_recv(const char *const *action)
137 {
138   char *name = NULL;
139   char mailbox_name[250];
140   msg_task_t task = NULL;
141   double clock = MSG_get_clock();
142
143   sprintf(mailbox_name, "%s_%s", action[2],
144           MSG_process_get_name(MSG_process_self()));
145
146   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
147     name = xbt_str_join_array(action, " ");
148
149   XBT_DEBUG("Receiving: %s", name);
150   msg_error_t res = MSG_task_receive(&task, mailbox_name);
151   //  MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
152   XBT_VERB("%s %f", name, MSG_get_clock() - clock);
153
154   if (res == MSG_OK) {
155     MSG_task_destroy(task);
156   }
157
158   free(name);
159   asynchronous_cleanup();
160 }
161
162 static void action_Irecv(const char *const *action)
163 {
164   char mailbox[250];
165   double clock = MSG_get_clock();
166   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
167
168   XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
169
170   sprintf(mailbox, "%s_%s", action[2],
171           MSG_process_get_name(MSG_process_self()));
172   msg_task_t t=NULL;
173   xbt_dynar_push(globals->tasks,&t);
174   msg_comm_t c =
175       MSG_task_irecv(
176           xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks)-1),
177           mailbox);
178   xbt_dynar_push(globals->irecvs,&c);
179
180   XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
181
182   asynchronous_cleanup();
183 }
184
185
186 static void action_wait(const char *const *action)
187 {
188   char *name = NULL;
189   msg_task_t task = NULL;
190   msg_comm_t comm;
191   double clock = MSG_get_clock();
192   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
193
194   xbt_assert(xbt_dynar_length(globals->irecvs),
195       "action wait not preceded by any irecv: %s", 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,processes_arrived_sofar);
228
229   simcall_mutex_lock(mutex);
230   if (++processes_arrived_sofar == communicator_size) {
231     simcall_cond_broadcast(cond);
232     simcall_mutex_unlock(mutex);
233   } else {
234     simcall_cond_wait(cond,mutex);
235     simcall_mutex_unlock(mutex);
236   }
237
238   XBT_DEBUG("Exiting barrier: %s", name);
239
240   processes_arrived_sofar--;
241   if (!processes_arrived_sofar) {
242     simcall_cond_destroy(cond);
243     simcall_mutex_destroy(mutex);
244     mutex=NULL;
245   }
246
247   free(name);
248
249 }
250
251 static void action_reduce(const char *const *action)
252 {
253   int i;
254   char *reduce_identifier;
255   char mailbox[80];
256   double comm_size = parse_double(action[2]);
257   double comp_size = parse_double(action[3]);
258   msg_task_t comp_task = NULL;
259   const char *process_name;
260   double clock = MSG_get_clock();
261
262   process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
263
264   xbt_assert(communicator_size, "Size of Communicator is not defined, "
265       "can't use collective operations");
266
267   process_name = MSG_process_get_name(MSG_process_self());
268
269   reduce_identifier = bprintf("reduce_%d", counters->reduce_counter++);
270
271   if (!strcmp(process_name, "p0")) {
272     XBT_DEBUG("%s: %s is the Root", reduce_identifier, process_name);
273
274     msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
275       msg_task_t *tasks = xbt_new0(msg_task_t,communicator_size-1);
276       for (i = 1; i < communicator_size; i++) {
277         sprintf(mailbox, "%s_p%d_p0", reduce_identifier, i);
278         comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
279       }
280       MSG_comm_waitall(comms,communicator_size-1,-1);
281       for (i = 1; i < communicator_size; i++) {
282         MSG_comm_destroy(comms[i-1]);
283         MSG_task_destroy(tasks[i-1]);
284       }
285       free(tasks);
286
287       comp_task = MSG_task_create("reduce_comp", comp_size, 0, NULL);
288       XBT_DEBUG("%s: computing 'reduce_comp'", reduce_identifier);
289       MSG_task_execute(comp_task);
290       MSG_task_destroy(comp_task);
291       XBT_DEBUG("%s: computed", reduce_identifier);
292
293   } else {
294     XBT_DEBUG("%s: %s sends", reduce_identifier, process_name);
295     sprintf(mailbox, "%s_%s_p0", reduce_identifier, process_name);
296       XBT_DEBUG("put on %s", mailbox);
297       MSG_task_send(MSG_task_create(reduce_identifier, 0, comm_size, NULL),
298                     mailbox);
299   }
300
301   XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
302   free(reduce_identifier);
303 }
304
305 static void action_bcast(const char *const *action)
306 {
307   int i;
308   char *bcast_identifier;
309   char mailbox[80];
310   double comm_size = parse_double(action[2]);
311   msg_task_t task = NULL;
312   const char *process_name;
313   double clock = MSG_get_clock();
314
315   process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
316
317   xbt_assert(communicator_size, "Size of Communicator is not defined, "
318       "can't use collective operations");
319
320   process_name = MSG_process_get_name(MSG_process_self());
321
322   bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
323
324   if (!strcmp(process_name, "p0")) {
325     XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
326
327       msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
328
329       for (i = 1; i < communicator_size; i++) {
330         sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
331         comms[i-1] =
332             MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
333                 mailbox);
334       }
335       MSG_comm_waitall(comms,communicator_size-1,-1);
336     for (i = 1; i < communicator_size; i++)
337          MSG_comm_destroy(comms[i-1]);
338       free(comms);
339
340       XBT_DEBUG("%s: all messages sent by %s have been received",
341              bcast_identifier, process_name);
342
343   } else {
344       sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
345       MSG_task_receive(&task, mailbox);
346       MSG_task_destroy(task);
347       XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
348   }
349
350   XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
351   free(bcast_identifier);
352 }
353
354
355 static void action_sleep(const char *const *action)
356 {
357   char *name = NULL;
358   const char *duration = action[2];
359   double clock = MSG_get_clock();
360
361   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
362     name = xbt_str_join_array(action, " ");
363
364   XBT_DEBUG("Entering %s", name);
365   MSG_process_sleep(parse_double(duration));
366   XBT_VERB("%s %f ", name, MSG_get_clock() - clock);
367
368   free(name);
369 }
370
371 static void action_allReduce(const char *const *action) {
372   int i;
373   char *allreduce_identifier;
374   char mailbox[80];
375   double comm_size = parse_double(action[2]);
376   double comp_size = parse_double(action[3]);
377   msg_task_t task = NULL, comp_task = NULL;
378   const char *process_name;
379   double clock = MSG_get_clock();
380
381   process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
382
383   xbt_assert(communicator_size, "Size of Communicator is not defined, "
384               "can't use collective operations");
385
386   process_name = MSG_process_get_name(MSG_process_self());
387
388   allreduce_identifier = bprintf("allReduce_%d", counters->allReduce_counter++);
389
390   if (!strcmp(process_name, "p0")) {
391     XBT_DEBUG("%s: %s is the Root", allreduce_identifier, process_name);
392
393     msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
394     msg_task_t *tasks = xbt_new0(msg_task_t,communicator_size-1);
395     for (i = 1; i < communicator_size; i++) {
396       sprintf(mailbox, "%s_p%d_p0", allreduce_identifier, i);
397       comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
398     }
399     MSG_comm_waitall(comms,communicator_size-1,-1);
400     for (i = 1; i < communicator_size; i++) {
401       MSG_comm_destroy(comms[i-1]);
402       MSG_task_destroy(tasks[i-1]);
403     }
404     free(tasks);
405
406     comp_task = MSG_task_create("allReduce_comp", comp_size, 0, NULL);
407     XBT_DEBUG("%s: computing 'reduce_comp'", allreduce_identifier);
408     MSG_task_execute(comp_task);
409     MSG_task_destroy(comp_task);
410     XBT_DEBUG("%s: computed", allreduce_identifier);
411
412     for (i = 1; i < communicator_size; i++) {
413       sprintf(mailbox, "%s_p0_p%d", allreduce_identifier, i);
414       comms[i-1] =
415           MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
416               mailbox);
417     }
418     MSG_comm_waitall(comms,communicator_size-1,-1);
419     for (i = 1; i < communicator_size; i++)
420        MSG_comm_destroy(comms[i-1]);
421     free(comms);
422
423     XBT_DEBUG("%s: all messages sent by %s have been received",
424            allreduce_identifier, process_name);
425
426   } else {
427     XBT_DEBUG("%s: %s sends", allreduce_identifier, process_name);
428     sprintf(mailbox, "%s_%s_p0", allreduce_identifier, process_name);
429     XBT_DEBUG("put on %s", mailbox);
430     MSG_task_send(MSG_task_create(allreduce_identifier, 0, comm_size, NULL),
431                   mailbox);
432
433     sprintf(mailbox, "%s_p0_%s", allreduce_identifier, process_name);
434     MSG_task_receive(&task, mailbox);
435     MSG_task_destroy(task);
436     XBT_DEBUG("%s: %s has received", allreduce_identifier, process_name);
437   }
438
439   if (XBT_LOG_ISENABLED(actions,xbt_log_priority_verbose)) {
440     char *a =  xbt_str_join_array(action, " ");
441     XBT_VERB("%s %f", a, MSG_get_clock() - clock);
442     free(a);
443   }
444   free(allreduce_identifier);
445 }
446
447 static void action_comm_size(const char *const *action)
448 {
449   char *name = NULL;
450   const char *size = action[2];
451   double clock = MSG_get_clock();
452
453   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
454     name = xbt_str_join_array(action, " ");
455   communicator_size = parse_double(size);
456   XBT_VERB("%s %f", name, MSG_get_clock() - clock);
457   free(name);
458 }
459
460 static void action_compute(const char *const *action)
461 {
462   char *name = NULL;
463   const char *amout = action[2];
464   msg_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
465   double clock = MSG_get_clock();
466
467   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
468     name = xbt_str_join_array(action, " ");
469   XBT_DEBUG("Entering %s", name);
470   MSG_task_execute(task);
471   MSG_task_destroy(task);
472   XBT_VERB("%s %f", name, MSG_get_clock() - clock);
473   free(name);
474 }
475
476 static void action_init(const char *const *action)
477
478   XBT_DEBUG("Initialize the counters");
479   process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
480   globals->isends = xbt_dynar_new(sizeof(msg_comm_t),NULL);
481   globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t),NULL);
482   globals->tasks  = xbt_dynar_new(sizeof(msg_task_t),NULL);
483   MSG_process_set_data(MSG_process_self(),globals);
484
485 }
486
487 static void action_finalize(const char *const *action)
488 {
489   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
490   if (globals){
491     xbt_dynar_free_container(&(globals->isends));
492     xbt_dynar_free_container(&(globals->irecvs));
493     xbt_dynar_free_container(&(globals->tasks));
494     free(globals);
495   }
496 }
497
498 /** Main function */
499 int main(int argc, char *argv[])
500 {
501   msg_error_t res = MSG_OK;
502
503   /* Check the given arguments */
504   MSG_init(&argc, argv);
505   if (argc < 3) {
506     printf("Usage: %s platform_file deployment_file [action_files]\n",
507            argv[0]);
508     printf
509         ("example: %s msg_platform.xml msg_deployment.xml actions # if all actions are in the same file\n",
510          argv[0]);
511     printf
512         ("example: %s msg_platform.xml msg_deployment.xml # if actions are in separate files, specified in deployment\n",
513          argv[0]);
514     exit(1);
515   }
516
517   /*  Simulation setting */
518   MSG_create_environment(argv[1]);
519
520   /* No need to register functions as in classical MSG programs: the actions get started anyway */
521   MSG_launch_application(argv[2]);
522
523   /*   Action registration */
524   xbt_replay_action_register("init",     action_init);
525   xbt_replay_action_register("finalize", action_finalize);
526   xbt_replay_action_register("comm_size",action_comm_size);
527   xbt_replay_action_register("send",     action_send);
528   xbt_replay_action_register("Isend",    action_Isend);
529   xbt_replay_action_register("recv",     action_recv);
530   xbt_replay_action_register("Irecv",    action_Irecv);
531   xbt_replay_action_register("wait",     action_wait);
532   xbt_replay_action_register("barrier",  action_barrier);
533   xbt_replay_action_register("bcast",    action_bcast);
534   xbt_replay_action_register("reduce",   action_reduce);
535   xbt_replay_action_register("allReduce",action_allReduce);
536   xbt_replay_action_register("sleep",    action_sleep);
537   xbt_replay_action_register("compute",  action_compute);
538
539
540   /* Actually do the simulation using MSG_action_trace_run */
541   res = MSG_action_trace_run(argv[3]);  // it's ok to pass a NULL argument here
542
543   XBT_INFO("Simulation time %g", MSG_get_clock());
544
545   if (res == MSG_OK)
546     return 0;
547   else
548     return 1;
549 }                               /* end_of_main */