Logo AND Algorithmique Numérique Distribuée

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