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;
20 int last_Irecv_sender_id;
23 int allReduce_counter;
24 } s_process_globals_t, *process_globals_t;
27 static double parse_double(const char *string)
32 value = strtod(string, &endptr);
34 THROW1(unknown_error, 0, "%s is not a double", string);
38 static int get_rank (const char *process_name)
40 return atoi(&(process_name[1]));
44 static int spawned_send(int argc, char *argv[])
46 DEBUG3("%s: Sending %s on %s", MSG_process_get_name(MSG_process_self()),
48 MSG_task_send(MSG_task_create(argv[0], 0, parse_double(argv[1]), NULL),
53 static void action_send(xbt_dynar_t action)
57 char *size_str = xbt_dynar_get_as(action, 3, char *);
58 double size=parse_double(size_str);
59 double clock = MSG_get_clock(); /* this "call" is free thanks to inlining */
61 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),
62 xbt_dynar_get_as(action, 2, char *));
63 // char *to = xbt_dynar_get_as(action, 2, char *);
65 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
66 name = xbt_str_join(action, " ");
69 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
70 int dst_traced = get_rank(xbt_dynar_get_as(action, 2, char *));
71 TRACE_smpi_ptp_in(rank, rank, dst_traced, "send");
72 TRACE_smpi_send(rank, rank, dst_traced);
75 DEBUG2("Entering Send: %s (size: %lg)", name, size);
79 m_process_t comm_helper;
80 DEBUG1("Isend on %s: spawn process ",
81 MSG_process_get_name(MSG_process_self()));
82 myargv = (char **) calloc(3, sizeof(char *));
83 myargv[0] = xbt_strdup(to);
84 myargv[1] = xbt_strdup(size_str);
87 sprintf(spawn_name, "%s_wait", to);
89 MSG_process_create_with_arguments(spawn_name, spawned_send,
90 NULL, MSG_host_self(), 2, myargv);
93 MSG_task_send(MSG_task_create(name, 0, size, NULL), to);
96 VERB2("%s %f", name, MSG_get_clock() - clock);
98 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
102 TRACE_smpi_ptp_out(rank, rank, dst_traced, "send");
106 static void action_Isend(xbt_dynar_t action)
110 // char *to = xbt_dynar_get_as(action, 2, char *);
111 char *size = xbt_dynar_get_as(action, 3, char *);
113 m_process_t comm_helper;
114 double clock = MSG_get_clock();
115 DEBUG1("Isend on %s: spawn process ",
116 MSG_process_get_name(MSG_process_self()));
118 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),
119 xbt_dynar_get_as(action, 2, char *));
120 myargv = (char **) calloc(3, sizeof(char *));
122 myargv[0] = xbt_strdup(to);
123 myargv[1] = xbt_strdup(size);
126 // sprintf(spawn_name,"%s_wait",MSG_process_get_name(MSG_process_self()));
127 sprintf(spawn_name, "%s_wait", to);
129 MSG_process_create_with_arguments(spawn_name, spawned_send,
130 NULL, MSG_host_self(), 2, myargv);
131 VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
135 static void action_recv(xbt_dynar_t action)
138 char mailbox_name[250];
139 m_task_t task = NULL;
140 double clock = MSG_get_clock();
141 //FIXME: argument of action ignored so far; semantic not clear
142 //char *from=xbt_dynar_get_as(action,2,char*);
143 sprintf(mailbox_name, "%s_%s", xbt_dynar_get_as(action, 2, char *),
144 MSG_process_get_name(MSG_process_self()));
146 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
147 name = xbt_str_join(action, " ");
150 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
151 int src_traced = get_rank(xbt_dynar_get_as(action, 2, char *));
152 TRACE_smpi_ptp_in(rank, src_traced, rank, "recv");
155 DEBUG1("Receiving: %s", name);
156 MSG_task_receive(&task, mailbox_name);
157 // MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
158 VERB2("%s %f", name, MSG_get_clock() - clock);
159 MSG_task_destroy(task);
161 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
164 TRACE_smpi_ptp_out(rank, src_traced, rank, "recv");
165 TRACE_smpi_recv(rank, src_traced, rank);
170 static int spawned_recv(int argc, char *argv[])
172 m_task_t task = NULL;
173 DEBUG1("Receiving on %s", argv[0]);
174 MSG_task_receive(&task, argv[0]);
175 DEBUG1("Received %s", MSG_task_get_name(task));
176 DEBUG1("waiter on %s", MSG_process_get_name(MSG_process_self()));
177 MSG_task_send(MSG_task_create("waiter", 0, 0, NULL),
178 MSG_process_get_name(MSG_process_self()));
180 MSG_task_destroy(task);
185 static void action_Irecv(xbt_dynar_t action)
188 m_process_t comm_helper;
189 char mailbox_name[250];
191 double clock = MSG_get_clock();
193 DEBUG1("Irecv on %s: spawn process ",
194 MSG_process_get_name(MSG_process_self()));
196 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
197 int src_traced = get_rank(xbt_dynar_get_as(action, 2, char *));
198 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
199 counters->last_Irecv_sender_id = src_traced;
200 MSG_process_set_data(MSG_process_self(), (void *) counters);
202 TRACE_smpi_ptp_in(rank, src_traced, rank, "Irecv");
205 sprintf(mailbox_name, "%s_%s", xbt_dynar_get_as(action, 2, char *),
206 MSG_process_get_name(MSG_process_self()));
207 name = bprintf("%s_wait", MSG_process_get_name(MSG_process_self()));
208 myargv = (char **) calloc(2, sizeof(char *));
210 myargv[0] = xbt_strdup(mailbox_name);
212 comm_helper = MSG_process_create_with_arguments(name, spawned_recv,
213 NULL, MSG_host_self(),
216 VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
220 TRACE_smpi_ptp_out(rank, src_traced, rank, "Irecv");
226 static void action_wait(xbt_dynar_t action)
230 m_task_t task = NULL;
231 double clock = MSG_get_clock();
233 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
234 name = xbt_str_join(action, " ");
236 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
237 int src_traced = counters->last_Irecv_sender_id;
238 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
239 TRACE_smpi_ptp_in(rank, src_traced, rank, "wait");
242 DEBUG1("Entering %s", name);
243 sprintf(task_name, "%s_wait", MSG_process_get_name(MSG_process_self()));
244 DEBUG1("wait: %s", task_name);
245 MSG_task_receive(&task, task_name);
246 MSG_task_destroy(task);
247 VERB2("%s %f", name, MSG_get_clock() - clock);
248 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
251 TRACE_smpi_ptp_out(rank, src_traced, rank, "wait");
252 TRACE_smpi_recv(rank, src_traced, rank);
257 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
258 static void action_barrier(xbt_dynar_t action)
261 static smx_mutex_t mutex = NULL;
262 static smx_cond_t cond = NULL;
263 static int processes_arrived_sofar=0;
265 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
266 name = xbt_str_join(action, " ");
268 if (mutex == NULL) { // first arriving on the barrier
269 mutex = SIMIX_req_mutex_init();
270 cond = SIMIX_req_cond_init();
271 processes_arrived_sofar=0;
273 DEBUG2("Entering barrier: %s (%d already there)", name,processes_arrived_sofar);
275 SIMIX_req_mutex_lock(mutex);
276 if (++processes_arrived_sofar == communicator_size) {
277 SIMIX_req_cond_broadcast(cond);
278 SIMIX_req_mutex_unlock(mutex);
280 SIMIX_req_cond_wait(cond,mutex);
281 SIMIX_req_mutex_unlock(mutex);
284 DEBUG1("Exiting barrier: %s", name);
286 processes_arrived_sofar--;
287 if (!processes_arrived_sofar) {
288 SIMIX_req_cond_destroy(cond);
289 SIMIX_req_mutex_destroy(mutex);
293 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
298 static void action_reduce(xbt_dynar_t action)
305 char *comm_size = xbt_dynar_get_as(action, 2, char *);
306 char *comp_size = xbt_dynar_get_as(action, 3, char *);
307 m_process_t comm_helper = NULL;
308 m_task_t task = NULL, comp_task = NULL;
309 const char *process_name;
310 double clock = MSG_get_clock();
312 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
314 xbt_assert0(communicator_size, "Size of Communicator is not defined"
315 ", can't use collective operations");
317 process_name = MSG_process_get_name(MSG_process_self());
319 name = bprintf("reduce_%d", counters->reduce_counter++);
321 if (!strcmp(process_name, "p0")) {
322 DEBUG2("%s: %s is the Root", name, process_name);
323 for (i = 1; i < communicator_size; i++) {
324 sprintf(spawn_name, "%s_p%d_%s", name, i,
325 MSG_process_get_name(MSG_process_self()));
326 sprintf(task_name, "%s_wait", spawn_name);
327 myargv = (char **) calloc(2, sizeof(char *));
329 myargv[0] = xbt_strdup(spawn_name);
333 MSG_process_create_with_arguments(task_name, spawned_recv,
334 NULL, MSG_host_self(),
338 for (i = 1; i < communicator_size; i++) {
339 sprintf(task_name, "%s_p%d_p0_wait", name, i);
340 MSG_task_receive(&task, task_name);
341 MSG_task_destroy(task);
346 MSG_task_create("reduce_comp", parse_double(comp_size), 0, NULL);
347 DEBUG1("%s: computing 'reduce_comp'", name);
348 MSG_task_execute(comp_task);
349 MSG_task_destroy(comp_task);
350 DEBUG1("%s: computed", name);
352 DEBUG2("%s: %s sends", name, process_name);
353 sprintf(task_name, "%s_%s_p0", name, process_name);
354 DEBUG1("put on %s", task_name);
355 MSG_task_send(MSG_task_create(name, 0, parse_double(comm_size), NULL),
359 MSG_process_set_data(MSG_process_self(), (void *) counters);
360 VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
364 static void action_bcast(xbt_dynar_t action)
368 const char *process_name;
372 m_process_t comm_helper = NULL;
373 m_task_t task = NULL;
374 char *size = xbt_dynar_get_as(action, 2, char *);
375 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
376 double clock = MSG_get_clock();
378 xbt_assert0(communicator_size, "Size of Communicator is not defined"
379 ", can't use collective operations");
382 process_name = MSG_process_get_name(MSG_process_self());
384 name = bprintf("bcast_%d", counters->bcast_counter++);
385 if (!strcmp(process_name, "p0")) {
386 DEBUG2("%s: %s is the Root", name, process_name);
388 for (i = 1; i < communicator_size; i++) {
389 myargv = (char **) calloc(3, sizeof(char *));
390 myargv[0] = xbt_strdup(name);
391 myargv[1] = xbt_strdup(size);
394 sprintf(spawn_name, "%s_%d", myargv[0], i);
396 MSG_process_create_with_arguments(spawn_name, spawned_send,
397 NULL, MSG_host_self(), 2,
401 for (i = 1; i < communicator_size; i++) {
402 sprintf(task_name, "p%d_wait", i);
403 DEBUG1("get on %s", task_name);
404 MSG_task_receive(&task, task_name);
405 MSG_task_destroy(task);
408 DEBUG2("%s: all messages sent by %s have been received",
411 DEBUG2("%s: %s receives", name, process_name);
412 MSG_task_receive(&task, name);
413 MSG_task_destroy(task);
414 DEBUG2("%s: %s has received", name, process_name);
415 sprintf(task_name, "%s_wait", process_name);
416 DEBUG1("put on %s", task_name);
417 MSG_task_send(MSG_task_create("waiter", 0, 0, NULL), task_name);
420 MSG_process_set_data(MSG_process_self(), (void *) counters);
421 VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
426 static void action_sleep(xbt_dynar_t action)
429 char *duration = xbt_dynar_get_as(action, 2, char *);
430 double clock = MSG_get_clock();
432 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
433 name = xbt_str_join(action, " ");
435 DEBUG1("Entering %s", name);
436 MSG_process_sleep(parse_double(duration));
437 VERB2("%s %f ", name, MSG_get_clock() - clock);
439 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
443 static void action_allReduce(xbt_dynar_t action)
450 char *comm_size = xbt_dynar_get_as(action, 2, char *);
451 char *comp_size = xbt_dynar_get_as(action, 3, char *);
452 m_process_t comm_helper = NULL;
453 m_task_t task = NULL, comp_task = NULL;
454 const char *process_name;
455 double clock = MSG_get_clock();
457 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
459 xbt_assert0(communicator_size, "Size of Communicator is not defined"
460 ", can't use collective operations");
462 process_name = MSG_process_get_name(MSG_process_self());
464 name = bprintf("allReduce_%d", counters->allReduce_counter++);
466 if (!strcmp(process_name, "p0")) {
467 DEBUG2("%s: %s is the Root", name, process_name);
468 for (i = 1; i < communicator_size; i++) {
469 sprintf(spawn_name, "%s_p%d_%s", name, i,
470 MSG_process_get_name(MSG_process_self()));
471 sprintf(task_name, "%s_wait", spawn_name);
472 myargv = (char **) calloc(2, sizeof(char *));
474 myargv[0] = xbt_strdup(spawn_name);
478 MSG_process_create_with_arguments(task_name, spawned_recv,
479 NULL, MSG_host_self(),
483 for (i = 1; i < communicator_size; i++) {
484 sprintf(task_name, "%s_p%d_p0_wait", name, i);
485 MSG_task_receive(&task, task_name);
486 MSG_task_destroy(task);
491 MSG_task_create("allReduce_comp", parse_double(comp_size), 0,
493 DEBUG1("%s: computing 'reduce_comp'", name);
494 MSG_task_execute(comp_task);
495 MSG_task_destroy(comp_task);
496 DEBUG1("%s: computed", name);
498 for (i = 1; i < communicator_size; i++) {
499 myargv = (char **) calloc(3, sizeof(char *));
500 myargv[0] = xbt_strdup(name);
501 myargv[1] = xbt_strdup(comm_size);
504 sprintf(spawn_name, "%s_%d", myargv[0], i);
506 MSG_process_create_with_arguments(spawn_name, spawned_send,
507 NULL, MSG_host_self(), 2,
511 for (i = 1; i < communicator_size; i++) {
512 sprintf(task_name, "p%d_wait", i);
513 DEBUG1("get on %s", task_name);
514 MSG_task_receive(&task, task_name);
515 MSG_task_destroy(task);
518 DEBUG2("%s: all messages sent by %s have been received",
522 DEBUG2("%s: %s sends", name, process_name);
523 sprintf(task_name, "%s_%s_p0", name, process_name);
524 DEBUG1("put on %s", task_name);
525 MSG_task_send(MSG_task_create(name, 0, parse_double(comm_size), NULL),
528 MSG_task_receive(&task, name);
529 MSG_task_destroy(task);
530 DEBUG2("%s: %s has received", name, process_name);
531 sprintf(task_name, "%s_wait", process_name);
532 DEBUG1("put on %s", task_name);
533 MSG_task_send(MSG_task_create("waiter", 0, 0, NULL), task_name);
537 MSG_process_set_data(MSG_process_self(), (void *) counters);
538 VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
542 static void action_comm_size(xbt_dynar_t action)
545 char *size = xbt_dynar_get_as(action, 2, char *);
546 double clock = MSG_get_clock();
548 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
549 name = xbt_str_join(action, " ");
550 communicator_size = parse_double(size);
551 VERB2("%s %f", name, MSG_get_clock() - clock);
552 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
556 static void action_compute(xbt_dynar_t action)
559 char *amout = xbt_dynar_get_as(action, 2, char *);
560 m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
561 double clock = MSG_get_clock();
563 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
564 name = xbt_str_join(action, " ");
565 DEBUG1("Entering %s", name);
566 MSG_task_execute(task);
567 MSG_task_destroy(task);
568 VERB2("%s %f", name, MSG_get_clock() - clock);
569 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
573 static void action_init(xbt_dynar_t action)
576 TRACE_smpi_init(get_rank(MSG_process_get_name(MSG_process_self())));
578 DEBUG0("Initialize the counters");
579 counters = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
582 static void action_finalize(xbt_dynar_t action)
585 TRACE_smpi_finalize(get_rank(MSG_process_get_name(MSG_process_self())));
587 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
593 int main(int argc, char *argv[])
595 MSG_error_t res = MSG_OK;
597 /* Check the given arguments */
598 MSG_global_init(&argc, argv);
600 printf("Usage: %s platform_file deployment_file [action_files]\n",
603 ("example: %s msg_platform.xml msg_deployment.xml actions # if all actions are in the same file\n",
606 ("example: %s msg_platform.xml msg_deployment.xml # if actions are in separate files, specified in deployment\n",
611 /* Simulation setting */
612 MSG_create_environment(argv[1]);
614 /* No need to register functions as in classical MSG programs: the actions get started anyway */
615 MSG_launch_application(argv[2]);
617 /* Action registration */
618 MSG_action_register("init", action_init);
619 MSG_action_register("finalize", action_finalize);
620 MSG_action_register("comm_size",action_comm_size);
621 MSG_action_register("send", action_send);
622 MSG_action_register("Isend", action_Isend);
623 MSG_action_register("recv", action_recv);
624 MSG_action_register("Irecv", action_Irecv);
625 MSG_action_register("wait", action_wait);
626 MSG_action_register("barrier", action_barrier);
627 MSG_action_register("bcast", action_bcast);
628 MSG_action_register("reduce", action_reduce);
629 MSG_action_register("allReduce",action_allReduce);
630 MSG_action_register("sleep", action_sleep);
631 MSG_action_register("compute", action_compute);
634 /* Actually do the simulation using MSG_action_trace_run */
635 res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
637 INFO1("Simulation time %g", MSG_get_clock());