1 /* Copyright (c) 2009-2015. 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 "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>
14 /** @addtogroup MSG_examples
16 * @section MSG_ex_actions Trace driven simulations
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.
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.
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,
42 XBT_LOG_NEW_DEFAULT_CATEGORY(actions, "Messages specific for this msg example");
43 int communicator_size = 0;
45 static void action_Isend(const char *const *action);
48 int last_Irecv_sender_id;
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;
59 static double parse_double(const char *string)
64 value = strtod(string, &endptr);
66 THROWF(unknown_error, 0, "%s is not a double", string);
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__); \
77 static void log_action(const char *const *action, double date)
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);
86 static void asynchronous_cleanup(void)
88 process_globals_t globals =
89 (process_globals_t) MSG_process_get_data(MSG_process_self());
91 /* Destroy any isend which correspond to completed communications */
94 while ((found = MSG_comm_testany(globals->isends)) != -1) {
95 xbt_dynar_remove_at(globals->isends, found, &comm);
96 MSG_comm_destroy(comm);
101 static void action_send(const char *const *action)
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 */
108 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
110 ACT_DEBUG("Entering Send: %s (size: %g)", NAME, size);
112 action_Isend(action);
114 MSG_task_send(MSG_task_create(to, 0, size, NULL), to);
117 log_action(action, MSG_get_clock() - clock);
118 asynchronous_cleanup();
121 static void action_Isend(const char *const *action)
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());
129 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
131 MSG_task_isend(MSG_task_create(to, 0, parse_double(size), NULL), to);
132 xbt_dynar_push(globals->isends, &comm);
134 XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
135 log_action(action, MSG_get_clock() - clock);
136 asynchronous_cleanup();
140 static void action_recv(const char *const *action)
142 char mailbox_name[250];
143 msg_task_t task = NULL;
144 double clock = MSG_get_clock();
146 sprintf(mailbox_name, "%s_%s", action[2],
147 MSG_process_get_name(MSG_process_self()));
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);
154 MSG_task_destroy(task);
156 asynchronous_cleanup();
159 static void action_Irecv(const char *const *action)
162 double clock = MSG_get_clock();
163 process_globals_t globals =
164 (process_globals_t) MSG_process_get_data(MSG_process_self());
166 XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
168 sprintf(mailbox, "%s_%s", action[2],
169 MSG_process_get_name(MSG_process_self()));
171 xbt_dynar_push(globals->tasks, &t);
173 MSG_task_irecv(xbt_dynar_get_ptr
174 (globals->tasks, xbt_dynar_length(globals->tasks) - 1),
176 xbt_dynar_push(globals->irecvs, &c);
178 log_action(action, MSG_get_clock() - clock);
179 asynchronous_cleanup();
183 static void action_wait(const char *const *action)
185 msg_task_t task = NULL;
187 double clock = MSG_get_clock();
188 process_globals_t globals =
189 (process_globals_t) MSG_process_get_data(MSG_process_self());
191 xbt_assert(xbt_dynar_length(globals->irecvs),
192 "action wait not preceded by any irecv: %s",
193 xbt_str_join_array(action, " "));
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);
202 log_action(action, MSG_get_clock() - clock);
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)
208 static smx_mutex_t mutex = NULL;
209 static smx_cond_t cond = NULL;
210 static int processes_arrived_sofar = 0;
212 if (mutex == NULL) { // first arriving on the barrier
213 mutex = simcall_mutex_init();
214 cond = simcall_cond_init();
215 processes_arrived_sofar = 0;
217 ACT_DEBUG("Entering barrier: %s (%d already there)", NAME,
218 processes_arrived_sofar);
220 simcall_mutex_lock(mutex);
221 if (++processes_arrived_sofar == communicator_size) {
222 simcall_cond_broadcast(cond);
223 simcall_mutex_unlock(mutex);
225 simcall_cond_wait(cond, mutex);
226 simcall_mutex_unlock(mutex);
229 ACT_DEBUG("Exiting barrier: %s", NAME);
231 processes_arrived_sofar--;
232 if (!processes_arrived_sofar) {
233 SIMIX_cond_destroy(cond);
234 SIMIX_mutex_destroy(mutex);
239 static void action_reduce(const char *const *action)
242 char *reduce_identifier;
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();
250 process_globals_t counters =
251 (process_globals_t) MSG_process_get_data(MSG_process_self());
253 xbt_assert(communicator_size, "Size of Communicator is not defined, "
254 "can't use collective operations");
256 process_name = MSG_process_get_name(MSG_process_self());
258 reduce_identifier = bprintf("reduce_%d", counters->reduce_counter++);
260 if (!strcmp(process_name, "p0")) {
261 XBT_DEBUG("%s: %s is the Root", reduce_identifier, process_name);
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);
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]);
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);
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),
291 log_action(action, MSG_get_clock() - clock);
292 xbt_free(reduce_identifier);
295 static void action_bcast(const char *const *action)
298 char *bcast_identifier;
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();
305 process_globals_t counters =
306 (process_globals_t) MSG_process_get_data(MSG_process_self());
308 xbt_assert(communicator_size, "Size of Communicator is not defined, "
309 "can't use collective operations");
311 process_name = MSG_process_get_name(MSG_process_self());
313 bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
315 if (!strcmp(process_name, "p0")) {
316 XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
318 msg_comm_t *comms = xbt_new0(msg_comm_t, communicator_size - 1);
320 for (i = 1; i < communicator_size; i++) {
321 sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
323 MSG_task_isend(MSG_task_create(mailbox, 0, comm_size, NULL), mailbox);
325 MSG_comm_waitall(comms, communicator_size - 1, -1);
326 for (i = 1; i < communicator_size; i++)
327 MSG_comm_destroy(comms[i - 1]);
330 XBT_DEBUG("%s: all messages sent by %s have been received",
331 bcast_identifier, process_name);
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);
340 log_action(action, MSG_get_clock() - clock);
341 xbt_free(bcast_identifier);
345 static void action_sleep(const char *const *action)
347 const char *duration = action[2];
348 double clock = MSG_get_clock();
350 ACT_DEBUG("Entering %s", NAME);
351 MSG_process_sleep(parse_double(duration));
352 log_action(action, MSG_get_clock() - clock);
355 static void action_allReduce(const char *const *action)
358 char *allreduce_identifier;
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();
366 process_globals_t counters =
367 (process_globals_t) MSG_process_get_data(MSG_process_self());
369 xbt_assert(communicator_size, "Size of Communicator is not defined, "
370 "can't use collective operations");
372 process_name = MSG_process_get_name(MSG_process_self());
374 allreduce_identifier = bprintf("allReduce_%d", counters->allReduce_counter++);
376 if (!strcmp(process_name, "p0")) {
377 XBT_DEBUG("%s: %s is the Root", allreduce_identifier, process_name);
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);
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]);
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);
398 for (i = 1; i < communicator_size; i++) {
399 sprintf(mailbox, "%s_p0_p%d", allreduce_identifier, i);
401 MSG_task_isend(MSG_task_create(mailbox, 0, comm_size, NULL), mailbox);
403 MSG_comm_waitall(comms, communicator_size - 1, -1);
404 for (i = 1; i < communicator_size; i++)
405 MSG_comm_destroy(comms[i - 1]);
408 XBT_DEBUG("%s: all messages sent by %s have been received",
409 allreduce_identifier, process_name);
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),
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);
424 log_action(action, MSG_get_clock() - clock);
425 xbt_free(allreduce_identifier);
428 static void action_comm_size(const char *const *action)
430 const char *size = action[2];
431 double clock = MSG_get_clock();
433 communicator_size = parse_double(size);
434 log_action(action, MSG_get_clock() - clock);
437 static void action_compute(const char *const *action)
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();
443 ACT_DEBUG("Entering %s", NAME);
444 MSG_task_execute(task);
445 MSG_task_destroy(task);
446 log_action(action, MSG_get_clock() - clock);
449 static void action_init(const char *const *action)
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);
461 static void action_finalize(const char *const *action)
463 process_globals_t globals =
464 (process_globals_t) MSG_process_get_data(MSG_process_self());
466 asynchronous_cleanup();
467 xbt_dynar_free_container(&(globals->isends));
468 xbt_dynar_free_container(&(globals->irecvs));
469 xbt_dynar_free_container(&(globals->tasks));
475 int main(int argc, char *argv[])
477 msg_error_t res = MSG_OK;
479 /* Check the given arguments */
480 MSG_init(&argc, argv);
481 /* Explicit initialization of the action module is required now*/
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]);
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");
497 /* Simulation setting */
498 MSG_create_environment(argv[1]);
500 /* No need to register functions as in classical MSG programs: the actions get started anyway */
501 MSG_launch_application(argv[2]);
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);
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
523 XBT_INFO("Simulation time %g", MSG_get_clock());
525 /* Explicit finalization of the action module is required now*/
528 return res != MSG_OK;