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 #include "msg/private.h" /* You don't want to know why, trust us */
16 #include "simix/private.h"
18 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory);
20 XBT_LOG_NEW_DEFAULT_CATEGORY(actions,
21 "Messages specific for this msg example");
22 int communicator_size = 0;
24 static void action_Isend(const char *const *action);
27 int last_Irecv_sender_id;
30 int allReduce_counter;
31 xbt_dynar_t isends; /* of msg_comm_t */
32 /* Used to implement irecv+wait */
33 xbt_dynar_t irecvs; /* of msg_comm_t */
34 xbt_dynar_t tasks; /* of m_task_t */
35 } s_process_globals_t, *process_globals_t;
38 static double parse_double(const char *string)
43 value = strtod(string, &endptr);
45 THROW1(unknown_error, 0, "%s is not a double", string);
49 static int get_rank (const char *process_name)
51 return atoi(&(process_name[1]));
54 static void asynchronous_cleanup(void) {
55 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
57 /* Destroy any isend which correspond to completed communications */
60 while ((found = MSG_comm_testany(globals->isends)) != -1) {
61 xbt_dynar_remove_at(globals->isends,found,&comm);
62 MSG_comm_destroy(comm);
67 static void action_send(const char *const *action)
71 const char *size_str = action[3];
72 double size=parse_double(size_str);
73 double clock = MSG_get_clock(); /* this "call" is free thanks to inlining */
75 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),action[2]);
77 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
78 name = xbt_str_join_array(action, " ");
81 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
82 int dst_traced = get_rank(action[2]);
83 TRACE_smpi_ptp_in(rank, rank, dst_traced, "send");
84 TRACE_smpi_send(rank, rank, dst_traced);
87 XBT_DEBUG("Entering Send: %s (size: %lg)", name, size);
91 MSG_task_send(MSG_task_create(name, 0, size, NULL), to);
94 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
96 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
100 TRACE_smpi_ptp_out(rank, rank, dst_traced, "send");
103 asynchronous_cleanup();
106 static int task_recv_matching(void*ignored,void*recv_task) {
107 XBT_DEBUG("Trying to recv_match with %p", recv_task);
108 return recv_task != NULL;
111 static void action_Isend(const char *const *action)
114 const char *size = action[3];
115 double clock = MSG_get_clock();
116 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
119 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),action[2]);
120 m_task_t task = MSG_task_create(to,0,parse_double(size),NULL);
122 smx_rdv_t rdv = MSG_mailbox_get_by_alias(to);
124 if(SIMIX_comm_has_recv_match(rdv, task_recv_matching, NULL)) {
125 XBT_DEBUG("Switching back to MSG_task_send: %s", to);
126 MSG_task_send(task, to);
130 msg_comm_t comm = MSG_task_isend_with_matching(task, to, /*matching madness*/NULL,task);
131 xbt_dynar_push(globals->isends,&comm);
133 if (task->simdata->message_size < 65536) {
134 /* Close your eyes, it burns ! */
135 comm->s_comm->comm.dst_proc = SIMIX_process_get_by_name(action[2]);
136 comm->s_comm->comm.dst_buff = NULL;
137 comm->s_comm->comm.dst_buff_size = NULL;
138 comm->s_comm->comm.dst_data = NULL;
139 comm->s_comm->state = SIMIX_READY;
140 comm->s_comm->comm.refcount++;
141 SIMIX_comm_start(comm->s_comm);
144 XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
145 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
147 asynchronous_cleanup();
150 static int task_sent_matching(void*ignored,void*sent_task) {
151 m_task_t t = (m_task_t)sent_task;
152 if (t!=NULL && MSG_task_get_data_size(t)<65536)
153 return 1; /* that's supposed to be already arrived */
154 return 0; /* rendez-vous mode: it's not there yet */
157 static void action_recv(const char *const *action)
160 char mailbox_name[250];
161 m_task_t task = NULL;
162 double clock = MSG_get_clock();
164 sprintf(mailbox_name, "%s_%s", action[2],
165 MSG_process_get_name(MSG_process_self()));
167 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
168 name = xbt_str_join_array(action, " ");
170 /* The next chunk is to deal with the fact that for short messages,
171 * if the send occurs before the receive, the message is already sent and
172 * buffered on receiver side when the recv() occurs.
174 * So the next chunk detects this fact and cancel the simix communication instead.
177 /* make sure the rdv is created on need by asking to MSG instead of simix directly */
178 smx_rdv_t rdv = MSG_mailbox_get_by_alias(mailbox_name);
179 smx_action_t act = SIMIX_comm_get_send_match(rdv, task_sent_matching, NULL);
181 /* FIXME account for the memcopy time if needed */
182 task = act->comm.src_data;
184 if (task->simdata->message_size < 65536) {
185 act->comm.refcount--; /* See action_send for more pain */
186 if(act->state == SIMIX_DONE)
187 SIMIX_comm_finish(act);
189 SIMIX_req_comm_wait(act, -1.0);
195 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
196 int src_traced = get_rank(action[2]);
197 TRACE_smpi_ptp_in(rank, src_traced, rank, "recv");
200 XBT_DEBUG("Receiving: %s", name);
201 /* Mimic a call to MSG_task_receive(&task, mailbox_name); */
202 SIMIX_req_comm_recv(rdv, &task, NULL, NULL, &task, -1.0);
204 // MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
205 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
206 MSG_task_destroy(task);
208 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
211 TRACE_smpi_ptp_out(rank, src_traced, rank, "recv");
212 TRACE_smpi_recv(rank, src_traced, rank);
215 asynchronous_cleanup();
218 static void action_Irecv(const char *const *action)
221 double clock = MSG_get_clock();
222 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
224 XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
226 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
227 int src_traced = get_rank(action[2]);
228 globals->last_Irecv_sender_id = src_traced;
229 MSG_process_set_data(MSG_process_self(), (void *) globals);
231 TRACE_smpi_ptp_in(rank, src_traced, rank, "Irecv");
234 sprintf(mailbox, "%s_%s", action[2],
235 MSG_process_get_name(MSG_process_self()));
237 xbt_dynar_push(globals->tasks,&t);
240 xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks)-1),
242 xbt_dynar_push(globals->irecvs,&c);
244 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
247 TRACE_smpi_ptp_out(rank, src_traced, rank, "Irecv");
250 asynchronous_cleanup();
254 static void action_wait(const char *const *action)
257 m_task_t task = NULL;
259 double clock = MSG_get_clock();
260 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
262 xbt_assert1(xbt_dynar_length(globals->irecvs),
263 "action wait not preceded by any irecv: %s", xbt_str_join_array(action," "));
265 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
266 name = xbt_str_join_array(action, " ");
268 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
269 int src_traced = counters->last_Irecv_sender_id;
270 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
271 TRACE_smpi_ptp_in(rank, src_traced, rank, "wait");
274 XBT_DEBUG("Entering %s", name);
275 comm = xbt_dynar_pop_as(globals->irecvs,msg_comm_t);
276 MSG_comm_wait(comm,-1);
277 task = xbt_dynar_pop_as(globals->tasks,m_task_t);
278 MSG_comm_destroy(comm);
279 MSG_task_destroy(task);
281 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
282 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
285 TRACE_smpi_ptp_out(rank, src_traced, rank, "wait");
286 TRACE_smpi_recv(rank, src_traced, rank);
291 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
292 static void action_barrier(const char *const *action)
295 static smx_mutex_t mutex = NULL;
296 static smx_cond_t cond = NULL;
297 static int processes_arrived_sofar=0;
299 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
300 name = xbt_str_join_array(action, " ");
302 if (mutex == NULL) { // first arriving on the barrier
303 mutex = SIMIX_req_mutex_init();
304 cond = SIMIX_req_cond_init();
305 processes_arrived_sofar=0;
307 XBT_DEBUG("Entering barrier: %s (%d already there)", name,processes_arrived_sofar);
309 SIMIX_req_mutex_lock(mutex);
310 if (++processes_arrived_sofar == communicator_size) {
311 SIMIX_req_cond_broadcast(cond);
312 SIMIX_req_mutex_unlock(mutex);
314 SIMIX_req_cond_wait(cond,mutex);
315 SIMIX_req_mutex_unlock(mutex);
318 XBT_DEBUG("Exiting barrier: %s", name);
320 processes_arrived_sofar--;
321 if (!processes_arrived_sofar) {
322 SIMIX_req_cond_destroy(cond);
323 SIMIX_req_mutex_destroy(mutex);
327 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
332 static void action_reduce(const char *const *action)
335 char *reduce_identifier;
337 double comm_size = parse_double(action[2]);
338 double comp_size = parse_double(action[3]);
339 m_task_t comp_task = NULL;
340 const char *process_name;
341 double clock = MSG_get_clock();
343 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
345 xbt_assert0(communicator_size, "Size of Communicator is not defined, "
346 "can't use collective operations");
348 process_name = MSG_process_get_name(MSG_process_self());
350 reduce_identifier = bprintf("reduce_%d", counters->reduce_counter++);
352 if (!strcmp(process_name, "p0")) {
353 XBT_DEBUG("%s: %s is the Root", reduce_identifier, process_name);
355 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
356 m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
357 for (i = 1; i < communicator_size; i++) {
358 sprintf(mailbox, "%s_p%d_p0", reduce_identifier, i);
359 comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
361 MSG_comm_waitall(comms,communicator_size-1,-1);
362 for (i = 1; i < communicator_size; i++) {
363 MSG_comm_destroy(comms[i-1]);
364 MSG_task_destroy(tasks[i-1]);
368 comp_task = MSG_task_create("reduce_comp", comp_size, 0, NULL);
369 XBT_DEBUG("%s: computing 'reduce_comp'", reduce_identifier);
370 MSG_task_execute(comp_task);
371 MSG_task_destroy(comp_task);
372 XBT_DEBUG("%s: computed", reduce_identifier);
375 XBT_DEBUG("%s: %s sends", reduce_identifier, process_name);
376 sprintf(mailbox, "%s_%s_p0", reduce_identifier, process_name);
377 XBT_DEBUG("put on %s", mailbox);
378 MSG_task_send(MSG_task_create(reduce_identifier, 0, comm_size, NULL),
382 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
383 free(reduce_identifier);
386 static void action_bcast(const char *const *action)
389 char *bcast_identifier;
391 double comm_size = parse_double(action[2]);
392 m_task_t task = NULL;
393 const char *process_name;
394 double clock = MSG_get_clock();
396 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
398 xbt_assert0(communicator_size, "Size of Communicator is not defined, "
399 "can't use collective operations");
401 process_name = MSG_process_get_name(MSG_process_self());
403 bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
405 if (!strcmp(process_name, "p0")) {
406 XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
408 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
410 for (i = 1; i < communicator_size; i++) {
411 sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
413 MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
416 MSG_comm_waitall(comms,communicator_size-1,-1);
417 for (i = 1; i < communicator_size; i++)
418 MSG_comm_destroy(comms[i-1]);
421 XBT_DEBUG("%s: all messages sent by %s have been received",
422 bcast_identifier, process_name);
425 sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
426 MSG_task_receive(&task, mailbox);
427 MSG_task_destroy(task);
428 XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
431 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
432 free(bcast_identifier);
436 static void action_sleep(const char *const *action)
439 const char *duration = action[2];
440 double clock = MSG_get_clock();
442 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
443 name = xbt_str_join_array(action, " ");
445 XBT_DEBUG("Entering %s", name);
446 MSG_process_sleep(parse_double(duration));
447 XBT_VERB("%s %f ", name, MSG_get_clock() - clock);
449 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
453 static void action_allReduce(const char *const *action) {
455 char *allreduce_identifier;
457 double comm_size = parse_double(action[2]);
458 double comp_size = parse_double(action[3]);
459 m_task_t task = NULL, comp_task = NULL;
460 const char *process_name;
461 double clock = MSG_get_clock();
463 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
465 xbt_assert0(communicator_size, "Size of Communicator is not defined, "
466 "can't use collective operations");
468 process_name = MSG_process_get_name(MSG_process_self());
470 allreduce_identifier = bprintf("allReduce_%d", counters->allReduce_counter++);
472 if (!strcmp(process_name, "p0")) {
473 XBT_DEBUG("%s: %s is the Root", allreduce_identifier, process_name);
475 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
476 m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
477 for (i = 1; i < communicator_size; i++) {
478 sprintf(mailbox, "%s_p%d_p0", allreduce_identifier, i);
479 comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
481 MSG_comm_waitall(comms,communicator_size-1,-1);
482 for (i = 1; i < communicator_size; i++) {
483 MSG_comm_destroy(comms[i-1]);
484 MSG_task_destroy(tasks[i-1]);
488 comp_task = MSG_task_create("allReduce_comp", comp_size, 0, NULL);
489 XBT_DEBUG("%s: computing 'reduce_comp'", allreduce_identifier);
490 MSG_task_execute(comp_task);
491 MSG_task_destroy(comp_task);
492 XBT_DEBUG("%s: computed", allreduce_identifier);
494 for (i = 1; i < communicator_size; i++) {
495 sprintf(mailbox, "%s_p0_p%d", allreduce_identifier, i);
497 MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
500 MSG_comm_waitall(comms,communicator_size-1,-1);
501 for (i = 1; i < communicator_size; i++)
502 MSG_comm_destroy(comms[i-1]);
505 XBT_DEBUG("%s: all messages sent by %s have been received",
506 allreduce_identifier, process_name);
509 XBT_DEBUG("%s: %s sends", allreduce_identifier, process_name);
510 sprintf(mailbox, "%s_%s_p0", allreduce_identifier, process_name);
511 XBT_DEBUG("put on %s", mailbox);
512 MSG_task_send(MSG_task_create(allreduce_identifier, 0, comm_size, NULL),
515 sprintf(mailbox, "%s_p0_%s", allreduce_identifier, process_name);
516 MSG_task_receive(&task, mailbox);
517 MSG_task_destroy(task);
518 XBT_DEBUG("%s: %s has received", allreduce_identifier, process_name);
521 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
522 free(allreduce_identifier);
525 static void action_comm_size(const char *const *action)
528 const char *size = action[2];
529 double clock = MSG_get_clock();
531 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
532 name = xbt_str_join_array(action, " ");
533 communicator_size = parse_double(size);
534 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
535 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
539 static void action_compute(const char *const *action)
542 const char *amout = action[2];
543 m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
544 double clock = MSG_get_clock();
546 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
547 name = xbt_str_join_array(action, " ");
548 XBT_DEBUG("Entering %s", name);
549 MSG_task_execute(task);
550 MSG_task_destroy(task);
551 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
552 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
556 static void action_init(const char *const *action)
559 TRACE_smpi_init(get_rank(MSG_process_get_name(MSG_process_self())));
561 XBT_DEBUG("Initialize the counters");
562 process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
563 globals->isends = xbt_dynar_new(sizeof(msg_comm_t),NULL);
564 globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t),NULL);
565 globals->tasks = xbt_dynar_new(sizeof(m_task_t),NULL);
566 MSG_process_set_data(MSG_process_self(),globals);
570 static void action_finalize(const char *const *action)
573 TRACE_smpi_finalize(get_rank(MSG_process_get_name(MSG_process_self())));
575 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
577 xbt_dynar_free_container(&(globals->isends));
578 xbt_dynar_free_container(&(globals->irecvs));
579 xbt_dynar_free_container(&(globals->tasks));
585 int main(int argc, char *argv[])
587 MSG_error_t res = MSG_OK;
589 smx_factory_initializer_to_use = SIMIX_ctx_raw_factory_init;
591 /* Check the given arguments */
592 MSG_global_init(&argc, argv);
594 printf("Usage: %s platform_file deployment_file [action_files]\n",
597 ("example: %s msg_platform.xml msg_deployment.xml actions # if all actions are in the same file\n",
600 ("example: %s msg_platform.xml msg_deployment.xml # if actions are in separate files, specified in deployment\n",
605 /* Simulation setting */
606 MSG_create_environment(argv[1]);
608 /* No need to register functions as in classical MSG programs: the actions get started anyway */
609 MSG_launch_application(argv[2]);
611 /* Action registration */
612 MSG_action_register("init", action_init);
613 MSG_action_register("finalize", action_finalize);
614 MSG_action_register("comm_size",action_comm_size);
615 MSG_action_register("send", action_send);
616 MSG_action_register("Isend", action_Isend);
617 MSG_action_register("recv", action_recv);
618 MSG_action_register("Irecv", action_Irecv);
619 MSG_action_register("wait", action_wait);
620 MSG_action_register("barrier", action_barrier);
621 MSG_action_register("bcast", action_bcast);
622 MSG_action_register("reduce", action_reduce);
623 MSG_action_register("allReduce",action_allReduce);
624 MSG_action_register("sleep", action_sleep);
625 MSG_action_register("compute", action_compute);
628 /* Actually do the simulation using MSG_action_trace_run */
629 res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
631 XBT_INFO("Simulation time %g", MSG_get_clock());