Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use msg_error_t instead of MSG_error_t
[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 #ifdef HAVE_TRACING
104   int rank = get_rank(MSG_process_get_name(MSG_process_self()));
105   int dst_traced = get_rank(action[2]);
106   TRACE_smpi_ptp_in(rank, rank, dst_traced, "send");
107   TRACE_smpi_send(rank, rank, dst_traced);
108 #endif
109
110   XBT_DEBUG("Entering Send: %s (size: %lg)", name, size);
111    if (size<65536) {
112      action_Isend(action);
113    } else {
114      MSG_task_send(MSG_task_create(name, 0, size, NULL), to);
115    }
116    
117    XBT_VERB("%s %f", name, MSG_get_clock() - clock);
118
119   free(name);
120
121 #ifdef HAVE_TRACING
122   TRACE_smpi_ptp_out(rank, rank, dst_traced, "send");
123 #endif
124
125   asynchronous_cleanup();
126 }
127
128 static void action_Isend(const char *const *action)
129 {
130   char to[250];
131   const char *size = action[3];
132   double clock = MSG_get_clock();
133   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
134
135
136   sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),action[2]);
137   msg_comm_t comm =
138       MSG_task_isend( MSG_task_create(to,0,parse_double(size),NULL), to);
139   xbt_dynar_push(globals->isends,&comm);
140
141   XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
142   XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
143
144   asynchronous_cleanup();
145 }
146
147
148 static void action_recv(const char *const *action)
149 {
150   char *name = NULL;
151   char mailbox_name[250];
152   msg_task_t task = NULL;
153   double clock = MSG_get_clock();
154
155   sprintf(mailbox_name, "%s_%s", action[2],
156           MSG_process_get_name(MSG_process_self()));
157
158   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
159     name = xbt_str_join_array(action, " ");
160
161 #ifdef HAVE_TRACING
162   int rank = get_rank(MSG_process_get_name(MSG_process_self()));
163   int src_traced = get_rank(action[2]);
164   TRACE_smpi_ptp_in(rank, src_traced, rank, "recv");
165 #endif
166
167   XBT_DEBUG("Receiving: %s", name);
168   msg_error_t res = MSG_task_receive(&task, mailbox_name);
169   //  MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
170   XBT_VERB("%s %f", name, MSG_get_clock() - clock);
171
172   if (res == MSG_OK) {
173     MSG_task_destroy(task);
174   }
175
176   free(name);
177 #ifdef HAVE_TRACING
178   TRACE_smpi_ptp_out(rank, src_traced, rank, "recv");
179   TRACE_smpi_recv(rank, src_traced, rank);
180 #endif
181
182   asynchronous_cleanup();
183 }
184
185 static void action_Irecv(const char *const *action)
186 {
187   char mailbox[250];
188   double clock = MSG_get_clock();
189   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
190
191   XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
192 #ifdef HAVE_TRACING
193   int rank = get_rank(MSG_process_get_name(MSG_process_self()));
194   int src_traced = get_rank(action[2]);
195   globals->last_Irecv_sender_id = src_traced;
196   MSG_process_set_data(MSG_process_self(), (void *) globals);
197
198   TRACE_smpi_ptp_in(rank, src_traced, rank, "Irecv");
199 #endif
200
201   sprintf(mailbox, "%s_%s", action[2],
202           MSG_process_get_name(MSG_process_self()));
203   msg_task_t t=NULL;
204   xbt_dynar_push(globals->tasks,&t);
205   msg_comm_t c =
206       MSG_task_irecv(
207           xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks)-1),
208           mailbox);
209   xbt_dynar_push(globals->irecvs,&c);
210
211   XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
212
213 #ifdef HAVE_TRACING
214   TRACE_smpi_ptp_out(rank, src_traced, rank, "Irecv");
215 #endif
216
217   asynchronous_cleanup();
218 }
219
220
221 static void action_wait(const char *const *action)
222 {
223   char *name = NULL;
224   msg_task_t task = NULL;
225   msg_comm_t comm;
226   double clock = MSG_get_clock();
227   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
228
229   xbt_assert(xbt_dynar_length(globals->irecvs),
230       "action wait not preceded by any irecv: %s", xbt_str_join_array(action," "));
231
232   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
233     name = xbt_str_join_array(action, " ");
234 #ifdef HAVE_TRACING
235   process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
236   int src_traced = counters->last_Irecv_sender_id;
237   int rank = get_rank(MSG_process_get_name(MSG_process_self()));
238   TRACE_smpi_ptp_in(rank, src_traced, rank, "wait");
239 #endif
240
241   XBT_DEBUG("Entering %s", name);
242   comm = xbt_dynar_pop_as(globals->irecvs,msg_comm_t);
243   MSG_comm_wait(comm,-1);
244   task = xbt_dynar_pop_as(globals->tasks,msg_task_t);
245   MSG_comm_destroy(comm);
246   MSG_task_destroy(task);
247
248   XBT_VERB("%s %f", name, MSG_get_clock() - clock);
249   free(name);
250 #ifdef HAVE_TRACING
251   TRACE_smpi_ptp_out(rank, src_traced, rank, "wait");
252   TRACE_smpi_recv(rank, src_traced, rank);
253 #endif
254
255 }
256
257 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
258 static void action_barrier(const char *const *action)
259 {
260   char *name = NULL;
261   static smx_mutex_t mutex = NULL;
262   static smx_cond_t cond = NULL;
263   static int processes_arrived_sofar=0;
264
265   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
266     name = xbt_str_join_array(action, " ");
267
268   if (mutex == NULL) {       // first arriving on the barrier
269     mutex = simcall_mutex_init();
270     cond = simcall_cond_init();
271     processes_arrived_sofar=0;
272   }
273   XBT_DEBUG("Entering barrier: %s (%d already there)", name,processes_arrived_sofar);
274
275   simcall_mutex_lock(mutex);
276   if (++processes_arrived_sofar == communicator_size) {
277     simcall_cond_broadcast(cond);
278     simcall_mutex_unlock(mutex);
279   } else {
280     simcall_cond_wait(cond,mutex);
281     simcall_mutex_unlock(mutex);
282   }
283
284   XBT_DEBUG("Exiting barrier: %s", name);
285
286   processes_arrived_sofar--;
287   if (!processes_arrived_sofar) {
288     simcall_cond_destroy(cond);
289     simcall_mutex_destroy(mutex);
290     mutex=NULL;
291   }
292
293   free(name);
294
295 }
296
297 static void action_reduce(const char *const *action)
298 {
299   int i;
300   char *reduce_identifier;
301   char mailbox[80];
302   double comm_size = parse_double(action[2]);
303   double comp_size = parse_double(action[3]);
304   msg_task_t comp_task = NULL;
305   const char *process_name;
306   double clock = MSG_get_clock();
307
308   process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
309
310   xbt_assert(communicator_size, "Size of Communicator is not defined, "
311       "can't use collective operations");
312
313   process_name = MSG_process_get_name(MSG_process_self());
314
315   reduce_identifier = bprintf("reduce_%d", counters->reduce_counter++);
316
317   if (!strcmp(process_name, "p0")) {
318     XBT_DEBUG("%s: %s is the Root", reduce_identifier, process_name);
319
320     msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
321       msg_task_t *tasks = xbt_new0(msg_task_t,communicator_size-1);
322       for (i = 1; i < communicator_size; i++) {
323         sprintf(mailbox, "%s_p%d_p0", reduce_identifier, i);
324         comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
325       }
326       MSG_comm_waitall(comms,communicator_size-1,-1);
327       for (i = 1; i < communicator_size; i++) {
328         MSG_comm_destroy(comms[i-1]);
329         MSG_task_destroy(tasks[i-1]);
330       }
331       free(tasks);
332
333       comp_task = MSG_task_create("reduce_comp", comp_size, 0, NULL);
334       XBT_DEBUG("%s: computing 'reduce_comp'", reduce_identifier);
335       MSG_task_execute(comp_task);
336       MSG_task_destroy(comp_task);
337       XBT_DEBUG("%s: computed", reduce_identifier);
338
339   } else {
340     XBT_DEBUG("%s: %s sends", reduce_identifier, process_name);
341     sprintf(mailbox, "%s_%s_p0", reduce_identifier, process_name);
342       XBT_DEBUG("put on %s", mailbox);
343       MSG_task_send(MSG_task_create(reduce_identifier, 0, comm_size, NULL),
344                     mailbox);
345   }
346
347   XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
348   free(reduce_identifier);
349 }
350
351 static void action_bcast(const char *const *action)
352 {
353   int i;
354   char *bcast_identifier;
355   char mailbox[80];
356   double comm_size = parse_double(action[2]);
357   msg_task_t task = NULL;
358   const char *process_name;
359   double clock = MSG_get_clock();
360
361   process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
362
363   xbt_assert(communicator_size, "Size of Communicator is not defined, "
364       "can't use collective operations");
365
366   process_name = MSG_process_get_name(MSG_process_self());
367
368   bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
369
370   if (!strcmp(process_name, "p0")) {
371     XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
372
373       msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
374
375       for (i = 1; i < communicator_size; i++) {
376         sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
377         comms[i-1] =
378             MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
379                 mailbox);
380       }
381       MSG_comm_waitall(comms,communicator_size-1,-1);
382     for (i = 1; i < communicator_size; i++)
383          MSG_comm_destroy(comms[i-1]);
384       free(comms);
385
386       XBT_DEBUG("%s: all messages sent by %s have been received",
387              bcast_identifier, process_name);
388
389   } else {
390       sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
391       MSG_task_receive(&task, mailbox);
392       MSG_task_destroy(task);
393       XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
394   }
395
396   XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
397   free(bcast_identifier);
398 }
399
400
401 static void action_sleep(const char *const *action)
402 {
403   char *name = NULL;
404   const char *duration = action[2];
405   double clock = MSG_get_clock();
406
407   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
408     name = xbt_str_join_array(action, " ");
409
410   XBT_DEBUG("Entering %s", name);
411   MSG_process_sleep(parse_double(duration));
412   XBT_VERB("%s %f ", name, MSG_get_clock() - clock);
413
414   free(name);
415 }
416
417 static void action_allReduce(const char *const *action) {
418   int i;
419   char *allreduce_identifier;
420   char mailbox[80];
421   double comm_size = parse_double(action[2]);
422   double comp_size = parse_double(action[3]);
423   msg_task_t task = NULL, comp_task = NULL;
424   const char *process_name;
425   double clock = MSG_get_clock();
426
427   process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
428
429   xbt_assert(communicator_size, "Size of Communicator is not defined, "
430               "can't use collective operations");
431
432   process_name = MSG_process_get_name(MSG_process_self());
433
434   allreduce_identifier = bprintf("allReduce_%d", counters->allReduce_counter++);
435
436   if (!strcmp(process_name, "p0")) {
437     XBT_DEBUG("%s: %s is the Root", allreduce_identifier, process_name);
438
439     msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
440     msg_task_t *tasks = xbt_new0(msg_task_t,communicator_size-1);
441     for (i = 1; i < communicator_size; i++) {
442       sprintf(mailbox, "%s_p%d_p0", allreduce_identifier, i);
443       comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
444     }
445     MSG_comm_waitall(comms,communicator_size-1,-1);
446     for (i = 1; i < communicator_size; i++) {
447       MSG_comm_destroy(comms[i-1]);
448       MSG_task_destroy(tasks[i-1]);
449     }
450     free(tasks);
451
452     comp_task = MSG_task_create("allReduce_comp", comp_size, 0, NULL);
453     XBT_DEBUG("%s: computing 'reduce_comp'", allreduce_identifier);
454     MSG_task_execute(comp_task);
455     MSG_task_destroy(comp_task);
456     XBT_DEBUG("%s: computed", allreduce_identifier);
457
458     for (i = 1; i < communicator_size; i++) {
459       sprintf(mailbox, "%s_p0_p%d", allreduce_identifier, i);
460       comms[i-1] =
461           MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
462               mailbox);
463     }
464     MSG_comm_waitall(comms,communicator_size-1,-1);
465     for (i = 1; i < communicator_size; i++)
466        MSG_comm_destroy(comms[i-1]);
467     free(comms);
468
469     XBT_DEBUG("%s: all messages sent by %s have been received",
470            allreduce_identifier, process_name);
471
472   } else {
473     XBT_DEBUG("%s: %s sends", allreduce_identifier, process_name);
474     sprintf(mailbox, "%s_%s_p0", allreduce_identifier, process_name);
475     XBT_DEBUG("put on %s", mailbox);
476     MSG_task_send(MSG_task_create(allreduce_identifier, 0, comm_size, NULL),
477                   mailbox);
478
479     sprintf(mailbox, "%s_p0_%s", allreduce_identifier, process_name);
480     MSG_task_receive(&task, mailbox);
481     MSG_task_destroy(task);
482     XBT_DEBUG("%s: %s has received", allreduce_identifier, process_name);
483   }
484
485   XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
486   free(allreduce_identifier);
487 }
488
489 static void action_comm_size(const char *const *action)
490 {
491   char *name = NULL;
492   const char *size = action[2];
493   double clock = MSG_get_clock();
494
495   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
496     name = xbt_str_join_array(action, " ");
497   communicator_size = parse_double(size);
498   XBT_VERB("%s %f", name, MSG_get_clock() - clock);
499   free(name);
500 }
501
502 static void action_compute(const char *const *action)
503 {
504   char *name = NULL;
505   const char *amout = action[2];
506   msg_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
507   double clock = MSG_get_clock();
508
509   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
510     name = xbt_str_join_array(action, " ");
511   XBT_DEBUG("Entering %s", name);
512   MSG_task_execute(task);
513   MSG_task_destroy(task);
514   XBT_VERB("%s %f", name, MSG_get_clock() - clock);
515   free(name);
516 }
517
518 static void action_init(const char *const *action)
519
520 #ifdef HAVE_TRACING
521   TRACE_smpi_init(get_rank(MSG_process_get_name(MSG_process_self())));
522 #endif
523   XBT_DEBUG("Initialize the counters");
524   process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
525   globals->isends = xbt_dynar_new(sizeof(msg_comm_t),NULL);
526   globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t),NULL);
527   globals->tasks  = xbt_dynar_new(sizeof(msg_task_t),NULL);
528   MSG_process_set_data(MSG_process_self(),globals);
529
530 }
531
532 static void action_finalize(const char *const *action)
533 {
534 #ifdef HAVE_TRACING
535   TRACE_smpi_finalize(get_rank(MSG_process_get_name(MSG_process_self())));
536 #endif
537   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
538   if (globals){
539     xbt_dynar_free_container(&(globals->isends));
540     xbt_dynar_free_container(&(globals->irecvs));
541     xbt_dynar_free_container(&(globals->tasks));
542     free(globals);
543   }
544 }
545
546 /** Main function */
547 int main(int argc, char *argv[])
548 {
549   msg_error_t res = MSG_OK;
550
551   /* Check the given arguments */
552   MSG_init(&argc, argv);
553   if (argc < 3) {
554     printf("Usage: %s platform_file deployment_file [action_files]\n",
555            argv[0]);
556     printf
557         ("example: %s msg_platform.xml msg_deployment.xml actions # if all actions are in the same file\n",
558          argv[0]);
559     printf
560         ("example: %s msg_platform.xml msg_deployment.xml # if actions are in separate files, specified in deployment\n",
561          argv[0]);
562     exit(1);
563   }
564
565   /*  Simulation setting */
566   MSG_create_environment(argv[1]);
567
568   /* No need to register functions as in classical MSG programs: the actions get started anyway */
569   MSG_launch_application(argv[2]);
570
571   /*   Action registration */
572   xbt_replay_action_register("init",     action_init);
573   xbt_replay_action_register("finalize", action_finalize);
574   xbt_replay_action_register("comm_size",action_comm_size);
575   xbt_replay_action_register("send",     action_send);
576   xbt_replay_action_register("Isend",    action_Isend);
577   xbt_replay_action_register("recv",     action_recv);
578   xbt_replay_action_register("Irecv",    action_Irecv);
579   xbt_replay_action_register("wait",     action_wait);
580   xbt_replay_action_register("barrier",  action_barrier);
581   xbt_replay_action_register("bcast",    action_bcast);
582   xbt_replay_action_register("reduce",   action_reduce);
583   xbt_replay_action_register("allReduce",action_allReduce);
584   xbt_replay_action_register("sleep",    action_sleep);
585   xbt_replay_action_register("compute",  action_compute);
586
587
588   /* Actually do the simulation using MSG_action_trace_run */
589   res = MSG_action_trace_run(argv[3]);  // it's ok to pass a NULL argument here
590
591   XBT_INFO("Simulation time %g", MSG_get_clock());
592   MSG_clean();
593
594   if (res == MSG_OK)
595     return 0;
596   else
597     return 1;
598 }                               /* end_of_main */