3 /* Copyright (c) 2009. The SimGrid team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
11 #include "simix/simix.h" /* semaphores for the barrier */
12 #include "xbt.h" /* calloc, printf */
13 #include "simgrid_config.h" /* getline */
15 XBT_LOG_NEW_DEFAULT_CATEGORY(actions,
16 "Messages specific for this msg example");
17 int communicator_size=0;
19 typedef struct coll_ctr_t{
22 int allReduce_counter;
26 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_debug))
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 DEBUG2("%s %f", name, MSG_get_clock()-clock);
55 if (XBT_LOG_ISENABLED(actions,xbt_log_priority_debug))
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 DEBUG2("%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_debug))
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 DEBUG2("%s %f", name, MSG_get_clock()-clock);
116 MSG_task_destroy(task);
118 if (XBT_LOG_ISENABLED(actions,xbt_log_priority_debug))
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 DEBUG2("%s %f", xbt_str_join(action, " "),
159 MSG_get_clock()-clock);
165 static void action_wait(xbt_dynar_t action)
169 m_task_t task = NULL;
170 double clock = MSG_get_clock();
172 if (XBT_LOG_ISENABLED(actions,xbt_log_priority_debug))
173 name = xbt_str_join(action, " ");
175 DEBUG1("Entering %s", name);
176 sprintf(task_name,"%s_wait",MSG_process_get_name(MSG_process_self()));
177 DEBUG1("wait: %s", task_name);
178 MSG_task_receive(&task,task_name);
179 MSG_task_destroy(task);
180 DEBUG2("%s %f", name, MSG_get_clock()-clock);
181 if (XBT_LOG_ISENABLED(actions,xbt_log_priority_debug))
185 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
186 smx_sem_t barrier_semaphore=NULL;
187 static void barrier (xbt_dynar_t action)
191 if (XBT_LOG_ISENABLED(actions,xbt_log_priority_debug))
192 name = xbt_str_join(action, " ");
194 DEBUG1("Entering barrier: %s", name);
195 if (barrier_semaphore == NULL) // first arriving on the barrier
196 barrier_semaphore = SIMIX_sem_init(0);
198 if (SIMIX_sem_get_capacity(barrier_semaphore)==-communicator_size +1) { // last arriving
199 SIMIX_sem_release_forever(barrier_semaphore);
200 SIMIX_sem_destroy(barrier_semaphore);
201 barrier_semaphore = NULL;
203 SIMIX_sem_acquire(barrier_semaphore);
206 DEBUG1("Exiting barrier: %s", name);
208 if (XBT_LOG_ISENABLED(actions,xbt_log_priority_debug))
213 static void reduce(xbt_dynar_t action)
220 char *comm_size = xbt_dynar_get_as(action, 2, char *);
221 char *comp_size = xbt_dynar_get_as(action, 3, char *);
222 m_process_t comm_helper=NULL;
223 m_task_t task=NULL, comp_task=NULL;
224 const char* process_name;
225 double clock = MSG_get_clock();
227 coll_ctr counters = (coll_ctr) MSG_process_get_data(MSG_process_self());
229 xbt_assert0(communicator_size, "Size of Communicator is not defined"
230 ", can't use collective operations");
232 process_name = MSG_process_get_name(MSG_process_self());
235 DEBUG0("Initialize the counters");
236 counters = (coll_ctr) calloc (1, sizeof(struct coll_ctr_t));
239 name = bprintf("reduce_%d", counters->reduce_counter++);
241 if (!strcmp(process_name, "p0")){
242 DEBUG2("%s: %s is the Root",name, process_name);
243 for(i=1;i<communicator_size;i++){
244 sprintf(spawn_name,"%s_p%d_%s", name, i,
245 MSG_process_get_name(MSG_process_self()));
246 sprintf(task_name,"%s_wait", spawn_name);
247 myargv = (char**) calloc (2, sizeof (char*));
249 myargv[0] = xbt_strdup(spawn_name);
253 MSG_process_create_with_arguments(task_name, spawned_recv,
254 NULL, MSG_host_self(),
258 for(i=1;i<communicator_size;i++){
259 sprintf(task_name,"%s_p%d_p0_wait", name, i);
260 MSG_task_receive(&task,task_name);
261 MSG_task_destroy(task);
266 MSG_task_create("reduce_comp", parse_double(comp_size), 0, NULL);
267 DEBUG1("%s: computing 'reduce_comp'", name);
268 MSG_task_execute(comp_task);
269 MSG_task_destroy(comp_task);
270 DEBUG1("%s: computed", name);
272 DEBUG2("%s: %s sends", name, process_name);
273 sprintf(task_name,"%s_%s_p0", name, process_name);
274 DEBUG1("put on %s", task_name);
275 MSG_task_send(MSG_task_create(name, 0, parse_double(comm_size), NULL),
279 MSG_process_set_data(MSG_process_self(), (void*)counters);
280 DEBUG2("%s %f", xbt_str_join(action, " "), MSG_get_clock()-clock);
284 static void bcast (xbt_dynar_t action)
288 const char* process_name;
292 m_process_t comm_helper=NULL;
294 char *size = xbt_dynar_get_as(action, 2, char *);
295 coll_ctr counters = (coll_ctr) MSG_process_get_data(MSG_process_self());
296 double clock = MSG_get_clock();
298 xbt_assert0(communicator_size, "Size of Communicator is not defined"
299 ", can't use collective operations");
302 process_name = MSG_process_get_name(MSG_process_self());
304 DEBUG0("Initialize the counters");
305 counters = (coll_ctr) calloc (1, sizeof(struct coll_ctr_t));
308 name = bprintf("bcast_%d", counters->bcast_counter++);
309 if (!strcmp(process_name, "p0")){
310 DEBUG2("%s: %s is the Root",name, process_name);
312 for(i=1;i<communicator_size;i++){
313 myargv = (char**) calloc (3, sizeof (char*));
314 myargv[0] = xbt_strdup(name);
315 myargv[1] = xbt_strdup(size);
318 sprintf(spawn_name,"%s_%d", myargv[0], i);
320 MSG_process_create_with_arguments(spawn_name, spawned_send,
321 NULL, MSG_host_self(), 2, myargv);
324 for(i=1;i<communicator_size;i++){
325 sprintf(task_name,"p%d_wait", i);
326 DEBUG1("get on %s", task_name);
327 MSG_task_receive(&task,task_name);
328 MSG_task_destroy(task);
331 DEBUG2("%s: all messages sent by %s have been received",
334 DEBUG2("%s: %s receives", name, process_name);
335 MSG_task_receive(&task, name);
336 MSG_task_destroy(task);
337 DEBUG2("%s: %s has received", name,process_name);
338 sprintf(task_name,"%s_wait", process_name);
339 DEBUG1("put on %s", task_name);
340 MSG_task_send(MSG_task_create("waiter",0,0,NULL),task_name);
343 MSG_process_set_data(MSG_process_self(), (void*)counters);
344 DEBUG2("%s %f", xbt_str_join(action, " "), MSG_get_clock()-clock);
349 static void action_sleep(xbt_dynar_t action)
352 char *duration = xbt_dynar_get_as(action, 2, char *);
353 double clock = MSG_get_clock();
355 if (XBT_LOG_ISENABLED(actions,xbt_log_priority_debug))
356 name = xbt_str_join(action, " ");
358 DEBUG1("Entering %s", name);
359 MSG_process_sleep(parse_double(duration));
360 DEBUG2("%s %f ", name, MSG_get_clock()-clock);
362 if (XBT_LOG_ISENABLED(actions,xbt_log_priority_debug))
366 static void allReduce(xbt_dynar_t action)
373 char *comm_size = xbt_dynar_get_as(action, 2, char *);
374 char *comp_size = xbt_dynar_get_as(action, 3, char *);
375 m_process_t comm_helper=NULL;
376 m_task_t task=NULL, comp_task=NULL;
377 const char* process_name;
378 double clock = MSG_get_clock();
380 coll_ctr counters = (coll_ctr) MSG_process_get_data(MSG_process_self());
382 xbt_assert0(communicator_size, "Size of Communicator is not defined"
383 ", can't use collective operations");
385 process_name = MSG_process_get_name(MSG_process_self());
388 DEBUG0("Initialize the counters");
389 counters = (coll_ctr) calloc (1, sizeof(struct coll_ctr_t));
392 name = bprintf("allReduce_%d", counters->allReduce_counter++);
394 if (!strcmp(process_name, "p0")){
395 DEBUG2("%s: %s is the Root",name, process_name);
396 for(i=1;i<communicator_size;i++){
397 sprintf(spawn_name,"%s_p%d_%s", name, i,
398 MSG_process_get_name(MSG_process_self()));
399 sprintf(task_name,"%s_wait", spawn_name);
400 myargv = (char**) calloc (2, sizeof (char*));
402 myargv[0] = xbt_strdup(spawn_name);
406 MSG_process_create_with_arguments(task_name, spawned_recv,
407 NULL, MSG_host_self(),
411 for(i=1;i<communicator_size;i++){
412 sprintf(task_name,"%s_p%d_p0_wait", name, i);
413 MSG_task_receive(&task,task_name);
414 MSG_task_destroy(task);
419 MSG_task_create("allReduce_comp", parse_double(comp_size), 0, NULL);
420 DEBUG1("%s: computing 'reduce_comp'", name);
421 MSG_task_execute(comp_task);
422 MSG_task_destroy(comp_task);
423 DEBUG1("%s: computed", name);
425 for(i=1;i<communicator_size;i++){
426 myargv = (char**) calloc (3, sizeof (char*));
427 myargv[0] = xbt_strdup(name);
428 myargv[1] = xbt_strdup(comm_size);
431 sprintf(spawn_name,"%s_%d", myargv[0], i);
433 MSG_process_create_with_arguments(spawn_name, spawned_send,
434 NULL, MSG_host_self(), 2, myargv);
437 for(i=1;i<communicator_size;i++){
438 sprintf(task_name,"p%d_wait", i);
439 DEBUG1("get on %s", task_name);
440 MSG_task_receive(&task,task_name);
441 MSG_task_destroy(task);
444 DEBUG2("%s: all messages sent by %s have been received",
448 DEBUG2("%s: %s sends", name, process_name);
449 sprintf(task_name,"%s_%s_p0", name, process_name);
450 DEBUG1("put on %s", task_name);
451 MSG_task_send(MSG_task_create(name, 0, parse_double(comm_size), NULL),
454 MSG_task_receive(&task, name);
455 MSG_task_destroy(task);
456 DEBUG2("%s: %s has received", name,process_name);
457 sprintf(task_name,"%s_wait", process_name);
458 DEBUG1("put on %s", task_name);
459 MSG_task_send(MSG_task_create("waiter",0,0,NULL),task_name);
463 MSG_process_set_data(MSG_process_self(), (void*)counters);
464 DEBUG2("%s %f", xbt_str_join(action, " "), MSG_get_clock()-clock);
468 static void comm_size(xbt_dynar_t action)
470 char *size = xbt_dynar_get_as(action, 2, char *);
471 communicator_size = parse_double(size);
474 static void compute(xbt_dynar_t action)
477 char *amout = xbt_dynar_get_as(action, 2, char *);
478 m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
479 double clock = MSG_get_clock();
481 if (XBT_LOG_ISENABLED(actions,xbt_log_priority_debug))
482 name = xbt_str_join(action, " ");
483 DEBUG1("Entering %s", name);
484 MSG_task_execute(task);
485 MSG_task_destroy(task);
486 DEBUG2("%s %f", name, MSG_get_clock()-clock);
487 if (XBT_LOG_ISENABLED(actions,xbt_log_priority_debug))
492 int main(int argc, char *argv[])
494 MSG_error_t res = MSG_OK;
496 /* Check the given arguments */
497 MSG_global_init(&argc, argv);
499 printf("Usage: %s platform_file deployment_file [action_files]\n", argv[0]);
500 printf("example: %s msg_platform.xml msg_deployment.xml actions # if all actions are in the same file\n",
502 printf("example: %s msg_platform.xml msg_deployment.xml # if actions are in separate files, specified in deployment\n",
507 /* Simulation setting */
508 MSG_create_environment(argv[1]);
510 /* No need to register functions as in classical MSG programs: the actions get started anyway */
511 MSG_launch_application(argv[2]);
513 /* Action registration */
514 MSG_action_register("comm_size", comm_size);
515 MSG_action_register("send", action_send);
516 MSG_action_register("Isend", Isend);
517 MSG_action_register("recv", action_recv);
518 MSG_action_register("Irecv", Irecv);
519 MSG_action_register("wait", action_wait);
520 MSG_action_register("barrier", barrier);
521 MSG_action_register("bcast", bcast);
522 MSG_action_register("reduce", reduce);
523 MSG_action_register("allReduce", allReduce);
524 MSG_action_register("sleep", action_sleep);
525 MSG_action_register("compute", compute);
528 /* Actually do the simulation using MSG_action_trace_run */
529 res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
531 INFO1("Simulation time %g", MSG_get_clock());