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. */
7 #include "simgrid/msg.h"
8 #include "simgrid/simix.h" /* semaphores for the barrier */
9 #include <xbt/replay.h>
11 /** @addtogroup MSG_examples
13 * @section MSG_ex_actions Trace driven simulations
15 * The <b>actions/actions.c</b> example demonstrates how to run trace-driven simulations. It is very handy when you
16 * want to test an algorithm or protocol that does nothing unless it receives some events from outside. For example,
17 * a P2P protocol reacts to requests from the user, but does nothing if there is no such event.
19 * In such situations, SimGrid allows to write your protocol in your C file, and the events to react to in a separate
20 * text file. Declare a function handling each of the events that you want to accept in your trace files, register
21 * them using \ref xbt_replay_action_register in your main, and then use \ref MSG_action_trace_run to launch the
22 * simulation. You can either have one trace file containing all your events, or a file per simulated process. Check
23 * the tesh files in the example directory for details on how to do it.
25 * This example uses this approach to replay MPI-like traces. It comes with a set of event handlers reproducing MPI
26 * events. This is somehow similar to SMPI, yet differently implemented. This code should probably be changed to use
27 * SMPI internals instead, but wasn't, so far.
30 XBT_LOG_NEW_DEFAULT_CATEGORY(actions, "Messages specific for this msg example");
31 int communicator_size = 0;
33 static void action_Isend(const char *const *action);
36 int last_Irecv_sender_id;
39 int allReduce_counter;
40 xbt_dynar_t isends; /* of msg_comm_t */
41 /* Used to implement irecv+wait */
42 xbt_dynar_t irecvs; /* of msg_comm_t */
43 xbt_dynar_t tasks; /* of msg_task_t */
44 } s_process_globals_t, *process_globals_t;
47 static double parse_double(const char *string)
52 value = strtod(string, &endptr);
54 THROWF(unknown_error, 0, "%s is not a double", string);
58 #define ACT_DEBUG(...) \
59 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) { \
60 char *NAME = xbt_str_join_array(action, " "); \
61 XBT_DEBUG(__VA_ARGS__); \
65 static void log_action(const char *const *action, double date)
67 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) {
68 char *name = xbt_str_join_array(action, " ");
69 XBT_VERB("%s %f", name, date);
74 static void asynchronous_cleanup(void)
76 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
78 /* Destroy any isend which correspond to completed communications */
81 while ((found = MSG_comm_testany(globals->isends)) != -1) {
82 xbt_dynar_remove_at(globals->isends, found, &comm);
83 MSG_comm_destroy(comm);
88 static void action_send(const char *const *action)
91 const char *size_str = action[3];
92 double size = parse_double(size_str);
93 double clock = MSG_get_clock();
95 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
97 ACT_DEBUG("Entering Send: %s (size: %g)", NAME, size);
101 MSG_task_send(MSG_task_create(to, 0, size, NULL), to);
104 log_action(action, MSG_get_clock() - clock);
105 asynchronous_cleanup();
108 static void action_Isend(const char *const *action)
111 const char *size = action[3];
112 double clock = MSG_get_clock();
113 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
115 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
116 msg_comm_t comm = MSG_task_isend(MSG_task_create(to, 0, parse_double(size), NULL), to);
117 xbt_dynar_push(globals->isends, &comm);
119 XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
120 log_action(action, MSG_get_clock() - clock);
121 asynchronous_cleanup();
124 static void action_recv(const char *const *action)
126 char mailbox_name[250];
127 msg_task_t task = NULL;
128 double clock = MSG_get_clock();
130 sprintf(mailbox_name, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
132 ACT_DEBUG("Receiving: %s", NAME);
133 msg_error_t res = MSG_task_receive(&task, mailbox_name);
134 log_action(action, MSG_get_clock() - clock);
137 MSG_task_destroy(task);
139 asynchronous_cleanup();
142 static void action_Irecv(const char *const *action)
145 double clock = MSG_get_clock();
146 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
148 XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
150 sprintf(mailbox, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
152 xbt_dynar_push(globals->tasks, &t);
153 msg_comm_t c = MSG_task_irecv(xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks) - 1), mailbox);
154 xbt_dynar_push(globals->irecvs, &c);
156 log_action(action, MSG_get_clock() - clock);
157 asynchronous_cleanup();
160 static void action_wait(const char *const *action)
162 msg_task_t task = NULL;
164 double clock = MSG_get_clock();
165 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
167 xbt_assert(xbt_dynar_length(globals->irecvs), "action wait not preceded by any irecv: %s",
168 xbt_str_join_array(action, " "));
170 ACT_DEBUG("Entering %s", NAME);
171 comm = xbt_dynar_pop_as(globals->irecvs, msg_comm_t);
172 MSG_comm_wait(comm, -1);
173 task = xbt_dynar_pop_as(globals->tasks, msg_task_t);
174 MSG_comm_destroy(comm);
175 MSG_task_destroy(task);
177 log_action(action, MSG_get_clock() - clock);
180 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
181 static void action_barrier(const char *const *action)
183 static smx_mutex_t mutex = NULL;
184 static smx_cond_t cond = NULL;
185 static int processes_arrived_sofar = 0;
187 if (mutex == NULL) { // first arriving on the barrier
188 mutex = simcall_mutex_init();
189 cond = simcall_cond_init();
190 processes_arrived_sofar = 0;
192 ACT_DEBUG("Entering barrier: %s (%d already there)", NAME, processes_arrived_sofar);
194 simcall_mutex_lock(mutex);
195 if (++processes_arrived_sofar == communicator_size) {
196 simcall_cond_broadcast(cond);
197 simcall_mutex_unlock(mutex);
199 simcall_cond_wait(cond, mutex);
200 simcall_mutex_unlock(mutex);
203 ACT_DEBUG("Exiting barrier: %s", NAME);
205 processes_arrived_sofar--;
206 if (!processes_arrived_sofar) {
207 SIMIX_cond_destroy(cond);
208 SIMIX_mutex_destroy(mutex);
213 static void action_reduce(const char *const *action)
216 char *reduce_identifier;
218 double comm_size = parse_double(action[2]);
219 double comp_size = parse_double(action[3]);
220 msg_task_t comp_task = NULL;
221 const char *process_name;
222 double clock = MSG_get_clock();
224 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
226 xbt_assert(communicator_size, "Size of Communicator is not defined can't use collective operations");
228 process_name = MSG_process_get_name(MSG_process_self());
230 reduce_identifier = bprintf("reduce_%d", counters->reduce_counter++);
232 if (!strcmp(process_name, "p0")) {
233 XBT_DEBUG("%s: %s is the Root", reduce_identifier, process_name);
235 msg_comm_t *comms = xbt_new0(msg_comm_t, communicator_size - 1);
236 msg_task_t *tasks = xbt_new0(msg_task_t, communicator_size - 1);
237 for (i = 1; i < communicator_size; i++) {
238 sprintf(mailbox, "%s_p%d_p0", reduce_identifier, i);
239 comms[i - 1] = MSG_task_irecv(&(tasks[i - 1]), mailbox);
241 MSG_comm_waitall(comms, communicator_size - 1, -1);
242 for (i = 1; i < communicator_size; i++) {
243 MSG_comm_destroy(comms[i - 1]);
244 MSG_task_destroy(tasks[i - 1]);
249 comp_task = MSG_task_create("reduce_comp", comp_size, 0, NULL);
250 XBT_DEBUG("%s: computing 'reduce_comp'", reduce_identifier);
251 MSG_task_execute(comp_task);
252 MSG_task_destroy(comp_task);
253 XBT_DEBUG("%s: computed", reduce_identifier);
255 XBT_DEBUG("%s: %s sends", reduce_identifier, process_name);
256 sprintf(mailbox, "%s_%s_p0", reduce_identifier, process_name);
257 XBT_DEBUG("put on %s", mailbox);
258 MSG_task_send(MSG_task_create(reduce_identifier, 0, comm_size, NULL), mailbox);
261 log_action(action, MSG_get_clock() - clock);
262 xbt_free(reduce_identifier);
265 static void action_bcast(const char *const *action)
268 char *bcast_identifier;
270 double comm_size = parse_double(action[2]);
271 msg_task_t task = NULL;
272 const char *process_name;
273 double clock = MSG_get_clock();
275 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
277 xbt_assert(communicator_size, "Size of Communicator is not defined, can't use collective operations");
279 process_name = MSG_process_get_name(MSG_process_self());
281 bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
283 if (!strcmp(process_name, "p0")) {
284 XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
286 msg_comm_t *comms = xbt_new0(msg_comm_t, communicator_size - 1);
288 for (i = 1; i < communicator_size; i++) {
289 sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
290 comms[i - 1] = MSG_task_isend(MSG_task_create(mailbox, 0, comm_size, NULL), mailbox);
292 MSG_comm_waitall(comms, communicator_size - 1, -1);
293 for (i = 1; i < communicator_size; i++)
294 MSG_comm_destroy(comms[i - 1]);
297 XBT_DEBUG("%s: all messages sent by %s have been received", bcast_identifier, process_name);
299 sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
300 MSG_task_receive(&task, mailbox);
301 MSG_task_destroy(task);
302 XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
305 log_action(action, MSG_get_clock() - clock);
306 xbt_free(bcast_identifier);
309 static void action_sleep(const char *const *action)
311 const char *duration = action[2];
312 double clock = MSG_get_clock();
314 ACT_DEBUG("Entering %s", NAME);
315 MSG_process_sleep(parse_double(duration));
316 log_action(action, MSG_get_clock() - clock);
319 static void action_allReduce(const char *const *action)
322 char *allreduce_identifier;
324 double comm_size = parse_double(action[2]);
325 double comp_size = parse_double(action[3]);
326 msg_task_t task = NULL, comp_task = NULL;
327 const char *process_name;
328 double clock = MSG_get_clock();
330 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
332 xbt_assert(communicator_size, "Size of Communicator is not defined, can't use collective operations");
334 process_name = MSG_process_get_name(MSG_process_self());
336 allreduce_identifier = bprintf("allReduce_%d", counters->allReduce_counter++);
338 if (!strcmp(process_name, "p0")) {
339 XBT_DEBUG("%s: %s is the Root", allreduce_identifier, process_name);
341 msg_comm_t *comms = xbt_new0(msg_comm_t, communicator_size - 1);
342 msg_task_t *tasks = xbt_new0(msg_task_t, communicator_size - 1);
343 for (i = 1; i < communicator_size; i++) {
344 sprintf(mailbox, "%s_p%d_p0", allreduce_identifier, i);
345 comms[i - 1] = MSG_task_irecv(&(tasks[i - 1]), mailbox);
347 MSG_comm_waitall(comms, communicator_size - 1, -1);
348 for (i = 1; i < communicator_size; i++) {
349 MSG_comm_destroy(comms[i - 1]);
350 MSG_task_destroy(tasks[i - 1]);
354 comp_task = MSG_task_create("allReduce_comp", comp_size, 0, NULL);
355 XBT_DEBUG("%s: computing 'reduce_comp'", allreduce_identifier);
356 MSG_task_execute(comp_task);
357 MSG_task_destroy(comp_task);
358 XBT_DEBUG("%s: computed", allreduce_identifier);
360 for (i = 1; i < communicator_size; i++) {
361 sprintf(mailbox, "%s_p0_p%d", allreduce_identifier, i);
362 comms[i - 1] = MSG_task_isend(MSG_task_create(mailbox, 0, comm_size, NULL), mailbox);
364 MSG_comm_waitall(comms, communicator_size - 1, -1);
365 for (i = 1; i < communicator_size; i++)
366 MSG_comm_destroy(comms[i - 1]);
369 XBT_DEBUG("%s: all messages sent by %s have been received", allreduce_identifier, process_name);
371 XBT_DEBUG("%s: %s sends", allreduce_identifier, process_name);
372 sprintf(mailbox, "%s_%s_p0", allreduce_identifier, process_name);
373 XBT_DEBUG("put on %s", mailbox);
374 MSG_task_send(MSG_task_create(allreduce_identifier, 0, comm_size, NULL), mailbox);
376 sprintf(mailbox, "%s_p0_%s", allreduce_identifier, process_name);
377 MSG_task_receive(&task, mailbox);
378 MSG_task_destroy(task);
379 XBT_DEBUG("%s: %s has received", allreduce_identifier, process_name);
382 log_action(action, MSG_get_clock() - clock);
383 xbt_free(allreduce_identifier);
386 static void action_comm_size(const char *const *action)
388 const char *size = action[2];
389 double clock = MSG_get_clock();
391 communicator_size = parse_double(size);
392 log_action(action, MSG_get_clock() - clock);
395 static void action_compute(const char *const *action)
397 const char *amount = action[2];
398 msg_task_t task = MSG_task_create("task", parse_double(amount), 0, NULL);
399 double clock = MSG_get_clock();
401 ACT_DEBUG("Entering %s", NAME);
402 MSG_task_execute(task);
403 MSG_task_destroy(task);
404 log_action(action, MSG_get_clock() - clock);
407 static void action_init(const char *const *action)
409 XBT_DEBUG("Initialize the counters");
410 process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
411 globals->isends = xbt_dynar_new(sizeof(msg_comm_t), NULL);
412 globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t), NULL);
413 globals->tasks = xbt_dynar_new(sizeof(msg_task_t), NULL);
414 MSG_process_set_data(MSG_process_self(), globals);
417 static void action_finalize(const char *const *action)
419 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
421 asynchronous_cleanup();
422 xbt_dynar_free_container(&(globals->isends));
423 xbt_dynar_free_container(&(globals->irecvs));
424 xbt_dynar_free_container(&(globals->tasks));
430 int main(int argc, char *argv[])
432 msg_error_t res = MSG_OK;
434 /* Check the given arguments */
435 MSG_init(&argc, argv);
436 /* Explicit initialization of the action module is required now*/
440 "Usage: %s platform_file deployment_file [action_files]\n"
441 "\t# if all actions are in the same file\n"
442 "\tExample: %s msg_platform.xml msg_deployment.xml actions\n"
443 "\t# if actions are in separate files, specified in deployment\n"
444 "\tExample: %s msg_platform.xml msg_deployment.xml ",
445 argv[0],argv[0],argv[0]);
447 printf("WARNING: THIS BINARY IS KINDA DEPRECATED\n"
448 "This example is still relevant if you want to learn about MSG-based trace replay, but if you want to simulate "
449 "MPI-like traces, you should use the newer version that is in the examples/smpi/replay directory instead.\n");
451 MSG_create_environment(argv[1]);
452 MSG_launch_application(argv[2]);
454 /* Action registration */
455 xbt_replay_action_register("init", action_init);
456 xbt_replay_action_register("finalize", action_finalize);
457 xbt_replay_action_register("comm_size", action_comm_size);
458 xbt_replay_action_register("send", action_send);
459 xbt_replay_action_register("Isend", action_Isend);
460 xbt_replay_action_register("recv", action_recv);
461 xbt_replay_action_register("Irecv", action_Irecv);
462 xbt_replay_action_register("wait", action_wait);
463 xbt_replay_action_register("barrier", action_barrier);
464 xbt_replay_action_register("bcast", action_bcast);
465 xbt_replay_action_register("reduce", action_reduce);
466 xbt_replay_action_register("allReduce", action_allReduce);
467 xbt_replay_action_register("sleep", action_sleep);
468 xbt_replay_action_register("compute", action_compute);
470 /* Actually do the simulation using MSG_action_trace_run */
471 res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
473 XBT_INFO("Simulation time %g", MSG_get_clock());
475 /* Explicit finalization of the action module is required now*/
478 return res != MSG_OK;