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 "simgrid_config.h" /* getline */
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(xbt_dynar_t 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 THROW1(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(xbt_dynar_t action)
66 char *size_str = xbt_dynar_get_as(action, 3, char *);
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()),
71 xbt_dynar_get_as(action, 2, char *));
72 // char *to = xbt_dynar_get_as(action, 2, char *);
74 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
75 name = xbt_str_join(action, " ");
78 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
79 int dst_traced = get_rank(xbt_dynar_get_as(action, 2, char *));
80 TRACE_smpi_ptp_in(rank, rank, dst_traced, "send");
81 TRACE_smpi_send(rank, rank, dst_traced);
84 DEBUG2("Entering Send: %s (size: %lg)", name, size);
88 MSG_task_send(MSG_task_create(name, 0, size, NULL), to);
91 VERB2("%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(xbt_dynar_t action)
106 // char *to = xbt_dynar_get_as(action, 2, char *);
107 char *size = xbt_dynar_get_as(action, 3, char *);
108 double clock = MSG_get_clock();
109 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
112 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),
113 xbt_dynar_get_as(action, 2, char *));
116 MSG_task_isend( MSG_task_create(to,0,parse_double(size),NULL), to);
117 xbt_dynar_push(globals->isends,&comm);
119 DEBUG1("Isend on %s", MSG_process_get_name(MSG_process_self()));
120 VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
122 asynchronous_cleanup();
126 static void action_recv(xbt_dynar_t action)
129 char mailbox_name[250];
130 m_task_t task = NULL;
131 double clock = MSG_get_clock();
133 sprintf(mailbox_name, "%s_%s", xbt_dynar_get_as(action, 2, char *),
134 MSG_process_get_name(MSG_process_self()));
136 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
137 name = xbt_str_join(action, " ");
140 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
141 int src_traced = get_rank(xbt_dynar_get_as(action, 2, char *));
142 TRACE_smpi_ptp_in(rank, src_traced, rank, "recv");
145 DEBUG1("Receiving: %s", name);
146 MSG_task_receive(&task, mailbox_name);
147 // MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
148 VERB2("%s %f", name, MSG_get_clock() - clock);
149 MSG_task_destroy(task);
151 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
154 TRACE_smpi_ptp_out(rank, src_traced, rank, "recv");
155 TRACE_smpi_recv(rank, src_traced, rank);
158 asynchronous_cleanup();
161 static void action_Irecv(xbt_dynar_t action)
164 double clock = MSG_get_clock();
165 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
167 DEBUG1("Irecv on %s", MSG_process_get_name(MSG_process_self()));
169 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
170 int src_traced = get_rank(xbt_dynar_get_as(action, 2, char *));
171 globals->last_Irecv_sender_id = src_traced;
172 MSG_process_set_data(MSG_process_self(), (void *) globals);
174 TRACE_smpi_ptp_in(rank, src_traced, rank, "Irecv");
177 sprintf(mailbox, "%s_%s", xbt_dynar_get_as(action, 2, char *),
178 MSG_process_get_name(MSG_process_self()));
180 xbt_dynar_push(globals->tasks,&t);
183 xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks)-1),
185 xbt_dynar_push(globals->irecvs,&c);
187 VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
190 TRACE_smpi_ptp_out(rank, src_traced, rank, "Irecv");
193 asynchronous_cleanup();
197 static void action_wait(xbt_dynar_t action)
200 m_task_t task = NULL;
202 double clock = MSG_get_clock();
203 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
205 xbt_assert1(xbt_dynar_length(globals->irecvs),
206 "action wait not preceeded by any irecv: %s", xbt_str_join(action," "));
208 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
209 name = xbt_str_join(action, " ");
211 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
212 int src_traced = counters->last_Irecv_sender_id;
213 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
214 TRACE_smpi_ptp_in(rank, src_traced, rank, "wait");
217 DEBUG1("Entering %s", name);
218 comm = xbt_dynar_pop_as(globals->irecvs,msg_comm_t);
219 MSG_comm_wait(comm,-1);
220 task = xbt_dynar_pop_as(globals->tasks,m_task_t);
221 MSG_comm_destroy(comm);
222 MSG_task_destroy(task);
224 VERB2("%s %f", name, MSG_get_clock() - clock);
225 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
228 TRACE_smpi_ptp_out(rank, src_traced, rank, "wait");
229 TRACE_smpi_recv(rank, src_traced, rank);
234 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
235 static void action_barrier(xbt_dynar_t action)
238 static smx_mutex_t mutex = NULL;
239 static smx_cond_t cond = NULL;
240 static int processes_arrived_sofar=0;
242 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
243 name = xbt_str_join(action, " ");
245 if (mutex == NULL) { // first arriving on the barrier
246 mutex = SIMIX_req_mutex_init();
247 cond = SIMIX_req_cond_init();
248 processes_arrived_sofar=0;
250 DEBUG2("Entering barrier: %s (%d already there)", name,processes_arrived_sofar);
252 SIMIX_req_mutex_lock(mutex);
253 if (++processes_arrived_sofar == communicator_size) {
254 SIMIX_req_cond_broadcast(cond);
255 SIMIX_req_mutex_unlock(mutex);
257 SIMIX_req_cond_wait(cond,mutex);
258 SIMIX_req_mutex_unlock(mutex);
261 DEBUG1("Exiting barrier: %s", name);
263 processes_arrived_sofar--;
264 if (!processes_arrived_sofar) {
265 SIMIX_req_cond_destroy(cond);
266 SIMIX_req_mutex_destroy(mutex);
270 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
275 static void action_reduce(xbt_dynar_t action)
278 char *reduce_identifier;
280 double comm_size = parse_double(xbt_dynar_get_as(action, 2, char *));
281 double comp_size = parse_double(xbt_dynar_get_as(action, 3, char *));
282 m_task_t comp_task = NULL;
283 const char *process_name;
284 double clock = MSG_get_clock();
286 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
288 xbt_assert0(communicator_size, "Size of Communicator is not defined, "
289 "can't use collective operations");
291 process_name = MSG_process_get_name(MSG_process_self());
293 reduce_identifier = bprintf("reduce_%d", counters->reduce_counter++);
295 if (!strcmp(process_name, "p0")) {
296 DEBUG2("%s: %s is the Root", reduce_identifier, process_name);
298 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
299 m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
300 for (i = 1; i < communicator_size; i++) {
301 sprintf(mailbox, "%s_p%d_p0", reduce_identifier, i);
302 comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
304 MSG_comm_waitall(comms,communicator_size-1,-1);
305 for (i = 1; i < communicator_size; i++) {
306 MSG_comm_destroy(comms[i-1]);
307 MSG_task_destroy(tasks[i-1]);
311 comp_task = MSG_task_create("reduce_comp", comp_size, 0, NULL);
312 DEBUG1("%s: computing 'reduce_comp'", reduce_identifier);
313 MSG_task_execute(comp_task);
314 MSG_task_destroy(comp_task);
315 DEBUG1("%s: computed", reduce_identifier);
318 DEBUG2("%s: %s sends", reduce_identifier, process_name);
319 sprintf(mailbox, "%s_%s_p0", reduce_identifier, process_name);
320 DEBUG1("put on %s", mailbox);
321 MSG_task_send(MSG_task_create(reduce_identifier, 0, comm_size, NULL),
325 VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
326 free(reduce_identifier);
329 static void action_bcast(xbt_dynar_t action)
332 char *bcast_identifier;
334 double comm_size = parse_double(xbt_dynar_get_as(action, 2, char *));
335 m_task_t task = NULL;
336 const char *process_name;
337 double clock = MSG_get_clock();
339 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
341 xbt_assert0(communicator_size, "Size of Communicator is not defined, "
342 "can't use collective operations");
344 process_name = MSG_process_get_name(MSG_process_self());
346 bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
348 if (!strcmp(process_name, "p0")) {
349 DEBUG2("%s: %s is the Root", bcast_identifier, process_name);
351 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
353 for (i = 1; i < communicator_size; i++) {
354 sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
356 MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
359 MSG_comm_waitall(comms,communicator_size-1,-1);
360 for (i = 1; i < communicator_size; i++)
361 MSG_comm_destroy(comms[i-1]);
364 DEBUG2("%s: all messages sent by %s have been received",
365 bcast_identifier, process_name);
368 sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
369 MSG_task_receive(&task, mailbox);
370 MSG_task_destroy(task);
371 DEBUG2("%s: %s has received", bcast_identifier, process_name);
374 VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
375 free(bcast_identifier);
379 static void action_sleep(xbt_dynar_t action)
382 char *duration = xbt_dynar_get_as(action, 2, char *);
383 double clock = MSG_get_clock();
385 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
386 name = xbt_str_join(action, " ");
388 DEBUG1("Entering %s", name);
389 MSG_process_sleep(parse_double(duration));
390 VERB2("%s %f ", name, MSG_get_clock() - clock);
392 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
396 static void action_allReduce(xbt_dynar_t action) {
398 char *allreduce_identifier;
400 double comm_size = parse_double(xbt_dynar_get_as(action, 2, char *));
401 double comp_size = parse_double(xbt_dynar_get_as(action, 3, char *));
402 m_task_t task = NULL, comp_task = NULL;
403 const char *process_name;
404 double clock = MSG_get_clock();
406 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
408 xbt_assert0(communicator_size, "Size of Communicator is not defined, "
409 "can't use collective operations");
411 process_name = MSG_process_get_name(MSG_process_self());
413 allreduce_identifier = bprintf("allReduce_%d", counters->allReduce_counter++);
415 if (!strcmp(process_name, "p0")) {
416 DEBUG2("%s: %s is the Root", allreduce_identifier, process_name);
418 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
419 m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
420 for (i = 1; i < communicator_size; i++) {
421 sprintf(mailbox, "%s_p%d_p0", allreduce_identifier, i);
422 comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
424 MSG_comm_waitall(comms,communicator_size-1,-1);
425 for (i = 1; i < communicator_size; i++) {
426 MSG_comm_destroy(comms[i-1]);
427 MSG_task_destroy(tasks[i-1]);
431 comp_task = MSG_task_create("allReduce_comp", comp_size, 0, NULL);
432 DEBUG1("%s: computing 'reduce_comp'", allreduce_identifier);
433 MSG_task_execute(comp_task);
434 MSG_task_destroy(comp_task);
435 DEBUG1("%s: computed", allreduce_identifier);
437 for (i = 1; i < communicator_size; i++) {
438 sprintf(mailbox, "%s_p0_p%d", allreduce_identifier, i);
440 MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
443 MSG_comm_waitall(comms,communicator_size-1,-1);
444 for (i = 1; i < communicator_size; i++)
445 MSG_comm_destroy(comms[i-1]);
448 DEBUG2("%s: all messages sent by %s have been received",
449 allreduce_identifier, process_name);
452 DEBUG2("%s: %s sends", allreduce_identifier, process_name);
453 sprintf(mailbox, "%s_%s_p0", allreduce_identifier, process_name);
454 DEBUG1("put on %s", mailbox);
455 MSG_task_send(MSG_task_create(allreduce_identifier, 0, comm_size, NULL),
458 sprintf(mailbox, "%s_p0_%s", allreduce_identifier, process_name);
459 MSG_task_receive(&task, mailbox);
460 MSG_task_destroy(task);
461 DEBUG2("%s: %s has received", allreduce_identifier, process_name);
464 VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
465 free(allreduce_identifier);
468 static void action_comm_size(xbt_dynar_t action)
471 char *size = xbt_dynar_get_as(action, 2, char *);
472 double clock = MSG_get_clock();
474 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
475 name = xbt_str_join(action, " ");
476 communicator_size = parse_double(size);
477 VERB2("%s %f", name, MSG_get_clock() - clock);
478 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
482 static void action_compute(xbt_dynar_t action)
485 char *amout = xbt_dynar_get_as(action, 2, char *);
486 m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
487 double clock = MSG_get_clock();
489 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
490 name = xbt_str_join(action, " ");
491 DEBUG1("Entering %s", name);
492 MSG_task_execute(task);
493 MSG_task_destroy(task);
494 VERB2("%s %f", name, MSG_get_clock() - clock);
495 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
499 static void action_init(xbt_dynar_t action)
502 TRACE_smpi_init(get_rank(MSG_process_get_name(MSG_process_self())));
504 DEBUG0("Initialize the counters");
505 process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
506 globals->isends = xbt_dynar_new(sizeof(msg_comm_t),NULL);
507 globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t),NULL);
508 globals->tasks = xbt_dynar_new(sizeof(m_task_t),NULL);
509 MSG_process_set_data(MSG_process_self(),globals);
513 static void action_finalize(xbt_dynar_t action)
516 TRACE_smpi_finalize(get_rank(MSG_process_get_name(MSG_process_self())));
518 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
520 xbt_dynar_free_container(&(globals->isends));
521 xbt_dynar_free_container(&(globals->irecvs));
522 xbt_dynar_free_container(&(globals->tasks));
528 int main(int argc, char *argv[])
530 MSG_error_t res = MSG_OK;
532 /* Check the given arguments */
533 MSG_global_init(&argc, argv);
535 printf("Usage: %s platform_file deployment_file [action_files]\n",
538 ("example: %s msg_platform.xml msg_deployment.xml actions # if all actions are in the same file\n",
541 ("example: %s msg_platform.xml msg_deployment.xml # if actions are in separate files, specified in deployment\n",
546 /* Simulation setting */
547 MSG_create_environment(argv[1]);
549 /* No need to register functions as in classical MSG programs: the actions get started anyway */
550 MSG_launch_application(argv[2]);
552 /* Action registration */
553 MSG_action_register("init", action_init);
554 MSG_action_register("finalize", action_finalize);
555 MSG_action_register("comm_size",action_comm_size);
556 MSG_action_register("send", action_send);
557 MSG_action_register("Isend", action_Isend);
558 MSG_action_register("recv", action_recv);
559 MSG_action_register("Irecv", action_Irecv);
560 MSG_action_register("wait", action_wait);
561 MSG_action_register("barrier", action_barrier);
562 MSG_action_register("bcast", action_bcast);
563 MSG_action_register("reduce", action_reduce);
564 MSG_action_register("allReduce",action_allReduce);
565 MSG_action_register("sleep", action_sleep);
566 MSG_action_register("compute", action_compute);
569 /* Actually do the simulation using MSG_action_trace_run */
570 res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
572 INFO1("Simulation time %g", MSG_get_clock());