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 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(const char *const *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 THROWF(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(const char *const *action)
68 const char *size_str = action[3];
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()),action[2]);
74 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
75 name = xbt_str_join_array(action, " ");
78 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
79 int dst_traced = get_rank(action[2]);
80 TRACE_smpi_ptp_in(rank, rank, dst_traced, "send");
81 TRACE_smpi_send(rank, rank, dst_traced);
84 XBT_DEBUG("Entering Send: %s (size: %lg)", name, size);
88 MSG_task_send(MSG_task_create(name, 0, size, NULL), to);
91 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
93 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
97 TRACE_smpi_ptp_out(rank, rank, dst_traced, "send");
100 asynchronous_cleanup();
103 static void action_Isend(const char *const *action)
106 const char *size = action[3];
107 double clock = MSG_get_clock();
108 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
111 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),action[2]);
112 m_task_t task = MSG_task_create(to,0,parse_double(size),NULL);
113 msg_comm_t comm = MSG_task_isend_with_matching(task, to, /*matching madness*/NULL,task);
114 xbt_dynar_push(globals->isends,&comm);
116 XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
117 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
119 asynchronous_cleanup();
122 static int task_matching(void*ignored,void*sent_task) {
123 m_task_t t = (m_task_t)sent_task;
124 if (t!=NULL && MSG_task_get_data_size(t)<65536)
125 return 1; /* that's supposed to be already arrived */
126 return 0; /* rendez-vous mode: it's not there yet */
129 static void action_recv(const char *const *action)
132 char mailbox_name[250];
133 m_task_t task = NULL;
134 double clock = MSG_get_clock();
136 sprintf(mailbox_name, "%s_%s", action[2],
137 MSG_process_get_name(MSG_process_self()));
139 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
140 name = xbt_str_join_array(action, " ");
142 /* The next chunk is to deal with the fact that for short messages,
143 * if the send occurs before the receive, the message is already sent and
144 * buffered on receiver side when the recv() occurs.
146 * So the next chunk detects this fact and cancel the simix communication instead.
149 /* make sure the rdv is created on need by asking to MSG instead of simix directly */
150 smx_rdv_t rdv = MSG_mailbox_get_by_alias(mailbox_name);
151 smx_action_t act = SIMIX_comm_get_send_match(rdv,task_matching,NULL);
153 /* FIXME account for the memcopy time if needed */
154 SIMIX_comm_finish(act);
159 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
160 int src_traced = get_rank(action[2]);
161 TRACE_smpi_ptp_in(rank, src_traced, rank, "recv");
164 XBT_DEBUG("Receiving: %s", name);
165 MSG_task_receive(&task, mailbox_name);
166 // MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
167 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
168 MSG_task_destroy(task);
170 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
173 TRACE_smpi_ptp_out(rank, src_traced, rank, "recv");
174 TRACE_smpi_recv(rank, src_traced, rank);
177 asynchronous_cleanup();
180 static void action_Irecv(const char *const *action)
183 double clock = MSG_get_clock();
184 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
186 XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
188 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
189 int src_traced = get_rank(action[2]);
190 globals->last_Irecv_sender_id = src_traced;
191 MSG_process_set_data(MSG_process_self(), (void *) globals);
193 TRACE_smpi_ptp_in(rank, src_traced, rank, "Irecv");
196 sprintf(mailbox, "%s_%s", action[2],
197 MSG_process_get_name(MSG_process_self()));
199 xbt_dynar_push(globals->tasks,&t);
202 xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks)-1),
204 xbt_dynar_push(globals->irecvs,&c);
206 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
209 TRACE_smpi_ptp_out(rank, src_traced, rank, "Irecv");
212 asynchronous_cleanup();
216 static void action_wait(const char *const *action)
219 m_task_t task = NULL;
221 double clock = MSG_get_clock();
222 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
224 xbt_assert(xbt_dynar_length(globals->irecvs),
225 "action wait not preceded by any irecv: %s", xbt_str_join_array(action," "));
227 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
228 name = xbt_str_join_array(action, " ");
230 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
231 int src_traced = counters->last_Irecv_sender_id;
232 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
233 TRACE_smpi_ptp_in(rank, src_traced, rank, "wait");
236 XBT_DEBUG("Entering %s", name);
237 comm = xbt_dynar_pop_as(globals->irecvs,msg_comm_t);
238 MSG_comm_wait(comm,-1);
239 task = xbt_dynar_pop_as(globals->tasks,m_task_t);
240 MSG_comm_destroy(comm);
241 MSG_task_destroy(task);
243 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
244 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
247 TRACE_smpi_ptp_out(rank, src_traced, rank, "wait");
248 TRACE_smpi_recv(rank, src_traced, rank);
253 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
254 static void action_barrier(const char *const *action)
257 static smx_mutex_t mutex = NULL;
258 static smx_cond_t cond = NULL;
259 static int processes_arrived_sofar=0;
261 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
262 name = xbt_str_join_array(action, " ");
264 if (mutex == NULL) { // first arriving on the barrier
265 mutex = SIMIX_req_mutex_init();
266 cond = SIMIX_req_cond_init();
267 processes_arrived_sofar=0;
269 XBT_DEBUG("Entering barrier: %s (%d already there)", name,processes_arrived_sofar);
271 SIMIX_req_mutex_lock(mutex);
272 if (++processes_arrived_sofar == communicator_size) {
273 SIMIX_req_cond_broadcast(cond);
274 SIMIX_req_mutex_unlock(mutex);
276 SIMIX_req_cond_wait(cond,mutex);
277 SIMIX_req_mutex_unlock(mutex);
280 XBT_DEBUG("Exiting barrier: %s", name);
282 processes_arrived_sofar--;
283 if (!processes_arrived_sofar) {
284 SIMIX_req_cond_destroy(cond);
285 SIMIX_req_mutex_destroy(mutex);
289 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
294 static void action_reduce(const char *const *action)
297 char *reduce_identifier;
299 double comm_size = parse_double(action[2]);
300 double comp_size = parse_double(action[3]);
301 m_task_t comp_task = NULL;
302 const char *process_name;
303 double clock = MSG_get_clock();
305 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
307 xbt_assert(communicator_size, "Size of Communicator is not defined, "
308 "can't use collective operations");
310 process_name = MSG_process_get_name(MSG_process_self());
312 reduce_identifier = bprintf("reduce_%d", counters->reduce_counter++);
314 if (!strcmp(process_name, "p0")) {
315 XBT_DEBUG("%s: %s is the Root", reduce_identifier, process_name);
317 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
318 m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
319 for (i = 1; i < communicator_size; i++) {
320 sprintf(mailbox, "%s_p%d_p0", reduce_identifier, i);
321 comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
323 MSG_comm_waitall(comms,communicator_size-1,-1);
324 for (i = 1; i < communicator_size; i++) {
325 MSG_comm_destroy(comms[i-1]);
326 MSG_task_destroy(tasks[i-1]);
330 comp_task = MSG_task_create("reduce_comp", comp_size, 0, NULL);
331 XBT_DEBUG("%s: computing 'reduce_comp'", reduce_identifier);
332 MSG_task_execute(comp_task);
333 MSG_task_destroy(comp_task);
334 XBT_DEBUG("%s: computed", reduce_identifier);
337 XBT_DEBUG("%s: %s sends", reduce_identifier, process_name);
338 sprintf(mailbox, "%s_%s_p0", reduce_identifier, process_name);
339 XBT_DEBUG("put on %s", mailbox);
340 MSG_task_send(MSG_task_create(reduce_identifier, 0, comm_size, NULL),
344 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
345 free(reduce_identifier);
348 static void action_bcast(const char *const *action)
351 char *bcast_identifier;
353 double comm_size = parse_double(action[2]);
354 m_task_t task = NULL;
355 const char *process_name;
356 double clock = MSG_get_clock();
358 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
360 xbt_assert(communicator_size, "Size of Communicator is not defined, "
361 "can't use collective operations");
363 process_name = MSG_process_get_name(MSG_process_self());
365 bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
367 if (!strcmp(process_name, "p0")) {
368 XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
370 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
372 for (i = 1; i < communicator_size; i++) {
373 sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
375 MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
378 MSG_comm_waitall(comms,communicator_size-1,-1);
379 for (i = 1; i < communicator_size; i++)
380 MSG_comm_destroy(comms[i-1]);
383 XBT_DEBUG("%s: all messages sent by %s have been received",
384 bcast_identifier, process_name);
387 sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
388 MSG_task_receive(&task, mailbox);
389 MSG_task_destroy(task);
390 XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
393 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
394 free(bcast_identifier);
398 static void action_sleep(const char *const *action)
401 const char *duration = action[2];
402 double clock = MSG_get_clock();
404 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
405 name = xbt_str_join_array(action, " ");
407 XBT_DEBUG("Entering %s", name);
408 MSG_process_sleep(parse_double(duration));
409 XBT_VERB("%s %f ", name, MSG_get_clock() - clock);
411 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
415 static void action_allReduce(const char *const *action) {
417 char *allreduce_identifier;
419 double comm_size = parse_double(action[2]);
420 double comp_size = parse_double(action[3]);
421 m_task_t task = NULL, comp_task = NULL;
422 const char *process_name;
423 double clock = MSG_get_clock();
425 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
427 xbt_assert(communicator_size, "Size of Communicator is not defined, "
428 "can't use collective operations");
430 process_name = MSG_process_get_name(MSG_process_self());
432 allreduce_identifier = bprintf("allReduce_%d", counters->allReduce_counter++);
434 if (!strcmp(process_name, "p0")) {
435 XBT_DEBUG("%s: %s is the Root", allreduce_identifier, process_name);
437 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
438 m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
439 for (i = 1; i < communicator_size; i++) {
440 sprintf(mailbox, "%s_p%d_p0", allreduce_identifier, i);
441 comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
443 MSG_comm_waitall(comms,communicator_size-1,-1);
444 for (i = 1; i < communicator_size; i++) {
445 MSG_comm_destroy(comms[i-1]);
446 MSG_task_destroy(tasks[i-1]);
450 comp_task = MSG_task_create("allReduce_comp", comp_size, 0, NULL);
451 XBT_DEBUG("%s: computing 'reduce_comp'", allreduce_identifier);
452 MSG_task_execute(comp_task);
453 MSG_task_destroy(comp_task);
454 XBT_DEBUG("%s: computed", allreduce_identifier);
456 for (i = 1; i < communicator_size; i++) {
457 sprintf(mailbox, "%s_p0_p%d", allreduce_identifier, i);
459 MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
462 MSG_comm_waitall(comms,communicator_size-1,-1);
463 for (i = 1; i < communicator_size; i++)
464 MSG_comm_destroy(comms[i-1]);
467 XBT_DEBUG("%s: all messages sent by %s have been received",
468 allreduce_identifier, process_name);
471 XBT_DEBUG("%s: %s sends", allreduce_identifier, process_name);
472 sprintf(mailbox, "%s_%s_p0", allreduce_identifier, process_name);
473 XBT_DEBUG("put on %s", mailbox);
474 MSG_task_send(MSG_task_create(allreduce_identifier, 0, comm_size, NULL),
477 sprintf(mailbox, "%s_p0_%s", allreduce_identifier, process_name);
478 MSG_task_receive(&task, mailbox);
479 MSG_task_destroy(task);
480 XBT_DEBUG("%s: %s has received", allreduce_identifier, process_name);
483 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
484 free(allreduce_identifier);
487 static void action_comm_size(const char *const *action)
490 const char *size = action[2];
491 double clock = MSG_get_clock();
493 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
494 name = xbt_str_join_array(action, " ");
495 communicator_size = parse_double(size);
496 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
497 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
501 static void action_compute(const char *const *action)
504 const char *amout = action[2];
505 m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
506 double clock = MSG_get_clock();
508 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
509 name = xbt_str_join_array(action, " ");
510 XBT_DEBUG("Entering %s", name);
511 MSG_task_execute(task);
512 MSG_task_destroy(task);
513 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
514 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
518 static void action_init(const char *const *action)
521 TRACE_smpi_init(get_rank(MSG_process_get_name(MSG_process_self())));
523 XBT_DEBUG("Initialize the counters");
524 process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
525 globals->isends = xbt_dynar_new(sizeof(msg_comm_t),NULL);
526 globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t),NULL);
527 globals->tasks = xbt_dynar_new(sizeof(m_task_t),NULL);
528 MSG_process_set_data(MSG_process_self(),globals);
532 static void action_finalize(const char *const *action)
535 TRACE_smpi_finalize(get_rank(MSG_process_get_name(MSG_process_self())));
537 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
539 xbt_dynar_free_container(&(globals->isends));
540 xbt_dynar_free_container(&(globals->irecvs));
541 xbt_dynar_free_container(&(globals->tasks));
547 int main(int argc, char *argv[])
549 MSG_error_t res = MSG_OK;
551 smx_factory_initializer_to_use = SIMIX_ctx_raw_factory_init;
553 /* Check the given arguments */
554 MSG_global_init(&argc, argv);
556 printf("Usage: %s platform_file deployment_file [action_files]\n",
559 ("example: %s msg_platform.xml msg_deployment.xml actions # if all actions are in the same file\n",
562 ("example: %s msg_platform.xml msg_deployment.xml # if actions are in separate files, specified in deployment\n",
567 /* Simulation setting */
568 MSG_create_environment(argv[1]);
570 /* No need to register functions as in classical MSG programs: the actions get started anyway */
571 MSG_launch_application(argv[2]);
573 /* Action registration */
574 MSG_action_register("init", action_init);
575 MSG_action_register("finalize", action_finalize);
576 MSG_action_register("comm_size",action_comm_size);
577 MSG_action_register("send", action_send);
578 MSG_action_register("Isend", action_Isend);
579 MSG_action_register("recv", action_recv);
580 MSG_action_register("Irecv", action_Irecv);
581 MSG_action_register("wait", action_wait);
582 MSG_action_register("barrier", action_barrier);
583 MSG_action_register("bcast", action_bcast);
584 MSG_action_register("reduce", action_reduce);
585 MSG_action_register("allReduce",action_allReduce);
586 MSG_action_register("sleep", action_sleep);
587 MSG_action_register("compute", action_compute);
590 /* Actually do the simulation using MSG_action_trace_run */
591 res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
593 XBT_INFO("Simulation time %g", MSG_get_clock());