Logo AND Algorithmique Numérique Distribuée

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