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;
27 } s_process_globals_t, *process_globals_t;
30 static double parse_double(const char *string)
35 value = strtod(string, &endptr);
37 THROW1(unknown_error, 0, "%s is not a double", string);
41 static int get_rank (const char *process_name)
43 return atoi(&(process_name[1]));
46 static void asynchronous_cleanup(void) {
47 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
49 /* Destroy any isend which correspond to completed communications */
52 while ((found = MSG_comm_testany(globals->isends)) != -1) {
53 xbt_dynar_remove_at(globals->isends,found,&comm);
54 MSG_comm_destroy(comm);
59 static int spawned_send(int argc, char *argv[])
61 DEBUG3("%s: Sending %s on %s", MSG_process_get_name(MSG_process_self()),
63 MSG_task_send(MSG_task_create(argv[0], 0, parse_double(argv[1]), NULL),
68 static void action_send(xbt_dynar_t action)
72 char *size_str = xbt_dynar_get_as(action, 3, char *);
73 double size=parse_double(size_str);
74 double clock = MSG_get_clock(); /* this "call" is free thanks to inlining */
76 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),
77 xbt_dynar_get_as(action, 2, char *));
78 // char *to = xbt_dynar_get_as(action, 2, char *);
80 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
81 name = xbt_str_join(action, " ");
84 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
85 int dst_traced = get_rank(xbt_dynar_get_as(action, 2, char *));
86 TRACE_smpi_ptp_in(rank, rank, dst_traced, "send");
87 TRACE_smpi_send(rank, rank, dst_traced);
90 DEBUG2("Entering Send: %s (size: %lg)", name, size);
94 MSG_task_send(MSG_task_create(name, 0, size, NULL), to);
97 VERB2("%s %f", name, MSG_get_clock() - clock);
99 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
103 TRACE_smpi_ptp_out(rank, rank, dst_traced, "send");
106 asynchronous_cleanup();
109 static void action_Isend(xbt_dynar_t action)
112 // char *to = xbt_dynar_get_as(action, 2, char *);
113 char *size = xbt_dynar_get_as(action, 3, char *);
114 double clock = MSG_get_clock();
115 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
118 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),
119 xbt_dynar_get_as(action, 2, char *));
122 MSG_task_isend( MSG_task_create(to,0,parse_double(size),NULL), to);
123 xbt_dynar_push(globals->isends,&comm);
125 DEBUG1("Isend on %s", MSG_process_get_name(MSG_process_self()));
126 VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
128 asynchronous_cleanup();
132 static void action_recv(xbt_dynar_t action)
135 char mailbox_name[250];
136 m_task_t task = NULL;
137 double clock = MSG_get_clock();
138 //FIXME: argument of action ignored so far; semantic not clear
139 //char *from=xbt_dynar_get_as(action,2,char*);
140 sprintf(mailbox_name, "%s_%s", xbt_dynar_get_as(action, 2, char *),
141 MSG_process_get_name(MSG_process_self()));
143 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
144 name = xbt_str_join(action, " ");
147 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
148 int src_traced = get_rank(xbt_dynar_get_as(action, 2, char *));
149 TRACE_smpi_ptp_in(rank, src_traced, rank, "recv");
152 DEBUG1("Receiving: %s", name);
153 MSG_task_receive(&task, mailbox_name);
154 // MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
155 VERB2("%s %f", name, MSG_get_clock() - clock);
156 MSG_task_destroy(task);
158 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
161 TRACE_smpi_ptp_out(rank, src_traced, rank, "recv");
162 TRACE_smpi_recv(rank, src_traced, rank);
165 asynchronous_cleanup();
168 static int spawned_recv(int argc, char *argv[])
170 m_task_t task = NULL;
171 DEBUG1("Receiving on %s", argv[0]);
172 MSG_task_receive(&task, argv[0]);
173 DEBUG1("Received %s", MSG_task_get_name(task));
174 DEBUG1("waiter on %s", MSG_process_get_name(MSG_process_self()));
175 MSG_task_send(MSG_task_create("waiter", 0, 0, NULL),
176 MSG_process_get_name(MSG_process_self()));
178 MSG_task_destroy(task);
183 static void action_Irecv(xbt_dynar_t action)
186 m_process_t comm_helper;
187 char mailbox_name[250];
189 double clock = MSG_get_clock();
191 DEBUG1("Irecv on %s: spawn process ",
192 MSG_process_get_name(MSG_process_self()));
194 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
195 int src_traced = get_rank(xbt_dynar_get_as(action, 2, char *));
196 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
197 counters->last_Irecv_sender_id = src_traced;
198 MSG_process_set_data(MSG_process_self(), (void *) counters);
200 TRACE_smpi_ptp_in(rank, src_traced, rank, "Irecv");
203 sprintf(mailbox_name, "%s_%s", xbt_dynar_get_as(action, 2, char *),
204 MSG_process_get_name(MSG_process_self()));
205 name = bprintf("%s_wait", MSG_process_get_name(MSG_process_self()));
206 myargv = (char **) calloc(2, sizeof(char *));
208 myargv[0] = xbt_strdup(mailbox_name);
210 comm_helper = MSG_process_create_with_arguments(name, spawned_recv,
211 NULL, MSG_host_self(),
214 VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
218 TRACE_smpi_ptp_out(rank, src_traced, rank, "Irecv");
221 asynchronous_cleanup();
225 static void action_wait(xbt_dynar_t action)
229 m_task_t task = NULL;
230 double clock = MSG_get_clock();
232 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
233 name = xbt_str_join(action, " ");
235 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
236 int src_traced = counters->last_Irecv_sender_id;
237 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
238 TRACE_smpi_ptp_in(rank, src_traced, rank, "wait");
241 DEBUG1("Entering %s", name);
242 sprintf(task_name, "%s_wait", MSG_process_get_name(MSG_process_self()));
243 DEBUG1("wait: %s", task_name);
244 MSG_task_receive(&task, task_name);
245 MSG_task_destroy(task);
246 VERB2("%s %f", name, MSG_get_clock() - clock);
247 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
250 TRACE_smpi_ptp_out(rank, src_traced, rank, "wait");
251 TRACE_smpi_recv(rank, src_traced, rank);
256 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
257 static void action_barrier(xbt_dynar_t action)
260 static smx_mutex_t mutex = NULL;
261 static smx_cond_t cond = NULL;
262 static int processes_arrived_sofar=0;
264 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
265 name = xbt_str_join(action, " ");
267 if (mutex == NULL) { // first arriving on the barrier
268 mutex = SIMIX_req_mutex_init();
269 cond = SIMIX_req_cond_init();
270 processes_arrived_sofar=0;
272 DEBUG2("Entering barrier: %s (%d already there)", name,processes_arrived_sofar);
274 SIMIX_req_mutex_lock(mutex);
275 if (++processes_arrived_sofar == communicator_size) {
276 SIMIX_req_cond_broadcast(cond);
277 SIMIX_req_mutex_unlock(mutex);
279 SIMIX_req_cond_wait(cond,mutex);
280 SIMIX_req_mutex_unlock(mutex);
283 DEBUG1("Exiting barrier: %s", name);
285 processes_arrived_sofar--;
286 if (!processes_arrived_sofar) {
287 SIMIX_req_cond_destroy(cond);
288 SIMIX_req_mutex_destroy(mutex);
292 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
297 static void action_reduce(xbt_dynar_t action)
304 char *comm_size = xbt_dynar_get_as(action, 2, char *);
305 char *comp_size = xbt_dynar_get_as(action, 3, char *);
306 m_process_t comm_helper = NULL;
307 m_task_t task = NULL, comp_task = NULL;
308 const char *process_name;
309 double clock = MSG_get_clock();
311 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
313 xbt_assert0(communicator_size, "Size of Communicator is not defined"
314 ", can't use collective operations");
316 process_name = MSG_process_get_name(MSG_process_self());
318 name = bprintf("reduce_%d", counters->reduce_counter++);
320 if (!strcmp(process_name, "p0")) {
321 DEBUG2("%s: %s is the Root", name, process_name);
322 for (i = 1; i < communicator_size; i++) {
323 sprintf(spawn_name, "%s_p%d_%s", name, i,
324 MSG_process_get_name(MSG_process_self()));
325 sprintf(task_name, "%s_wait", spawn_name);
326 myargv = (char **) calloc(2, sizeof(char *));
328 myargv[0] = xbt_strdup(spawn_name);
332 MSG_process_create_with_arguments(task_name, spawned_recv,
333 NULL, MSG_host_self(),
337 for (i = 1; i < communicator_size; i++) {
338 sprintf(task_name, "%s_p%d_p0_wait", name, i);
339 MSG_task_receive(&task, task_name);
340 MSG_task_destroy(task);
345 MSG_task_create("reduce_comp", parse_double(comp_size), 0, NULL);
346 DEBUG1("%s: computing 'reduce_comp'", name);
347 MSG_task_execute(comp_task);
348 MSG_task_destroy(comp_task);
349 DEBUG1("%s: computed", name);
351 DEBUG2("%s: %s sends", name, process_name);
352 sprintf(task_name, "%s_%s_p0", name, process_name);
353 DEBUG1("put on %s", task_name);
354 MSG_task_send(MSG_task_create(name, 0, parse_double(comm_size), NULL),
358 MSG_process_set_data(MSG_process_self(), (void *) counters);
359 VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
363 static void action_bcast(xbt_dynar_t action)
367 const char *process_name;
371 m_process_t comm_helper = NULL;
372 m_task_t task = NULL;
373 char *size = xbt_dynar_get_as(action, 2, char *);
374 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
375 double clock = MSG_get_clock();
377 xbt_assert0(communicator_size, "Size of Communicator is not defined"
378 ", can't use collective operations");
381 process_name = MSG_process_get_name(MSG_process_self());
383 name = bprintf("bcast_%d", counters->bcast_counter++);
384 if (!strcmp(process_name, "p0")) {
385 DEBUG2("%s: %s is the Root", name, process_name);
387 for (i = 1; i < communicator_size; i++) {
388 myargv = (char **) calloc(3, sizeof(char *));
389 myargv[0] = xbt_strdup(name);
390 myargv[1] = xbt_strdup(size);
393 sprintf(spawn_name, "%s_%d", myargv[0], i);
395 MSG_process_create_with_arguments(spawn_name, spawned_send,
396 NULL, MSG_host_self(), 2,
400 for (i = 1; i < communicator_size; i++) {
401 sprintf(task_name, "p%d_wait", i);
402 DEBUG1("get on %s", task_name);
403 MSG_task_receive(&task, task_name);
404 MSG_task_destroy(task);
407 DEBUG2("%s: all messages sent by %s have been received",
410 DEBUG2("%s: %s receives", name, process_name);
411 MSG_task_receive(&task, name);
412 MSG_task_destroy(task);
413 DEBUG2("%s: %s has received", name, process_name);
414 sprintf(task_name, "%s_wait", process_name);
415 DEBUG1("put on %s", task_name);
416 MSG_task_send(MSG_task_create("waiter", 0, 0, NULL), task_name);
419 MSG_process_set_data(MSG_process_self(), (void *) counters);
420 VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
425 static void action_sleep(xbt_dynar_t action)
428 char *duration = xbt_dynar_get_as(action, 2, char *);
429 double clock = MSG_get_clock();
431 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
432 name = xbt_str_join(action, " ");
434 DEBUG1("Entering %s", name);
435 MSG_process_sleep(parse_double(duration));
436 VERB2("%s %f ", name, MSG_get_clock() - clock);
438 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
442 static void action_allReduce(xbt_dynar_t action) {
444 char *allreduce_identifier;
446 double comm_size = parse_double(xbt_dynar_get_as(action, 2, char *));
447 double comp_size = parse_double(xbt_dynar_get_as(action, 3, char *));
448 m_task_t task = NULL, comp_task = NULL;
449 const char *process_name;
450 double clock = MSG_get_clock();
452 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
454 xbt_assert0(communicator_size, "Size of Communicator is not defined, "
455 "can't use collective operations");
457 process_name = MSG_process_get_name(MSG_process_self());
459 allreduce_identifier = bprintf("allReduce_%d", counters->allReduce_counter++);
461 if (!strcmp(process_name, "p0")) {
462 DEBUG2("%s: %s is the Root", allreduce_identifier, process_name);
464 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
465 m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
466 for (i = 1; i < communicator_size; i++) {
467 sprintf(mailbox, "%s_p%d_p0", allreduce_identifier, i);
468 comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
470 MSG_comm_waitall(comms,communicator_size-1,-1);
471 for (i = 1; i < communicator_size; i++) {
472 MSG_comm_destroy(comms[i-1]);
473 MSG_task_destroy(tasks[i-1]);
477 comp_task = MSG_task_create("allReduce_comp", comp_size, 0, NULL);
478 DEBUG1("%s: computing 'reduce_comp'", allreduce_identifier);
479 MSG_task_execute(comp_task);
480 MSG_task_destroy(comp_task);
481 DEBUG1("%s: computed", allreduce_identifier);
483 for (i = 1; i < communicator_size; i++) {
484 sprintf(mailbox, "%s_p0_p%d", allreduce_identifier, i);
486 MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
489 MSG_comm_waitall(comms,communicator_size-1,-1);
490 /* for (i = 1; i < communicator_size; i++) */
491 MSG_comm_destroy(comms[i-1]);
494 DEBUG2("%s: all messages sent by %s have been received",
495 allreduce_identifier, process_name);
498 DEBUG2("%s: %s sends", allreduce_identifier, process_name);
499 sprintf(mailbox, "%s_%s_p0", allreduce_identifier, process_name);
500 DEBUG1("put on %s", mailbox);
501 MSG_task_send(MSG_task_create(allreduce_identifier, 0, comm_size, NULL),
504 sprintf(mailbox, "%s_p0_%s", allreduce_identifier, process_name);
505 MSG_task_receive(&task, mailbox);
506 MSG_task_destroy(task);
507 DEBUG2("%s: %s has received", allreduce_identifier, process_name);
510 VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
511 free(allreduce_identifier);
514 static void action_comm_size(xbt_dynar_t action)
517 char *size = xbt_dynar_get_as(action, 2, char *);
518 double clock = MSG_get_clock();
520 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
521 name = xbt_str_join(action, " ");
522 communicator_size = parse_double(size);
523 VERB2("%s %f", name, MSG_get_clock() - clock);
524 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
528 static void action_compute(xbt_dynar_t action)
531 char *amout = xbt_dynar_get_as(action, 2, char *);
532 m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
533 double clock = MSG_get_clock();
535 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
536 name = xbt_str_join(action, " ");
537 DEBUG1("Entering %s", name);
538 MSG_task_execute(task);
539 MSG_task_destroy(task);
540 VERB2("%s %f", name, MSG_get_clock() - clock);
541 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
545 static void action_init(xbt_dynar_t action)
548 TRACE_smpi_init(get_rank(MSG_process_get_name(MSG_process_self())));
550 DEBUG0("Initialize the counters");
551 process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
552 globals->isends = xbt_dynar_new(sizeof(msg_comm_t),NULL);
553 MSG_process_set_data(MSG_process_self(),globals);
557 static void action_finalize(xbt_dynar_t action)
560 TRACE_smpi_finalize(get_rank(MSG_process_get_name(MSG_process_self())));
562 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
564 xbt_dynar_free_container(&(globals->isends));
570 int main(int argc, char *argv[])
572 MSG_error_t res = MSG_OK;
574 /* Check the given arguments */
575 MSG_global_init(&argc, argv);
577 printf("Usage: %s platform_file deployment_file [action_files]\n",
580 ("example: %s msg_platform.xml msg_deployment.xml actions # if all actions are in the same file\n",
583 ("example: %s msg_platform.xml msg_deployment.xml # if actions are in separate files, specified in deployment\n",
588 /* Simulation setting */
589 MSG_create_environment(argv[1]);
591 /* No need to register functions as in classical MSG programs: the actions get started anyway */
592 MSG_launch_application(argv[2]);
594 /* Action registration */
595 MSG_action_register("init", action_init);
596 MSG_action_register("finalize", action_finalize);
597 MSG_action_register("comm_size",action_comm_size);
598 MSG_action_register("send", action_send);
599 MSG_action_register("Isend", action_Isend);
600 MSG_action_register("recv", action_recv);
601 MSG_action_register("Irecv", action_Irecv);
602 MSG_action_register("wait", action_wait);
603 MSG_action_register("barrier", action_barrier);
604 MSG_action_register("bcast", action_bcast);
605 MSG_action_register("reduce", action_reduce);
606 MSG_action_register("allReduce",action_allReduce);
607 MSG_action_register("sleep", action_sleep);
608 MSG_action_register("compute", action_compute);
611 /* Actually do the simulation using MSG_action_trace_run */
612 res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
614 INFO1("Simulation time %g", MSG_get_clock());