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 "instr/instr_private.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(actions,
15 "Messages specific for this msg example");
16 int communicator_size = 0;
18 static void action_Isend(const char *const *action);
21 int last_Irecv_sender_id;
24 int allReduce_counter;
25 xbt_dynar_t isends; /* of msg_comm_t */
26 /* Used to implement irecv+wait */
27 xbt_dynar_t irecvs; /* of msg_comm_t */
28 xbt_dynar_t tasks; /* of m_task_t */
29 } s_process_globals_t, *process_globals_t;
32 static double parse_double(const char *string)
37 value = strtod(string, &endptr);
39 THROWF(unknown_error, 0, "%s is not a double", string);
43 static int get_rank (const char *process_name)
45 return atoi(&(process_name[1]));
48 static void asynchronous_cleanup(void) {
49 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
51 /* Destroy any isend which correspond to completed communications */
54 while ((found = MSG_comm_testany(globals->isends)) != -1) {
55 xbt_dynar_remove_at(globals->isends,found,&comm);
56 MSG_comm_destroy(comm);
61 static void action_send(const char *const *action)
65 const char *size_str = action[3];
66 double size=parse_double(size_str);
67 double clock = MSG_get_clock(); /* this "call" is free thanks to inlining */
69 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),action[2]);
71 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
72 name = xbt_str_join_array(action, " ");
75 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
76 int dst_traced = get_rank(action[2]);
77 TRACE_smpi_ptp_in(rank, rank, dst_traced, "send");
78 TRACE_smpi_send(rank, rank, dst_traced);
81 XBT_DEBUG("Entering Send: %s (size: %lg)", name, size);
85 MSG_task_send(MSG_task_create(name, 0, size, NULL), to);
88 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
93 TRACE_smpi_ptp_out(rank, rank, dst_traced, "send");
96 asynchronous_cleanup();
99 static void action_Isend(const char *const *action)
102 const char *size = action[3];
103 double clock = MSG_get_clock();
104 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
107 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),action[2]);
109 MSG_task_isend( MSG_task_create(to,0,parse_double(size),NULL), to);
110 xbt_dynar_push(globals->isends,&comm);
112 XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
113 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
115 asynchronous_cleanup();
119 static void action_recv(const char *const *action)
122 char mailbox_name[250];
123 m_task_t task = NULL;
124 double clock = MSG_get_clock();
126 sprintf(mailbox_name, "%s_%s", action[2],
127 MSG_process_get_name(MSG_process_self()));
129 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
130 name = xbt_str_join_array(action, " ");
133 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
134 int src_traced = get_rank(action[2]);
135 TRACE_smpi_ptp_in(rank, src_traced, rank, "recv");
138 XBT_DEBUG("Receiving: %s", name);
139 MSG_error_t res = MSG_task_receive(&task, mailbox_name);
140 // MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
141 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
144 MSG_task_destroy(task);
149 TRACE_smpi_ptp_out(rank, src_traced, rank, "recv");
150 TRACE_smpi_recv(rank, src_traced, rank);
153 asynchronous_cleanup();
156 static void action_Irecv(const char *const *action)
159 double clock = MSG_get_clock();
160 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
162 XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
164 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
165 int src_traced = get_rank(action[2]);
166 globals->last_Irecv_sender_id = src_traced;
167 MSG_process_set_data(MSG_process_self(), (void *) globals);
169 TRACE_smpi_ptp_in(rank, src_traced, rank, "Irecv");
172 sprintf(mailbox, "%s_%s", action[2],
173 MSG_process_get_name(MSG_process_self()));
175 xbt_dynar_push(globals->tasks,&t);
178 xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks)-1),
180 xbt_dynar_push(globals->irecvs,&c);
182 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
185 TRACE_smpi_ptp_out(rank, src_traced, rank, "Irecv");
188 asynchronous_cleanup();
192 static void action_wait(const char *const *action)
195 m_task_t task = NULL;
197 double clock = MSG_get_clock();
198 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
200 xbt_assert(xbt_dynar_length(globals->irecvs),
201 "action wait not preceded by any irecv: %s", xbt_str_join_array(action," "));
203 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
204 name = xbt_str_join_array(action, " ");
206 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
207 int src_traced = counters->last_Irecv_sender_id;
208 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
209 TRACE_smpi_ptp_in(rank, src_traced, rank, "wait");
212 XBT_DEBUG("Entering %s", name);
213 comm = xbt_dynar_pop_as(globals->irecvs,msg_comm_t);
214 MSG_comm_wait(comm,-1);
215 task = xbt_dynar_pop_as(globals->tasks,m_task_t);
216 MSG_comm_destroy(comm);
217 MSG_task_destroy(task);
219 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
222 TRACE_smpi_ptp_out(rank, src_traced, rank, "wait");
223 TRACE_smpi_recv(rank, src_traced, rank);
228 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
229 static void action_barrier(const char *const *action)
232 static smx_mutex_t mutex = NULL;
233 static smx_cond_t cond = NULL;
234 static int processes_arrived_sofar=0;
236 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
237 name = xbt_str_join_array(action, " ");
239 if (mutex == NULL) { // first arriving on the barrier
240 mutex = simcall_mutex_init();
241 cond = simcall_cond_init();
242 processes_arrived_sofar=0;
244 XBT_DEBUG("Entering barrier: %s (%d already there)", name,processes_arrived_sofar);
246 simcall_mutex_lock(mutex);
247 if (++processes_arrived_sofar == communicator_size) {
248 simcall_cond_broadcast(cond);
249 simcall_mutex_unlock(mutex);
251 simcall_cond_wait(cond,mutex);
252 simcall_mutex_unlock(mutex);
255 XBT_DEBUG("Exiting barrier: %s", name);
257 processes_arrived_sofar--;
258 if (!processes_arrived_sofar) {
259 simcall_cond_destroy(cond);
260 simcall_mutex_destroy(mutex);
268 static void action_reduce(const char *const *action)
271 char *reduce_identifier;
273 double comm_size = parse_double(action[2]);
274 double comp_size = parse_double(action[3]);
275 m_task_t comp_task = NULL;
276 const char *process_name;
277 double clock = MSG_get_clock();
279 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
281 xbt_assert(communicator_size, "Size of Communicator is not defined, "
282 "can't use collective operations");
284 process_name = MSG_process_get_name(MSG_process_self());
286 reduce_identifier = bprintf("reduce_%d", counters->reduce_counter++);
288 if (!strcmp(process_name, "p0")) {
289 XBT_DEBUG("%s: %s is the Root", reduce_identifier, process_name);
291 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
292 m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
293 for (i = 1; i < communicator_size; i++) {
294 sprintf(mailbox, "%s_p%d_p0", reduce_identifier, i);
295 comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
297 MSG_comm_waitall(comms,communicator_size-1,-1);
298 for (i = 1; i < communicator_size; i++) {
299 MSG_comm_destroy(comms[i-1]);
300 MSG_task_destroy(tasks[i-1]);
304 comp_task = MSG_task_create("reduce_comp", comp_size, 0, NULL);
305 XBT_DEBUG("%s: computing 'reduce_comp'", reduce_identifier);
306 MSG_task_execute(comp_task);
307 MSG_task_destroy(comp_task);
308 XBT_DEBUG("%s: computed", reduce_identifier);
311 XBT_DEBUG("%s: %s sends", reduce_identifier, process_name);
312 sprintf(mailbox, "%s_%s_p0", reduce_identifier, process_name);
313 XBT_DEBUG("put on %s", mailbox);
314 MSG_task_send(MSG_task_create(reduce_identifier, 0, comm_size, NULL),
318 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
319 free(reduce_identifier);
322 static void action_bcast(const char *const *action)
325 char *bcast_identifier;
327 double comm_size = parse_double(action[2]);
328 m_task_t task = NULL;
329 const char *process_name;
330 double clock = MSG_get_clock();
332 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
334 xbt_assert(communicator_size, "Size of Communicator is not defined, "
335 "can't use collective operations");
337 process_name = MSG_process_get_name(MSG_process_self());
339 bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
341 if (!strcmp(process_name, "p0")) {
342 XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
344 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
346 for (i = 1; i < communicator_size; i++) {
347 sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
349 MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
352 MSG_comm_waitall(comms,communicator_size-1,-1);
353 for (i = 1; i < communicator_size; i++)
354 MSG_comm_destroy(comms[i-1]);
357 XBT_DEBUG("%s: all messages sent by %s have been received",
358 bcast_identifier, process_name);
361 sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
362 MSG_task_receive(&task, mailbox);
363 MSG_task_destroy(task);
364 XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
367 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
368 free(bcast_identifier);
372 static void action_sleep(const char *const *action)
375 const char *duration = action[2];
376 double clock = MSG_get_clock();
378 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
379 name = xbt_str_join_array(action, " ");
381 XBT_DEBUG("Entering %s", name);
382 MSG_process_sleep(parse_double(duration));
383 XBT_VERB("%s %f ", name, MSG_get_clock() - clock);
388 static void action_allReduce(const char *const *action) {
390 char *allreduce_identifier;
392 double comm_size = parse_double(action[2]);
393 double comp_size = parse_double(action[3]);
394 m_task_t task = NULL, comp_task = NULL;
395 const char *process_name;
396 double clock = MSG_get_clock();
398 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
400 xbt_assert(communicator_size, "Size of Communicator is not defined, "
401 "can't use collective operations");
403 process_name = MSG_process_get_name(MSG_process_self());
405 allreduce_identifier = bprintf("allReduce_%d", counters->allReduce_counter++);
407 if (!strcmp(process_name, "p0")) {
408 XBT_DEBUG("%s: %s is the Root", allreduce_identifier, process_name);
410 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
411 m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
412 for (i = 1; i < communicator_size; i++) {
413 sprintf(mailbox, "%s_p%d_p0", allreduce_identifier, i);
414 comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
416 MSG_comm_waitall(comms,communicator_size-1,-1);
417 for (i = 1; i < communicator_size; i++) {
418 MSG_comm_destroy(comms[i-1]);
419 MSG_task_destroy(tasks[i-1]);
423 comp_task = MSG_task_create("allReduce_comp", comp_size, 0, NULL);
424 XBT_DEBUG("%s: computing 'reduce_comp'", allreduce_identifier);
425 MSG_task_execute(comp_task);
426 MSG_task_destroy(comp_task);
427 XBT_DEBUG("%s: computed", allreduce_identifier);
429 for (i = 1; i < communicator_size; i++) {
430 sprintf(mailbox, "%s_p0_p%d", allreduce_identifier, i);
432 MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
435 MSG_comm_waitall(comms,communicator_size-1,-1);
436 for (i = 1; i < communicator_size; i++)
437 MSG_comm_destroy(comms[i-1]);
440 XBT_DEBUG("%s: all messages sent by %s have been received",
441 allreduce_identifier, process_name);
444 XBT_DEBUG("%s: %s sends", allreduce_identifier, process_name);
445 sprintf(mailbox, "%s_%s_p0", allreduce_identifier, process_name);
446 XBT_DEBUG("put on %s", mailbox);
447 MSG_task_send(MSG_task_create(allreduce_identifier, 0, comm_size, NULL),
450 sprintf(mailbox, "%s_p0_%s", allreduce_identifier, process_name);
451 MSG_task_receive(&task, mailbox);
452 MSG_task_destroy(task);
453 XBT_DEBUG("%s: %s has received", allreduce_identifier, process_name);
456 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
457 free(allreduce_identifier);
460 static void action_comm_size(const char *const *action)
463 const char *size = action[2];
464 double clock = MSG_get_clock();
466 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
467 name = xbt_str_join_array(action, " ");
468 communicator_size = parse_double(size);
469 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
473 static void action_compute(const char *const *action)
476 const char *amout = action[2];
477 m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
478 double clock = MSG_get_clock();
480 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
481 name = xbt_str_join_array(action, " ");
482 XBT_DEBUG("Entering %s", name);
483 MSG_task_execute(task);
484 MSG_task_destroy(task);
485 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
489 static void action_init(const char *const *action)
492 TRACE_smpi_init(get_rank(MSG_process_get_name(MSG_process_self())));
494 XBT_DEBUG("Initialize the counters");
495 process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
496 globals->isends = xbt_dynar_new(sizeof(msg_comm_t),NULL);
497 globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t),NULL);
498 globals->tasks = xbt_dynar_new(sizeof(m_task_t),NULL);
499 MSG_process_set_data(MSG_process_self(),globals);
503 static void action_finalize(const char *const *action)
506 TRACE_smpi_finalize(get_rank(MSG_process_get_name(MSG_process_self())));
508 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
510 xbt_dynar_free_container(&(globals->isends));
511 xbt_dynar_free_container(&(globals->irecvs));
512 xbt_dynar_free_container(&(globals->tasks));
518 int main(int argc, char *argv[])
520 MSG_error_t res = MSG_OK;
522 /* Check the given arguments */
523 MSG_global_init(&argc, argv);
525 printf("Usage: %s platform_file deployment_file [action_files]\n",
528 ("example: %s msg_platform.xml msg_deployment.xml actions # if all actions are in the same file\n",
531 ("example: %s msg_platform.xml msg_deployment.xml # if actions are in separate files, specified in deployment\n",
536 /* Simulation setting */
537 MSG_create_environment(argv[1]);
539 /* No need to register functions as in classical MSG programs: the actions get started anyway */
540 MSG_launch_application(argv[2]);
542 /* Action registration */
543 MSG_action_register("init", action_init);
544 MSG_action_register("finalize", action_finalize);
545 MSG_action_register("comm_size",action_comm_size);
546 MSG_action_register("send", action_send);
547 MSG_action_register("Isend", action_Isend);
548 MSG_action_register("recv", action_recv);
549 MSG_action_register("Irecv", action_Irecv);
550 MSG_action_register("wait", action_wait);
551 MSG_action_register("barrier", action_barrier);
552 MSG_action_register("bcast", action_bcast);
553 MSG_action_register("reduce", action_reduce);
554 MSG_action_register("allReduce",action_allReduce);
555 MSG_action_register("sleep", action_sleep);
556 MSG_action_register("compute", action_compute);
559 /* Actually do the simulation using MSG_action_trace_run */
560 res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
562 XBT_INFO("Simulation time %g", MSG_get_clock());