1 /* Copyright (c) 2009, 2010. 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,
44 "Messages specific for this msg example");
45 int communicator_size = 0;
47 static void action_Isend(const char *const *action);
50 int last_Irecv_sender_id;
53 int allReduce_counter;
54 xbt_dynar_t isends; /* of msg_comm_t */
55 /* Used to implement irecv+wait */
56 xbt_dynar_t irecvs; /* of msg_comm_t */
57 xbt_dynar_t tasks; /* of msg_task_t */
58 } s_process_globals_t, *process_globals_t;
61 static double parse_double(const char *string)
66 value = strtod(string, &endptr);
68 THROWF(unknown_error, 0, "%s is not a double", string);
72 static int get_rank (const char *process_name)
74 return atoi(&(process_name[1]));
77 static void asynchronous_cleanup(void) {
78 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
80 /* Destroy any isend which correspond to completed communications */
83 while ((found = MSG_comm_testany(globals->isends)) != -1) {
84 xbt_dynar_remove_at(globals->isends,found,&comm);
85 MSG_comm_destroy(comm);
90 static void action_send(const char *const *action)
94 const char *size_str = action[3];
95 double size=parse_double(size_str);
96 double clock = MSG_get_clock(); /* this "call" is free thanks to inlining */
98 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),action[2]);
100 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
101 name = xbt_str_join_array(action, " ");
103 XBT_DEBUG("Entering Send: %s (size: %lg)", name, size);
105 action_Isend(action);
107 MSG_task_send(MSG_task_create(name, 0, size, NULL), to);
110 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
113 asynchronous_cleanup();
116 static void action_Isend(const char *const *action)
119 const char *size = action[3];
120 double clock = MSG_get_clock();
121 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
124 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),action[2]);
126 MSG_task_isend( MSG_task_create(to,0,parse_double(size),NULL), to);
127 xbt_dynar_push(globals->isends,&comm);
129 XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
130 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
132 asynchronous_cleanup();
136 static void action_recv(const char *const *action)
139 char mailbox_name[250];
140 msg_task_t task = NULL;
141 double clock = MSG_get_clock();
143 sprintf(mailbox_name, "%s_%s", action[2],
144 MSG_process_get_name(MSG_process_self()));
146 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
147 name = xbt_str_join_array(action, " ");
149 XBT_DEBUG("Receiving: %s", name);
150 msg_error_t res = MSG_task_receive(&task, mailbox_name);
151 // MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
152 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
155 MSG_task_destroy(task);
159 asynchronous_cleanup();
162 static void action_Irecv(const char *const *action)
165 double clock = MSG_get_clock();
166 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
168 XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
170 sprintf(mailbox, "%s_%s", action[2],
171 MSG_process_get_name(MSG_process_self()));
173 xbt_dynar_push(globals->tasks,&t);
176 xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks)-1),
178 xbt_dynar_push(globals->irecvs,&c);
180 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
182 asynchronous_cleanup();
186 static void action_wait(const char *const *action)
189 msg_task_t task = NULL;
191 double clock = MSG_get_clock();
192 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
194 xbt_assert(xbt_dynar_length(globals->irecvs),
195 "action wait not preceded by any irecv: %s", xbt_str_join_array(action," "));
197 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
198 name = xbt_str_join_array(action, " ");
200 XBT_DEBUG("Entering %s", name);
201 comm = xbt_dynar_pop_as(globals->irecvs,msg_comm_t);
202 MSG_comm_wait(comm,-1);
203 task = xbt_dynar_pop_as(globals->tasks,msg_task_t);
204 MSG_comm_destroy(comm);
205 MSG_task_destroy(task);
207 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
211 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
212 static void action_barrier(const char *const *action)
215 static smx_mutex_t mutex = NULL;
216 static smx_cond_t cond = NULL;
217 static int processes_arrived_sofar=0;
219 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
220 name = xbt_str_join_array(action, " ");
222 if (mutex == NULL) { // first arriving on the barrier
223 mutex = simcall_mutex_init();
224 cond = simcall_cond_init();
225 processes_arrived_sofar=0;
227 XBT_DEBUG("Entering barrier: %s (%d already there)", name,processes_arrived_sofar);
229 simcall_mutex_lock(mutex);
230 if (++processes_arrived_sofar == communicator_size) {
231 simcall_cond_broadcast(cond);
232 simcall_mutex_unlock(mutex);
234 simcall_cond_wait(cond,mutex);
235 simcall_mutex_unlock(mutex);
238 XBT_DEBUG("Exiting barrier: %s", name);
240 processes_arrived_sofar--;
241 if (!processes_arrived_sofar) {
242 simcall_cond_destroy(cond);
243 simcall_mutex_destroy(mutex);
251 static void action_reduce(const char *const *action)
254 char *reduce_identifier;
256 double comm_size = parse_double(action[2]);
257 double comp_size = parse_double(action[3]);
258 msg_task_t comp_task = NULL;
259 const char *process_name;
260 double clock = MSG_get_clock();
262 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
264 xbt_assert(communicator_size, "Size of Communicator is not defined, "
265 "can't use collective operations");
267 process_name = MSG_process_get_name(MSG_process_self());
269 reduce_identifier = bprintf("reduce_%d", counters->reduce_counter++);
271 if (!strcmp(process_name, "p0")) {
272 XBT_DEBUG("%s: %s is the Root", reduce_identifier, process_name);
274 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
275 msg_task_t *tasks = xbt_new0(msg_task_t,communicator_size-1);
276 for (i = 1; i < communicator_size; i++) {
277 sprintf(mailbox, "%s_p%d_p0", reduce_identifier, i);
278 xbt_dynar_push_as(comms, msg_comm_t, MSG_task_irecv(&(tasks[i-1]),mailbox));
280 MSG_comm_waitall(comms, -1);
284 xbt_dynar_foreach(comms, cursor, comm) {
285 MSG_comm_destroy(comm);
286 MSG_task_destroy(tasks[i]);
289 xbt_dynar_free(&comms);
291 comp_task = MSG_task_create("reduce_comp", comp_size, 0, NULL);
292 XBT_DEBUG("%s: computing 'reduce_comp'", reduce_identifier);
293 MSG_task_execute(comp_task);
294 MSG_task_destroy(comp_task);
295 XBT_DEBUG("%s: computed", reduce_identifier);
298 XBT_DEBUG("%s: %s sends", reduce_identifier, process_name);
299 sprintf(mailbox, "%s_%s_p0", reduce_identifier, process_name);
300 XBT_DEBUG("put on %s", mailbox);
301 MSG_task_send(MSG_task_create(reduce_identifier, 0, comm_size, NULL),
305 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
306 free(reduce_identifier);
309 static void action_bcast(const char *const *action)
312 char *bcast_identifier;
314 double comm_size = parse_double(action[2]);
315 msg_task_t task = NULL;
316 const char *process_name;
317 double clock = MSG_get_clock();
319 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
321 xbt_assert(communicator_size, "Size of Communicator is not defined, "
322 "can't use collective operations");
324 process_name = MSG_process_get_name(MSG_process_self());
326 bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
328 if (!strcmp(process_name, "p0")) {
329 XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
331 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
334 for (i = 1; i < communicator_size; i++) {
335 sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
336 xbt_dynar_push_as(comms, msg_comm_t,
337 MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
340 MSG_comm_waitall(comms, -1);
344 xbt_dynar_foreach(comms, cursor, comm) {
345 MSG_comm_destroy(comm);
347 xbt_dynar_free(&comms);
350 XBT_DEBUG("%s: all messages sent by %s have been received",
351 bcast_identifier, process_name);
354 sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
355 MSG_task_receive(&task, mailbox);
356 MSG_task_destroy(task);
357 XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
360 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
361 free(bcast_identifier);
365 static void action_sleep(const char *const *action)
368 const char *duration = action[2];
369 double clock = MSG_get_clock();
371 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
372 name = xbt_str_join_array(action, " ");
374 XBT_DEBUG("Entering %s", name);
375 MSG_process_sleep(parse_double(duration));
376 XBT_VERB("%s %f ", name, MSG_get_clock() - clock);
381 static void action_allReduce(const char *const *action) {
383 char *allreduce_identifier;
385 double comm_size = parse_double(action[2]);
386 double comp_size = parse_double(action[3]);
387 msg_task_t task = NULL, comp_task = NULL;
388 const char *process_name;
389 double clock = MSG_get_clock();
391 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
393 xbt_assert(communicator_size, "Size of Communicator is not defined, "
394 "can't use collective operations");
396 process_name = MSG_process_get_name(MSG_process_self());
398 allreduce_identifier = bprintf("allReduce_%d", counters->allReduce_counter++);
400 if (!strcmp(process_name, "p0")) {
401 XBT_DEBUG("%s: %s is the Root", allreduce_identifier, process_name);
403 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
404 msg_task_t *tasks = xbt_new0(msg_task_t,communicator_size-1);
405 for (i = 1; i < communicator_size; i++) {
406 sprintf(mailbox, "%s_p%d_p0", allreduce_identifier, i);
407 xbt_dynar_push_as(comms, msg_comm_t, MSG_task_irecv(&(tasks[i-1]),mailbox));
409 MSG_comm_waitall(comms, -1);
413 xbt_dynar_foreach(comms, cursor, comm) {
414 MSG_comm_destroy(comm);
415 MSG_task_destroy(tasks[i]);
419 comp_task = MSG_task_create("allReduce_comp", comp_size, 0, NULL);
420 XBT_DEBUG("%s: computing 'reduce_comp'", allreduce_identifier);
421 MSG_task_execute(comp_task);
422 MSG_task_destroy(comp_task);
423 XBT_DEBUG("%s: computed", allreduce_identifier);
425 xbt_dynar_reset(comms);
426 for (i = 1; i < communicator_size; i++) {
427 sprintf(mailbox, "%s_p0_p%d", allreduce_identifier, i);
428 xbt_dynar_push_as(comms, msg_comm_t,
429 MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
432 MSG_comm_waitall(comms, -1);
433 xbt_dynar_foreach(comms, cursor, comm) {
434 MSG_comm_destroy(comm);
436 xbt_dynar_free(&comms);
438 XBT_DEBUG("%s: all messages sent by %s have been received",
439 allreduce_identifier, process_name);
442 XBT_DEBUG("%s: %s sends", allreduce_identifier, process_name);
443 sprintf(mailbox, "%s_%s_p0", allreduce_identifier, process_name);
444 XBT_DEBUG("put on %s", mailbox);
445 MSG_task_send(MSG_task_create(allreduce_identifier, 0, comm_size, NULL),
448 sprintf(mailbox, "%s_p0_%s", allreduce_identifier, process_name);
449 MSG_task_receive(&task, mailbox);
450 MSG_task_destroy(task);
451 XBT_DEBUG("%s: %s has received", allreduce_identifier, process_name);
454 if (XBT_LOG_ISENABLED(actions,xbt_log_priority_verbose)) {
455 char *a = xbt_str_join_array(action, " ");
456 XBT_VERB("%s %f", a, MSG_get_clock() - clock);
459 free(allreduce_identifier);
462 static void action_comm_size(const char *const *action)
465 const char *size = action[2];
466 double clock = MSG_get_clock();
468 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
469 name = xbt_str_join_array(action, " ");
470 communicator_size = parse_double(size);
471 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
475 static void action_compute(const char *const *action)
478 const char *amout = action[2];
479 msg_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
480 double clock = MSG_get_clock();
482 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
483 name = xbt_str_join_array(action, " ");
484 XBT_DEBUG("Entering %s", name);
485 MSG_task_execute(task);
486 MSG_task_destroy(task);
487 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
491 static void action_init(const char *const *action)
493 XBT_DEBUG("Initialize the counters");
494 process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
495 globals->isends = xbt_dynar_new(sizeof(msg_comm_t),NULL);
496 globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t),NULL);
497 globals->tasks = xbt_dynar_new(sizeof(msg_task_t),NULL);
498 MSG_process_set_data(MSG_process_self(),globals);
502 static void action_finalize(const char *const *action)
504 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
506 xbt_dynar_free_container(&(globals->isends));
507 xbt_dynar_free_container(&(globals->irecvs));
508 xbt_dynar_free_container(&(globals->tasks));
514 int main(int argc, char *argv[])
516 msg_error_t res = MSG_OK;
518 /* Check the given arguments */
519 MSG_init(&argc, argv);
521 printf("Usage: %s platform_file deployment_file [action_files]\n",
524 ("example: %s msg_platform.xml msg_deployment.xml actions # if all actions are in the same file\n",
527 ("example: %s msg_platform.xml msg_deployment.xml # if actions are in separate files, specified in deployment\n",
532 /* Simulation setting */
533 MSG_create_environment(argv[1]);
535 /* No need to register functions as in classical MSG programs: the actions get started anyway */
536 MSG_launch_application(argv[2]);
538 /* Action registration */
539 xbt_replay_action_register("init", action_init);
540 xbt_replay_action_register("finalize", action_finalize);
541 xbt_replay_action_register("comm_size",action_comm_size);
542 xbt_replay_action_register("send", action_send);
543 xbt_replay_action_register("Isend", action_Isend);
544 xbt_replay_action_register("recv", action_recv);
545 xbt_replay_action_register("Irecv", action_Irecv);
546 xbt_replay_action_register("wait", action_wait);
547 xbt_replay_action_register("barrier", action_barrier);
548 xbt_replay_action_register("bcast", action_bcast);
549 xbt_replay_action_register("reduce", action_reduce);
550 xbt_replay_action_register("allReduce",action_allReduce);
551 xbt_replay_action_register("sleep", action_sleep);
552 xbt_replay_action_register("compute", action_compute);
555 /* Actually do the simulation using MSG_action_trace_run */
556 res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
558 XBT_INFO("Simulation time %g", MSG_get_clock());