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, "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 static int get_rank(const char *process_name)
73 return atoi(&(process_name[1]));
76 static void asynchronous_cleanup(void)
78 process_globals_t globals =
79 (process_globals_t) MSG_process_get_data(MSG_process_self());
81 /* Destroy any isend which correspond to completed communications */
84 while ((found = MSG_comm_testany(globals->isends)) != -1) {
85 xbt_dynar_remove_at(globals->isends, found, &comm);
86 MSG_comm_destroy(comm);
91 static void action_send(const char *const *action)
95 const char *size_str = action[3];
96 double size = parse_double(size_str);
97 double clock = MSG_get_clock(); /* this "call" is free thanks to inlining */
99 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
101 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
102 name = xbt_str_join_array(action, " ");
104 XBT_DEBUG("Entering Send: %s (size: %lg)", name, size);
106 action_Isend(action);
108 MSG_task_send(MSG_task_create(name, 0, size, NULL), to);
111 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
114 asynchronous_cleanup();
117 static void action_Isend(const char *const *action)
120 const char *size = action[3];
121 double clock = MSG_get_clock();
122 process_globals_t globals =
123 (process_globals_t) MSG_process_get_data(MSG_process_self());
126 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
128 MSG_task_isend(MSG_task_create(to, 0, parse_double(size), NULL), to);
129 xbt_dynar_push(globals->isends, &comm);
131 XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
132 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
134 asynchronous_cleanup();
138 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 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
149 name = xbt_str_join_array(action, " ");
151 XBT_DEBUG("Receiving: %s", name);
152 msg_error_t res = MSG_task_receive(&task, mailbox_name);
153 // MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
154 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
157 MSG_task_destroy(task);
161 asynchronous_cleanup();
164 static void action_Irecv(const char *const *action)
167 double clock = MSG_get_clock();
168 process_globals_t globals =
169 (process_globals_t) MSG_process_get_data(MSG_process_self());
171 XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
173 sprintf(mailbox, "%s_%s", action[2],
174 MSG_process_get_name(MSG_process_self()));
176 xbt_dynar_push(globals->tasks, &t);
178 MSG_task_irecv(xbt_dynar_get_ptr
179 (globals->tasks, xbt_dynar_length(globals->tasks) - 1),
181 xbt_dynar_push(globals->irecvs, &c);
183 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
185 asynchronous_cleanup();
189 static void action_wait(const char *const *action)
192 msg_task_t task = NULL;
194 double clock = MSG_get_clock();
195 process_globals_t globals =
196 (process_globals_t) MSG_process_get_data(MSG_process_self());
198 xbt_assert(xbt_dynar_length(globals->irecvs),
199 "action wait not preceded by any irecv: %s",
200 xbt_str_join_array(action, " "));
202 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
203 name = xbt_str_join_array(action, " ");
205 XBT_DEBUG("Entering %s", name);
206 comm = xbt_dynar_pop_as(globals->irecvs, msg_comm_t);
207 MSG_comm_wait(comm, -1);
208 task = xbt_dynar_pop_as(globals->tasks, msg_task_t);
209 MSG_comm_destroy(comm);
210 MSG_task_destroy(task);
212 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
216 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
217 static void action_barrier(const char *const *action)
220 static smx_mutex_t mutex = NULL;
221 static smx_cond_t cond = NULL;
222 static int processes_arrived_sofar = 0;
224 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
225 name = xbt_str_join_array(action, " ");
227 if (mutex == NULL) { // first arriving on the barrier
228 mutex = simcall_mutex_init();
229 cond = simcall_cond_init();
230 processes_arrived_sofar = 0;
232 XBT_DEBUG("Entering barrier: %s (%d already there)", name,
233 processes_arrived_sofar);
235 simcall_mutex_lock(mutex);
236 if (++processes_arrived_sofar == communicator_size) {
237 simcall_cond_broadcast(cond);
238 simcall_mutex_unlock(mutex);
240 simcall_cond_wait(cond, mutex);
241 simcall_mutex_unlock(mutex);
244 XBT_DEBUG("Exiting barrier: %s", name);
246 processes_arrived_sofar--;
247 if (!processes_arrived_sofar) {
248 simcall_cond_destroy(cond);
249 simcall_mutex_destroy(mutex);
257 static void action_reduce(const char *const *action)
260 char *reduce_identifier;
262 double comm_size = parse_double(action[2]);
263 double comp_size = parse_double(action[3]);
264 msg_task_t comp_task = NULL;
265 const char *process_name;
266 double clock = MSG_get_clock();
268 process_globals_t counters =
269 (process_globals_t) MSG_process_get_data(MSG_process_self());
271 xbt_assert(communicator_size, "Size of Communicator is not defined, "
272 "can't use collective operations");
274 process_name = MSG_process_get_name(MSG_process_self());
276 reduce_identifier = bprintf("reduce_%d", counters->reduce_counter++);
278 if (!strcmp(process_name, "p0")) {
279 XBT_DEBUG("%s: %s is the Root", reduce_identifier, process_name);
281 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
282 msg_task_t *tasks = xbt_new0(msg_task_t,communicator_size-1);
283 for (i = 1; i < communicator_size; i++) {
284 sprintf(mailbox, "%s_p%d_p0", reduce_identifier, i);
285 comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
287 MSG_comm_waitall(comms,communicator_size-1,-1);
288 for (i = 1; i < communicator_size; i++) {
289 MSG_comm_destroy(comms[i-1]);
290 MSG_task_destroy(tasks[i-1]);
294 comp_task = MSG_task_create("reduce_comp", comp_size, 0, NULL);
295 XBT_DEBUG("%s: computing 'reduce_comp'", reduce_identifier);
296 MSG_task_execute(comp_task);
297 MSG_task_destroy(comp_task);
298 XBT_DEBUG("%s: computed", reduce_identifier);
301 XBT_DEBUG("%s: %s sends", reduce_identifier, process_name);
302 sprintf(mailbox, "%s_%s_p0", reduce_identifier, process_name);
303 XBT_DEBUG("put on %s", mailbox);
304 MSG_task_send(MSG_task_create(reduce_identifier, 0, comm_size, NULL),
308 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
309 free(reduce_identifier);
312 static void action_bcast(const char *const *action)
315 char *bcast_identifier;
317 double comm_size = parse_double(action[2]);
318 msg_task_t task = NULL;
319 const char *process_name;
320 double clock = MSG_get_clock();
322 process_globals_t counters =
323 (process_globals_t) MSG_process_get_data(MSG_process_self());
325 xbt_assert(communicator_size, "Size of Communicator is not defined, "
326 "can't use collective operations");
328 process_name = MSG_process_get_name(MSG_process_self());
330 bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
332 if (!strcmp(process_name, "p0")) {
333 XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
335 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
337 for (i = 1; i < communicator_size; i++) {
338 sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
340 MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
343 MSG_comm_waitall(comms,communicator_size-1,-1);
344 for (i = 1; i < communicator_size; i++)
345 MSG_comm_destroy(comms[i-1]);
348 XBT_DEBUG("%s: all messages sent by %s have been received",
349 bcast_identifier, process_name);
352 sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
353 MSG_task_receive(&task, mailbox);
354 MSG_task_destroy(task);
355 XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
358 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
359 free(bcast_identifier);
363 static void action_sleep(const char *const *action)
366 const char *duration = action[2];
367 double clock = MSG_get_clock();
369 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
370 name = xbt_str_join_array(action, " ");
372 XBT_DEBUG("Entering %s", name);
373 MSG_process_sleep(parse_double(duration));
374 XBT_VERB("%s %f ", name, MSG_get_clock() - clock);
379 static void action_allReduce(const char *const *action)
382 char *allreduce_identifier;
384 double comm_size = parse_double(action[2]);
385 double comp_size = parse_double(action[3]);
386 msg_task_t task = NULL, comp_task = NULL;
387 const char *process_name;
388 double clock = MSG_get_clock();
390 process_globals_t counters =
391 (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 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
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 comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
409 MSG_comm_waitall(comms,communicator_size-1,-1);
410 for (i = 1; i < communicator_size; i++) {
411 MSG_comm_destroy(comms[i-1]);
412 MSG_task_destroy(tasks[i-1]);
416 comp_task = MSG_task_create("allReduce_comp", comp_size, 0, NULL);
417 XBT_DEBUG("%s: computing 'reduce_comp'", allreduce_identifier);
418 MSG_task_execute(comp_task);
419 MSG_task_destroy(comp_task);
420 XBT_DEBUG("%s: computed", allreduce_identifier);
422 for (i = 1; i < communicator_size; i++) {
423 sprintf(mailbox, "%s_p0_p%d", allreduce_identifier, i);
425 MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
428 MSG_comm_waitall(comms,communicator_size-1,-1);
429 for (i = 1; i < communicator_size; i++)
430 MSG_comm_destroy(comms[i-1]);
433 XBT_DEBUG("%s: all messages sent by %s have been received",
434 allreduce_identifier, process_name);
437 XBT_DEBUG("%s: %s sends", allreduce_identifier, process_name);
438 sprintf(mailbox, "%s_%s_p0", allreduce_identifier, process_name);
439 XBT_DEBUG("put on %s", mailbox);
440 MSG_task_send(MSG_task_create(allreduce_identifier, 0, comm_size, NULL),
443 sprintf(mailbox, "%s_p0_%s", allreduce_identifier, process_name);
444 MSG_task_receive(&task, mailbox);
445 MSG_task_destroy(task);
446 XBT_DEBUG("%s: %s has received", allreduce_identifier, process_name);
449 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) {
450 char *a = xbt_str_join_array(action, " ");
451 XBT_VERB("%s %f", a, MSG_get_clock() - clock);
454 free(allreduce_identifier);
457 static void action_comm_size(const char *const *action)
460 const char *size = action[2];
461 double clock = MSG_get_clock();
463 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
464 name = xbt_str_join_array(action, " ");
465 communicator_size = parse_double(size);
466 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
470 static void action_compute(const char *const *action)
473 const char *amout = action[2];
474 msg_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
475 double clock = MSG_get_clock();
477 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
478 name = xbt_str_join_array(action, " ");
479 XBT_DEBUG("Entering %s", name);
480 MSG_task_execute(task);
481 MSG_task_destroy(task);
482 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
486 static void action_init(const char *const *action)
488 XBT_DEBUG("Initialize the counters");
489 process_globals_t globals =
490 (process_globals_t) calloc(1, sizeof(s_process_globals_t));
491 globals->isends = xbt_dynar_new(sizeof(msg_comm_t), NULL);
492 globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t), NULL);
493 globals->tasks = xbt_dynar_new(sizeof(msg_task_t), NULL);
494 MSG_process_set_data(MSG_process_self(), globals);
498 static void action_finalize(const char *const *action)
500 process_globals_t globals =
501 (process_globals_t) MSG_process_get_data(MSG_process_self());
503 xbt_dynar_free_container(&(globals->isends));
504 xbt_dynar_free_container(&(globals->irecvs));
505 xbt_dynar_free_container(&(globals->tasks));
511 int main(int argc, char *argv[])
513 msg_error_t res = MSG_OK;
515 /* Check the given arguments */
516 MSG_init(&argc, argv);
518 printf("Usage: %s platform_file deployment_file [action_files]\n", argv[0]);
520 ("example: %s msg_platform.xml msg_deployment.xml actions # if all actions are in the same file\n",
523 ("example: %s msg_platform.xml msg_deployment.xml # if actions are in separate files, specified in deployment\n",
528 /* Simulation setting */
529 MSG_create_environment(argv[1]);
531 /* No need to register functions as in classical MSG programs: the actions get started anyway */
532 MSG_launch_application(argv[2]);
534 /* Action registration */
535 xbt_replay_action_register("init", action_init);
536 xbt_replay_action_register("finalize", action_finalize);
537 xbt_replay_action_register("comm_size", action_comm_size);
538 xbt_replay_action_register("send", action_send);
539 xbt_replay_action_register("Isend", action_Isend);
540 xbt_replay_action_register("recv", action_recv);
541 xbt_replay_action_register("Irecv", action_Irecv);
542 xbt_replay_action_register("wait", action_wait);
543 xbt_replay_action_register("barrier", action_barrier);
544 xbt_replay_action_register("bcast", action_bcast);
545 xbt_replay_action_register("reduce", action_reduce);
546 xbt_replay_action_register("allReduce", action_allReduce);
547 xbt_replay_action_register("sleep", action_sleep);
548 xbt_replay_action_register("compute", action_compute);
551 /* Actually do the simulation using MSG_action_trace_run */
552 res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
554 XBT_INFO("Simulation time %g", MSG_get_clock());