1 /* Copyright (c) 2009-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
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>
15 /** @addtogroup MSG_examples
17 * @section MSG_ex_actions Trace driven simulations
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.
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.
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,
43 XBT_LOG_NEW_DEFAULT_CATEGORY(actions, "Messages specific for this msg example");
44 int communicator_size = 0;
46 static void action_Isend(const char *const *action);
49 int last_Irecv_sender_id;
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;
60 static double parse_double(const char *string)
65 value = strtod(string, &endptr);
67 THROWF(unknown_error, 0, "%s is not a double", string);
71 #define ACT_DEBUG(...) \
72 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) { \
73 char *NAME = xbt_str_join_array(action, " "); \
74 XBT_DEBUG(__VA_ARGS__); \
78 static void log_action(const char *const *action, double date)
80 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) {
81 char *name = xbt_str_join_array(action, " ");
82 XBT_VERB("%s %f", name, date);
87 static void asynchronous_cleanup(void)
89 process_globals_t globals =
90 (process_globals_t) MSG_process_get_data(MSG_process_self());
92 /* Destroy any isend which correspond to completed communications */
95 while ((found = MSG_comm_testany(globals->isends)) != -1) {
96 xbt_dynar_remove_at(globals->isends, found, &comm);
97 MSG_comm_destroy(comm);
102 static void action_send(const char *const *action)
105 const char *size_str = action[3];
106 double size = parse_double(size_str);
107 double clock = MSG_get_clock(); /* this "call" is free thanks to inlining */
109 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
111 ACT_DEBUG("Entering Send: %s (size: %g)", NAME, size);
113 action_Isend(action);
115 MSG_task_send(MSG_task_create(to, 0, size, NULL), to);
118 log_action(action, MSG_get_clock() - clock);
119 asynchronous_cleanup();
122 static void action_Isend(const char *const *action)
125 const char *size = action[3];
126 double clock = MSG_get_clock();
127 process_globals_t globals =
128 (process_globals_t) MSG_process_get_data(MSG_process_self());
130 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
132 MSG_task_isend(MSG_task_create(to, 0, parse_double(size), NULL), to);
133 xbt_dynar_push(globals->isends, &comm);
135 XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
136 log_action(action, MSG_get_clock() - clock);
137 asynchronous_cleanup();
141 static void action_recv(const char *const *action)
143 char mailbox_name[250];
144 msg_task_t task = NULL;
145 double clock = MSG_get_clock();
147 sprintf(mailbox_name, "%s_%s", action[2],
148 MSG_process_get_name(MSG_process_self()));
150 ACT_DEBUG("Receiving: %s", NAME);
151 msg_error_t res = MSG_task_receive(&task, mailbox_name);
152 log_action(action, MSG_get_clock() - clock);
155 MSG_task_destroy(task);
157 asynchronous_cleanup();
160 static void action_Irecv(const char *const *action)
163 double clock = MSG_get_clock();
164 process_globals_t globals =
165 (process_globals_t) MSG_process_get_data(MSG_process_self());
167 XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
169 sprintf(mailbox, "%s_%s", action[2],
170 MSG_process_get_name(MSG_process_self()));
172 xbt_dynar_push(globals->tasks, &t);
174 MSG_task_irecv(xbt_dynar_get_ptr
175 (globals->tasks, xbt_dynar_length(globals->tasks) - 1),
177 xbt_dynar_push(globals->irecvs, &c);
179 log_action(action, MSG_get_clock() - clock);
180 asynchronous_cleanup();
184 static void action_wait(const char *const *action)
186 msg_task_t task = NULL;
188 double clock = MSG_get_clock();
189 process_globals_t globals =
190 (process_globals_t) MSG_process_get_data(MSG_process_self());
192 xbt_assert(xbt_dynar_length(globals->irecvs),
193 "action wait not preceded by any irecv: %s",
194 xbt_str_join_array(action, " "));
196 ACT_DEBUG("Entering %s", NAME);
197 comm = xbt_dynar_pop_as(globals->irecvs, msg_comm_t);
198 MSG_comm_wait(comm, -1);
199 task = xbt_dynar_pop_as(globals->tasks, msg_task_t);
200 MSG_comm_destroy(comm);
201 MSG_task_destroy(task);
203 log_action(action, MSG_get_clock() - clock);
206 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
207 static void action_barrier(const char *const *action)
209 static smx_mutex_t mutex = NULL;
210 static smx_cond_t cond = NULL;
211 static int processes_arrived_sofar = 0;
213 if (mutex == NULL) { // first arriving on the barrier
214 mutex = simcall_mutex_init();
215 cond = simcall_cond_init();
216 processes_arrived_sofar = 0;
218 ACT_DEBUG("Entering barrier: %s (%d already there)", NAME,
219 processes_arrived_sofar);
221 simcall_mutex_lock(mutex);
222 if (++processes_arrived_sofar == communicator_size) {
223 simcall_cond_broadcast(cond);
224 simcall_mutex_unlock(mutex);
226 simcall_cond_wait(cond, mutex);
227 simcall_mutex_unlock(mutex);
230 ACT_DEBUG("Exiting barrier: %s", NAME);
232 processes_arrived_sofar--;
233 if (!processes_arrived_sofar) {
234 simcall_cond_destroy(cond);
235 simcall_mutex_destroy(mutex);
240 static void action_reduce(const char *const *action)
243 char *reduce_identifier;
245 double comm_size = parse_double(action[2]);
246 double comp_size = parse_double(action[3]);
247 msg_task_t comp_task = NULL;
248 const char *process_name;
249 double clock = MSG_get_clock();
251 process_globals_t counters =
252 (process_globals_t) MSG_process_get_data(MSG_process_self());
254 xbt_assert(communicator_size, "Size of Communicator is not defined, "
255 "can't use collective operations");
257 process_name = MSG_process_get_name(MSG_process_self());
259 reduce_identifier = bprintf("reduce_%d", counters->reduce_counter++);
261 if (!strcmp(process_name, "p0")) {
262 XBT_DEBUG("%s: %s is the Root", reduce_identifier, process_name);
264 msg_comm_t *comms = xbt_new0(msg_comm_t, communicator_size - 1);
265 msg_task_t *tasks = xbt_new0(msg_task_t, communicator_size - 1);
266 for (i = 1; i < communicator_size; i++) {
267 sprintf(mailbox, "%s_p%d_p0", reduce_identifier, i);
268 comms[i - 1] = MSG_task_irecv(&(tasks[i - 1]), mailbox);
270 MSG_comm_waitall(comms, communicator_size - 1, -1);
271 for (i = 1; i < communicator_size; i++) {
272 MSG_comm_destroy(comms[i - 1]);
273 MSG_task_destroy(tasks[i - 1]);
278 comp_task = MSG_task_create("reduce_comp", comp_size, 0, NULL);
279 XBT_DEBUG("%s: computing 'reduce_comp'", reduce_identifier);
280 MSG_task_execute(comp_task);
281 MSG_task_destroy(comp_task);
282 XBT_DEBUG("%s: computed", reduce_identifier);
285 XBT_DEBUG("%s: %s sends", reduce_identifier, process_name);
286 sprintf(mailbox, "%s_%s_p0", reduce_identifier, process_name);
287 XBT_DEBUG("put on %s", mailbox);
288 MSG_task_send(MSG_task_create(reduce_identifier, 0, comm_size, NULL),
292 log_action(action, MSG_get_clock() - clock);
293 xbt_free(reduce_identifier);
296 static void action_bcast(const char *const *action)
299 char *bcast_identifier;
301 double comm_size = parse_double(action[2]);
302 msg_task_t task = NULL;
303 const char *process_name;
304 double clock = MSG_get_clock();
306 process_globals_t counters =
307 (process_globals_t) MSG_process_get_data(MSG_process_self());
309 xbt_assert(communicator_size, "Size of Communicator is not defined, "
310 "can't use collective operations");
312 process_name = MSG_process_get_name(MSG_process_self());
314 bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
316 if (!strcmp(process_name, "p0")) {
317 XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
319 msg_comm_t *comms = xbt_new0(msg_comm_t, communicator_size - 1);
321 for (i = 1; i < communicator_size; i++) {
322 sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
324 MSG_task_isend(MSG_task_create(mailbox, 0, comm_size, NULL), mailbox);
326 MSG_comm_waitall(comms, communicator_size - 1, -1);
327 for (i = 1; i < communicator_size; i++)
328 MSG_comm_destroy(comms[i - 1]);
331 XBT_DEBUG("%s: all messages sent by %s have been received",
332 bcast_identifier, process_name);
335 sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
336 MSG_task_receive(&task, mailbox);
337 MSG_task_destroy(task);
338 XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
341 log_action(action, MSG_get_clock() - clock);
342 xbt_free(bcast_identifier);
346 static void action_sleep(const char *const *action)
348 const char *duration = action[2];
349 double clock = MSG_get_clock();
351 ACT_DEBUG("Entering %s", NAME);
352 MSG_process_sleep(parse_double(duration));
353 log_action(action, MSG_get_clock() - clock);
356 static void action_allReduce(const char *const *action)
359 char *allreduce_identifier;
361 double comm_size = parse_double(action[2]);
362 double comp_size = parse_double(action[3]);
363 msg_task_t task = NULL, comp_task = NULL;
364 const char *process_name;
365 double clock = MSG_get_clock();
367 process_globals_t counters =
368 (process_globals_t) MSG_process_get_data(MSG_process_self());
370 xbt_assert(communicator_size, "Size of Communicator is not defined, "
371 "can't use collective operations");
373 process_name = MSG_process_get_name(MSG_process_self());
375 allreduce_identifier = bprintf("allReduce_%d", counters->allReduce_counter++);
377 if (!strcmp(process_name, "p0")) {
378 XBT_DEBUG("%s: %s is the Root", allreduce_identifier, process_name);
380 msg_comm_t *comms = xbt_new0(msg_comm_t, communicator_size - 1);
381 msg_task_t *tasks = xbt_new0(msg_task_t, communicator_size - 1);
382 for (i = 1; i < communicator_size; i++) {
383 sprintf(mailbox, "%s_p%d_p0", allreduce_identifier, i);
384 comms[i - 1] = MSG_task_irecv(&(tasks[i - 1]), mailbox);
386 MSG_comm_waitall(comms, communicator_size - 1, -1);
387 for (i = 1; i < communicator_size; i++) {
388 MSG_comm_destroy(comms[i - 1]);
389 MSG_task_destroy(tasks[i - 1]);
393 comp_task = MSG_task_create("allReduce_comp", comp_size, 0, NULL);
394 XBT_DEBUG("%s: computing 'reduce_comp'", allreduce_identifier);
395 MSG_task_execute(comp_task);
396 MSG_task_destroy(comp_task);
397 XBT_DEBUG("%s: computed", allreduce_identifier);
399 for (i = 1; i < communicator_size; i++) {
400 sprintf(mailbox, "%s_p0_p%d", allreduce_identifier, i);
402 MSG_task_isend(MSG_task_create(mailbox, 0, comm_size, NULL), mailbox);
404 MSG_comm_waitall(comms, communicator_size - 1, -1);
405 for (i = 1; i < communicator_size; i++)
406 MSG_comm_destroy(comms[i - 1]);
409 XBT_DEBUG("%s: all messages sent by %s have been received",
410 allreduce_identifier, process_name);
413 XBT_DEBUG("%s: %s sends", allreduce_identifier, process_name);
414 sprintf(mailbox, "%s_%s_p0", allreduce_identifier, process_name);
415 XBT_DEBUG("put on %s", mailbox);
416 MSG_task_send(MSG_task_create(allreduce_identifier, 0, comm_size, NULL),
419 sprintf(mailbox, "%s_p0_%s", allreduce_identifier, process_name);
420 MSG_task_receive(&task, mailbox);
421 MSG_task_destroy(task);
422 XBT_DEBUG("%s: %s has received", allreduce_identifier, process_name);
425 log_action(action, MSG_get_clock() - clock);
426 xbt_free(allreduce_identifier);
429 static void action_comm_size(const char *const *action)
431 const char *size = action[2];
432 double clock = MSG_get_clock();
434 communicator_size = parse_double(size);
435 log_action(action, MSG_get_clock() - clock);
438 static void action_compute(const char *const *action)
440 const char *amount = action[2];
441 msg_task_t task = MSG_task_create("task", parse_double(amount), 0, NULL);
442 double clock = MSG_get_clock();
444 ACT_DEBUG("Entering %s", NAME);
445 MSG_task_execute(task);
446 MSG_task_destroy(task);
447 log_action(action, MSG_get_clock() - clock);
450 static void action_init(const char *const *action)
452 XBT_DEBUG("Initialize the counters");
453 process_globals_t globals =
454 (process_globals_t) calloc(1, sizeof(s_process_globals_t));
455 globals->isends = xbt_dynar_new(sizeof(msg_comm_t), NULL);
456 globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t), NULL);
457 globals->tasks = xbt_dynar_new(sizeof(msg_task_t), NULL);
458 MSG_process_set_data(MSG_process_self(), globals);
462 static void action_finalize(const char *const *action)
464 process_globals_t globals =
465 (process_globals_t) MSG_process_get_data(MSG_process_self());
467 asynchronous_cleanup();
468 xbt_dynar_free_container(&(globals->isends));
469 xbt_dynar_free_container(&(globals->irecvs));
470 xbt_dynar_free_container(&(globals->tasks));
476 int main(int argc, char *argv[])
478 msg_error_t res = MSG_OK;
480 /* Check the given arguments */
481 MSG_init(&argc, argv);
482 /* Explicit initialization of the action module is required now*/
486 printf("Usage: %s platform_file deployment_file [action_files]\n", argv[0]);
488 ("example: %s msg_platform.xml msg_deployment.xml actions # if all actions are in the same file\n",
491 ("example: %s msg_platform.xml msg_deployment.xml # if actions are in separate files, specified in deployment\n",
495 printf("WARNING: THIS BINARY IS KINDA DEPRECATED\n"
496 "This example is still relevant if you want to learn about MSG-based trace replay, "
497 "but if you want to simulate MPI-like traces, you should use the newer version "
498 "that is in the examples/smpi/replay directory instead.\n");
500 /* Simulation setting */
501 MSG_create_environment(argv[1]);
503 /* No need to register functions as in classical MSG programs: the actions get started anyway */
504 MSG_launch_application(argv[2]);
506 /* Action registration */
507 xbt_replay_action_register("init", action_init);
508 xbt_replay_action_register("finalize", action_finalize);
509 xbt_replay_action_register("comm_size", action_comm_size);
510 xbt_replay_action_register("send", action_send);
511 xbt_replay_action_register("Isend", action_Isend);
512 xbt_replay_action_register("recv", action_recv);
513 xbt_replay_action_register("Irecv", action_Irecv);
514 xbt_replay_action_register("wait", action_wait);
515 xbt_replay_action_register("barrier", action_barrier);
516 xbt_replay_action_register("bcast", action_bcast);
517 xbt_replay_action_register("reduce", action_reduce);
518 xbt_replay_action_register("allReduce", action_allReduce);
519 xbt_replay_action_register("sleep", action_sleep);
520 xbt_replay_action_register("compute", action_compute);
523 /* Actually do the simulation using MSG_action_trace_run */
524 res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
526 XBT_INFO("Simulation time %g", MSG_get_clock());
528 /* Explicit finalization of the action module is required now*/