Logo AND Algorithmique Numérique Distribuée

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