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 "simix/simix.h" /* semaphores for the barrier */
11 #include "xbt.h" /* calloc, printf */
12 #include "instr/instr_private.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 * MSG_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,
43 "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 m_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 static int get_rank (const char *process_name)
73 return atoi(&(process_name[1]));
76 static void asynchronous_cleanup(void) {
77 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
79 /* Destroy any isend which correspond to completed communications */
82 while ((found = MSG_comm_testany(globals->isends)) != -1) {
83 xbt_dynar_remove_at(globals->isends,found,&comm);
84 MSG_comm_destroy(comm);
89 static void action_send(const char *const *action)
93 const char *size_str = action[3];
94 double size=parse_double(size_str);
95 double clock = MSG_get_clock(); /* this "call" is free thanks to inlining */
97 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),action[2]);
99 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
100 name = xbt_str_join_array(action, " ");
103 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
104 int dst_traced = get_rank(action[2]);
105 TRACE_smpi_ptp_in(rank, rank, dst_traced, "send");
106 TRACE_smpi_send(rank, rank, dst_traced);
109 XBT_DEBUG("Entering Send: %s (size: %lg)", name, size);
111 action_Isend(action);
113 MSG_task_send(MSG_task_create(name, 0, size, NULL), to);
116 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
121 TRACE_smpi_ptp_out(rank, rank, dst_traced, "send");
124 asynchronous_cleanup();
127 static void action_Isend(const char *const *action)
130 const char *size = action[3];
131 double clock = MSG_get_clock();
132 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
135 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),action[2]);
137 MSG_task_isend( MSG_task_create(to,0,parse_double(size),NULL), to);
138 xbt_dynar_push(globals->isends,&comm);
140 XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
141 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
143 asynchronous_cleanup();
147 static void action_recv(const char *const *action)
150 char mailbox_name[250];
151 m_task_t task = NULL;
152 double clock = MSG_get_clock();
154 sprintf(mailbox_name, "%s_%s", action[2],
155 MSG_process_get_name(MSG_process_self()));
157 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
158 name = xbt_str_join_array(action, " ");
161 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
162 int src_traced = get_rank(action[2]);
163 TRACE_smpi_ptp_in(rank, src_traced, rank, "recv");
166 XBT_DEBUG("Receiving: %s", name);
167 MSG_error_t res = MSG_task_receive(&task, mailbox_name);
168 // MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
169 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
172 MSG_task_destroy(task);
177 TRACE_smpi_ptp_out(rank, src_traced, rank, "recv");
178 TRACE_smpi_recv(rank, src_traced, rank);
181 asynchronous_cleanup();
184 static void action_Irecv(const char *const *action)
187 double clock = MSG_get_clock();
188 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
190 XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
192 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
193 int src_traced = get_rank(action[2]);
194 globals->last_Irecv_sender_id = src_traced;
195 MSG_process_set_data(MSG_process_self(), (void *) globals);
197 TRACE_smpi_ptp_in(rank, src_traced, rank, "Irecv");
200 sprintf(mailbox, "%s_%s", action[2],
201 MSG_process_get_name(MSG_process_self()));
203 xbt_dynar_push(globals->tasks,&t);
206 xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks)-1),
208 xbt_dynar_push(globals->irecvs,&c);
210 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
213 TRACE_smpi_ptp_out(rank, src_traced, rank, "Irecv");
216 asynchronous_cleanup();
220 static void action_wait(const char *const *action)
223 m_task_t task = NULL;
225 double clock = MSG_get_clock();
226 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
228 xbt_assert(xbt_dynar_length(globals->irecvs),
229 "action wait not preceded by any irecv: %s", xbt_str_join_array(action," "));
231 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
232 name = xbt_str_join_array(action, " ");
234 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
235 int src_traced = counters->last_Irecv_sender_id;
236 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
237 TRACE_smpi_ptp_in(rank, src_traced, rank, "wait");
240 XBT_DEBUG("Entering %s", name);
241 comm = xbt_dynar_pop_as(globals->irecvs,msg_comm_t);
242 MSG_comm_wait(comm,-1);
243 task = xbt_dynar_pop_as(globals->tasks,m_task_t);
244 MSG_comm_destroy(comm);
245 MSG_task_destroy(task);
247 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
250 TRACE_smpi_ptp_out(rank, src_traced, rank, "wait");
251 TRACE_smpi_recv(rank, src_traced, rank);
256 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
257 static void action_barrier(const char *const *action)
260 static smx_mutex_t mutex = NULL;
261 static smx_cond_t cond = NULL;
262 static int processes_arrived_sofar=0;
264 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
265 name = xbt_str_join_array(action, " ");
267 if (mutex == NULL) { // first arriving on the barrier
268 mutex = simcall_mutex_init();
269 cond = simcall_cond_init();
270 processes_arrived_sofar=0;
272 XBT_DEBUG("Entering barrier: %s (%d already there)", name,processes_arrived_sofar);
274 simcall_mutex_lock(mutex);
275 if (++processes_arrived_sofar == communicator_size) {
276 simcall_cond_broadcast(cond);
277 simcall_mutex_unlock(mutex);
279 simcall_cond_wait(cond,mutex);
280 simcall_mutex_unlock(mutex);
283 XBT_DEBUG("Exiting barrier: %s", name);
285 processes_arrived_sofar--;
286 if (!processes_arrived_sofar) {
287 simcall_cond_destroy(cond);
288 simcall_mutex_destroy(mutex);
296 static void action_reduce(const char *const *action)
299 char *reduce_identifier;
301 double comm_size = parse_double(action[2]);
302 double comp_size = parse_double(action[3]);
303 m_task_t comp_task = NULL;
304 const char *process_name;
305 double clock = MSG_get_clock();
307 process_globals_t counters = (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 reduce_identifier = bprintf("reduce_%d", counters->reduce_counter++);
316 if (!strcmp(process_name, "p0")) {
317 XBT_DEBUG("%s: %s is the Root", reduce_identifier, process_name);
319 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
320 m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
321 for (i = 1; i < communicator_size; i++) {
322 sprintf(mailbox, "%s_p%d_p0", reduce_identifier, i);
323 comms[i-1] = MSG_task_irecv(&(tasks[i-1]),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]);
328 MSG_task_destroy(tasks[i-1]);
332 comp_task = MSG_task_create("reduce_comp", comp_size, 0, NULL);
333 XBT_DEBUG("%s: computing 'reduce_comp'", reduce_identifier);
334 MSG_task_execute(comp_task);
335 MSG_task_destroy(comp_task);
336 XBT_DEBUG("%s: computed", reduce_identifier);
339 XBT_DEBUG("%s: %s sends", reduce_identifier, process_name);
340 sprintf(mailbox, "%s_%s_p0", reduce_identifier, process_name);
341 XBT_DEBUG("put on %s", mailbox);
342 MSG_task_send(MSG_task_create(reduce_identifier, 0, comm_size, NULL),
346 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
347 free(reduce_identifier);
350 static void action_bcast(const char *const *action)
353 char *bcast_identifier;
355 double comm_size = parse_double(action[2]);
356 m_task_t task = NULL;
357 const char *process_name;
358 double clock = MSG_get_clock();
360 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
362 xbt_assert(communicator_size, "Size of Communicator is not defined, "
363 "can't use collective operations");
365 process_name = MSG_process_get_name(MSG_process_self());
367 bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
369 if (!strcmp(process_name, "p0")) {
370 XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
372 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
374 for (i = 1; i < communicator_size; i++) {
375 sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
377 MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
380 MSG_comm_waitall(comms,communicator_size-1,-1);
381 for (i = 1; i < communicator_size; i++)
382 MSG_comm_destroy(comms[i-1]);
385 XBT_DEBUG("%s: all messages sent by %s have been received",
386 bcast_identifier, process_name);
389 sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
390 MSG_task_receive(&task, mailbox);
391 MSG_task_destroy(task);
392 XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
395 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
396 free(bcast_identifier);
400 static void action_sleep(const char *const *action)
403 const char *duration = action[2];
404 double clock = MSG_get_clock();
406 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
407 name = xbt_str_join_array(action, " ");
409 XBT_DEBUG("Entering %s", name);
410 MSG_process_sleep(parse_double(duration));
411 XBT_VERB("%s %f ", name, MSG_get_clock() - clock);
416 static void action_allReduce(const char *const *action) {
418 char *allreduce_identifier;
420 double comm_size = parse_double(action[2]);
421 double comp_size = parse_double(action[3]);
422 m_task_t task = NULL, comp_task = NULL;
423 const char *process_name;
424 double clock = MSG_get_clock();
426 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
428 xbt_assert(communicator_size, "Size of Communicator is not defined, "
429 "can't use collective operations");
431 process_name = MSG_process_get_name(MSG_process_self());
433 allreduce_identifier = bprintf("allReduce_%d", counters->allReduce_counter++);
435 if (!strcmp(process_name, "p0")) {
436 XBT_DEBUG("%s: %s is the Root", allreduce_identifier, process_name);
438 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
439 m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
440 for (i = 1; i < communicator_size; i++) {
441 sprintf(mailbox, "%s_p%d_p0", allreduce_identifier, i);
442 comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
444 MSG_comm_waitall(comms,communicator_size-1,-1);
445 for (i = 1; i < communicator_size; i++) {
446 MSG_comm_destroy(comms[i-1]);
447 MSG_task_destroy(tasks[i-1]);
451 comp_task = MSG_task_create("allReduce_comp", comp_size, 0, NULL);
452 XBT_DEBUG("%s: computing 'reduce_comp'", allreduce_identifier);
453 MSG_task_execute(comp_task);
454 MSG_task_destroy(comp_task);
455 XBT_DEBUG("%s: computed", allreduce_identifier);
457 for (i = 1; i < communicator_size; i++) {
458 sprintf(mailbox, "%s_p0_p%d", allreduce_identifier, i);
460 MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
463 MSG_comm_waitall(comms,communicator_size-1,-1);
464 for (i = 1; i < communicator_size; i++)
465 MSG_comm_destroy(comms[i-1]);
468 XBT_DEBUG("%s: all messages sent by %s have been received",
469 allreduce_identifier, process_name);
472 XBT_DEBUG("%s: %s sends", allreduce_identifier, process_name);
473 sprintf(mailbox, "%s_%s_p0", allreduce_identifier, process_name);
474 XBT_DEBUG("put on %s", mailbox);
475 MSG_task_send(MSG_task_create(allreduce_identifier, 0, comm_size, NULL),
478 sprintf(mailbox, "%s_p0_%s", allreduce_identifier, process_name);
479 MSG_task_receive(&task, mailbox);
480 MSG_task_destroy(task);
481 XBT_DEBUG("%s: %s has received", allreduce_identifier, process_name);
484 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
485 free(allreduce_identifier);
488 static void action_comm_size(const char *const *action)
491 const char *size = action[2];
492 double clock = MSG_get_clock();
494 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
495 name = xbt_str_join_array(action, " ");
496 communicator_size = parse_double(size);
497 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
501 static void action_compute(const char *const *action)
504 const char *amout = action[2];
505 m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
506 double clock = MSG_get_clock();
508 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
509 name = xbt_str_join_array(action, " ");
510 XBT_DEBUG("Entering %s", name);
511 MSG_task_execute(task);
512 MSG_task_destroy(task);
513 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
517 static void action_init(const char *const *action)
520 TRACE_smpi_init(get_rank(MSG_process_get_name(MSG_process_self())));
522 XBT_DEBUG("Initialize the counters");
523 process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
524 globals->isends = xbt_dynar_new(sizeof(msg_comm_t),NULL);
525 globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t),NULL);
526 globals->tasks = xbt_dynar_new(sizeof(m_task_t),NULL);
527 MSG_process_set_data(MSG_process_self(),globals);
531 static void action_finalize(const char *const *action)
534 TRACE_smpi_finalize(get_rank(MSG_process_get_name(MSG_process_self())));
536 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
538 xbt_dynar_free_container(&(globals->isends));
539 xbt_dynar_free_container(&(globals->irecvs));
540 xbt_dynar_free_container(&(globals->tasks));
546 int main(int argc, char *argv[])
548 MSG_error_t res = MSG_OK;
550 /* Check the given arguments */
551 MSG_global_init(&argc, argv);
553 printf("Usage: %s platform_file deployment_file [action_files]\n",
556 ("example: %s msg_platform.xml msg_deployment.xml actions # if all actions are in the same file\n",
559 ("example: %s msg_platform.xml msg_deployment.xml # if actions are in separate files, specified in deployment\n",
564 /* Simulation setting */
565 MSG_create_environment(argv[1]);
567 /* No need to register functions as in classical MSG programs: the actions get started anyway */
568 MSG_launch_application(argv[2]);
570 /* Action registration */
571 MSG_action_register("init", action_init);
572 MSG_action_register("finalize", action_finalize);
573 MSG_action_register("comm_size",action_comm_size);
574 MSG_action_register("send", action_send);
575 MSG_action_register("Isend", action_Isend);
576 MSG_action_register("recv", action_recv);
577 MSG_action_register("Irecv", action_Irecv);
578 MSG_action_register("wait", action_wait);
579 MSG_action_register("barrier", action_barrier);
580 MSG_action_register("bcast", action_bcast);
581 MSG_action_register("reduce", action_reduce);
582 MSG_action_register("allReduce",action_allReduce);
583 MSG_action_register("sleep", action_sleep);
584 MSG_action_register("compute", action_compute);
587 /* Actually do the simulation using MSG_action_trace_run */
588 res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
590 XBT_INFO("Simulation time %g", MSG_get_clock());