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 "simgrid_config.h" /* getline */
13 #include "instr/instr_private.h"
14 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory);
17 XBT_LOG_NEW_DEFAULT_CATEGORY(actions,
18 "Messages specific for this msg example");
19 int communicator_size = 0;
21 static void action_Isend(xbt_dynar_t action);
24 int last_Irecv_sender_id;
27 int allReduce_counter;
28 xbt_dynar_t isends; /* of msg_comm_t */
29 /* Used to implement irecv+wait */
30 xbt_dynar_t irecvs; /* of msg_comm_t */
31 xbt_dynar_t tasks; /* of m_task_t */
32 } s_process_globals_t, *process_globals_t;
35 static double parse_double(const char *string)
40 value = strtod(string, &endptr);
42 THROW1(unknown_error, 0, "%s is not a double", string);
46 static int get_rank (const char *process_name)
48 return atoi(&(process_name[1]));
51 static void asynchronous_cleanup(void) {
52 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
54 /* Destroy any isend which correspond to completed communications */
57 while ((found = MSG_comm_testany(globals->isends)) != -1) {
58 xbt_dynar_remove_at(globals->isends,found,&comm);
59 MSG_comm_destroy(comm);
64 static void action_send(xbt_dynar_t action)
68 char *size_str = xbt_dynar_get_as(action, 3, char *);
69 double size=parse_double(size_str);
70 double clock = MSG_get_clock(); /* this "call" is free thanks to inlining */
72 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),
73 xbt_dynar_get_as(action, 2, char *));
74 // char *to = xbt_dynar_get_as(action, 2, char *);
76 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
77 name = xbt_str_join(action, " ");
80 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
81 int dst_traced = get_rank(xbt_dynar_get_as(action, 2, char *));
82 TRACE_smpi_ptp_in(rank, rank, dst_traced, "send");
83 TRACE_smpi_send(rank, rank, dst_traced);
86 DEBUG2("Entering Send: %s (size: %lg)", name, size);
90 MSG_task_send(MSG_task_create(name, 0, size, NULL), to);
93 VERB2("%s %f", name, MSG_get_clock() - clock);
95 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
99 TRACE_smpi_ptp_out(rank, rank, dst_traced, "send");
102 asynchronous_cleanup();
105 static void action_Isend(xbt_dynar_t action)
108 // char *to = xbt_dynar_get_as(action, 2, char *);
109 char *size = xbt_dynar_get_as(action, 3, char *);
110 double clock = MSG_get_clock();
111 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
114 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),
115 xbt_dynar_get_as(action, 2, char *));
118 MSG_task_isend( MSG_task_create(to,0,parse_double(size),NULL), to);
119 xbt_dynar_push(globals->isends,&comm);
121 DEBUG1("Isend on %s", MSG_process_get_name(MSG_process_self()));
122 VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
124 asynchronous_cleanup();
128 static void action_recv(xbt_dynar_t action)
131 char mailbox_name[250];
132 m_task_t task = NULL;
133 double clock = MSG_get_clock();
135 sprintf(mailbox_name, "%s_%s", xbt_dynar_get_as(action, 2, char *),
136 MSG_process_get_name(MSG_process_self()));
138 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
139 name = xbt_str_join(action, " ");
142 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
143 int src_traced = get_rank(xbt_dynar_get_as(action, 2, char *));
144 TRACE_smpi_ptp_in(rank, src_traced, rank, "recv");
147 DEBUG1("Receiving: %s", name);
148 MSG_task_receive(&task, mailbox_name);
149 // MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
150 VERB2("%s %f", name, MSG_get_clock() - clock);
151 MSG_task_destroy(task);
153 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
156 TRACE_smpi_ptp_out(rank, src_traced, rank, "recv");
157 TRACE_smpi_recv(rank, src_traced, rank);
160 asynchronous_cleanup();
163 static void action_Irecv(xbt_dynar_t action)
166 double clock = MSG_get_clock();
167 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
169 DEBUG1("Irecv on %s", MSG_process_get_name(MSG_process_self()));
171 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
172 int src_traced = get_rank(xbt_dynar_get_as(action, 2, char *));
173 globals->last_Irecv_sender_id = src_traced;
174 MSG_process_set_data(MSG_process_self(), (void *) globals);
176 TRACE_smpi_ptp_in(rank, src_traced, rank, "Irecv");
179 sprintf(mailbox, "%s_%s", xbt_dynar_get_as(action, 2, char *),
180 MSG_process_get_name(MSG_process_self()));
182 xbt_dynar_push(globals->tasks,&t);
185 xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks)-1),
187 xbt_dynar_push(globals->irecvs,&c);
189 VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
192 TRACE_smpi_ptp_out(rank, src_traced, rank, "Irecv");
195 asynchronous_cleanup();
199 static void action_wait(xbt_dynar_t action)
202 m_task_t task = NULL;
204 double clock = MSG_get_clock();
205 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
207 xbt_assert1(xbt_dynar_length(globals->irecvs),
208 "action wait not preceeded by any irecv: %s", xbt_str_join(action," "));
210 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
211 name = xbt_str_join(action, " ");
213 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
214 int src_traced = counters->last_Irecv_sender_id;
215 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
216 TRACE_smpi_ptp_in(rank, src_traced, rank, "wait");
219 DEBUG1("Entering %s", name);
220 comm = xbt_dynar_pop_as(globals->irecvs,msg_comm_t);
221 MSG_comm_wait(comm,-1);
222 task = xbt_dynar_pop_as(globals->tasks,m_task_t);
223 MSG_comm_destroy(comm);
224 MSG_task_destroy(task);
226 VERB2("%s %f", name, MSG_get_clock() - clock);
227 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
230 TRACE_smpi_ptp_out(rank, src_traced, rank, "wait");
231 TRACE_smpi_recv(rank, src_traced, rank);
236 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
237 static void action_barrier(xbt_dynar_t action)
240 static smx_mutex_t mutex = NULL;
241 static smx_cond_t cond = NULL;
242 static int processes_arrived_sofar=0;
244 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
245 name = xbt_str_join(action, " ");
247 if (mutex == NULL) { // first arriving on the barrier
248 mutex = SIMIX_req_mutex_init();
249 cond = SIMIX_req_cond_init();
250 processes_arrived_sofar=0;
252 DEBUG2("Entering barrier: %s (%d already there)", name,processes_arrived_sofar);
254 SIMIX_req_mutex_lock(mutex);
255 if (++processes_arrived_sofar == communicator_size) {
256 SIMIX_req_cond_broadcast(cond);
257 SIMIX_req_mutex_unlock(mutex);
259 SIMIX_req_cond_wait(cond,mutex);
260 SIMIX_req_mutex_unlock(mutex);
263 DEBUG1("Exiting barrier: %s", name);
265 processes_arrived_sofar--;
266 if (!processes_arrived_sofar) {
267 SIMIX_req_cond_destroy(cond);
268 SIMIX_req_mutex_destroy(mutex);
272 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
277 static void action_reduce(xbt_dynar_t action)
280 char *reduce_identifier;
282 double comm_size = parse_double(xbt_dynar_get_as(action, 2, char *));
283 double comp_size = parse_double(xbt_dynar_get_as(action, 3, char *));
284 m_task_t comp_task = NULL;
285 const char *process_name;
286 double clock = MSG_get_clock();
288 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
290 xbt_assert0(communicator_size, "Size of Communicator is not defined, "
291 "can't use collective operations");
293 process_name = MSG_process_get_name(MSG_process_self());
295 reduce_identifier = bprintf("reduce_%d", counters->reduce_counter++);
297 if (!strcmp(process_name, "p0")) {
298 DEBUG2("%s: %s is the Root", reduce_identifier, process_name);
300 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
301 m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
302 for (i = 1; i < communicator_size; i++) {
303 sprintf(mailbox, "%s_p%d_p0", reduce_identifier, i);
304 comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
306 MSG_comm_waitall(comms,communicator_size-1,-1);
307 for (i = 1; i < communicator_size; i++) {
308 MSG_comm_destroy(comms[i-1]);
309 MSG_task_destroy(tasks[i-1]);
313 comp_task = MSG_task_create("reduce_comp", comp_size, 0, NULL);
314 DEBUG1("%s: computing 'reduce_comp'", reduce_identifier);
315 MSG_task_execute(comp_task);
316 MSG_task_destroy(comp_task);
317 DEBUG1("%s: computed", reduce_identifier);
320 DEBUG2("%s: %s sends", reduce_identifier, process_name);
321 sprintf(mailbox, "%s_%s_p0", reduce_identifier, process_name);
322 DEBUG1("put on %s", mailbox);
323 MSG_task_send(MSG_task_create(reduce_identifier, 0, comm_size, NULL),
327 VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
328 free(reduce_identifier);
331 static void action_bcast(xbt_dynar_t action)
334 char *bcast_identifier;
336 double comm_size = parse_double(xbt_dynar_get_as(action, 2, char *));
337 m_task_t task = NULL;
338 const char *process_name;
339 double clock = MSG_get_clock();
341 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
343 xbt_assert0(communicator_size, "Size of Communicator is not defined, "
344 "can't use collective operations");
346 process_name = MSG_process_get_name(MSG_process_self());
348 bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
350 if (!strcmp(process_name, "p0")) {
351 DEBUG2("%s: %s is the Root", bcast_identifier, process_name);
353 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
355 for (i = 1; i < communicator_size; i++) {
356 sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
358 MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
361 MSG_comm_waitall(comms,communicator_size-1,-1);
362 for (i = 1; i < communicator_size; i++)
363 MSG_comm_destroy(comms[i-1]);
366 DEBUG2("%s: all messages sent by %s have been received",
367 bcast_identifier, process_name);
370 sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
371 MSG_task_receive(&task, mailbox);
372 MSG_task_destroy(task);
373 DEBUG2("%s: %s has received", bcast_identifier, process_name);
376 VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
377 free(bcast_identifier);
381 static void action_sleep(xbt_dynar_t action)
384 char *duration = xbt_dynar_get_as(action, 2, char *);
385 double clock = MSG_get_clock();
387 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
388 name = xbt_str_join(action, " ");
390 DEBUG1("Entering %s", name);
391 MSG_process_sleep(parse_double(duration));
392 VERB2("%s %f ", name, MSG_get_clock() - clock);
394 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
398 static void action_allReduce(xbt_dynar_t action) {
400 char *allreduce_identifier;
402 double comm_size = parse_double(xbt_dynar_get_as(action, 2, char *));
403 double comp_size = parse_double(xbt_dynar_get_as(action, 3, char *));
404 m_task_t task = NULL, comp_task = NULL;
405 const char *process_name;
406 double clock = MSG_get_clock();
408 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
410 xbt_assert0(communicator_size, "Size of Communicator is not defined, "
411 "can't use collective operations");
413 process_name = MSG_process_get_name(MSG_process_self());
415 allreduce_identifier = bprintf("allReduce_%d", counters->allReduce_counter++);
417 if (!strcmp(process_name, "p0")) {
418 DEBUG2("%s: %s is the Root", allreduce_identifier, process_name);
420 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
421 m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
422 for (i = 1; i < communicator_size; i++) {
423 sprintf(mailbox, "%s_p%d_p0", allreduce_identifier, i);
424 comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
426 MSG_comm_waitall(comms,communicator_size-1,-1);
427 for (i = 1; i < communicator_size; i++) {
428 MSG_comm_destroy(comms[i-1]);
429 MSG_task_destroy(tasks[i-1]);
433 comp_task = MSG_task_create("allReduce_comp", comp_size, 0, NULL);
434 DEBUG1("%s: computing 'reduce_comp'", allreduce_identifier);
435 MSG_task_execute(comp_task);
436 MSG_task_destroy(comp_task);
437 DEBUG1("%s: computed", allreduce_identifier);
439 for (i = 1; i < communicator_size; i++) {
440 sprintf(mailbox, "%s_p0_p%d", allreduce_identifier, i);
442 MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
445 MSG_comm_waitall(comms,communicator_size-1,-1);
446 for (i = 1; i < communicator_size; i++)
447 MSG_comm_destroy(comms[i-1]);
450 DEBUG2("%s: all messages sent by %s have been received",
451 allreduce_identifier, process_name);
454 DEBUG2("%s: %s sends", allreduce_identifier, process_name);
455 sprintf(mailbox, "%s_%s_p0", allreduce_identifier, process_name);
456 DEBUG1("put on %s", mailbox);
457 MSG_task_send(MSG_task_create(allreduce_identifier, 0, comm_size, NULL),
460 sprintf(mailbox, "%s_p0_%s", allreduce_identifier, process_name);
461 MSG_task_receive(&task, mailbox);
462 MSG_task_destroy(task);
463 DEBUG2("%s: %s has received", allreduce_identifier, process_name);
466 VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
467 free(allreduce_identifier);
470 static void action_comm_size(xbt_dynar_t action)
473 char *size = xbt_dynar_get_as(action, 2, char *);
474 double clock = MSG_get_clock();
476 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
477 name = xbt_str_join(action, " ");
478 communicator_size = parse_double(size);
479 VERB2("%s %f", name, MSG_get_clock() - clock);
480 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
484 static void action_compute(xbt_dynar_t action)
487 char *amout = xbt_dynar_get_as(action, 2, char *);
488 m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
489 double clock = MSG_get_clock();
491 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
492 name = xbt_str_join(action, " ");
493 DEBUG1("Entering %s", name);
494 MSG_task_execute(task);
495 MSG_task_destroy(task);
496 VERB2("%s %f", name, MSG_get_clock() - clock);
497 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
501 static void action_init(xbt_dynar_t action)
504 TRACE_smpi_init(get_rank(MSG_process_get_name(MSG_process_self())));
506 DEBUG0("Initialize the counters");
507 process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
508 globals->isends = xbt_dynar_new(sizeof(msg_comm_t),NULL);
509 globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t),NULL);
510 globals->tasks = xbt_dynar_new(sizeof(m_task_t),NULL);
511 MSG_process_set_data(MSG_process_self(),globals);
515 static void action_finalize(xbt_dynar_t action)
518 TRACE_smpi_finalize(get_rank(MSG_process_get_name(MSG_process_self())));
520 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
522 xbt_dynar_free_container(&(globals->isends));
523 xbt_dynar_free_container(&(globals->irecvs));
524 xbt_dynar_free_container(&(globals->tasks));
530 int main(int argc, char *argv[])
532 MSG_error_t res = MSG_OK;
534 factory_initializer_to_use = SIMIX_ctx_raw_factory_init;
535 /* Check the given arguments */
536 MSG_global_init(&argc, argv);
538 printf("Usage: %s platform_file deployment_file [action_files]\n",
541 ("example: %s msg_platform.xml msg_deployment.xml actions # if all actions are in the same file\n",
544 ("example: %s msg_platform.xml msg_deployment.xml # if actions are in separate files, specified in deployment\n",
549 /* Simulation setting */
550 MSG_create_environment(argv[1]);
552 /* No need to register functions as in classical MSG programs: the actions get started anyway */
553 MSG_launch_application(argv[2]);
555 /* Action registration */
556 MSG_action_register("init", action_init);
557 MSG_action_register("finalize", action_finalize);
558 MSG_action_register("comm_size",action_comm_size);
559 MSG_action_register("send", action_send);
560 MSG_action_register("Isend", action_Isend);
561 MSG_action_register("recv", action_recv);
562 MSG_action_register("Irecv", action_Irecv);
563 MSG_action_register("wait", action_wait);
564 MSG_action_register("barrier", action_barrier);
565 MSG_action_register("bcast", action_bcast);
566 MSG_action_register("reduce", action_reduce);
567 MSG_action_register("allReduce",action_allReduce);
568 MSG_action_register("sleep", action_sleep);
569 MSG_action_register("compute", action_compute);
572 /* Actually do the simulation using MSG_action_trace_run */
573 res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
575 INFO1("Simulation time %g", MSG_get_clock());