Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
aec65c79b447b7a4afc0469f0b4edd5fc24655ce
[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 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory);
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(char*const* 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(char*const* action)
65 {
66   char *name = NULL;
67   char to[250];
68   const char *size_str = action[3];
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()),action[2]);
73
74   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
75     name = xbt_str_join_array(action, " ");
76
77 #ifdef HAVE_TRACING
78   int rank = get_rank(MSG_process_get_name(MSG_process_self()));
79   int dst_traced = get_rank(action[2]);
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(char*const* action)
104 {
105   char to[250];
106   const char *size = action[3];
107   double clock = MSG_get_clock();
108   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
109
110
111   sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),action[2]);
112   msg_comm_t comm =
113       MSG_task_isend( MSG_task_create(to,0,parse_double(size),NULL), to);
114   xbt_dynar_push(globals->isends,&comm);
115
116   DEBUG1("Isend on %s", MSG_process_get_name(MSG_process_self()));
117   VERB2("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
118
119   asynchronous_cleanup();
120 }
121
122
123 static void action_recv(char*const* action)
124 {
125   char *name = NULL;
126   char mailbox_name[250];
127   m_task_t task = NULL;
128   double clock = MSG_get_clock();
129
130   sprintf(mailbox_name, "%s_%s", action[2],
131           MSG_process_get_name(MSG_process_self()));
132
133   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
134     name = xbt_str_join_array(action, " ");
135
136 #ifdef HAVE_TRACING
137   int rank = get_rank(MSG_process_get_name(MSG_process_self()));
138   int src_traced = get_rank(action[2]);
139   TRACE_smpi_ptp_in(rank, src_traced, rank, "recv");
140 #endif
141
142   DEBUG1("Receiving: %s", name);
143   MSG_task_receive(&task, mailbox_name);
144   //  MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
145   VERB2("%s %f", name, MSG_get_clock() - clock);
146   MSG_task_destroy(task);
147
148   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
149     free(name);
150 #ifdef HAVE_TRACING
151   TRACE_smpi_ptp_out(rank, src_traced, rank, "recv");
152   TRACE_smpi_recv(rank, src_traced, rank);
153 #endif
154
155   asynchronous_cleanup();
156 }
157
158 static void action_Irecv(char*const* action)
159 {
160   char mailbox[250];
161   double clock = MSG_get_clock();
162   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
163
164   DEBUG1("Irecv on %s", MSG_process_get_name(MSG_process_self()));
165 #ifdef HAVE_TRACING
166   int rank = get_rank(MSG_process_get_name(MSG_process_self()));
167   int src_traced = get_rank(action[2]);
168   globals->last_Irecv_sender_id = src_traced;
169   MSG_process_set_data(MSG_process_self(), (void *) globals);
170
171   TRACE_smpi_ptp_in(rank, src_traced, rank, "Irecv");
172 #endif
173
174   sprintf(mailbox, "%s_%s", action[2],
175           MSG_process_get_name(MSG_process_self()));
176   m_task_t t=NULL;
177   xbt_dynar_push(globals->tasks,&t);
178   msg_comm_t c =
179       MSG_task_irecv(
180           xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks)-1),
181           mailbox);
182   xbt_dynar_push(globals->irecvs,&c);
183
184   VERB2("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
185
186 #ifdef HAVE_TRACING
187   TRACE_smpi_ptp_out(rank, src_traced, rank, "Irecv");
188 #endif
189
190   asynchronous_cleanup();
191 }
192
193
194 static void action_wait(char*const* action)
195 {
196   char *name = NULL;
197   m_task_t task = NULL;
198   msg_comm_t comm;
199   double clock = MSG_get_clock();
200   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
201
202   xbt_assert1(xbt_dynar_length(globals->irecvs),
203       "action wait not preceded by any irecv: %s", xbt_str_join_array(action," "));
204
205   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
206     name = xbt_str_join_array(action, " ");
207 #ifdef HAVE_TRACING
208   process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
209   int src_traced = counters->last_Irecv_sender_id;
210   int rank = get_rank(MSG_process_get_name(MSG_process_self()));
211   TRACE_smpi_ptp_in(rank, src_traced, rank, "wait");
212 #endif
213
214   DEBUG1("Entering %s", name);
215   comm = xbt_dynar_pop_as(globals->irecvs,msg_comm_t);
216   MSG_comm_wait(comm,-1);
217   task = xbt_dynar_pop_as(globals->tasks,m_task_t);
218   MSG_comm_destroy(comm);
219   MSG_task_destroy(task);
220
221   VERB2("%s %f", name, MSG_get_clock() - clock);
222   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
223     free(name);
224 #ifdef HAVE_TRACING
225   TRACE_smpi_ptp_out(rank, src_traced, rank, "wait");
226   TRACE_smpi_recv(rank, src_traced, rank);
227 #endif
228
229 }
230
231 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
232 static void action_barrier(char*const* action)
233 {
234   char *name = NULL;
235   static smx_mutex_t mutex = NULL;
236   static smx_cond_t cond = NULL;
237   static int processes_arrived_sofar=0;
238
239   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
240     name = xbt_str_join_array(action, " ");
241
242   if (mutex == NULL) {       // first arriving on the barrier
243     mutex = SIMIX_req_mutex_init();
244     cond = SIMIX_req_cond_init();
245     processes_arrived_sofar=0;
246   }
247   DEBUG2("Entering barrier: %s (%d already there)", name,processes_arrived_sofar);
248
249   SIMIX_req_mutex_lock(mutex);
250   if (++processes_arrived_sofar == communicator_size) {
251     SIMIX_req_cond_broadcast(cond);
252     SIMIX_req_mutex_unlock(mutex);
253   } else {
254     SIMIX_req_cond_wait(cond,mutex);
255     SIMIX_req_mutex_unlock(mutex);
256   }
257
258   DEBUG1("Exiting barrier: %s", name);
259
260   processes_arrived_sofar--;
261   if (!processes_arrived_sofar) {
262     SIMIX_req_cond_destroy(cond);
263     SIMIX_req_mutex_destroy(mutex);
264     mutex=NULL;
265   }
266
267   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
268     free(name);
269
270 }
271
272 static void action_reduce(char*const* action)
273 {
274         int i;
275         char *reduce_identifier;
276         char mailbox[80];
277         double comm_size = parse_double(action[2]);
278         double comp_size = parse_double(action[3]);
279         m_task_t comp_task = NULL;
280         const char *process_name;
281         double clock = MSG_get_clock();
282
283         process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
284
285         xbt_assert0(communicator_size, "Size of Communicator is not defined, "
286                         "can't use collective operations");
287
288         process_name = MSG_process_get_name(MSG_process_self());
289
290         reduce_identifier = bprintf("reduce_%d", counters->reduce_counter++);
291
292         if (!strcmp(process_name, "p0")) {
293                 DEBUG2("%s: %s is the Root", reduce_identifier, process_name);
294
295                 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
296             m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
297             for (i = 1; i < communicator_size; i++) {
298               sprintf(mailbox, "%s_p%d_p0", reduce_identifier, i);
299               comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
300             }
301             MSG_comm_waitall(comms,communicator_size-1,-1);
302             for (i = 1; i < communicator_size; i++) {
303                 MSG_comm_destroy(comms[i-1]);
304                 MSG_task_destroy(tasks[i-1]);
305             }
306             free(tasks);
307
308             comp_task = MSG_task_create("reduce_comp", comp_size, 0, NULL);
309             DEBUG1("%s: computing 'reduce_comp'", reduce_identifier);
310             MSG_task_execute(comp_task);
311             MSG_task_destroy(comp_task);
312             DEBUG1("%s: computed", reduce_identifier);
313
314         } else {
315                 DEBUG2("%s: %s sends", reduce_identifier, process_name);
316                 sprintf(mailbox, "%s_%s_p0", reduce_identifier, process_name);
317             DEBUG1("put on %s", mailbox);
318             MSG_task_send(MSG_task_create(reduce_identifier, 0, comm_size, NULL),
319                           mailbox);
320         }
321
322         VERB2("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
323         free(reduce_identifier);
324 }
325
326 static void action_bcast(char*const* action)
327 {
328         int i;
329         char *bcast_identifier;
330         char mailbox[80];
331         double comm_size = parse_double(action[2]);
332         m_task_t task = NULL;
333         const char *process_name;
334         double clock = MSG_get_clock();
335
336         process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
337
338         xbt_assert0(communicator_size, "Size of Communicator is not defined, "
339                         "can't use collective operations");
340
341         process_name = MSG_process_get_name(MSG_process_self());
342
343         bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
344
345         if (!strcmp(process_name, "p0")) {
346                 DEBUG2("%s: %s is the Root", bcast_identifier, process_name);
347
348             msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
349
350             for (i = 1; i < communicator_size; i++) {
351               sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
352               comms[i-1] =
353                   MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
354                       mailbox);
355             }
356             MSG_comm_waitall(comms,communicator_size-1,-1);
357                 for (i = 1; i < communicator_size; i++)
358                MSG_comm_destroy(comms[i-1]);
359             free(comms);
360
361             DEBUG2("%s: all messages sent by %s have been received",
362                    bcast_identifier, process_name);
363
364         } else {
365             sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
366             MSG_task_receive(&task, mailbox);
367             MSG_task_destroy(task);
368             DEBUG2("%s: %s has received", bcast_identifier, process_name);
369         }
370
371         VERB2("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
372         free(bcast_identifier);
373 }
374
375
376 static void action_sleep(char*const* action)
377 {
378   char *name = NULL;
379   const char *duration = action[2];
380   double clock = MSG_get_clock();
381
382   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
383     name = xbt_str_join_array(action, " ");
384
385   DEBUG1("Entering %s", name);
386   MSG_process_sleep(parse_double(duration));
387   VERB2("%s %f ", name, MSG_get_clock() - clock);
388
389   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
390     free(name);
391 }
392
393 static void action_allReduce(char*const* action) {
394   int i;
395   char *allreduce_identifier;
396   char mailbox[80];
397   double comm_size = parse_double(action[2]);
398   double comp_size = parse_double(action[3]);
399   m_task_t task = NULL, comp_task = NULL;
400   const char *process_name;
401   double clock = MSG_get_clock();
402
403   process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
404
405   xbt_assert0(communicator_size, "Size of Communicator is not defined, "
406               "can't use collective operations");
407
408   process_name = MSG_process_get_name(MSG_process_self());
409
410   allreduce_identifier = bprintf("allReduce_%d", counters->allReduce_counter++);
411
412   if (!strcmp(process_name, "p0")) {
413     DEBUG2("%s: %s is the Root", allreduce_identifier, process_name);
414
415     msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
416     m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
417     for (i = 1; i < communicator_size; i++) {
418       sprintf(mailbox, "%s_p%d_p0", allreduce_identifier, i);
419       comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
420     }
421     MSG_comm_waitall(comms,communicator_size-1,-1);
422     for (i = 1; i < communicator_size; i++) {
423       MSG_comm_destroy(comms[i-1]);
424       MSG_task_destroy(tasks[i-1]);
425     }
426     free(tasks);
427
428     comp_task = MSG_task_create("allReduce_comp", comp_size, 0, NULL);
429     DEBUG1("%s: computing 'reduce_comp'", allreduce_identifier);
430     MSG_task_execute(comp_task);
431     MSG_task_destroy(comp_task);
432     DEBUG1("%s: computed", allreduce_identifier);
433
434     for (i = 1; i < communicator_size; i++) {
435       sprintf(mailbox, "%s_p0_p%d", allreduce_identifier, i);
436       comms[i-1] =
437           MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
438               mailbox);
439     }
440     MSG_comm_waitall(comms,communicator_size-1,-1);
441     for (i = 1; i < communicator_size; i++)
442        MSG_comm_destroy(comms[i-1]);
443     free(comms);
444
445     DEBUG2("%s: all messages sent by %s have been received",
446            allreduce_identifier, process_name);
447
448   } else {
449     DEBUG2("%s: %s sends", allreduce_identifier, process_name);
450     sprintf(mailbox, "%s_%s_p0", allreduce_identifier, process_name);
451     DEBUG1("put on %s", mailbox);
452     MSG_task_send(MSG_task_create(allreduce_identifier, 0, comm_size, NULL),
453                   mailbox);
454
455     sprintf(mailbox, "%s_p0_%s", allreduce_identifier, process_name);
456     MSG_task_receive(&task, mailbox);
457     MSG_task_destroy(task);
458     DEBUG2("%s: %s has received", allreduce_identifier, process_name);
459   }
460
461   VERB2("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
462   free(allreduce_identifier);
463 }
464
465 static void action_comm_size(char*const* action)
466 {
467   char *name = NULL;
468   const char *size = action[2];
469   double clock = MSG_get_clock();
470
471   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
472     name = xbt_str_join_array(action, " ");
473   communicator_size = parse_double(size);
474   VERB2("%s %f", name, MSG_get_clock() - clock);
475   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
476     free(name);
477 }
478
479 static void action_compute(char*const* action)
480 {
481   char *name = NULL;
482   const char *amout = action[2];
483   m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
484   double clock = MSG_get_clock();
485
486   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
487     name = xbt_str_join_array(action, " ");
488   DEBUG1("Entering %s", name);
489   MSG_task_execute(task);
490   MSG_task_destroy(task);
491   VERB2("%s %f", name, MSG_get_clock() - clock);
492   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
493     free(name);
494 }
495
496 static void action_init(char*const* action)
497
498 #ifdef HAVE_TRACING
499   TRACE_smpi_init(get_rank(MSG_process_get_name(MSG_process_self())));
500 #endif
501   DEBUG0("Initialize the counters");
502   process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
503   globals->isends = xbt_dynar_new(sizeof(msg_comm_t),NULL);
504   globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t),NULL);
505   globals->tasks  = xbt_dynar_new(sizeof(m_task_t),NULL);
506   MSG_process_set_data(MSG_process_self(),globals);
507
508 }
509
510 static void action_finalize(char*const* action)
511 {
512 #ifdef HAVE_TRACING
513   TRACE_smpi_finalize(get_rank(MSG_process_get_name(MSG_process_self())));
514 #endif
515   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
516   if (globals){
517     xbt_dynar_free_container(&(globals->isends));
518     xbt_dynar_free_container(&(globals->irecvs));
519     xbt_dynar_free_container(&(globals->tasks));
520     free(globals);
521   }
522 }
523
524 /** Main function */
525 int main(int argc, char *argv[])
526 {
527   MSG_error_t res = MSG_OK;
528
529   smx_factory_initializer_to_use = SIMIX_ctx_raw_factory_init;
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 */