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);
90 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
94 TRACE_smpi_ptp_out(rank, rank, dst_traced, "send");
97 asynchronous_cleanup();
100 static void action_Isend(const char *const *action)
103 const char *size = action[3];
104 double clock = MSG_get_clock();
105 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
108 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),action[2]);
110 MSG_task_isend( MSG_task_create(to,0,parse_double(size),NULL), to);
111 xbt_dynar_push(globals->isends,&comm);
113 XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
114 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
116 asynchronous_cleanup();
120 static void action_recv(const char *const *action)
123 char mailbox_name[250];
124 m_task_t task = NULL;
125 double clock = MSG_get_clock();
127 sprintf(mailbox_name, "%s_%s", action[2],
128 MSG_process_get_name(MSG_process_self()));
130 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
131 name = xbt_str_join_array(action, " ");
134 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
135 int src_traced = get_rank(action[2]);
136 TRACE_smpi_ptp_in(rank, src_traced, rank, "recv");
139 XBT_DEBUG("Receiving: %s", name);
140 MSG_task_receive(&task, mailbox_name);
141 // MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
142 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
143 MSG_task_destroy(task);
145 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
148 TRACE_smpi_ptp_out(rank, src_traced, rank, "recv");
149 TRACE_smpi_recv(rank, src_traced, rank);
152 asynchronous_cleanup();
155 static void action_Irecv(const char *const *action)
158 double clock = MSG_get_clock();
159 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
161 XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
163 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
164 int src_traced = get_rank(action[2]);
165 globals->last_Irecv_sender_id = src_traced;
166 MSG_process_set_data(MSG_process_self(), (void *) globals);
168 TRACE_smpi_ptp_in(rank, src_traced, rank, "Irecv");
171 sprintf(mailbox, "%s_%s", action[2],
172 MSG_process_get_name(MSG_process_self()));
174 xbt_dynar_push(globals->tasks,&t);
177 xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks)-1),
179 xbt_dynar_push(globals->irecvs,&c);
181 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
184 TRACE_smpi_ptp_out(rank, src_traced, rank, "Irecv");
187 asynchronous_cleanup();
191 static void action_wait(const char *const *action)
194 m_task_t task = NULL;
196 double clock = MSG_get_clock();
197 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
199 xbt_assert(xbt_dynar_length(globals->irecvs),
200 "action wait not preceded by any irecv: %s", xbt_str_join_array(action," "));
202 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
203 name = xbt_str_join_array(action, " ");
205 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
206 int src_traced = counters->last_Irecv_sender_id;
207 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
208 TRACE_smpi_ptp_in(rank, src_traced, rank, "wait");
211 XBT_DEBUG("Entering %s", name);
212 comm = xbt_dynar_pop_as(globals->irecvs,msg_comm_t);
213 MSG_comm_wait(comm,-1);
214 task = xbt_dynar_pop_as(globals->tasks,m_task_t);
215 MSG_comm_destroy(comm);
216 MSG_task_destroy(task);
218 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
219 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
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 = SIMIX_req_mutex_init();
241 cond = SIMIX_req_cond_init();
242 processes_arrived_sofar=0;
244 XBT_DEBUG("Entering barrier: %s (%d already there)", name,processes_arrived_sofar);
246 SIMIX_req_mutex_lock(mutex);
247 if (++processes_arrived_sofar == communicator_size) {
248 SIMIX_req_cond_broadcast(cond);
249 SIMIX_req_mutex_unlock(mutex);
251 SIMIX_req_cond_wait(cond,mutex);
252 SIMIX_req_mutex_unlock(mutex);
255 XBT_DEBUG("Exiting barrier: %s", name);
257 processes_arrived_sofar--;
258 if (!processes_arrived_sofar) {
259 SIMIX_req_cond_destroy(cond);
260 SIMIX_req_mutex_destroy(mutex);
264 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
269 static void action_reduce(const char *const *action)
272 char *reduce_identifier;
274 double comm_size = parse_double(action[2]);
275 double comp_size = parse_double(action[3]);
276 m_task_t comp_task = NULL;
277 const char *process_name;
278 double clock = MSG_get_clock();
280 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
282 xbt_assert(communicator_size, "Size of Communicator is not defined, "
283 "can't use collective operations");
285 process_name = MSG_process_get_name(MSG_process_self());
287 reduce_identifier = bprintf("reduce_%d", counters->reduce_counter++);
289 if (!strcmp(process_name, "p0")) {
290 XBT_DEBUG("%s: %s is the Root", reduce_identifier, process_name);
292 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
293 m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
294 for (i = 1; i < communicator_size; i++) {
295 sprintf(mailbox, "%s_p%d_p0", reduce_identifier, i);
296 comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
298 MSG_comm_waitall(comms,communicator_size-1,-1);
299 for (i = 1; i < communicator_size; i++) {
300 MSG_comm_destroy(comms[i-1]);
301 MSG_task_destroy(tasks[i-1]);
305 comp_task = MSG_task_create("reduce_comp", comp_size, 0, NULL);
306 XBT_DEBUG("%s: computing 'reduce_comp'", reduce_identifier);
307 MSG_task_execute(comp_task);
308 MSG_task_destroy(comp_task);
309 XBT_DEBUG("%s: computed", reduce_identifier);
312 XBT_DEBUG("%s: %s sends", reduce_identifier, process_name);
313 sprintf(mailbox, "%s_%s_p0", reduce_identifier, process_name);
314 XBT_DEBUG("put on %s", mailbox);
315 MSG_task_send(MSG_task_create(reduce_identifier, 0, comm_size, NULL),
319 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
320 free(reduce_identifier);
323 static void action_bcast(const char *const *action)
326 char *bcast_identifier;
328 double comm_size = parse_double(action[2]);
329 m_task_t task = NULL;
330 const char *process_name;
331 double clock = MSG_get_clock();
333 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
335 xbt_assert(communicator_size, "Size of Communicator is not defined, "
336 "can't use collective operations");
338 process_name = MSG_process_get_name(MSG_process_self());
340 bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
342 if (!strcmp(process_name, "p0")) {
343 XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
345 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
347 for (i = 1; i < communicator_size; i++) {
348 sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
350 MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
353 MSG_comm_waitall(comms,communicator_size-1,-1);
354 for (i = 1; i < communicator_size; i++)
355 MSG_comm_destroy(comms[i-1]);
358 XBT_DEBUG("%s: all messages sent by %s have been received",
359 bcast_identifier, process_name);
362 sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
363 MSG_task_receive(&task, mailbox);
364 MSG_task_destroy(task);
365 XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
368 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
369 free(bcast_identifier);
373 static void action_sleep(const char *const *action)
376 const char *duration = action[2];
377 double clock = MSG_get_clock();
379 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
380 name = xbt_str_join_array(action, " ");
382 XBT_DEBUG("Entering %s", name);
383 MSG_process_sleep(parse_double(duration));
384 XBT_VERB("%s %f ", name, MSG_get_clock() - clock);
386 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
390 static void action_allReduce(const char *const *action) {
392 char *allreduce_identifier;
394 double comm_size = parse_double(action[2]);
395 double comp_size = parse_double(action[3]);
396 m_task_t task = NULL, comp_task = NULL;
397 const char *process_name;
398 double clock = MSG_get_clock();
400 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
402 xbt_assert(communicator_size, "Size of Communicator is not defined, "
403 "can't use collective operations");
405 process_name = MSG_process_get_name(MSG_process_self());
407 allreduce_identifier = bprintf("allReduce_%d", counters->allReduce_counter++);
409 if (!strcmp(process_name, "p0")) {
410 XBT_DEBUG("%s: %s is the Root", allreduce_identifier, process_name);
412 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
413 m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
414 for (i = 1; i < communicator_size; i++) {
415 sprintf(mailbox, "%s_p%d_p0", allreduce_identifier, i);
416 comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
418 MSG_comm_waitall(comms,communicator_size-1,-1);
419 for (i = 1; i < communicator_size; i++) {
420 MSG_comm_destroy(comms[i-1]);
421 MSG_task_destroy(tasks[i-1]);
425 comp_task = MSG_task_create("allReduce_comp", comp_size, 0, NULL);
426 XBT_DEBUG("%s: computing 'reduce_comp'", allreduce_identifier);
427 MSG_task_execute(comp_task);
428 MSG_task_destroy(comp_task);
429 XBT_DEBUG("%s: computed", allreduce_identifier);
431 for (i = 1; i < communicator_size; i++) {
432 sprintf(mailbox, "%s_p0_p%d", allreduce_identifier, i);
434 MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
437 MSG_comm_waitall(comms,communicator_size-1,-1);
438 for (i = 1; i < communicator_size; i++)
439 MSG_comm_destroy(comms[i-1]);
442 XBT_DEBUG("%s: all messages sent by %s have been received",
443 allreduce_identifier, process_name);
446 XBT_DEBUG("%s: %s sends", allreduce_identifier, process_name);
447 sprintf(mailbox, "%s_%s_p0", allreduce_identifier, process_name);
448 XBT_DEBUG("put on %s", mailbox);
449 MSG_task_send(MSG_task_create(allreduce_identifier, 0, comm_size, NULL),
452 sprintf(mailbox, "%s_p0_%s", allreduce_identifier, process_name);
453 MSG_task_receive(&task, mailbox);
454 MSG_task_destroy(task);
455 XBT_DEBUG("%s: %s has received", allreduce_identifier, process_name);
458 XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
459 free(allreduce_identifier);
462 static void action_comm_size(const char *const *action)
465 const char *size = action[2];
466 double clock = MSG_get_clock();
468 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
469 name = xbt_str_join_array(action, " ");
470 communicator_size = parse_double(size);
471 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
472 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
476 static void action_compute(const char *const *action)
479 const char *amout = action[2];
480 m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
481 double clock = MSG_get_clock();
483 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
484 name = xbt_str_join_array(action, " ");
485 XBT_DEBUG("Entering %s", name);
486 MSG_task_execute(task);
487 MSG_task_destroy(task);
488 XBT_VERB("%s %f", name, MSG_get_clock() - clock);
489 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
493 static void action_init(const char *const *action)
496 TRACE_smpi_init(get_rank(MSG_process_get_name(MSG_process_self())));
498 XBT_DEBUG("Initialize the counters");
499 process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
500 globals->isends = xbt_dynar_new(sizeof(msg_comm_t),NULL);
501 globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t),NULL);
502 globals->tasks = xbt_dynar_new(sizeof(m_task_t),NULL);
503 MSG_process_set_data(MSG_process_self(),globals);
507 static void action_finalize(const char *const *action)
510 TRACE_smpi_finalize(get_rank(MSG_process_get_name(MSG_process_self())));
512 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
514 xbt_dynar_free_container(&(globals->isends));
515 xbt_dynar_free_container(&(globals->irecvs));
516 xbt_dynar_free_container(&(globals->tasks));
522 int main(int argc, char *argv[])
524 MSG_error_t res = MSG_OK;
526 /* Check the given arguments */
527 MSG_global_init(&argc, argv);
529 printf("Usage: %s platform_file deployment_file [action_files]\n",
532 ("example: %s msg_platform.xml msg_deployment.xml actions # if all actions are in the same file\n",
535 ("example: %s msg_platform.xml msg_deployment.xml # if actions are in separate files, specified in deployment\n",
540 /* Simulation setting */
541 MSG_create_environment(argv[1]);
543 /* No need to register functions as in classical MSG programs: the actions get started anyway */
544 MSG_launch_application(argv[2]);
546 /* Action registration */
547 MSG_action_register("init", action_init);
548 MSG_action_register("finalize", action_finalize);
549 MSG_action_register("comm_size",action_comm_size);
550 MSG_action_register("send", action_send);
551 MSG_action_register("Isend", action_Isend);
552 MSG_action_register("recv", action_recv);
553 MSG_action_register("Irecv", action_Irecv);
554 MSG_action_register("wait", action_wait);
555 MSG_action_register("barrier", action_barrier);
556 MSG_action_register("bcast", action_bcast);
557 MSG_action_register("reduce", action_reduce);
558 MSG_action_register("allReduce",action_allReduce);
559 MSG_action_register("sleep", action_sleep);
560 MSG_action_register("compute", action_compute);
563 /* Actually do the simulation using MSG_action_trace_run */
564 res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
566 XBT_INFO("Simulation time %g", MSG_get_clock());