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 "msg/mailbox.h" /* we play funny tricks with mailboxes and rdv points */
11 #include "simix/simix.h" /* semaphores for the barrier */
12 #include "xbt.h" /* calloc, printf */
13 #include "instr/instr_private.h"
15 XBT_LOG_NEW_DEFAULT_CATEGORY(actions,
16 "Messages specific for this msg example");
17 int communicator_size = 0;
19 static void action_Isend(const char *const *action);
22 int last_Irecv_sender_id;
25 int allReduce_counter;
26 xbt_dynar_t isends; /* of msg_comm_t */
27 /* Used to implement irecv+wait */
28 xbt_dynar_t irecvs; /* of msg_comm_t */
29 xbt_dynar_t tasks; /* of m_task_t */
30 } s_process_globals_t, *process_globals_t;
33 static double parse_double(const char *string)
38 value = strtod(string, &endptr);
40 THROWF(unknown_error, 0, "%s is not a double", string);
44 static int get_rank (const char *process_name)
46 return atoi(&(process_name[1]));
49 static void asynchronous_cleanup(void) {
50 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
52 /* Destroy any isend which correspond to completed communications */
55 while ((found = MSG_comm_testany(globals->isends)) != -1) {
56 xbt_dynar_remove_at(globals->isends,found,&comm);
57 MSG_comm_destroy(comm);
62 static void action_send(const char *const *action)
66 const char *size_str = action[3];
67 double size=parse_double(size_str);
68 double clock = MSG_get_clock(); /* this "call" is free thanks to inlining */
70 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),action[2]);
72 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
73 name = xbt_str_join_array(action, " ");
76 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
77 int dst_traced = get_rank(action[2]);
78 TRACE_smpi_ptp_in(rank, rank, dst_traced, "send");
79 TRACE_smpi_send(rank, rank, dst_traced);
82 XBT_DEBUG("Entering Send: %s (size: %lg)", name, size);
86 MSG_task_send(MSG_task_create(name, 0, size, NULL), to);
89 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
91 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
95 TRACE_smpi_ptp_out(rank, rank, dst_traced, "send");
98 asynchronous_cleanup();
101 static void action_Isend(const char *const *action)
104 const char *size = action[3];
105 double clock = MSG_get_clock();
106 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
109 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),action[2]);
110 m_task_t task = MSG_task_create(to,0,parse_double(size),NULL);
111 msg_comm_t comm = MSG_task_isend_with_matching(task, to, /*matching madness*/NULL,task);
112 xbt_dynar_push(globals->isends,&comm);
114 XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
115 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
117 asynchronous_cleanup();
120 static int task_matching(void*ignored,void*sent_task) {
121 m_task_t t = (m_task_t)sent_task;
122 if (t!=NULL && MSG_task_get_data_size(t)<65536)
123 return 1; /* that's supposed to be already arrived */
124 return 0; /* rendez-vous mode: it's not there yet */
127 static void action_recv(const char *const *action)
130 char mailbox_name[250];
131 m_task_t task = NULL;
132 double clock = MSG_get_clock();
134 sprintf(mailbox_name, "%s_%s", action[2],
135 MSG_process_get_name(MSG_process_self()));
137 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
138 name = xbt_str_join_array(action, " ");
140 /* The next chunk is to deal with the fact that for short messages,
141 * if the send occurs before the receive, the message is already sent and
142 * buffered on receiver side when the recv() occurs.
144 * So the next chunk detects this fact and cancel the simix communication instead.
147 /* make sure the rdv is created on need by asking to MSG instead of simix directly */
148 smx_rdv_t rdv = MSG_mailbox_get_by_alias(mailbox_name);
149 smx_action_t act = SIMIX_comm_get_send_match(rdv,task_matching,NULL);
151 /* FIXME account for the memcopy time if needed */
152 SIMIX_comm_finish(act);
157 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
158 int src_traced = get_rank(action[2]);
159 TRACE_smpi_ptp_in(rank, src_traced, rank, "recv");
162 XBT_DEBUG("Receiving: %s", name);
163 MSG_task_receive(&task, mailbox_name);
164 // MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
165 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
166 MSG_task_destroy(task);
168 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
171 TRACE_smpi_ptp_out(rank, src_traced, rank, "recv");
172 TRACE_smpi_recv(rank, src_traced, rank);
175 asynchronous_cleanup();
178 static void action_Irecv(const char *const *action)
181 double clock = MSG_get_clock();
182 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
184 XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
186 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
187 int src_traced = get_rank(action[2]);
188 globals->last_Irecv_sender_id = src_traced;
189 MSG_process_set_data(MSG_process_self(), (void *) globals);
191 TRACE_smpi_ptp_in(rank, src_traced, rank, "Irecv");
194 sprintf(mailbox, "%s_%s", action[2],
195 MSG_process_get_name(MSG_process_self()));
197 xbt_dynar_push(globals->tasks,&t);
200 xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks)-1),
202 xbt_dynar_push(globals->irecvs,&c);
204 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
207 TRACE_smpi_ptp_out(rank, src_traced, rank, "Irecv");
210 asynchronous_cleanup();
214 static void action_wait(const char *const *action)
217 m_task_t task = NULL;
219 double clock = MSG_get_clock();
220 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
222 xbt_assert(xbt_dynar_length(globals->irecvs),
223 "action wait not preceded by any irecv: %s", xbt_str_join_array(action," "));
225 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
226 name = xbt_str_join_array(action, " ");
228 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
229 int src_traced = counters->last_Irecv_sender_id;
230 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
231 TRACE_smpi_ptp_in(rank, src_traced, rank, "wait");
234 XBT_DEBUG("Entering %s", name);
235 comm = xbt_dynar_pop_as(globals->irecvs,msg_comm_t);
236 MSG_comm_wait(comm,-1);
237 task = xbt_dynar_pop_as(globals->tasks,m_task_t);
238 MSG_comm_destroy(comm);
239 MSG_task_destroy(task);
241 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
242 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
245 TRACE_smpi_ptp_out(rank, src_traced, rank, "wait");
246 TRACE_smpi_recv(rank, src_traced, rank);
251 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
252 static void action_barrier(const char *const *action)
255 static smx_mutex_t mutex = NULL;
256 static smx_cond_t cond = NULL;
257 static int processes_arrived_sofar=0;
259 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
260 name = xbt_str_join_array(action, " ");
262 if (mutex == NULL) { // first arriving on the barrier
263 mutex = SIMIX_req_mutex_init();
264 cond = SIMIX_req_cond_init();
265 processes_arrived_sofar=0;
267 XBT_DEBUG("Entering barrier: %s (%d already there)", name,processes_arrived_sofar);
269 SIMIX_req_mutex_lock(mutex);
270 if (++processes_arrived_sofar == communicator_size) {
271 SIMIX_req_cond_broadcast(cond);
272 SIMIX_req_mutex_unlock(mutex);
274 SIMIX_req_cond_wait(cond,mutex);
275 SIMIX_req_mutex_unlock(mutex);
278 XBT_DEBUG("Exiting barrier: %s", name);
280 processes_arrived_sofar--;
281 if (!processes_arrived_sofar) {
282 SIMIX_req_cond_destroy(cond);
283 SIMIX_req_mutex_destroy(mutex);
287 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
292 static void action_reduce(const char *const *action)
295 char *reduce_identifier;
297 double comm_size = parse_double(action[2]);
298 double comp_size = parse_double(action[3]);
299 m_task_t comp_task = NULL;
300 const char *process_name;
301 double clock = MSG_get_clock();
303 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
305 xbt_assert(communicator_size, "Size of Communicator is not defined, "
306 "can't use collective operations");
308 process_name = MSG_process_get_name(MSG_process_self());
310 reduce_identifier = bprintf("reduce_%d", counters->reduce_counter++);
312 if (!strcmp(process_name, "p0")) {
313 XBT_DEBUG("%s: %s is the Root", reduce_identifier, process_name);
315 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
316 m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
317 for (i = 1; i < communicator_size; i++) {
318 sprintf(mailbox, "%s_p%d_p0", reduce_identifier, i);
319 comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
321 MSG_comm_waitall(comms,communicator_size-1,-1);
322 for (i = 1; i < communicator_size; i++) {
323 MSG_comm_destroy(comms[i-1]);
324 MSG_task_destroy(tasks[i-1]);
328 comp_task = MSG_task_create("reduce_comp", comp_size, 0, NULL);
329 XBT_DEBUG("%s: computing 'reduce_comp'", reduce_identifier);
330 MSG_task_execute(comp_task);
331 MSG_task_destroy(comp_task);
332 XBT_DEBUG("%s: computed", reduce_identifier);
335 XBT_DEBUG("%s: %s sends", reduce_identifier, process_name);
336 sprintf(mailbox, "%s_%s_p0", reduce_identifier, process_name);
337 XBT_DEBUG("put on %s", mailbox);
338 MSG_task_send(MSG_task_create(reduce_identifier, 0, comm_size, NULL),
342 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
343 free(reduce_identifier);
346 static void action_bcast(const char *const *action)
349 char *bcast_identifier;
351 double comm_size = parse_double(action[2]);
352 m_task_t task = NULL;
353 const char *process_name;
354 double clock = MSG_get_clock();
356 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
358 xbt_assert(communicator_size, "Size of Communicator is not defined, "
359 "can't use collective operations");
361 process_name = MSG_process_get_name(MSG_process_self());
363 bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
365 if (!strcmp(process_name, "p0")) {
366 XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
368 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
370 for (i = 1; i < communicator_size; i++) {
371 sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
373 MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
376 MSG_comm_waitall(comms,communicator_size-1,-1);
377 for (i = 1; i < communicator_size; i++)
378 MSG_comm_destroy(comms[i-1]);
381 XBT_DEBUG("%s: all messages sent by %s have been received",
382 bcast_identifier, process_name);
385 sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
386 MSG_task_receive(&task, mailbox);
387 MSG_task_destroy(task);
388 XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
391 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
392 free(bcast_identifier);
396 static void action_sleep(const char *const *action)
399 const char *duration = action[2];
400 double clock = MSG_get_clock();
402 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
403 name = xbt_str_join_array(action, " ");
405 XBT_DEBUG("Entering %s", name);
406 MSG_process_sleep(parse_double(duration));
407 XBT_VERB("%s %f ", name, MSG_get_clock() - clock);
409 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
413 static void action_allReduce(const char *const *action) {
415 char *allreduce_identifier;
417 double comm_size = parse_double(action[2]);
418 double comp_size = parse_double(action[3]);
419 m_task_t task = NULL, comp_task = NULL;
420 const char *process_name;
421 double clock = MSG_get_clock();
423 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
425 xbt_assert(communicator_size, "Size of Communicator is not defined, "
426 "can't use collective operations");
428 process_name = MSG_process_get_name(MSG_process_self());
430 allreduce_identifier = bprintf("allReduce_%d", counters->allReduce_counter++);
432 if (!strcmp(process_name, "p0")) {
433 XBT_DEBUG("%s: %s is the Root", allreduce_identifier, process_name);
435 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
436 m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
437 for (i = 1; i < communicator_size; i++) {
438 sprintf(mailbox, "%s_p%d_p0", allreduce_identifier, i);
439 comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
441 MSG_comm_waitall(comms,communicator_size-1,-1);
442 for (i = 1; i < communicator_size; i++) {
443 MSG_comm_destroy(comms[i-1]);
444 MSG_task_destroy(tasks[i-1]);
448 comp_task = MSG_task_create("allReduce_comp", comp_size, 0, NULL);
449 XBT_DEBUG("%s: computing 'reduce_comp'", allreduce_identifier);
450 MSG_task_execute(comp_task);
451 MSG_task_destroy(comp_task);
452 XBT_DEBUG("%s: computed", allreduce_identifier);
454 for (i = 1; i < communicator_size; i++) {
455 sprintf(mailbox, "%s_p0_p%d", allreduce_identifier, i);
457 MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
460 MSG_comm_waitall(comms,communicator_size-1,-1);
461 for (i = 1; i < communicator_size; i++)
462 MSG_comm_destroy(comms[i-1]);
465 XBT_DEBUG("%s: all messages sent by %s have been received",
466 allreduce_identifier, process_name);
469 XBT_DEBUG("%s: %s sends", allreduce_identifier, process_name);
470 sprintf(mailbox, "%s_%s_p0", allreduce_identifier, process_name);
471 XBT_DEBUG("put on %s", mailbox);
472 MSG_task_send(MSG_task_create(allreduce_identifier, 0, comm_size, NULL),
475 sprintf(mailbox, "%s_p0_%s", allreduce_identifier, process_name);
476 MSG_task_receive(&task, mailbox);
477 MSG_task_destroy(task);
478 XBT_DEBUG("%s: %s has received", allreduce_identifier, process_name);
481 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
482 free(allreduce_identifier);
485 static void action_comm_size(const char *const *action)
488 const char *size = action[2];
489 double clock = MSG_get_clock();
491 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
492 name = xbt_str_join_array(action, " ");
493 communicator_size = parse_double(size);
494 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
495 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
499 static void action_compute(const char *const *action)
502 const char *amout = action[2];
503 m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
504 double clock = MSG_get_clock();
506 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
507 name = xbt_str_join_array(action, " ");
508 XBT_DEBUG("Entering %s", name);
509 MSG_task_execute(task);
510 MSG_task_destroy(task);
511 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
512 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
516 static void action_init(const char *const *action)
519 TRACE_smpi_init(get_rank(MSG_process_get_name(MSG_process_self())));
521 XBT_DEBUG("Initialize the counters");
522 process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
523 globals->isends = xbt_dynar_new(sizeof(msg_comm_t),NULL);
524 globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t),NULL);
525 globals->tasks = xbt_dynar_new(sizeof(m_task_t),NULL);
526 MSG_process_set_data(MSG_process_self(),globals);
530 static void action_finalize(const char *const *action)
533 TRACE_smpi_finalize(get_rank(MSG_process_get_name(MSG_process_self())));
535 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
537 xbt_dynar_free_container(&(globals->isends));
538 xbt_dynar_free_container(&(globals->irecvs));
539 xbt_dynar_free_container(&(globals->tasks));
545 int main(int argc, char *argv[])
547 MSG_error_t res = MSG_OK;
549 /* Check the given arguments */
550 MSG_global_init(&argc, argv);
552 printf("Usage: %s platform_file deployment_file [action_files]\n",
555 ("example: %s msg_platform.xml msg_deployment.xml actions # if all actions are in the same file\n",
558 ("example: %s msg_platform.xml msg_deployment.xml # if actions are in separate files, specified in deployment\n",
563 /* Simulation setting */
564 MSG_create_environment(argv[1]);
566 /* No need to register functions as in classical MSG programs: the actions get started anyway */
567 MSG_launch_application(argv[2]);
569 /* Action registration */
570 MSG_action_register("init", action_init);
571 MSG_action_register("finalize", action_finalize);
572 MSG_action_register("comm_size",action_comm_size);
573 MSG_action_register("send", action_send);
574 MSG_action_register("Isend", action_Isend);
575 MSG_action_register("recv", action_recv);
576 MSG_action_register("Irecv", action_Irecv);
577 MSG_action_register("wait", action_wait);
578 MSG_action_register("barrier", action_barrier);
579 MSG_action_register("bcast", action_bcast);
580 MSG_action_register("reduce", action_reduce);
581 MSG_action_register("allReduce",action_allReduce);
582 MSG_action_register("sleep", action_sleep);
583 MSG_action_register("compute", action_compute);
586 /* Actually do the simulation using MSG_action_trace_run */
587 res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
589 XBT_INFO("Simulation time %g", MSG_get_clock());