Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
implement actions irecv and wait w/o spawning; kill spawning functions
[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_task_destroy(task);
223
224   VERB2("%s %f", name, MSG_get_clock() - clock);
225   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
226     free(name);
227 #ifdef HAVE_TRACING
228   TRACE_smpi_ptp_out(rank, src_traced, rank, "wait");
229   TRACE_smpi_recv(rank, src_traced, rank);
230 #endif
231
232 }
233
234 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
235 static void action_barrier(xbt_dynar_t action)
236 {
237   char *name = NULL;
238   static smx_mutex_t mutex = NULL;
239   static smx_cond_t cond = NULL;
240   static int processes_arrived_sofar=0;
241
242   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
243     name = xbt_str_join(action, " ");
244
245   if (mutex == NULL) {       // first arriving on the barrier
246     mutex = SIMIX_req_mutex_init();
247     cond = SIMIX_req_cond_init();
248     processes_arrived_sofar=0;
249   }
250   DEBUG2("Entering barrier: %s (%d already there)", name,processes_arrived_sofar);
251
252   SIMIX_req_mutex_lock(mutex);
253   if (++processes_arrived_sofar == communicator_size) {
254     SIMIX_req_cond_broadcast(cond);
255     SIMIX_req_mutex_unlock(mutex);
256   } else {
257     SIMIX_req_cond_wait(cond,mutex);
258     SIMIX_req_mutex_unlock(mutex);
259   }
260
261   DEBUG1("Exiting barrier: %s", name);
262
263   processes_arrived_sofar--;
264   if (!processes_arrived_sofar) {
265     SIMIX_req_cond_destroy(cond);
266     SIMIX_req_mutex_destroy(mutex);
267     mutex=NULL;
268   }
269
270   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
271     free(name);
272
273 }
274
275 static void action_reduce(xbt_dynar_t action)
276 {
277         int i;
278         char *reduce_identifier;
279         char mailbox[80];
280         double comm_size = parse_double(xbt_dynar_get_as(action, 2, char *));
281         double comp_size = parse_double(xbt_dynar_get_as(action, 3, char *));
282         m_task_t comp_task = NULL;
283         const char *process_name;
284         double clock = MSG_get_clock();
285
286         process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
287
288         xbt_assert0(communicator_size, "Size of Communicator is not defined, "
289                         "can't use collective operations");
290
291         process_name = MSG_process_get_name(MSG_process_self());
292
293         reduce_identifier = bprintf("reduce_%d", counters->reduce_counter++);
294
295         if (!strcmp(process_name, "p0")) {
296                 DEBUG2("%s: %s is the Root", reduce_identifier, process_name);
297
298                 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
299             m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
300             for (i = 1; i < communicator_size; i++) {
301               sprintf(mailbox, "%s_p%d_p0", reduce_identifier, i);
302               comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
303             }
304             MSG_comm_waitall(comms,communicator_size-1,-1);
305             for (i = 1; i < communicator_size; i++) {
306                 MSG_comm_destroy(comms[i-1]);
307                 MSG_task_destroy(tasks[i-1]);
308             }
309             free(tasks);
310
311             comp_task = MSG_task_create("reduce_comp", comp_size, 0, NULL);
312             DEBUG1("%s: computing 'reduce_comp'", reduce_identifier);
313             MSG_task_execute(comp_task);
314             MSG_task_destroy(comp_task);
315             DEBUG1("%s: computed", reduce_identifier);
316
317         } else {
318                 DEBUG2("%s: %s sends", reduce_identifier, process_name);
319                 sprintf(mailbox, "%s_%s_p0", reduce_identifier, process_name);
320             DEBUG1("put on %s", mailbox);
321             MSG_task_send(MSG_task_create(reduce_identifier, 0, comm_size, NULL),
322                           mailbox);
323         }
324
325         VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
326         free(reduce_identifier);
327 }
328
329 static void action_bcast(xbt_dynar_t action)
330 {
331         int i;
332         char *bcast_identifier;
333         char mailbox[80];
334         double comm_size = parse_double(xbt_dynar_get_as(action, 2, char *));
335         m_task_t task = NULL;
336         const char *process_name;
337         double clock = MSG_get_clock();
338
339         process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
340
341         xbt_assert0(communicator_size, "Size of Communicator is not defined, "
342                         "can't use collective operations");
343
344         process_name = MSG_process_get_name(MSG_process_self());
345
346         bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
347
348         if (!strcmp(process_name, "p0")) {
349                 DEBUG2("%s: %s is the Root", bcast_identifier, process_name);
350
351             msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
352
353             for (i = 1; i < communicator_size; i++) {
354               sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
355               comms[i-1] =
356                   MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
357                       mailbox);
358             }
359             MSG_comm_waitall(comms,communicator_size-1,-1);
360                 for (i = 1; i < communicator_size; i++)
361                MSG_comm_destroy(comms[i-1]);
362             free(comms);
363
364             DEBUG2("%s: all messages sent by %s have been received",
365                    bcast_identifier, process_name);
366
367         } else {
368             sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
369             MSG_task_receive(&task, mailbox);
370             MSG_task_destroy(task);
371             DEBUG2("%s: %s has received", bcast_identifier, process_name);
372         }
373
374         VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
375         free(bcast_identifier);
376 }
377
378
379 static void action_sleep(xbt_dynar_t action)
380 {
381   char *name = NULL;
382   char *duration = xbt_dynar_get_as(action, 2, char *);
383   double clock = MSG_get_clock();
384
385   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
386     name = xbt_str_join(action, " ");
387
388   DEBUG1("Entering %s", name);
389   MSG_process_sleep(parse_double(duration));
390   VERB2("%s %f ", name, MSG_get_clock() - clock);
391
392   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
393     free(name);
394 }
395
396 static void action_allReduce(xbt_dynar_t action) {
397   int i;
398   char *allreduce_identifier;
399   char mailbox[80];
400   double comm_size = parse_double(xbt_dynar_get_as(action, 2, char *));
401   double comp_size = parse_double(xbt_dynar_get_as(action, 3, char *));
402   m_task_t task = NULL, comp_task = NULL;
403   const char *process_name;
404   double clock = MSG_get_clock();
405
406   process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
407
408   xbt_assert0(communicator_size, "Size of Communicator is not defined, "
409               "can't use collective operations");
410
411   process_name = MSG_process_get_name(MSG_process_self());
412
413   allreduce_identifier = bprintf("allReduce_%d", counters->allReduce_counter++);
414
415   if (!strcmp(process_name, "p0")) {
416     DEBUG2("%s: %s is the Root", allreduce_identifier, process_name);
417
418     msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
419     m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
420     for (i = 1; i < communicator_size; i++) {
421       sprintf(mailbox, "%s_p%d_p0", allreduce_identifier, i);
422       comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
423     }
424     MSG_comm_waitall(comms,communicator_size-1,-1);
425     for (i = 1; i < communicator_size; i++) {
426       MSG_comm_destroy(comms[i-1]);
427       MSG_task_destroy(tasks[i-1]);
428     }
429     free(tasks);
430
431     comp_task = MSG_task_create("allReduce_comp", comp_size, 0, NULL);
432     DEBUG1("%s: computing 'reduce_comp'", allreduce_identifier);
433     MSG_task_execute(comp_task);
434     MSG_task_destroy(comp_task);
435     DEBUG1("%s: computed", allreduce_identifier);
436
437     for (i = 1; i < communicator_size; i++) {
438       sprintf(mailbox, "%s_p0_p%d", allreduce_identifier, i);
439       comms[i-1] =
440           MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
441               mailbox);
442     }
443     MSG_comm_waitall(comms,communicator_size-1,-1);
444     for (i = 1; i < communicator_size; i++)
445        MSG_comm_destroy(comms[i-1]);
446     free(comms);
447
448     DEBUG2("%s: all messages sent by %s have been received",
449            allreduce_identifier, process_name);
450
451   } else {
452     DEBUG2("%s: %s sends", allreduce_identifier, process_name);
453     sprintf(mailbox, "%s_%s_p0", allreduce_identifier, process_name);
454     DEBUG1("put on %s", mailbox);
455     MSG_task_send(MSG_task_create(allreduce_identifier, 0, comm_size, NULL),
456                   mailbox);
457
458     sprintf(mailbox, "%s_p0_%s", allreduce_identifier, process_name);
459     MSG_task_receive(&task, mailbox);
460     MSG_task_destroy(task);
461     DEBUG2("%s: %s has received", allreduce_identifier, process_name);
462   }
463
464   VERB2("%s %f", xbt_str_join(action, " "), MSG_get_clock() - clock);
465   free(allreduce_identifier);
466 }
467
468 static void action_comm_size(xbt_dynar_t action)
469 {
470   char *name = NULL;
471   char *size = xbt_dynar_get_as(action, 2, char *);
472   double clock = MSG_get_clock();
473
474   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
475     name = xbt_str_join(action, " ");
476   communicator_size = parse_double(size);
477   VERB2("%s %f", name, MSG_get_clock() - clock);
478   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
479     free(name);
480 }
481
482 static void action_compute(xbt_dynar_t action)
483 {
484   char *name = NULL;
485   char *amout = xbt_dynar_get_as(action, 2, char *);
486   m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
487   double clock = MSG_get_clock();
488
489   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
490     name = xbt_str_join(action, " ");
491   DEBUG1("Entering %s", name);
492   MSG_task_execute(task);
493   MSG_task_destroy(task);
494   VERB2("%s %f", name, MSG_get_clock() - clock);
495   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
496     free(name);
497 }
498
499 static void action_init(xbt_dynar_t action)
500
501 #ifdef HAVE_TRACING
502   TRACE_smpi_init(get_rank(MSG_process_get_name(MSG_process_self())));
503 #endif
504   DEBUG0("Initialize the counters");
505   process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
506   globals->isends = xbt_dynar_new(sizeof(msg_comm_t),NULL);
507   globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t),NULL);
508   globals->tasks  = xbt_dynar_new(sizeof(m_task_t),NULL);
509   MSG_process_set_data(MSG_process_self(),globals);
510
511 }
512
513 static void action_finalize(xbt_dynar_t action)
514 {
515 #ifdef HAVE_TRACING
516   TRACE_smpi_finalize(get_rank(MSG_process_get_name(MSG_process_self())));
517 #endif
518   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
519   if (globals){
520     xbt_dynar_free_container(&(globals->isends));
521     free(globals);
522   }
523 }
524
525 /** Main function */
526 int main(int argc, char *argv[])
527 {
528   MSG_error_t res = MSG_OK;
529
530   /* Check the given arguments */
531   MSG_global_init(&argc, argv);
532   if (argc < 3) {
533     printf("Usage: %s platform_file deployment_file [action_files]\n",
534            argv[0]);
535     printf
536         ("example: %s msg_platform.xml msg_deployment.xml actions # if all actions are in the same file\n",
537          argv[0]);
538     printf
539         ("example: %s msg_platform.xml msg_deployment.xml # if actions are in separate files, specified in deployment\n",
540          argv[0]);
541     exit(1);
542   }
543
544   /*  Simulation setting */
545   MSG_create_environment(argv[1]);
546
547   /* No need to register functions as in classical MSG programs: the actions get started anyway */
548   MSG_launch_application(argv[2]);
549
550   /*   Action registration */
551   MSG_action_register("init",     action_init);
552   MSG_action_register("finalize", action_finalize);
553   MSG_action_register("comm_size",action_comm_size);
554   MSG_action_register("send",     action_send);
555   MSG_action_register("Isend",    action_Isend);
556   MSG_action_register("recv",     action_recv);
557   MSG_action_register("Irecv",    action_Irecv);
558   MSG_action_register("wait",     action_wait);
559   MSG_action_register("barrier",  action_barrier);
560   MSG_action_register("bcast",    action_bcast);
561   MSG_action_register("reduce",   action_reduce);
562   MSG_action_register("allReduce",action_allReduce);
563   MSG_action_register("sleep",    action_sleep);
564   MSG_action_register("compute",  action_compute);
565
566
567   /* Actually do the simulation using MSG_action_trace_run */
568   res = MSG_action_trace_run(argv[3]);  // it's ok to pass a NULL argument here
569
570   INFO1("Simulation time %g", MSG_get_clock());
571   MSG_clean();
572
573   if (res == MSG_OK)
574     return 0;
575   else
576     return 1;
577 }                               /* end_of_main */