Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
identify sender on the recv lines
[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   INFO2("%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   INFO2("%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   INFO2("%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   INFO2("%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   INFO1("wait: %s", task_name);
162   MSG_task_receive(&task,task_name);
163   MSG_task_destroy(task);
164   INFO2("%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   INFO1("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 *comm_size = xbt_dynar_get_as(action, 2, char *);
185   char *comp_size = xbt_dynar_get_as(action, 3, char *);
186   m_process_t comm_helper=NULL;
187   m_task_t task=NULL, comp_task=NULL;
188   const char* process_name;
189   double clock = MSG_get_clock();
190   
191   coll_ctr counters =  (coll_ctr) MSG_process_get_data(MSG_process_self());
192
193   xbt_assert0(communicator_size, "Size of Communicator is not defined"
194               ", can't use collective operations");
195
196   process_name = MSG_process_get_name(MSG_process_self());
197
198   if (!counters){
199     DEBUG0("Initialize the counters");
200     counters = (coll_ctr) calloc (1, sizeof(struct coll_ctr_t));
201   }
202
203   name = bprintf("reduce_%d", counters->reduce_counter++);
204
205   if (!strcmp(process_name, "process0")){
206     DEBUG2("%s: %s is the Root",name, process_name);
207     for(i=1;i<communicator_size;i++){
208       sprintf(spawn_name,"%s_process%d", name, i);
209       sprintf(task_name,"%s_wait", spawn_name);
210       comm_helper = 
211         MSG_process_create(task_name, spawned_recv, 
212                            (void *) xbt_strdup(spawn_name),
213                            MSG_host_self());      
214     }
215
216     for(i=1;i<communicator_size;i++){
217       sprintf(task_name,"%s_process%d_wait", name, i);
218       MSG_task_receive(&task,task_name);
219       MSG_task_destroy(task);
220       task=NULL;
221     }
222
223     comp_task = 
224       MSG_task_create("reduce_comp", parse_double(comp_size), 0, NULL);
225     DEBUG1("%s: computing 'reduce_comp'", name);
226     MSG_task_execute(comp_task);
227     MSG_task_destroy(comp_task);
228     DEBUG1("%s: computed", name);
229   } else {
230     DEBUG2("%s: %s sends", name, process_name);
231     sprintf(task_name,"%s_%s", name, process_name);
232     DEBUG1("put on %s", task_name);
233     MSG_task_send(MSG_task_create(name, 0, parse_double(comm_size), NULL),
234                   task_name);
235   }
236
237   MSG_process_set_data(MSG_process_self(), (void*)counters);
238   INFO2("%s %f", xbt_str_join(action, " "), MSG_get_clock()-clock);
239   free(name);
240 }
241
242 static void bcast (xbt_dynar_t action)
243 {
244   int i;
245   char *name;
246   const char* process_name;
247   char task_name[80];
248   char spawn_name[80];
249   char **myargv;
250   m_process_t comm_helper=NULL;
251   m_task_t task=NULL;
252   char *size = xbt_dynar_get_as(action, 2, char *);
253   coll_ctr counters =  (coll_ctr) MSG_process_get_data(MSG_process_self());
254   double clock = MSG_get_clock();
255   
256   xbt_assert0(communicator_size, "Size of Communicator is not defined"
257               ", can't use collective operations");
258
259
260   process_name = MSG_process_get_name(MSG_process_self());
261   if (!counters){
262     DEBUG0("Initialize the counters");
263     counters = (coll_ctr) calloc (1, sizeof(struct coll_ctr_t));
264   }
265
266   name = bprintf("bcast_%d", counters->bcast_counter++);
267   if (!strcmp(process_name, "process0")){
268     DEBUG2("%s: %s is the Root",name, process_name);
269
270     for(i=1;i<communicator_size;i++){
271       myargv = (char**) calloc (3, sizeof (char*));
272       myargv[0] = xbt_strdup(name);
273       myargv[1] = xbt_strdup(size);
274       myargv[2] = NULL;
275
276       sprintf(spawn_name,"%s_%d", myargv[0], i);
277       comm_helper = 
278         MSG_process_create_with_arguments(spawn_name, spawned_send, 
279                                           NULL, MSG_host_self(), 2, myargv);
280     }
281     
282     for(i=1;i<communicator_size;i++){
283       sprintf(task_name,"process%d_wait", i);
284       DEBUG1("get on %s", task_name);
285       MSG_task_receive(&task,task_name);      
286       MSG_task_destroy(task);
287       task=NULL;
288     }
289     DEBUG2("%s: all messages sent by %s have been received",
290           name, process_name);
291   } else {
292     DEBUG2("%s: %s receives", name, process_name);
293     MSG_task_receive(&task, name);
294     MSG_task_destroy(task);
295     DEBUG2("%s: %s has received", name,process_name);
296     sprintf(task_name,"%s_wait", process_name);
297     DEBUG1("put on %s", task_name);
298     MSG_task_send(MSG_task_create("waiter",0,0,NULL),task_name);
299   }
300
301   MSG_process_set_data(MSG_process_self(), (void*)counters);
302   INFO2("%s %f", xbt_str_join(action, " "), MSG_get_clock()-clock);
303   free(name);
304 }
305
306
307 static void sleep(xbt_dynar_t action)
308 {
309   char *name = xbt_str_join(action, " ");
310   char *duration = xbt_dynar_get_as(action, 2, char *);
311   double clock = MSG_get_clock();
312   DEBUG1("Entering %s", name);
313   MSG_process_sleep(parse_double(duration));
314   INFO2("%s %f ", name, MSG_get_clock()-clock);
315   free(name);
316 }
317
318 static void allReduce(xbt_dynar_t action)
319 {
320   int i;
321   char *name;
322   char task_name[80];
323   char spawn_name[80];
324   char **myargv;
325   char *comm_size = xbt_dynar_get_as(action, 2, char *);
326   char *comp_size = xbt_dynar_get_as(action, 3, char *);
327   m_process_t comm_helper=NULL;
328   m_task_t task=NULL, comp_task=NULL;
329   const char* process_name;
330   double clock = MSG_get_clock();
331  
332   coll_ctr counters =  (coll_ctr) MSG_process_get_data(MSG_process_self());
333
334   xbt_assert0(communicator_size, "Size of Communicator is not defined"
335               ", can't use collective operations");
336
337   process_name = MSG_process_get_name(MSG_process_self());
338
339   if (!counters){
340     DEBUG0("Initialize the counters");
341     counters = (coll_ctr) calloc (1, sizeof(struct coll_ctr_t));
342   }
343
344   name = bprintf("allReduce_%d", counters->allReduce_counter++);
345
346   if (!strcmp(process_name, "process0")){
347     DEBUG2("%s: %s is the Root",name, process_name);
348     for(i=1;i<communicator_size;i++){
349       sprintf(spawn_name,"%s_process%d", name, i);
350       sprintf(task_name,"%s_wait", spawn_name);
351       comm_helper = 
352         MSG_process_create(task_name, spawned_recv, 
353                            (void *) xbt_strdup(spawn_name),
354                            MSG_host_self());      
355     }
356
357     for(i=1;i<communicator_size;i++){
358       sprintf(task_name,"%s_process%d_wait", name, i);
359       MSG_task_receive(&task,task_name);
360       MSG_task_destroy(task);
361       task=NULL;
362     }
363
364     comp_task = 
365       MSG_task_create("allReduce_comp", parse_double(comp_size), 0, NULL);
366     DEBUG1("%s: computing 'reduce_comp'", name);
367     MSG_task_execute(comp_task);
368     MSG_task_destroy(comp_task);
369     DEBUG1("%s: computed", name);
370
371     for(i=1;i<communicator_size;i++){
372       myargv = (char**) calloc (3, sizeof (char*));
373       myargv[0] = xbt_strdup(name);
374       myargv[1] = xbt_strdup(comm_size);
375       myargv[2] = NULL;
376
377       sprintf(spawn_name,"%s_%d", myargv[0], i);
378       comm_helper = 
379         MSG_process_create_with_arguments(spawn_name, spawned_send, 
380                                           NULL, MSG_host_self(), 2, myargv);
381     }
382     
383     for(i=1;i<communicator_size;i++){
384       sprintf(task_name,"process%d_wait", i);
385       DEBUG1("get on %s", task_name);
386       MSG_task_receive(&task,task_name);      
387       MSG_task_destroy(task);
388       task=NULL;
389     }
390     DEBUG2("%s: all messages sent by %s have been received",
391           name, process_name);
392
393   } else {
394     DEBUG2("%s: %s sends", name, process_name);
395     sprintf(task_name,"%s_%s", name, process_name);
396     DEBUG1("put on %s", task_name);
397     MSG_task_send(MSG_task_create(name, 0, parse_double(comm_size), NULL),
398                   task_name);
399
400     MSG_task_receive(&task, name);
401     MSG_task_destroy(task);
402     DEBUG2("%s: %s has received", name,process_name);
403     sprintf(task_name,"%s_wait", process_name);
404     DEBUG1("put on %s", task_name);
405     MSG_task_send(MSG_task_create("waiter",0,0,NULL),task_name);
406
407   }
408
409   MSG_process_set_data(MSG_process_self(), (void*)counters);
410   INFO2("%s %f", xbt_str_join(action, " "), MSG_get_clock()-clock);
411   free(name);
412 }
413
414 static void comm_size(xbt_dynar_t action)
415 {
416   char *size = xbt_dynar_get_as(action, 2, char *);
417   communicator_size = parse_double(size);
418 }
419
420 static void compute(xbt_dynar_t action)
421 {
422   char *name = xbt_str_join(action, " ");
423   char *amout = xbt_dynar_get_as(action, 2, char *);
424   m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
425   double clock = MSG_get_clock();
426
427   DEBUG1("Entering %s", name);
428   MSG_task_execute(task);
429   MSG_task_destroy(task);
430   INFO2("%s %f", name, MSG_get_clock()-clock);
431   free(name);
432 }
433
434 /** Main function */
435 int main(int argc, char *argv[])
436 {
437   MSG_error_t res = MSG_OK;
438   
439   /* Check the given arguments */
440   MSG_global_init(&argc, argv);
441   if (argc < 4) {
442     printf("Usage: %s platform_file deployment_file action_files\n", argv[0]);
443     printf("example: %s msg_platform.xml msg_deployment.xml actions\n",
444            argv[0]);
445     exit(1);
446   }
447
448   /*  Simulation setting */
449   MSG_create_environment(argv[1]);
450
451   /* No need to register functions as in classical MSG programs: the actions get started anyway */
452   MSG_launch_application(argv[2]);
453
454   /*   Action registration */
455   MSG_action_register("comm_size", comm_size);
456   MSG_action_register("send", send);
457   MSG_action_register("Isend", Isend);
458   MSG_action_register("recv", recv);
459   MSG_action_register("Irecv", Irecv);
460   MSG_action_register("wait", wait_action);
461   MSG_action_register("barrier", barrier);
462   MSG_action_register("bcast", bcast);
463   MSG_action_register("reduce", reduce);
464   MSG_action_register("allReduce", allReduce);
465   MSG_action_register("sleep", sleep);
466   MSG_action_register("compute", compute);
467
468
469   /* Actually do the simulation using MSG_action_trace_run */
470   res = MSG_action_trace_run(argv[3]);
471
472   INFO1("Simulation time %g", MSG_get_clock());
473   MSG_clean();
474
475   if (res == MSG_OK)
476     return 0;
477   else
478     return 1;
479 }                               /* end_of_main */