Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Parameters were swapped.
[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 "msg/mailbox.h"        /* we play funny tricks with mailboxes and rdv points */
11 #include "simix/simix.h"        /* semaphores for the barrier */
12 #include "xbt.h"                /* calloc, printf */
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(const 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(const 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   XBT_DEBUG("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    XBT_VERB("%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(const 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   m_task_t task = MSG_task_create(to,0,parse_double(size),NULL);
113   msg_comm_t comm = MSG_task_isend_with_matching(task, to, /*matching madness*/NULL,task);
114   xbt_dynar_push(globals->isends,&comm);
115
116   XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
117   XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
118
119   asynchronous_cleanup();
120 }
121
122 static int task_matching(void*ignored,void*sent_task) {
123   m_task_t t = (m_task_t)sent_task;
124   if (t!=NULL && MSG_task_get_data_size(t)<65536)
125     return 1; /* that's supposed to be already arrived */
126   return 0; /* rendez-vous mode: it's not there yet */
127 }
128
129 static void action_recv(const char *const *action)
130 {
131   char *name = NULL;
132   char mailbox_name[250];
133   m_task_t task = NULL;
134   double clock = MSG_get_clock();
135
136   sprintf(mailbox_name, "%s_%s", action[2],
137           MSG_process_get_name(MSG_process_self()));
138
139   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
140     name = xbt_str_join_array(action, " ");
141
142   /* The next chunk is to deal with the fact that for short messages,
143    * if the send occurs before the receive, the message is already sent and
144    * buffered on receiver side when the recv() occurs.
145    *
146    * So the next chunk detects this fact and cancel the simix communication instead.
147    */
148
149   /* make sure the rdv is created on need by asking to MSG instead of simix directly */
150   smx_rdv_t rdv = MSG_mailbox_get_by_alias(mailbox_name);
151   smx_action_t act = SIMIX_comm_get_send_match(rdv,task_matching,NULL);
152   if (act!=NULL){
153     /* FIXME account for the memcopy time if needed */
154     SIMIX_comm_finish(act);
155     return;
156   }
157
158 #ifdef HAVE_TRACING
159   int rank = get_rank(MSG_process_get_name(MSG_process_self()));
160   int src_traced = get_rank(action[2]);
161   TRACE_smpi_ptp_in(rank, src_traced, rank, "recv");
162 #endif
163
164   XBT_DEBUG("Receiving: %s", name);
165   MSG_task_receive(&task, mailbox_name);
166   //  MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
167   XBT_VERB("%s %f", name, MSG_get_clock() - clock);
168   MSG_task_destroy(task);
169
170   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
171     free(name);
172 #ifdef HAVE_TRACING
173   TRACE_smpi_ptp_out(rank, src_traced, rank, "recv");
174   TRACE_smpi_recv(rank, src_traced, rank);
175 #endif
176
177   asynchronous_cleanup();
178 }
179
180 static void action_Irecv(const char *const *action)
181 {
182   char mailbox[250];
183   double clock = MSG_get_clock();
184   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
185
186   XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
187 #ifdef HAVE_TRACING
188   int rank = get_rank(MSG_process_get_name(MSG_process_self()));
189   int src_traced = get_rank(action[2]);
190   globals->last_Irecv_sender_id = src_traced;
191   MSG_process_set_data(MSG_process_self(), (void *) globals);
192
193   TRACE_smpi_ptp_in(rank, src_traced, rank, "Irecv");
194 #endif
195
196   sprintf(mailbox, "%s_%s", action[2],
197           MSG_process_get_name(MSG_process_self()));
198   m_task_t t=NULL;
199   xbt_dynar_push(globals->tasks,&t);
200   msg_comm_t c =
201       MSG_task_irecv(
202           xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks)-1),
203           mailbox);
204   xbt_dynar_push(globals->irecvs,&c);
205
206   XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
207
208 #ifdef HAVE_TRACING
209   TRACE_smpi_ptp_out(rank, src_traced, rank, "Irecv");
210 #endif
211
212   asynchronous_cleanup();
213 }
214
215
216 static void action_wait(const char *const *action)
217 {
218   char *name = NULL;
219   m_task_t task = NULL;
220   msg_comm_t comm;
221   double clock = MSG_get_clock();
222   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
223
224   xbt_assert1(xbt_dynar_length(globals->irecvs),
225       "action wait not preceded by any irecv: %s", xbt_str_join_array(action," "));
226
227   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
228     name = xbt_str_join_array(action, " ");
229 #ifdef HAVE_TRACING
230   process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
231   int src_traced = counters->last_Irecv_sender_id;
232   int rank = get_rank(MSG_process_get_name(MSG_process_self()));
233   TRACE_smpi_ptp_in(rank, src_traced, rank, "wait");
234 #endif
235
236   XBT_DEBUG("Entering %s", name);
237   comm = xbt_dynar_pop_as(globals->irecvs,msg_comm_t);
238   MSG_comm_wait(comm,-1);
239   task = xbt_dynar_pop_as(globals->tasks,m_task_t);
240   MSG_comm_destroy(comm);
241   MSG_task_destroy(task);
242
243   XBT_VERB("%s %f", name, MSG_get_clock() - clock);
244   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
245     free(name);
246 #ifdef HAVE_TRACING
247   TRACE_smpi_ptp_out(rank, src_traced, rank, "wait");
248   TRACE_smpi_recv(rank, src_traced, rank);
249 #endif
250
251 }
252
253 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
254 static void action_barrier(const char *const *action)
255 {
256   char *name = NULL;
257   static smx_mutex_t mutex = NULL;
258   static smx_cond_t cond = NULL;
259   static int processes_arrived_sofar=0;
260
261   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
262     name = xbt_str_join_array(action, " ");
263
264   if (mutex == NULL) {       // first arriving on the barrier
265     mutex = SIMIX_req_mutex_init();
266     cond = SIMIX_req_cond_init();
267     processes_arrived_sofar=0;
268   }
269   XBT_DEBUG("Entering barrier: %s (%d already there)", name,processes_arrived_sofar);
270
271   SIMIX_req_mutex_lock(mutex);
272   if (++processes_arrived_sofar == communicator_size) {
273     SIMIX_req_cond_broadcast(cond);
274     SIMIX_req_mutex_unlock(mutex);
275   } else {
276     SIMIX_req_cond_wait(cond,mutex);
277     SIMIX_req_mutex_unlock(mutex);
278   }
279
280   XBT_DEBUG("Exiting barrier: %s", name);
281
282   processes_arrived_sofar--;
283   if (!processes_arrived_sofar) {
284     SIMIX_req_cond_destroy(cond);
285     SIMIX_req_mutex_destroy(mutex);
286     mutex=NULL;
287   }
288
289   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
290     free(name);
291
292 }
293
294 static void action_reduce(const char *const *action)
295 {
296         int i;
297         char *reduce_identifier;
298         char mailbox[80];
299         double comm_size = parse_double(action[2]);
300         double comp_size = parse_double(action[3]);
301         m_task_t comp_task = NULL;
302         const char *process_name;
303         double clock = MSG_get_clock();
304
305         process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
306
307         xbt_assert0(communicator_size, "Size of Communicator is not defined, "
308                         "can't use collective operations");
309
310         process_name = MSG_process_get_name(MSG_process_self());
311
312         reduce_identifier = bprintf("reduce_%d", counters->reduce_counter++);
313
314         if (!strcmp(process_name, "p0")) {
315                 XBT_DEBUG("%s: %s is the Root", reduce_identifier, process_name);
316
317                 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
318             m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
319             for (i = 1; i < communicator_size; i++) {
320               sprintf(mailbox, "%s_p%d_p0", reduce_identifier, i);
321               comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
322             }
323             MSG_comm_waitall(comms,communicator_size-1,-1);
324             for (i = 1; i < communicator_size; i++) {
325                 MSG_comm_destroy(comms[i-1]);
326                 MSG_task_destroy(tasks[i-1]);
327             }
328             free(tasks);
329
330             comp_task = MSG_task_create("reduce_comp", comp_size, 0, NULL);
331             XBT_DEBUG("%s: computing 'reduce_comp'", reduce_identifier);
332             MSG_task_execute(comp_task);
333             MSG_task_destroy(comp_task);
334             XBT_DEBUG("%s: computed", reduce_identifier);
335
336         } else {
337                 XBT_DEBUG("%s: %s sends", reduce_identifier, process_name);
338                 sprintf(mailbox, "%s_%s_p0", reduce_identifier, process_name);
339             XBT_DEBUG("put on %s", mailbox);
340             MSG_task_send(MSG_task_create(reduce_identifier, 0, comm_size, NULL),
341                           mailbox);
342         }
343
344         XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
345         free(reduce_identifier);
346 }
347
348 static void action_bcast(const char *const *action)
349 {
350         int i;
351         char *bcast_identifier;
352         char mailbox[80];
353         double comm_size = parse_double(action[2]);
354         m_task_t task = NULL;
355         const char *process_name;
356         double clock = MSG_get_clock();
357
358         process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
359
360         xbt_assert0(communicator_size, "Size of Communicator is not defined, "
361                         "can't use collective operations");
362
363         process_name = MSG_process_get_name(MSG_process_self());
364
365         bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
366
367         if (!strcmp(process_name, "p0")) {
368                 XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
369
370             msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
371
372             for (i = 1; i < communicator_size; i++) {
373               sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
374               comms[i-1] =
375                   MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
376                       mailbox);
377             }
378             MSG_comm_waitall(comms,communicator_size-1,-1);
379                 for (i = 1; i < communicator_size; i++)
380                MSG_comm_destroy(comms[i-1]);
381             free(comms);
382
383             XBT_DEBUG("%s: all messages sent by %s have been received",
384                    bcast_identifier, process_name);
385
386         } else {
387             sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
388             MSG_task_receive(&task, mailbox);
389             MSG_task_destroy(task);
390             XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
391         }
392
393         XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
394         free(bcast_identifier);
395 }
396
397
398 static void action_sleep(const char *const *action)
399 {
400   char *name = NULL;
401   const char *duration = action[2];
402   double clock = MSG_get_clock();
403
404   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
405     name = xbt_str_join_array(action, " ");
406
407   XBT_DEBUG("Entering %s", name);
408   MSG_process_sleep(parse_double(duration));
409   XBT_VERB("%s %f ", name, MSG_get_clock() - clock);
410
411   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
412     free(name);
413 }
414
415 static void action_allReduce(const char *const *action) {
416   int i;
417   char *allreduce_identifier;
418   char mailbox[80];
419   double comm_size = parse_double(action[2]);
420   double comp_size = parse_double(action[3]);
421   m_task_t task = NULL, comp_task = NULL;
422   const char *process_name;
423   double clock = MSG_get_clock();
424
425   process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
426
427   xbt_assert0(communicator_size, "Size of Communicator is not defined, "
428               "can't use collective operations");
429
430   process_name = MSG_process_get_name(MSG_process_self());
431
432   allreduce_identifier = bprintf("allReduce_%d", counters->allReduce_counter++);
433
434   if (!strcmp(process_name, "p0")) {
435     XBT_DEBUG("%s: %s is the Root", allreduce_identifier, process_name);
436
437     msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
438     m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
439     for (i = 1; i < communicator_size; i++) {
440       sprintf(mailbox, "%s_p%d_p0", allreduce_identifier, i);
441       comms[i-1] = MSG_task_irecv(&(tasks[i-1]),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       MSG_task_destroy(tasks[i-1]);
447     }
448     free(tasks);
449
450     comp_task = MSG_task_create("allReduce_comp", comp_size, 0, NULL);
451     XBT_DEBUG("%s: computing 'reduce_comp'", allreduce_identifier);
452     MSG_task_execute(comp_task);
453     MSG_task_destroy(comp_task);
454     XBT_DEBUG("%s: computed", allreduce_identifier);
455
456     for (i = 1; i < communicator_size; i++) {
457       sprintf(mailbox, "%s_p0_p%d", allreduce_identifier, i);
458       comms[i-1] =
459           MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
460               mailbox);
461     }
462     MSG_comm_waitall(comms,communicator_size-1,-1);
463     for (i = 1; i < communicator_size; i++)
464        MSG_comm_destroy(comms[i-1]);
465     free(comms);
466
467     XBT_DEBUG("%s: all messages sent by %s have been received",
468            allreduce_identifier, process_name);
469
470   } else {
471     XBT_DEBUG("%s: %s sends", allreduce_identifier, process_name);
472     sprintf(mailbox, "%s_%s_p0", allreduce_identifier, process_name);
473     XBT_DEBUG("put on %s", mailbox);
474     MSG_task_send(MSG_task_create(allreduce_identifier, 0, comm_size, NULL),
475                   mailbox);
476
477     sprintf(mailbox, "%s_p0_%s", allreduce_identifier, process_name);
478     MSG_task_receive(&task, mailbox);
479     MSG_task_destroy(task);
480     XBT_DEBUG("%s: %s has received", allreduce_identifier, process_name);
481   }
482
483   XBT_VERB("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
484   free(allreduce_identifier);
485 }
486
487 static void action_comm_size(const char *const *action)
488 {
489   char *name = NULL;
490   const char *size = action[2];
491   double clock = MSG_get_clock();
492
493   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
494     name = xbt_str_join_array(action, " ");
495   communicator_size = parse_double(size);
496   XBT_VERB("%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_compute(const char *const *action)
502 {
503   char *name = NULL;
504   const char *amout = action[2];
505   m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
506   double clock = MSG_get_clock();
507
508   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
509     name = xbt_str_join_array(action, " ");
510   XBT_DEBUG("Entering %s", name);
511   MSG_task_execute(task);
512   MSG_task_destroy(task);
513   XBT_VERB("%s %f", name, MSG_get_clock() - clock);
514   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
515     free(name);
516 }
517
518 static void action_init(const char *const *action)
519
520 #ifdef HAVE_TRACING
521   TRACE_smpi_init(get_rank(MSG_process_get_name(MSG_process_self())));
522 #endif
523   XBT_DEBUG("Initialize the counters");
524   process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
525   globals->isends = xbt_dynar_new(sizeof(msg_comm_t),NULL);
526   globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t),NULL);
527   globals->tasks  = xbt_dynar_new(sizeof(m_task_t),NULL);
528   MSG_process_set_data(MSG_process_self(),globals);
529
530 }
531
532 static void action_finalize(const char *const *action)
533 {
534 #ifdef HAVE_TRACING
535   TRACE_smpi_finalize(get_rank(MSG_process_get_name(MSG_process_self())));
536 #endif
537   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
538   if (globals){
539     xbt_dynar_free_container(&(globals->isends));
540     xbt_dynar_free_container(&(globals->irecvs));
541     xbt_dynar_free_container(&(globals->tasks));
542     free(globals);
543   }
544 }
545
546 /** Main function */
547 int main(int argc, char *argv[])
548 {
549   MSG_error_t res = MSG_OK;
550
551   smx_factory_initializer_to_use = SIMIX_ctx_raw_factory_init;
552
553   /* Check the given arguments */
554   MSG_global_init(&argc, argv);
555   if (argc < 3) {
556     printf("Usage: %s platform_file deployment_file [action_files]\n",
557            argv[0]);
558     printf
559         ("example: %s msg_platform.xml msg_deployment.xml actions # if all actions are in the same file\n",
560          argv[0]);
561     printf
562         ("example: %s msg_platform.xml msg_deployment.xml # if actions are in separate files, specified in deployment\n",
563          argv[0]);
564     exit(1);
565   }
566
567   /*  Simulation setting */
568   MSG_create_environment(argv[1]);
569
570   /* No need to register functions as in classical MSG programs: the actions get started anyway */
571   MSG_launch_application(argv[2]);
572
573   /*   Action registration */
574   MSG_action_register("init",     action_init);
575   MSG_action_register("finalize", action_finalize);
576   MSG_action_register("comm_size",action_comm_size);
577   MSG_action_register("send",     action_send);
578   MSG_action_register("Isend",    action_Isend);
579   MSG_action_register("recv",     action_recv);
580   MSG_action_register("Irecv",    action_Irecv);
581   MSG_action_register("wait",     action_wait);
582   MSG_action_register("barrier",  action_barrier);
583   MSG_action_register("bcast",    action_bcast);
584   MSG_action_register("reduce",   action_reduce);
585   MSG_action_register("allReduce",action_allReduce);
586   MSG_action_register("sleep",    action_sleep);
587   MSG_action_register("compute",  action_compute);
588
589
590   /* Actually do the simulation using MSG_action_trace_run */
591   res = MSG_action_trace_run(argv[3]);  // it's ok to pass a NULL argument here
592
593   XBT_INFO("Simulation time %g", MSG_get_clock());
594   MSG_clean();
595
596   if (res == MSG_OK)
597     return 0;
598   else
599     return 1;
600 }                               /* end_of_main */