Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9661ad177a7bb4d33d75606a24c2e29cd61da136
[simgrid.git] / examples / msg / actions / actions.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2009. The SimGrid team. All rights reserved.               */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include "msg/msg.h"            /* Yeah! If you want to use msg, you need to include msg/msg.h */
11 #include "xbt.h"                /* calloc, printf */
12 #include "simgrid_config.h"     /* getline */
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15                  "Messages specific for this msg example");
16 int communicator_size=0;
17
18 typedef struct coll_ctr_t{
19   int bcast_counter;
20   int reduce_counter;
21   int allReduce_counter;
22 } *coll_ctr;
23
24 /* Helper function */
25 static double parse_double(const char *string) {
26   double value;
27   char *endptr;
28
29   value=strtod(string, &endptr);
30   if (*endptr != '\0')
31           THROW1(unknown_error, 0, "%s is not a double", string);
32   return value;
33 }
34
35
36 /* My actions */
37 static void send(xbt_dynar_t action)
38 {
39   char *name = xbt_str_join(action, " ");
40   char to[250];
41   char *size = xbt_dynar_get_as(action, 3, char *);
42   double clock = MSG_get_clock();
43   sprintf (to,"%s_%s", MSG_process_get_name(MSG_process_self()),
44            xbt_dynar_get_as(action, 2, char *));
45   //  char *to =  xbt_dynar_get_as(action, 2, char *);
46   DEBUG2("Entering Send: %s (size: %lg)", name, parse_double(size));
47   MSG_task_send(MSG_task_create(name, 0, parse_double(size), NULL), to);
48   DEBUG2("%s %f", name, MSG_get_clock()-clock);
49   free(name);
50 }
51
52
53 static int spawned_send(int argc, char *argv[])
54 {
55   DEBUG3("%s: Sending %s on %s", MSG_process_get_name(MSG_process_self()), 
56         argv[1],argv[0]);
57   MSG_task_send(MSG_task_create(argv[0], 0, parse_double(argv[1]), NULL), 
58                 argv[0]);
59   return 0;
60 }
61
62 static void Isend(xbt_dynar_t action)
63 {
64   char spawn_name[80];
65   char to[250];
66   //  char *to = xbt_dynar_get_as(action, 2, char *);
67   char *size = xbt_dynar_get_as(action, 3, char *);
68   char **myargv;
69   m_process_t comm_helper;
70   double clock = MSG_get_clock();
71   DEBUG1("Isend on %s: spawn process ", 
72          MSG_process_get_name(MSG_process_self()));
73
74   sprintf (to,"%s_%s", MSG_process_get_name(MSG_process_self()),
75            xbt_dynar_get_as(action, 2, char *));
76   myargv = (char**) calloc (3, sizeof (char*));
77   
78   myargv[0] = xbt_strdup(to);
79   myargv[1] = xbt_strdup(size);
80   myargv[2] = NULL;
81
82   //    sprintf(spawn_name,"%s_wait",MSG_process_get_name(MSG_process_self()));
83   sprintf(spawn_name,"%s_wait",to);
84   comm_helper =
85     MSG_process_create_with_arguments(spawn_name, spawned_send, 
86                                       NULL, MSG_host_self(), 2, myargv);
87   DEBUG2("%s %f",xbt_str_join(action, " "), MSG_get_clock()-clock);
88 }
89
90
91 static void recv(xbt_dynar_t action)
92 {
93   char *name = xbt_str_join(action, " ");
94     char mailbox_name[250];
95   m_task_t task = NULL;
96   double clock = MSG_get_clock();
97   //FIXME: argument of action ignored so far; semantic not clear
98   //char *from=xbt_dynar_get_as(action,2,char*);
99   sprintf (mailbox_name,"%s_%s", xbt_dynar_get_as(action, 2, char *),
100            MSG_process_get_name(MSG_process_self()));
101   DEBUG1("Receiving: %s", name);
102   MSG_task_receive(&task, mailbox_name);
103   //  MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
104   DEBUG2("%s %f", name, MSG_get_clock()-clock);
105   MSG_task_destroy(task);
106   free(name);
107 }
108
109 static int spawned_recv(int argc, char *argv[])
110 {
111   m_task_t task = NULL;
112   DEBUG1("Receiving on %s", argv[0]);
113   MSG_task_receive(&task, argv[0]);
114   DEBUG1("Received %s", MSG_task_get_name(task));
115   DEBUG1("waiter on %s", MSG_process_get_name(MSG_process_self()));
116   MSG_task_send(MSG_task_create("waiter",0,0,NULL),
117                 MSG_process_get_name(MSG_process_self())); 
118   
119   MSG_task_destroy(task);
120   return 0;
121 }
122
123
124 static void Irecv(xbt_dynar_t action)
125 {
126   char *name = xbt_str_join(action, " ");
127   m_process_t comm_helper;
128   char mailbox_name[250];
129   char **myargv;
130   double clock = MSG_get_clock();
131   DEBUG1("Irecv on %s: spawn process ", 
132          MSG_process_get_name(MSG_process_self()));
133
134   sprintf (mailbox_name,"%s_%s", xbt_dynar_get_as(action, 2, char *),
135            MSG_process_get_name(MSG_process_self()));
136   sprintf(name,"%s_wait",MSG_process_get_name(MSG_process_self()));
137   myargv = (char**) calloc (2, sizeof (char*));
138   
139   myargv[0] = xbt_strdup(mailbox_name);
140   myargv[1] = NULL;
141   comm_helper = MSG_process_create_with_arguments(name,spawned_recv,
142                                                   NULL, MSG_host_self(),
143                                                   1, myargv);
144
145   DEBUG2("%s %f",  xbt_str_join(action, " "), 
146         MSG_get_clock()-clock);
147  
148   free(name);
149 }
150
151
152 static void wait_action(xbt_dynar_t action)
153 {
154   char *name = xbt_str_join(action, " ");
155   char task_name[80];
156   m_task_t task = NULL;
157   double clock = MSG_get_clock();
158   
159   DEBUG1("Entering %s", name);
160   sprintf(task_name,"%s_wait",MSG_process_get_name(MSG_process_self()));
161   DEBUG1("wait: %s", task_name);
162   MSG_task_receive(&task,task_name);
163   MSG_task_destroy(task);
164   DEBUG2("%s %f", name, MSG_get_clock()-clock);
165   free(name);
166 }
167
168 static void barrier (xbt_dynar_t action)
169 {
170   char *name = xbt_str_join(action, " ");
171   DEBUG1("barrier: %s", name);
172   
173
174   free(name);
175
176 }
177
178 static void reduce(xbt_dynar_t action)
179 {
180   int i;
181   char *name;
182   char task_name[80];
183   char spawn_name[80];
184   char **myargv;
185   char *comm_size = xbt_dynar_get_as(action, 2, char *);
186   char *comp_size = xbt_dynar_get_as(action, 3, char *);
187   m_process_t comm_helper=NULL;
188   m_task_t task=NULL, comp_task=NULL;
189   const char* process_name;
190   double clock = MSG_get_clock();
191   
192   coll_ctr counters =  (coll_ctr) MSG_process_get_data(MSG_process_self());
193
194   xbt_assert0(communicator_size, "Size of Communicator is not defined"
195               ", can't use collective operations");
196
197   process_name = MSG_process_get_name(MSG_process_self());
198
199   if (!counters){
200     DEBUG0("Initialize the counters");
201     counters = (coll_ctr) calloc (1, sizeof(struct coll_ctr_t));
202   }
203
204   name = bprintf("reduce_%d", counters->reduce_counter++);
205
206   if (!strcmp(process_name, "p0")){
207     DEBUG2("%s: %s is the Root",name, process_name);
208     for(i=1;i<communicator_size;i++){
209       sprintf(spawn_name,"%s_p%d_%s", name, i,
210            MSG_process_get_name(MSG_process_self()));
211       sprintf(task_name,"%s_wait", spawn_name);
212       myargv = (char**) calloc (2, sizeof (char*));
213       
214       myargv[0] = xbt_strdup(spawn_name);
215       myargv[1] = NULL;
216
217       comm_helper = 
218         MSG_process_create_with_arguments(task_name, spawned_recv, 
219                                           NULL, MSG_host_self(),
220                                           1, myargv);
221     }
222
223     for(i=1;i<communicator_size;i++){
224       sprintf(task_name,"%s_p%d_p0_wait", name, i);
225       MSG_task_receive(&task,task_name);
226       MSG_task_destroy(task);
227       task=NULL;
228     }
229
230     comp_task = 
231       MSG_task_create("reduce_comp", parse_double(comp_size), 0, NULL);
232     DEBUG1("%s: computing 'reduce_comp'", name);
233     MSG_task_execute(comp_task);
234     MSG_task_destroy(comp_task);
235     DEBUG1("%s: computed", name);
236   } else {
237     DEBUG2("%s: %s sends", name, process_name);
238     sprintf(task_name,"%s_%s_p0", name, process_name);
239     DEBUG1("put on %s", task_name);
240     MSG_task_send(MSG_task_create(name, 0, parse_double(comm_size), NULL),
241                   task_name);
242   }
243
244   MSG_process_set_data(MSG_process_self(), (void*)counters);
245   DEBUG2("%s %f", xbt_str_join(action, " "), MSG_get_clock()-clock);
246   free(name);
247 }
248
249 static void bcast (xbt_dynar_t action)
250 {
251   int i;
252   char *name;
253   const char* process_name;
254   char task_name[80];
255   char spawn_name[80];
256   char **myargv;
257   m_process_t comm_helper=NULL;
258   m_task_t task=NULL;
259   char *size = xbt_dynar_get_as(action, 2, char *);
260   coll_ctr counters =  (coll_ctr) MSG_process_get_data(MSG_process_self());
261   double clock = MSG_get_clock();
262   
263   xbt_assert0(communicator_size, "Size of Communicator is not defined"
264               ", can't use collective operations");
265
266
267   process_name = MSG_process_get_name(MSG_process_self());
268   if (!counters){
269     DEBUG0("Initialize the counters");
270     counters = (coll_ctr) calloc (1, sizeof(struct coll_ctr_t));
271   }
272
273   name = bprintf("bcast_%d", counters->bcast_counter++);
274   if (!strcmp(process_name, "p0")){
275     DEBUG2("%s: %s is the Root",name, process_name);
276
277     for(i=1;i<communicator_size;i++){
278       myargv = (char**) calloc (3, sizeof (char*));
279       myargv[0] = xbt_strdup(name);
280       myargv[1] = xbt_strdup(size);
281       myargv[2] = NULL;
282
283       sprintf(spawn_name,"%s_%d", myargv[0], i);
284       comm_helper = 
285         MSG_process_create_with_arguments(spawn_name, spawned_send, 
286                                           NULL, MSG_host_self(), 2, myargv);
287     }
288     
289     for(i=1;i<communicator_size;i++){
290       sprintf(task_name,"p%d_wait", i);
291       DEBUG1("get on %s", task_name);
292       MSG_task_receive(&task,task_name);      
293       MSG_task_destroy(task);
294       task=NULL;
295     }
296     DEBUG2("%s: all messages sent by %s have been received",
297           name, process_name);
298   } else {
299     DEBUG2("%s: %s receives", name, process_name);
300     MSG_task_receive(&task, name);
301     MSG_task_destroy(task);
302     DEBUG2("%s: %s has received", name,process_name);
303     sprintf(task_name,"%s_wait", process_name);
304     DEBUG1("put on %s", task_name);
305     MSG_task_send(MSG_task_create("waiter",0,0,NULL),task_name);
306   }
307
308   MSG_process_set_data(MSG_process_self(), (void*)counters);
309   DEBUG2("%s %f", xbt_str_join(action, " "), MSG_get_clock()-clock);
310   free(name);
311 }
312
313
314 static void sleep(xbt_dynar_t action)
315 {
316   char *name = xbt_str_join(action, " ");
317   char *duration = xbt_dynar_get_as(action, 2, char *);
318   double clock = MSG_get_clock();
319   DEBUG1("Entering %s", name);
320   MSG_process_sleep(parse_double(duration));
321   DEBUG2("%s %f ", name, MSG_get_clock()-clock);
322   free(name);
323 }
324
325 static void allReduce(xbt_dynar_t action)
326 {
327   int i;
328   char *name;
329   char task_name[80];
330   char spawn_name[80];
331   char **myargv;
332   char *comm_size = xbt_dynar_get_as(action, 2, char *);
333   char *comp_size = xbt_dynar_get_as(action, 3, char *);
334   m_process_t comm_helper=NULL;
335   m_task_t task=NULL, comp_task=NULL;
336   const char* process_name;
337   double clock = MSG_get_clock();
338  
339   coll_ctr counters =  (coll_ctr) 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   if (!counters){
347     DEBUG0("Initialize the counters");
348     counters = (coll_ctr) calloc (1, sizeof(struct coll_ctr_t));
349   }
350
351   name = bprintf("allReduce_%d", counters->allReduce_counter++);
352
353   if (!strcmp(process_name, "p0")){
354     DEBUG2("%s: %s is the Root",name, process_name);
355     for(i=1;i<communicator_size;i++){
356       sprintf(spawn_name,"%s_p%d_%s", name, i,
357            MSG_process_get_name(MSG_process_self()));
358       sprintf(task_name,"%s_wait", spawn_name);
359       myargv = (char**) calloc (2, sizeof (char*));
360       
361       myargv[0] = xbt_strdup(spawn_name);
362       myargv[1] = NULL;
363
364       comm_helper = 
365         MSG_process_create_with_arguments(task_name, spawned_recv, 
366                                           NULL, MSG_host_self(),
367                                           1, myargv);
368     }
369
370     for(i=1;i<communicator_size;i++){
371       sprintf(task_name,"%s_p%d_p0_wait", name, i);
372       MSG_task_receive(&task,task_name);
373       MSG_task_destroy(task);
374       task=NULL;
375     }
376
377     comp_task = 
378       MSG_task_create("allReduce_comp", parse_double(comp_size), 0, NULL);
379     DEBUG1("%s: computing 'reduce_comp'", name);
380     MSG_task_execute(comp_task);
381     MSG_task_destroy(comp_task);
382     DEBUG1("%s: computed", name);
383
384     for(i=1;i<communicator_size;i++){
385       myargv = (char**) calloc (3, sizeof (char*));
386       myargv[0] = xbt_strdup(name);
387       myargv[1] = xbt_strdup(comm_size);
388       myargv[2] = NULL;
389
390       sprintf(spawn_name,"%s_%d", myargv[0], i);
391       comm_helper = 
392         MSG_process_create_with_arguments(spawn_name, spawned_send, 
393                                           NULL, MSG_host_self(), 2, myargv);
394     }
395     
396     for(i=1;i<communicator_size;i++){
397       sprintf(task_name,"p%d_wait", i);
398       DEBUG1("get on %s", task_name);
399       MSG_task_receive(&task,task_name);      
400       MSG_task_destroy(task);
401       task=NULL;
402     }
403     DEBUG2("%s: all messages sent by %s have been received",
404           name, process_name);
405
406   } else {
407     DEBUG2("%s: %s sends", name, process_name);
408     sprintf(task_name,"%s_%s_p0", name, process_name);
409     DEBUG1("put on %s", task_name);
410     MSG_task_send(MSG_task_create(name, 0, parse_double(comm_size), NULL),
411                   task_name);
412
413     MSG_task_receive(&task, name);
414     MSG_task_destroy(task);
415     DEBUG2("%s: %s has received", name,process_name);
416     sprintf(task_name,"%s_wait", process_name);
417     DEBUG1("put on %s", task_name);
418     MSG_task_send(MSG_task_create("waiter",0,0,NULL),task_name);
419
420   }
421
422   MSG_process_set_data(MSG_process_self(), (void*)counters);
423   DEBUG2("%s %f", xbt_str_join(action, " "), MSG_get_clock()-clock);
424   free(name);
425 }
426
427 static void comm_size(xbt_dynar_t action)
428 {
429   char *size = xbt_dynar_get_as(action, 2, char *);
430   communicator_size = parse_double(size);
431 }
432
433 static void compute(xbt_dynar_t action)
434 {
435   char *name = xbt_str_join(action, " ");
436   char *amout = xbt_dynar_get_as(action, 2, char *);
437   m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
438   double clock = MSG_get_clock();
439
440   DEBUG1("Entering %s", name);
441   MSG_task_execute(task);
442   MSG_task_destroy(task);
443   DEBUG2("%s %f", name, MSG_get_clock()-clock);
444   free(name);
445 }
446
447 /** Main function */
448 int main(int argc, char *argv[])
449 {
450   MSG_error_t res = MSG_OK;
451   
452   /* Check the given arguments */
453   MSG_global_init(&argc, argv);
454   if (argc < 4) {
455     printf("Usage: %s platform_file deployment_file action_files\n", argv[0]);
456     printf("example: %s msg_platform.xml msg_deployment.xml actions\n",
457            argv[0]);
458     exit(1);
459   }
460
461   /*  Simulation setting */
462   MSG_create_environment(argv[1]);
463
464   /* No need to register functions as in classical MSG programs: the actions get started anyway */
465   MSG_launch_application(argv[2]);
466
467   /*   Action registration */
468   MSG_action_register("comm_size", comm_size);
469   MSG_action_register("send", send);
470   MSG_action_register("Isend", Isend);
471   MSG_action_register("recv", recv);
472   MSG_action_register("Irecv", Irecv);
473   MSG_action_register("wait", wait_action);
474   MSG_action_register("barrier", barrier);
475   MSG_action_register("bcast", bcast);
476   MSG_action_register("reduce", reduce);
477   MSG_action_register("allReduce", allReduce);
478   MSG_action_register("sleep", sleep);
479   MSG_action_register("compute", compute);
480
481
482   /* Actually do the simulation using MSG_action_trace_run */
483   res = MSG_action_trace_run(argv[3]);
484
485   INFO1("Simulation time %g", MSG_get_clock());
486   MSG_clean();
487
488   if (res == MSG_OK)
489     return 0;
490   else
491     return 1;
492 }                               /* end_of_main */