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 */
14 XBT_LOG_NEW_DEFAULT_CATEGORY(actions,
15 "Messages specific for this msg example");
16 int communicator_size = 0;
18 typedef struct coll_ctr_t {
21 int allReduce_counter;
25 static double parse_double(const char *string)
30 value = strtod(string, &endptr);
32 THROW1(unknown_error, 0, "%s is not a double", string);
38 static void action_send(xbt_dynar_t action)
42 char *size = xbt_dynar_get_as(action, 3, char *);
43 double clock = MSG_get_clock();
44 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),
45 xbt_dynar_get_as(action, 2, char *));
46 // char *to = xbt_dynar_get_as(action, 2, char *);
48 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
49 name = xbt_str_join(action, " ");
51 DEBUG2("Entering Send: %s (size: %lg)", name, parse_double(size));
52 MSG_task_send(MSG_task_create(name, 0, parse_double(size), NULL), to);
53 VERB2("%s %f", name, MSG_get_clock() - clock);
55 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
60 static int spawned_send(int argc, char *argv[])
62 DEBUG3("%s: Sending %s on %s", MSG_process_get_name(MSG_process_self()),
64 MSG_task_send(MSG_task_create(argv[0], 0, parse_double(argv[1]), NULL),
69 static void Isend(xbt_dynar_t action)
73 // char *to = xbt_dynar_get_as(action, 2, char *);
74 char *size = xbt_dynar_get_as(action, 3, char *);
76 m_process_t comm_helper;
77 double clock = MSG_get_clock();
78 DEBUG1("Isend on %s: spawn process ",
79 MSG_process_get_name(MSG_process_self()));
81 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),
82 xbt_dynar_get_as(action, 2, char *));
83 myargv = (char **) calloc(3, sizeof(char *));
85 myargv[0] = xbt_strdup(to);
86 myargv[1] = xbt_strdup(size);
89 // sprintf(spawn_name,"%s_wait",MSG_process_get_name(MSG_process_self()));
90 sprintf(spawn_name, "%s_wait", to);
92 MSG_process_create_with_arguments(spawn_name, spawned_send,
93 NULL, MSG_host_self(), 2, myargv);
94 VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
98 static void action_recv(xbt_dynar_t action)
101 char mailbox_name[250];
102 m_task_t task = NULL;
103 double clock = MSG_get_clock();
104 //FIXME: argument of action ignored so far; semantic not clear
105 //char *from=xbt_dynar_get_as(action,2,char*);
106 sprintf(mailbox_name, "%s_%s", xbt_dynar_get_as(action, 2, char *),
107 MSG_process_get_name(MSG_process_self()));
109 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
110 name = xbt_str_join(action, " ");
112 DEBUG1("Receiving: %s", name);
113 MSG_task_receive(&task, mailbox_name);
114 // MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
115 VERB2("%s %f", name, MSG_get_clock() - clock);
116 MSG_task_destroy(task);
118 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
122 static int spawned_recv(int argc, char *argv[])
124 m_task_t task = NULL;
125 DEBUG1("Receiving on %s", argv[0]);
126 MSG_task_receive(&task, argv[0]);
127 DEBUG1("Received %s", MSG_task_get_name(task));
128 DEBUG1("waiter on %s", MSG_process_get_name(MSG_process_self()));
129 MSG_task_send(MSG_task_create("waiter", 0, 0, NULL),
130 MSG_process_get_name(MSG_process_self()));
132 MSG_task_destroy(task);
137 static void Irecv(xbt_dynar_t action)
140 m_process_t comm_helper;
141 char mailbox_name[250];
143 double clock = MSG_get_clock();
144 DEBUG1("Irecv on %s: spawn process ",
145 MSG_process_get_name(MSG_process_self()));
147 sprintf(mailbox_name, "%s_%s", xbt_dynar_get_as(action, 2, char *),
148 MSG_process_get_name(MSG_process_self()));
149 name = bprintf("%s_wait", MSG_process_get_name(MSG_process_self()));
150 myargv = (char **) calloc(2, sizeof(char *));
152 myargv[0] = xbt_strdup(mailbox_name);
154 comm_helper = MSG_process_create_with_arguments(name, spawned_recv,
155 NULL, MSG_host_self(),
158 VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
164 static void action_wait(xbt_dynar_t action)
168 m_task_t task = NULL;
169 double clock = MSG_get_clock();
171 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
172 name = xbt_str_join(action, " ");
174 DEBUG1("Entering %s", name);
175 sprintf(task_name, "%s_wait", MSG_process_get_name(MSG_process_self()));
176 DEBUG1("wait: %s", task_name);
177 MSG_task_receive(&task, task_name);
178 MSG_task_destroy(task);
179 VERB2("%s %f", name, MSG_get_clock() - clock);
180 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
184 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
185 static void barrier(xbt_dynar_t action)
188 static smx_mutex_t mutex = NULL;
189 static smx_cond_t cond = NULL;
190 static int processes_arrived_sofar=0;
192 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
193 name = xbt_str_join(action, " ");
195 if (mutex == NULL) { // first arriving on the barrier
196 mutex = SIMIX_mutex_init();
197 cond = SIMIX_cond_init();
198 processes_arrived_sofar=0;
200 DEBUG2("Entering barrier: %s (%d already there)", name,processes_arrived_sofar);
202 SIMIX_mutex_lock(mutex);
203 if (++processes_arrived_sofar == communicator_size) {
204 SIMIX_cond_broadcast(cond);
205 SIMIX_mutex_unlock(mutex);
207 SIMIX_cond_wait(cond,mutex);
208 SIMIX_mutex_unlock(mutex);
211 DEBUG1("Exiting barrier: %s", name);
213 processes_arrived_sofar--;
214 if (!processes_arrived_sofar) {
215 SIMIX_cond_destroy(cond);
216 SIMIX_mutex_destroy(mutex);
220 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
225 static void reduce(xbt_dynar_t action)
232 char *comm_size = xbt_dynar_get_as(action, 2, char *);
233 char *comp_size = xbt_dynar_get_as(action, 3, char *);
234 m_process_t comm_helper = NULL;
235 m_task_t task = NULL, comp_task = NULL;
236 const char *process_name;
237 double clock = MSG_get_clock();
239 coll_ctr counters = (coll_ctr) MSG_process_get_data(MSG_process_self());
241 xbt_assert0(communicator_size, "Size of Communicator is not defined"
242 ", can't use collective operations");
244 process_name = MSG_process_get_name(MSG_process_self());
247 DEBUG0("Initialize the counters");
248 counters = (coll_ctr) calloc(1, sizeof(struct coll_ctr_t));
251 name = bprintf("reduce_%d", counters->reduce_counter++);
253 if (!strcmp(process_name, "p0")) {
254 DEBUG2("%s: %s is the Root", name, process_name);
255 for (i = 1; i < communicator_size; i++) {
256 sprintf(spawn_name, "%s_p%d_%s", name, i,
257 MSG_process_get_name(MSG_process_self()));
258 sprintf(task_name, "%s_wait", spawn_name);
259 myargv = (char **) calloc(2, sizeof(char *));
261 myargv[0] = xbt_strdup(spawn_name);
265 MSG_process_create_with_arguments(task_name, spawned_recv,
266 NULL, MSG_host_self(),
270 for (i = 1; i < communicator_size; i++) {
271 sprintf(task_name, "%s_p%d_p0_wait", name, i);
272 MSG_task_receive(&task, task_name);
273 MSG_task_destroy(task);
278 MSG_task_create("reduce_comp", parse_double(comp_size), 0, NULL);
279 DEBUG1("%s: computing 'reduce_comp'", name);
280 MSG_task_execute(comp_task);
281 MSG_task_destroy(comp_task);
282 DEBUG1("%s: computed", name);
284 DEBUG2("%s: %s sends", name, process_name);
285 sprintf(task_name, "%s_%s_p0", name, process_name);
286 DEBUG1("put on %s", task_name);
287 MSG_task_send(MSG_task_create(name, 0, parse_double(comm_size), NULL),
291 MSG_process_set_data(MSG_process_self(), (void *) counters);
292 VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
296 static void bcast(xbt_dynar_t action)
300 const char *process_name;
304 m_process_t comm_helper = NULL;
305 m_task_t task = NULL;
306 char *size = xbt_dynar_get_as(action, 2, char *);
307 coll_ctr counters = (coll_ctr) MSG_process_get_data(MSG_process_self());
308 double clock = MSG_get_clock();
310 xbt_assert0(communicator_size, "Size of Communicator is not defined"
311 ", can't use collective operations");
314 process_name = MSG_process_get_name(MSG_process_self());
316 DEBUG0("Initialize the counters");
317 counters = (coll_ctr) calloc(1, sizeof(struct coll_ctr_t));
320 name = bprintf("bcast_%d", counters->bcast_counter++);
321 if (!strcmp(process_name, "p0")) {
322 DEBUG2("%s: %s is the Root", name, process_name);
324 for (i = 1; i < communicator_size; i++) {
325 myargv = (char **) calloc(3, sizeof(char *));
326 myargv[0] = xbt_strdup(name);
327 myargv[1] = xbt_strdup(size);
330 sprintf(spawn_name, "%s_%d", myargv[0], i);
332 MSG_process_create_with_arguments(spawn_name, spawned_send,
333 NULL, MSG_host_self(), 2,
337 for (i = 1; i < communicator_size; i++) {
338 sprintf(task_name, "p%d_wait", i);
339 DEBUG1("get on %s", task_name);
340 MSG_task_receive(&task, task_name);
341 MSG_task_destroy(task);
344 DEBUG2("%s: all messages sent by %s have been received",
347 DEBUG2("%s: %s receives", name, process_name);
348 MSG_task_receive(&task, name);
349 MSG_task_destroy(task);
350 DEBUG2("%s: %s has received", name, process_name);
351 sprintf(task_name, "%s_wait", process_name);
352 DEBUG1("put on %s", task_name);
353 MSG_task_send(MSG_task_create("waiter", 0, 0, NULL), task_name);
356 MSG_process_set_data(MSG_process_self(), (void *) counters);
357 VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
362 static void action_sleep(xbt_dynar_t action)
365 char *duration = xbt_dynar_get_as(action, 2, char *);
366 double clock = MSG_get_clock();
368 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
369 name = xbt_str_join(action, " ");
371 DEBUG1("Entering %s", name);
372 MSG_process_sleep(parse_double(duration));
373 VERB2("%s %f ", name, MSG_get_clock() - clock);
375 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
379 static void allReduce(xbt_dynar_t action)
386 char *comm_size = xbt_dynar_get_as(action, 2, char *);
387 char *comp_size = xbt_dynar_get_as(action, 3, char *);
388 m_process_t comm_helper = NULL;
389 m_task_t task = NULL, comp_task = NULL;
390 const char *process_name;
391 double clock = MSG_get_clock();
393 coll_ctr counters = (coll_ctr) MSG_process_get_data(MSG_process_self());
395 xbt_assert0(communicator_size, "Size of Communicator is not defined"
396 ", can't use collective operations");
398 process_name = MSG_process_get_name(MSG_process_self());
401 DEBUG0("Initialize the counters");
402 counters = (coll_ctr) calloc(1, sizeof(struct coll_ctr_t));
405 name = bprintf("allReduce_%d", counters->allReduce_counter++);
407 if (!strcmp(process_name, "p0")) {
408 DEBUG2("%s: %s is the Root", name, process_name);
409 for (i = 1; i < communicator_size; i++) {
410 sprintf(spawn_name, "%s_p%d_%s", name, i,
411 MSG_process_get_name(MSG_process_self()));
412 sprintf(task_name, "%s_wait", spawn_name);
413 myargv = (char **) calloc(2, sizeof(char *));
415 myargv[0] = xbt_strdup(spawn_name);
419 MSG_process_create_with_arguments(task_name, spawned_recv,
420 NULL, MSG_host_self(),
424 for (i = 1; i < communicator_size; i++) {
425 sprintf(task_name, "%s_p%d_p0_wait", name, i);
426 MSG_task_receive(&task, task_name);
427 MSG_task_destroy(task);
432 MSG_task_create("allReduce_comp", parse_double(comp_size), 0,
434 DEBUG1("%s: computing 'reduce_comp'", name);
435 MSG_task_execute(comp_task);
436 MSG_task_destroy(comp_task);
437 DEBUG1("%s: computed", name);
439 for (i = 1; i < communicator_size; i++) {
440 myargv = (char **) calloc(3, sizeof(char *));
441 myargv[0] = xbt_strdup(name);
442 myargv[1] = xbt_strdup(comm_size);
445 sprintf(spawn_name, "%s_%d", myargv[0], i);
447 MSG_process_create_with_arguments(spawn_name, spawned_send,
448 NULL, MSG_host_self(), 2,
452 for (i = 1; i < communicator_size; i++) {
453 sprintf(task_name, "p%d_wait", i);
454 DEBUG1("get on %s", task_name);
455 MSG_task_receive(&task, task_name);
456 MSG_task_destroy(task);
459 DEBUG2("%s: all messages sent by %s have been received",
463 DEBUG2("%s: %s sends", name, process_name);
464 sprintf(task_name, "%s_%s_p0", name, process_name);
465 DEBUG1("put on %s", task_name);
466 MSG_task_send(MSG_task_create(name, 0, parse_double(comm_size), NULL),
469 MSG_task_receive(&task, name);
470 MSG_task_destroy(task);
471 DEBUG2("%s: %s has received", name, process_name);
472 sprintf(task_name, "%s_wait", process_name);
473 DEBUG1("put on %s", task_name);
474 MSG_task_send(MSG_task_create("waiter", 0, 0, NULL), task_name);
478 MSG_process_set_data(MSG_process_self(), (void *) counters);
479 VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
483 static void comm_size(xbt_dynar_t action)
486 char *size = xbt_dynar_get_as(action, 2, char *);
487 double clock = MSG_get_clock();
489 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
490 name = xbt_str_join(action, " ");
491 communicator_size = parse_double(size);
492 VERB2("%s %f", name, MSG_get_clock() - clock);
493 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
497 static void compute(xbt_dynar_t action)
500 char *amout = xbt_dynar_get_as(action, 2, char *);
501 m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
502 double clock = MSG_get_clock();
504 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
505 name = xbt_str_join(action, " ");
506 DEBUG1("Entering %s", name);
507 MSG_task_execute(task);
508 MSG_task_destroy(task);
509 VERB2("%s %f", name, MSG_get_clock() - clock);
510 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
515 int main(int argc, char *argv[])
517 MSG_error_t res = MSG_OK;
519 /* Check the given arguments */
520 MSG_global_init(&argc, argv);
522 printf("Usage: %s platform_file deployment_file [action_files]\n",
525 ("example: %s msg_platform.xml msg_deployment.xml actions # if all actions are in the same file\n",
528 ("example: %s msg_platform.xml msg_deployment.xml # if actions are in separate files, specified in deployment\n",
533 /* Simulation setting */
534 MSG_create_environment(argv[1]);
536 /* No need to register functions as in classical MSG programs: the actions get started anyway */
537 MSG_launch_application(argv[2]);
539 /* Action registration */
540 MSG_action_register("comm_size", comm_size);
541 MSG_action_register("send", action_send);
542 MSG_action_register("Isend", Isend);
543 MSG_action_register("recv", action_recv);
544 MSG_action_register("Irecv", Irecv);
545 MSG_action_register("wait", action_wait);
546 MSG_action_register("barrier", barrier);
547 MSG_action_register("bcast", bcast);
548 MSG_action_register("reduce", reduce);
549 MSG_action_register("allReduce", allReduce);
550 MSG_action_register("sleep", action_sleep);
551 MSG_action_register("compute", compute);
554 /* Actually do the simulation using MSG_action_trace_run */
555 res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
557 INFO1("Simulation time %g", MSG_get_clock());