Logo AND Algorithmique Numérique Distribuée

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