Logo AND Algorithmique Numérique Distribuée

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