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/replay.h>
13 /** @addtogroup MSG_examples
15 * @section MSG_ex_actions Trace driven simulations
17 * The <b>actions/actions.c</b> example demonstrates how to run trace-driven simulations. It
18 * is very handy when you want to test an algorithm or protocol that
19 * does nothing unless it receives some events from outside. For
20 * example, a P2P protocol reacts to requests from the user, but
21 * does nothing if there is no such event.
23 * In such situations, SimGrid allows to write your protocol in your
24 * C file, and the events to react to in a separate text file.
25 * Declare a function handling each of the events that you want to
26 * accept in your trace files, register them using \ref
27 * xbt_replay_action_register in your main, and then use \ref
28 * MSG_action_trace_run to launch the simulation. You can either
29 * have one trace file containing all your events, or a file per
30 * simulated process. Check the tesh files in the example directory
31 * for details on how to do it.
33 * This example uses this approach to replay MPI-like traces. It
34 * comes with a set of event handlers reproducing MPI events. This
35 * is somehow similar to SMPI, yet differently implemented. This
36 * code should probably be changed to use SMPI internals instead,
41 XBT_LOG_NEW_DEFAULT_CATEGORY(actions, "Messages specific for this msg example");
42 int communicator_size = 0;
44 static void action_Isend(const char *const *action);
47 int last_Irecv_sender_id;
50 int allReduce_counter;
51 xbt_dynar_t isends; /* of msg_comm_t */
52 /* Used to implement irecv+wait */
53 xbt_dynar_t irecvs; /* of msg_comm_t */
54 xbt_dynar_t tasks; /* of msg_task_t */
55 } s_process_globals_t, *process_globals_t;
58 static double parse_double(const char *string)
63 value = strtod(string, &endptr);
65 THROWF(unknown_error, 0, "%s is not a double", string);
69 #define ACT_DEBUG(...) \
70 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) { \
71 char *NAME = xbt_str_join_array(action, " "); \
72 XBT_DEBUG(__VA_ARGS__); \
76 static void log_action(const char *const *action, double date)
78 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) {
79 char *name = xbt_str_join_array(action, " ");
80 XBT_VERB("%s %f", name, date);
85 static void asynchronous_cleanup(void)
87 process_globals_t globals =
88 (process_globals_t) MSG_process_get_data(MSG_process_self());
90 /* Destroy any isend which correspond to completed communications */
93 while ((found = MSG_comm_testany(globals->isends)) != -1) {
94 xbt_dynar_remove_at(globals->isends, found, &comm);
95 MSG_comm_destroy(comm);
100 static void action_send(const char *const *action)
103 const char *size_str = action[3];
104 double size = parse_double(size_str);
105 double clock = MSG_get_clock(); /* this "call" is free thanks to inlining */
107 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
109 ACT_DEBUG("Entering Send: %s (size: %g)", NAME, size);
111 action_Isend(action);
113 MSG_task_send(MSG_task_create(to, 0, size, NULL), to);
116 log_action(action, MSG_get_clock() - clock);
117 asynchronous_cleanup();
120 static void action_Isend(const char *const *action)
123 const char *size = action[3];
124 double clock = MSG_get_clock();
125 process_globals_t globals =
126 (process_globals_t) MSG_process_get_data(MSG_process_self());
128 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
130 MSG_task_isend(MSG_task_create(to, 0, parse_double(size), NULL), to);
131 xbt_dynar_push(globals->isends, &comm);
133 XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
134 log_action(action, MSG_get_clock() - clock);
135 asynchronous_cleanup();
139 static void action_recv(const char *const *action)
141 char mailbox_name[250];
142 msg_task_t task = NULL;
143 double clock = MSG_get_clock();
145 sprintf(mailbox_name, "%s_%s", action[2],
146 MSG_process_get_name(MSG_process_self()));
148 ACT_DEBUG("Receiving: %s", NAME);
149 msg_error_t res = MSG_task_receive(&task, mailbox_name);
150 log_action(action, MSG_get_clock() - clock);
153 MSG_task_destroy(task);
155 asynchronous_cleanup();
158 static void action_Irecv(const char *const *action)
161 double clock = MSG_get_clock();
162 process_globals_t globals =
163 (process_globals_t) MSG_process_get_data(MSG_process_self());
165 XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
167 sprintf(mailbox, "%s_%s", action[2],
168 MSG_process_get_name(MSG_process_self()));
170 xbt_dynar_push(globals->tasks, &t);
172 MSG_task_irecv(xbt_dynar_get_ptr
173 (globals->tasks, xbt_dynar_length(globals->tasks) - 1),
175 xbt_dynar_push(globals->irecvs, &c);
177 log_action(action, MSG_get_clock() - clock);
178 asynchronous_cleanup();
182 static void action_wait(const char *const *action)
184 msg_task_t task = NULL;
186 double clock = MSG_get_clock();
187 process_globals_t globals =
188 (process_globals_t) MSG_process_get_data(MSG_process_self());
190 xbt_assert(xbt_dynar_length(globals->irecvs),
191 "action wait not preceded by any irecv: %s",
192 xbt_str_join_array(action, " "));
194 ACT_DEBUG("Entering %s", NAME);
195 comm = xbt_dynar_pop_as(globals->irecvs, msg_comm_t);
196 MSG_comm_wait(comm, -1);
197 task = xbt_dynar_pop_as(globals->tasks, msg_task_t);
198 MSG_comm_destroy(comm);
199 MSG_task_destroy(task);
201 log_action(action, MSG_get_clock() - clock);
204 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
205 static void action_barrier(const char *const *action)
207 static smx_mutex_t mutex = NULL;
208 static smx_cond_t cond = NULL;
209 static int processes_arrived_sofar = 0;
211 if (mutex == NULL) { // first arriving on the barrier
212 mutex = simcall_mutex_init();
213 cond = simcall_cond_init();
214 processes_arrived_sofar = 0;
216 ACT_DEBUG("Entering barrier: %s (%d already there)", NAME,
217 processes_arrived_sofar);
219 simcall_mutex_lock(mutex);
220 if (++processes_arrived_sofar == communicator_size) {
221 simcall_cond_broadcast(cond);
222 simcall_mutex_unlock(mutex);
224 simcall_cond_wait(cond, mutex);
225 simcall_mutex_unlock(mutex);
228 ACT_DEBUG("Exiting barrier: %s", NAME);
230 processes_arrived_sofar--;
231 if (!processes_arrived_sofar) {
232 SIMIX_cond_destroy(cond);
233 SIMIX_mutex_destroy(mutex);
238 static void action_reduce(const char *const *action)
241 char *reduce_identifier;
243 double comm_size = parse_double(action[2]);
244 double comp_size = parse_double(action[3]);
245 msg_task_t comp_task = NULL;
246 const char *process_name;
247 double clock = MSG_get_clock();
249 process_globals_t counters =
250 (process_globals_t) MSG_process_get_data(MSG_process_self());
252 xbt_assert(communicator_size, "Size of Communicator is not defined, "
253 "can't use collective operations");
255 process_name = MSG_process_get_name(MSG_process_self());
257 reduce_identifier = bprintf("reduce_%d", counters->reduce_counter++);
259 if (!strcmp(process_name, "p0")) {
260 XBT_DEBUG("%s: %s is the Root", reduce_identifier, process_name);
262 msg_comm_t *comms = xbt_new0(msg_comm_t, communicator_size - 1);
263 msg_task_t *tasks = xbt_new0(msg_task_t, communicator_size - 1);
264 for (i = 1; i < communicator_size; i++) {
265 sprintf(mailbox, "%s_p%d_p0", reduce_identifier, i);
266 comms[i - 1] = MSG_task_irecv(&(tasks[i - 1]), mailbox);
268 MSG_comm_waitall(comms, communicator_size - 1, -1);
269 for (i = 1; i < communicator_size; i++) {
270 MSG_comm_destroy(comms[i - 1]);
271 MSG_task_destroy(tasks[i - 1]);
276 comp_task = MSG_task_create("reduce_comp", comp_size, 0, NULL);
277 XBT_DEBUG("%s: computing 'reduce_comp'", reduce_identifier);
278 MSG_task_execute(comp_task);
279 MSG_task_destroy(comp_task);
280 XBT_DEBUG("%s: computed", reduce_identifier);
283 XBT_DEBUG("%s: %s sends", reduce_identifier, process_name);
284 sprintf(mailbox, "%s_%s_p0", reduce_identifier, process_name);
285 XBT_DEBUG("put on %s", mailbox);
286 MSG_task_send(MSG_task_create(reduce_identifier, 0, comm_size, NULL),
290 log_action(action, MSG_get_clock() - clock);
291 xbt_free(reduce_identifier);
294 static void action_bcast(const char *const *action)
297 char *bcast_identifier;
299 double comm_size = parse_double(action[2]);
300 msg_task_t task = NULL;
301 const char *process_name;
302 double clock = MSG_get_clock();
304 process_globals_t counters =
305 (process_globals_t) MSG_process_get_data(MSG_process_self());
307 xbt_assert(communicator_size, "Size of Communicator is not defined, "
308 "can't use collective operations");
310 process_name = MSG_process_get_name(MSG_process_self());
312 bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
314 if (!strcmp(process_name, "p0")) {
315 XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
317 msg_comm_t *comms = xbt_new0(msg_comm_t, communicator_size - 1);
319 for (i = 1; i < communicator_size; i++) {
320 sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
322 MSG_task_isend(MSG_task_create(mailbox, 0, comm_size, NULL), mailbox);
324 MSG_comm_waitall(comms, communicator_size - 1, -1);
325 for (i = 1; i < communicator_size; i++)
326 MSG_comm_destroy(comms[i - 1]);
329 XBT_DEBUG("%s: all messages sent by %s have been received",
330 bcast_identifier, process_name);
333 sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
334 MSG_task_receive(&task, mailbox);
335 MSG_task_destroy(task);
336 XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
339 log_action(action, MSG_get_clock() - clock);
340 xbt_free(bcast_identifier);
344 static void action_sleep(const char *const *action)
346 const char *duration = action[2];
347 double clock = MSG_get_clock();
349 ACT_DEBUG("Entering %s", NAME);
350 MSG_process_sleep(parse_double(duration));
351 log_action(action, MSG_get_clock() - clock);
354 static void action_allReduce(const char *const *action)
357 char *allreduce_identifier;
359 double comm_size = parse_double(action[2]);
360 double comp_size = parse_double(action[3]);
361 msg_task_t task = NULL, comp_task = NULL;
362 const char *process_name;
363 double clock = MSG_get_clock();
365 process_globals_t counters =
366 (process_globals_t) MSG_process_get_data(MSG_process_self());
368 xbt_assert(communicator_size, "Size of Communicator is not defined, "
369 "can't use collective operations");
371 process_name = MSG_process_get_name(MSG_process_self());
373 allreduce_identifier = bprintf("allReduce_%d", counters->allReduce_counter++);
375 if (!strcmp(process_name, "p0")) {
376 XBT_DEBUG("%s: %s is the Root", allreduce_identifier, process_name);
378 msg_comm_t *comms = xbt_new0(msg_comm_t, communicator_size - 1);
379 msg_task_t *tasks = xbt_new0(msg_task_t, communicator_size - 1);
380 for (i = 1; i < communicator_size; i++) {
381 sprintf(mailbox, "%s_p%d_p0", allreduce_identifier, i);
382 comms[i - 1] = MSG_task_irecv(&(tasks[i - 1]), mailbox);
384 MSG_comm_waitall(comms, communicator_size - 1, -1);
385 for (i = 1; i < communicator_size; i++) {
386 MSG_comm_destroy(comms[i - 1]);
387 MSG_task_destroy(tasks[i - 1]);
391 comp_task = MSG_task_create("allReduce_comp", comp_size, 0, NULL);
392 XBT_DEBUG("%s: computing 'reduce_comp'", allreduce_identifier);
393 MSG_task_execute(comp_task);
394 MSG_task_destroy(comp_task);
395 XBT_DEBUG("%s: computed", allreduce_identifier);
397 for (i = 1; i < communicator_size; i++) {
398 sprintf(mailbox, "%s_p0_p%d", allreduce_identifier, i);
400 MSG_task_isend(MSG_task_create(mailbox, 0, comm_size, NULL), mailbox);
402 MSG_comm_waitall(comms, communicator_size - 1, -1);
403 for (i = 1; i < communicator_size; i++)
404 MSG_comm_destroy(comms[i - 1]);
407 XBT_DEBUG("%s: all messages sent by %s have been received",
408 allreduce_identifier, process_name);
411 XBT_DEBUG("%s: %s sends", allreduce_identifier, process_name);
412 sprintf(mailbox, "%s_%s_p0", allreduce_identifier, process_name);
413 XBT_DEBUG("put on %s", mailbox);
414 MSG_task_send(MSG_task_create(allreduce_identifier, 0, comm_size, NULL),
417 sprintf(mailbox, "%s_p0_%s", allreduce_identifier, process_name);
418 MSG_task_receive(&task, mailbox);
419 MSG_task_destroy(task);
420 XBT_DEBUG("%s: %s has received", allreduce_identifier, process_name);
423 log_action(action, MSG_get_clock() - clock);
424 xbt_free(allreduce_identifier);
427 static void action_comm_size(const char *const *action)
429 const char *size = action[2];
430 double clock = MSG_get_clock();
432 communicator_size = parse_double(size);
433 log_action(action, MSG_get_clock() - clock);
436 static void action_compute(const char *const *action)
438 const char *amount = action[2];
439 msg_task_t task = MSG_task_create("task", parse_double(amount), 0, NULL);
440 double clock = MSG_get_clock();
442 ACT_DEBUG("Entering %s", NAME);
443 MSG_task_execute(task);
444 MSG_task_destroy(task);
445 log_action(action, MSG_get_clock() - clock);
448 static void action_init(const char *const *action)
450 XBT_DEBUG("Initialize the counters");
451 process_globals_t globals =
452 (process_globals_t) calloc(1, sizeof(s_process_globals_t));
453 globals->isends = xbt_dynar_new(sizeof(msg_comm_t), NULL);
454 globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t), NULL);
455 globals->tasks = xbt_dynar_new(sizeof(msg_task_t), NULL);
456 MSG_process_set_data(MSG_process_self(), globals);
460 static void action_finalize(const char *const *action)
462 process_globals_t globals =
463 (process_globals_t) MSG_process_get_data(MSG_process_self());
465 asynchronous_cleanup();
466 xbt_dynar_free_container(&(globals->isends));
467 xbt_dynar_free_container(&(globals->irecvs));
468 xbt_dynar_free_container(&(globals->tasks));
474 int main(int argc, char *argv[])
476 msg_error_t res = MSG_OK;
478 /* Check the given arguments */
479 MSG_init(&argc, argv);
480 /* Explicit initialization of the action module is required now*/
484 "Usage: %s platform_file deployment_file [action_files]\n"
485 "\t# if all actions are in the same file\n"
486 "\tExample: %s msg_platform.xml msg_deployment.xml actions\n"
487 "\t# if actions are in separate files, specified in deployment\n"
488 "\tExample: %s msg_platform.xml msg_deployment.xml ",
489 argv[0],argv[0],argv[0]);
491 printf("WARNING: THIS BINARY IS KINDA DEPRECATED\n"
492 "This example is still relevant if you want to learn about MSG-based trace replay, "
493 "but if you want to simulate MPI-like traces, you should use the newer version "
494 "that is in the examples/smpi/replay directory instead.\n");
496 /* Simulation setting */
497 MSG_create_environment(argv[1]);
499 /* No need to register functions as in classical MSG programs: the actions get started anyway */
500 MSG_launch_application(argv[2]);
502 /* Action registration */
503 xbt_replay_action_register("init", action_init);
504 xbt_replay_action_register("finalize", action_finalize);
505 xbt_replay_action_register("comm_size", action_comm_size);
506 xbt_replay_action_register("send", action_send);
507 xbt_replay_action_register("Isend", action_Isend);
508 xbt_replay_action_register("recv", action_recv);
509 xbt_replay_action_register("Irecv", action_Irecv);
510 xbt_replay_action_register("wait", action_wait);
511 xbt_replay_action_register("barrier", action_barrier);
512 xbt_replay_action_register("bcast", action_bcast);
513 xbt_replay_action_register("reduce", action_reduce);
514 xbt_replay_action_register("allReduce", action_allReduce);
515 xbt_replay_action_register("sleep", action_sleep);
516 xbt_replay_action_register("compute", action_compute);
519 /* Actually do the simulation using MSG_action_trace_run */
520 res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
522 XBT_INFO("Simulation time %g", MSG_get_clock());
524 /* Explicit finalization of the action module is required now*/
527 return res != MSG_OK;