Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7a00e85b9c77a4da8c882d49e985ecadc2683d28
[simgrid.git] / examples / msg / actions / actions.c
1 /* Copyright (c) 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <stdio.h>
8 #include <stdlib.h>
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"
14
15 XBT_LOG_NEW_DEFAULT_CATEGORY(actions,
16                              "Messages specific for this msg example");
17 int communicator_size = 0;
18
19 static void action_Isend(xbt_dynar_t action);
20
21 typedef struct  {
22   int last_Irecv_sender_id;
23   int bcast_counter;
24   int reduce_counter;
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;
31
32 /* Helper function */
33 static double parse_double(const char *string)
34 {
35   double value;
36   char *endptr;
37
38   value = strtod(string, &endptr);
39   if (*endptr != '\0')
40     THROW1(unknown_error, 0, "%s is not a double", string);
41   return value;
42 }
43
44 static int get_rank (const char *process_name)
45 {
46   return atoi(&(process_name[1]));
47
48
49 static void asynchronous_cleanup(void) {
50   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
51
52   /* Destroy any isend which correspond to completed communications */
53   int found;
54   msg_comm_t comm;
55   while ((found = MSG_comm_testany(globals->isends)) != -1) {
56     xbt_dynar_remove_at(globals->isends,found,&comm);
57     MSG_comm_destroy(comm);
58   }
59 }
60
61 /* My actions */
62 static void action_send(xbt_dynar_t action)
63 {
64   char *name = NULL;
65   char to[250];
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 */
69
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 *);
73
74   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
75     name = xbt_str_join(action, " ");
76
77 #ifdef HAVE_TRACING
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);
82 #endif
83
84   DEBUG2("Entering Send: %s (size: %lg)", name, size);
85    if (size<65536) {
86      action_Isend(action);
87    } else {
88      MSG_task_send(MSG_task_create(name, 0, size, NULL), to);
89    }
90    
91    VERB2("%s %f", name, MSG_get_clock() - clock);
92
93   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
94     free(name);
95
96 #ifdef HAVE_TRACING
97   TRACE_smpi_ptp_out(rank, rank, dst_traced, "send");
98 #endif
99
100   asynchronous_cleanup();
101 }
102
103 static void action_Isend(xbt_dynar_t action)
104 {
105   char to[250];
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());
110
111
112   sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),
113           xbt_dynar_get_as(action, 2, char *));
114
115   msg_comm_t comm =
116       MSG_task_isend( MSG_task_create(to,0,parse_double(size),NULL), to);
117   xbt_dynar_push(globals->isends,&comm);
118
119   DEBUG1("Isend on %s", MSG_process_get_name(MSG_process_self()));
120   VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
121
122   asynchronous_cleanup();
123 }
124
125
126 static void action_recv(xbt_dynar_t action)
127 {
128   char *name = NULL;
129   char mailbox_name[250];
130   m_task_t task = NULL;
131   double clock = MSG_get_clock();
132   //FIXME: argument of action ignored so far; semantic not clear
133   //char *from=xbt_dynar_get_as(action,2,char*);
134   sprintf(mailbox_name, "%s_%s", xbt_dynar_get_as(action, 2, char *),
135           MSG_process_get_name(MSG_process_self()));
136
137   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
138     name = xbt_str_join(action, " ");
139
140 #ifdef HAVE_TRACING
141   int rank = get_rank(MSG_process_get_name(MSG_process_self()));
142   int src_traced = get_rank(xbt_dynar_get_as(action, 2, char *));
143   TRACE_smpi_ptp_in(rank, src_traced, rank, "recv");
144 #endif
145
146   DEBUG1("Receiving: %s", name);
147   MSG_task_receive(&task, mailbox_name);
148   //  MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
149   VERB2("%s %f", name, MSG_get_clock() - clock);
150   MSG_task_destroy(task);
151
152   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
153     free(name);
154 #ifdef HAVE_TRACING
155   TRACE_smpi_ptp_out(rank, src_traced, rank, "recv");
156   TRACE_smpi_recv(rank, src_traced, rank);
157 #endif
158
159   asynchronous_cleanup();
160 }
161
162 static void action_Irecv(xbt_dynar_t action)
163 {
164   char mailbox[250];
165   double clock = MSG_get_clock();
166   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
167
168   DEBUG1("Irecv on %s", MSG_process_get_name(MSG_process_self()));
169 #ifdef HAVE_TRACING
170   int rank = get_rank(MSG_process_get_name(MSG_process_self()));
171   int src_traced = get_rank(xbt_dynar_get_as(action, 2, char *));
172   globals->last_Irecv_sender_id = src_traced;
173   MSG_process_set_data(MSG_process_self(), (void *) globals);
174
175   TRACE_smpi_ptp_in(rank, src_traced, rank, "Irecv");
176 #endif
177
178   sprintf(mailbox, "%s_%s", xbt_dynar_get_as(action, 2, char *),
179           MSG_process_get_name(MSG_process_self()));
180   m_task_t t=NULL;
181   xbt_dynar_push(globals->tasks,&t);
182   msg_comm_t c =
183       MSG_task_irecv(
184           xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks)-1),
185           mailbox);
186   xbt_dynar_push(globals->irecvs,&c);
187
188   VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
189
190 #ifdef HAVE_TRACING
191   TRACE_smpi_ptp_out(rank, src_traced, rank, "Irecv");
192 #endif
193
194   asynchronous_cleanup();
195 }
196
197
198 static void action_wait(xbt_dynar_t action)
199 {
200   char *name = NULL;
201   m_task_t task = NULL;
202   msg_comm_t comm;
203   double clock = MSG_get_clock();
204   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
205
206   xbt_assert1(xbt_dynar_length(globals->irecvs),
207       "action wait not preceeded by any irecv: %s", xbt_str_join(action," "));
208
209   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
210     name = xbt_str_join(action, " ");
211 #ifdef HAVE_TRACING
212   process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
213   int src_traced = counters->last_Irecv_sender_id;
214   int rank = get_rank(MSG_process_get_name(MSG_process_self()));
215   TRACE_smpi_ptp_in(rank, src_traced, rank, "wait");
216 #endif
217
218   DEBUG1("Entering %s", name);
219   comm = xbt_dynar_pop_as(globals->irecvs,msg_comm_t);
220   MSG_comm_wait(comm,-1);
221   task = xbt_dynar_pop_as(globals->tasks,m_task_t);
222   MSG_comm_destroy(comm);
223   MSG_task_destroy(task);
224
225   VERB2("%s %f", name, MSG_get_clock() - clock);
226   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
227     free(name);
228 #ifdef HAVE_TRACING
229   TRACE_smpi_ptp_out(rank, src_traced, rank, "wait");
230   TRACE_smpi_recv(rank, src_traced, rank);
231 #endif
232
233 }
234
235 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
236 static void action_barrier(xbt_dynar_t action)
237 {
238   char *name = NULL;
239   static smx_mutex_t mutex = NULL;
240   static smx_cond_t cond = NULL;
241   static int processes_arrived_sofar=0;
242
243   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
244     name = xbt_str_join(action, " ");
245
246   if (mutex == NULL) {       // first arriving on the barrier
247     mutex = SIMIX_req_mutex_init();
248     cond = SIMIX_req_cond_init();
249     processes_arrived_sofar=0;
250   }
251   DEBUG2("Entering barrier: %s (%d already there)", name,processes_arrived_sofar);
252
253   SIMIX_req_mutex_lock(mutex);
254   if (++processes_arrived_sofar == communicator_size) {
255     SIMIX_req_cond_broadcast(cond);
256     SIMIX_req_mutex_unlock(mutex);
257   } else {
258     SIMIX_req_cond_wait(cond,mutex);
259     SIMIX_req_mutex_unlock(mutex);
260   }
261
262   DEBUG1("Exiting barrier: %s", name);
263
264   processes_arrived_sofar--;
265   if (!processes_arrived_sofar) {
266     SIMIX_req_cond_destroy(cond);
267     SIMIX_req_mutex_destroy(mutex);
268     mutex=NULL;
269   }
270
271   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
272     free(name);
273
274 }
275
276 static void action_reduce(xbt_dynar_t action)
277 {
278         int i;
279         char *reduce_identifier;
280         char mailbox[80];
281         double comm_size = parse_double(xbt_dynar_get_as(action, 2, char *));
282         double comp_size = parse_double(xbt_dynar_get_as(action, 3, char *));
283         m_task_t comp_task = NULL;
284         const char *process_name;
285         double clock = MSG_get_clock();
286
287         process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
288
289         xbt_assert0(communicator_size, "Size of Communicator is not defined, "
290                         "can't use collective operations");
291
292         process_name = MSG_process_get_name(MSG_process_self());
293
294         reduce_identifier = bprintf("reduce_%d", counters->reduce_counter++);
295
296         if (!strcmp(process_name, "p0")) {
297                 DEBUG2("%s: %s is the Root", reduce_identifier, process_name);
298
299                 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
300             m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
301             for (i = 1; i < communicator_size; i++) {
302               sprintf(mailbox, "%s_p%d_p0", reduce_identifier, i);
303               comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
304             }
305             MSG_comm_waitall(comms,communicator_size-1,-1);
306             for (i = 1; i < communicator_size; i++) {
307                 MSG_comm_destroy(comms[i-1]);
308                 MSG_task_destroy(tasks[i-1]);
309             }
310             free(tasks);
311
312             comp_task = MSG_task_create("reduce_comp", comp_size, 0, NULL);
313             DEBUG1("%s: computing 'reduce_comp'", reduce_identifier);
314             MSG_task_execute(comp_task);
315             MSG_task_destroy(comp_task);
316             DEBUG1("%s: computed", reduce_identifier);
317
318         } else {
319                 DEBUG2("%s: %s sends", reduce_identifier, process_name);
320                 sprintf(mailbox, "%s_%s_p0", reduce_identifier, process_name);
321             DEBUG1("put on %s", mailbox);
322             MSG_task_send(MSG_task_create(reduce_identifier, 0, comm_size, NULL),
323                           mailbox);
324         }
325
326         VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
327         free(reduce_identifier);
328 }
329
330 static void action_bcast(xbt_dynar_t action)
331 {
332         int i;
333         char *bcast_identifier;
334         char mailbox[80];
335         double comm_size = parse_double(xbt_dynar_get_as(action, 2, char *));
336         m_task_t task = NULL;
337         const char *process_name;
338         double clock = MSG_get_clock();
339
340         process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
341
342         xbt_assert0(communicator_size, "Size of Communicator is not defined, "
343                         "can't use collective operations");
344
345         process_name = MSG_process_get_name(MSG_process_self());
346
347         bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
348
349         if (!strcmp(process_name, "p0")) {
350                 DEBUG2("%s: %s is the Root", bcast_identifier, process_name);
351
352             msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
353
354             for (i = 1; i < communicator_size; i++) {
355               sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
356               comms[i-1] =
357                   MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
358                       mailbox);
359             }
360             MSG_comm_waitall(comms,communicator_size-1,-1);
361                 for (i = 1; i < communicator_size; i++)
362                MSG_comm_destroy(comms[i-1]);
363             free(comms);
364
365             DEBUG2("%s: all messages sent by %s have been received",
366                    bcast_identifier, process_name);
367
368         } else {
369             sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
370             MSG_task_receive(&task, mailbox);
371             MSG_task_destroy(task);
372             DEBUG2("%s: %s has received", bcast_identifier, process_name);
373         }
374
375         VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
376         free(bcast_identifier);
377 }
378
379
380 static void action_sleep(xbt_dynar_t action)
381 {
382   char *name = NULL;
383   char *duration = xbt_dynar_get_as(action, 2, char *);
384   double clock = MSG_get_clock();
385
386   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
387     name = xbt_str_join(action, " ");
388
389   DEBUG1("Entering %s", name);
390   MSG_process_sleep(parse_double(duration));
391   VERB2("%s %f ", name, MSG_get_clock() - clock);
392
393   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
394     free(name);
395 }
396
397 static void action_allReduce(xbt_dynar_t action) {
398   int i;
399   char *allreduce_identifier;
400   char mailbox[80];
401   double comm_size = parse_double(xbt_dynar_get_as(action, 2, char *));
402   double comp_size = parse_double(xbt_dynar_get_as(action, 3, char *));
403   m_task_t task = NULL, comp_task = NULL;
404   const char *process_name;
405   double clock = MSG_get_clock();
406
407   process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
408
409   xbt_assert0(communicator_size, "Size of Communicator is not defined, "
410               "can't use collective operations");
411
412   process_name = MSG_process_get_name(MSG_process_self());
413
414   allreduce_identifier = bprintf("allReduce_%d", counters->allReduce_counter++);
415
416   if (!strcmp(process_name, "p0")) {
417     DEBUG2("%s: %s is the Root", allreduce_identifier, process_name);
418
419     msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
420     m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
421     for (i = 1; i < communicator_size; i++) {
422       sprintf(mailbox, "%s_p%d_p0", allreduce_identifier, i);
423       comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
424     }
425     MSG_comm_waitall(comms,communicator_size-1,-1);
426     for (i = 1; i < communicator_size; i++) {
427       MSG_comm_destroy(comms[i-1]);
428       MSG_task_destroy(tasks[i-1]);
429     }
430     free(tasks);
431
432     comp_task = MSG_task_create("allReduce_comp", comp_size, 0, NULL);
433     DEBUG1("%s: computing 'reduce_comp'", allreduce_identifier);
434     MSG_task_execute(comp_task);
435     MSG_task_destroy(comp_task);
436     DEBUG1("%s: computed", allreduce_identifier);
437
438     for (i = 1; i < communicator_size; i++) {
439       sprintf(mailbox, "%s_p0_p%d", allreduce_identifier, i);
440       comms[i-1] =
441           MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
442               mailbox);
443     }
444     MSG_comm_waitall(comms,communicator_size-1,-1);
445     for (i = 1; i < communicator_size; i++)
446        MSG_comm_destroy(comms[i-1]);
447     free(comms);
448
449     DEBUG2("%s: all messages sent by %s have been received",
450            allreduce_identifier, process_name);
451
452   } else {
453     DEBUG2("%s: %s sends", allreduce_identifier, process_name);
454     sprintf(mailbox, "%s_%s_p0", allreduce_identifier, process_name);
455     DEBUG1("put on %s", mailbox);
456     MSG_task_send(MSG_task_create(allreduce_identifier, 0, comm_size, NULL),
457                   mailbox);
458
459     sprintf(mailbox, "%s_p0_%s", allreduce_identifier, process_name);
460     MSG_task_receive(&task, mailbox);
461     MSG_task_destroy(task);
462     DEBUG2("%s: %s has received", allreduce_identifier, process_name);
463   }
464
465   VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
466   free(allreduce_identifier);
467 }
468
469 static void action_comm_size(xbt_dynar_t action)
470 {
471   char *name = NULL;
472   char *size = xbt_dynar_get_as(action, 2, char *);
473   double clock = MSG_get_clock();
474
475   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
476     name = xbt_str_join(action, " ");
477   communicator_size = parse_double(size);
478   VERB2("%s %f", name, MSG_get_clock() - clock);
479   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
480     free(name);
481 }
482
483 static void action_compute(xbt_dynar_t action)
484 {
485   char *name = NULL;
486   char *amout = xbt_dynar_get_as(action, 2, char *);
487   m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
488   double clock = MSG_get_clock();
489
490   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
491     name = xbt_str_join(action, " ");
492   DEBUG1("Entering %s", name);
493   MSG_task_execute(task);
494   MSG_task_destroy(task);
495   VERB2("%s %f", name, MSG_get_clock() - clock);
496   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
497     free(name);
498 }
499
500 static void action_init(xbt_dynar_t action)
501
502 #ifdef HAVE_TRACING
503   TRACE_smpi_init(get_rank(MSG_process_get_name(MSG_process_self())));
504 #endif
505   DEBUG0("Initialize the counters");
506   process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
507   globals->isends = xbt_dynar_new(sizeof(msg_comm_t),NULL);
508   globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t),NULL);
509   globals->tasks  = xbt_dynar_new(sizeof(m_task_t),NULL);
510   MSG_process_set_data(MSG_process_self(),globals);
511
512 }
513
514 static void action_finalize(xbt_dynar_t action)
515 {
516 #ifdef HAVE_TRACING
517   TRACE_smpi_finalize(get_rank(MSG_process_get_name(MSG_process_self())));
518 #endif
519   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
520   if (globals){
521     xbt_dynar_free_container(&(globals->isends));
522     free(globals);
523   }
524 }
525
526 /** Main function */
527 int main(int argc, char *argv[])
528 {
529   MSG_error_t res = MSG_OK;
530
531   /* Check the given arguments */
532   MSG_global_init(&argc, argv);
533   if (argc < 3) {
534     printf("Usage: %s platform_file deployment_file [action_files]\n",
535            argv[0]);
536     printf
537         ("example: %s msg_platform.xml msg_deployment.xml actions # if all actions are in the same file\n",
538          argv[0]);
539     printf
540         ("example: %s msg_platform.xml msg_deployment.xml # if actions are in separate files, specified in deployment\n",
541          argv[0]);
542     exit(1);
543   }
544
545   /*  Simulation setting */
546   MSG_create_environment(argv[1]);
547
548   /* No need to register functions as in classical MSG programs: the actions get started anyway */
549   MSG_launch_application(argv[2]);
550
551   /*   Action registration */
552   MSG_action_register("init",     action_init);
553   MSG_action_register("finalize", action_finalize);
554   MSG_action_register("comm_size",action_comm_size);
555   MSG_action_register("send",     action_send);
556   MSG_action_register("Isend",    action_Isend);
557   MSG_action_register("recv",     action_recv);
558   MSG_action_register("Irecv",    action_Irecv);
559   MSG_action_register("wait",     action_wait);
560   MSG_action_register("barrier",  action_barrier);
561   MSG_action_register("bcast",    action_bcast);
562   MSG_action_register("reduce",   action_reduce);
563   MSG_action_register("allReduce",action_allReduce);
564   MSG_action_register("sleep",    action_sleep);
565   MSG_action_register("compute",  action_compute);
566
567
568   /* Actually do the simulation using MSG_action_trace_run */
569   res = MSG_action_trace_run(argv[3]);  // it's ok to pass a NULL argument here
570
571   INFO1("Simulation time %g", MSG_get_clock());
572   MSG_clean();
573
574   if (res == MSG_OK)
575     return 0;
576   else
577     return 1;
578 }                               /* end_of_main */