Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add a reduce X bytes Y flops action
[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
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 } *coll_ctr;
22
23 /* Helper function */
24 static double parse_double(const char *string) {
25   double value;
26   char *endptr;
27
28   value=strtod(string, &endptr);
29   if (*endptr != '\0')
30           THROW1(unknown_error, 0, "%s is not a double", string);
31   return value;
32 }
33
34
35 /* My actions */
36 static void send(xbt_dynar_t action)
37 {
38   char *name = xbt_str_join(action, " ");
39   char *to = xbt_dynar_get_as(action, 2, char *);
40   char *size = xbt_dynar_get_as(action, 3, char *);
41   INFO2("Send: %s (size: %lg)", name, parse_double(size));
42   MSG_task_send(MSG_task_create(name, 0, parse_double(size), NULL), to);
43   INFO1("Sent %s", name);
44   free(name);
45 }
46
47
48 static int spawned_send(int argc, char *argv[])
49 {
50   INFO3("%s: Sending %s on %s", MSG_process_self()->name, 
51         argv[1],argv[0]);
52   MSG_task_send(MSG_task_create(argv[0], 0, parse_double(argv[1]), NULL), 
53                 argv[0]);
54   return 0;
55 }
56
57 static void Isend(xbt_dynar_t action)
58 {
59   char spawn_name[80];
60   char *to = xbt_dynar_get_as(action, 2, char *);
61   char *size = xbt_dynar_get_as(action, 3, char *);
62   char **myargv;
63   m_process_t comm_helper;
64
65   INFO1("Isend on %s: spawn process ", 
66          MSG_process_self()->name);
67
68   myargv = (char**) calloc (3, sizeof (char*));
69   
70   myargv[0] = xbt_strdup(to);
71   myargv[1] = xbt_strdup(size);
72   myargv[2] = NULL;
73
74   sprintf(spawn_name,"%s_wait",MSG_process_self()->name);
75   comm_helper =
76     MSG_process_create_with_arguments(spawn_name, spawned_send, 
77                                       NULL, MSG_host_self(), 2, myargv);
78 }
79
80
81 static void recv(xbt_dynar_t action)
82 {
83   char *name = xbt_str_join(action, " ");
84   m_task_t task = NULL;
85   INFO1("Receiving: %s", name);
86   //FIXME: argument of action ignored so far; semantic not clear
87   //char *from=xbt_dynar_get_as(action,2,char*);
88   MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
89   INFO1("Received %s", MSG_task_get_name(task));
90   MSG_task_destroy(task);
91   free(name);
92 }
93
94 static int spawned_recv(int argc, char *argv[])
95 {
96   m_task_t task = NULL;
97   char* name = (char *) MSG_process_self()->data;
98   INFO1("Receiving on %s", name);
99   MSG_task_receive(&task, name);
100   INFO1("Received %s", MSG_task_get_name(task));
101   INFO1("waiter on %s", MSG_process_self()->name);
102   MSG_task_send(MSG_task_create("waiter",0,0,NULL),MSG_process_self()->name); 
103   
104   MSG_task_destroy(task);
105   return 0;
106 }
107
108
109 static void Irecv(xbt_dynar_t action)
110 {
111   char *name = xbt_str_join(action, " ");
112   m_process_t comm_helper;
113
114   INFO1("Irecv on %s: spawn process ", 
115          MSG_process_get_name(MSG_process_self()));
116
117   sprintf(name,"%s_wait",MSG_process_self()->name);
118   comm_helper = MSG_process_create(name,spawned_recv,
119                  (void *) MSG_process_self()->name,
120                  MSG_host_self());
121
122
123   free(name);
124 }
125
126
127 static void wait(xbt_dynar_t action)
128 {
129   char *name = xbt_str_join(action, " ");
130   char task_name[80];
131   m_task_t task = NULL;
132   
133   INFO1("wait: %s", name);
134   sprintf(task_name,"%s_wait",MSG_process_self()->name);
135   MSG_task_receive(&task,task_name);
136   MSG_task_destroy(task);
137   INFO1("waited: %s", name);
138   free(name);
139 }
140
141 static void barrier (xbt_dynar_t action)
142 {
143   char *name = xbt_str_join(action, " ");
144   INFO1("barrier: %s", name);
145   
146
147   free(name);
148
149 }
150
151 static void reduce(xbt_dynar_t action)
152 {
153   int i;
154   char *name;
155   char task_name[80];
156   char spawn_name[80];
157   char *comm_size = xbt_dynar_get_as(action, 2, char *);
158   char *comp_size = xbt_dynar_get_as(action, 3, char *);
159   m_process_t comm_helper=NULL;
160   m_task_t task=NULL, comp_task=NULL;
161   const char* process_name;
162   
163   coll_ctr counters =  (coll_ctr) MSG_process_get_data(MSG_process_self());
164
165   xbt_assert0(communicator_size, "Size of Communicator is not defined"
166               ", can't use collective operations");
167
168   MSG_process_self()->data=NULL;
169
170   process_name = MSG_process_self()->name;
171
172   if (!counters){
173     DEBUG0("Initialize the counters");
174     counters = (coll_ctr) calloc (1, sizeof(struct coll_ctr_t));
175   }
176
177   name = bprintf("reduce_%d", counters->reduce_counter++);
178
179   if (!strcmp(process_name, "process0")){
180     INFO2("%s: %s is the Root",name, process_name);
181     for(i=1;i<communicator_size;i++){
182       sprintf(spawn_name,"%s_process%d", name, i);
183       sprintf(task_name,"%s_wait", spawn_name);
184       comm_helper = 
185         MSG_process_create(task_name, spawned_recv, 
186                            (void *) xbt_strdup(spawn_name),
187                            MSG_host_self());      
188     }
189
190     for(i=1;i<communicator_size;i++){
191       sprintf(task_name,"%s_process%d_wait", name, i);
192       MSG_task_receive(&task,task_name);
193       MSG_task_destroy(task);
194       task=NULL;
195     }
196
197     comp_task = 
198       MSG_task_create("reduce_comp", parse_double(comp_size), 0, NULL);
199     INFO1("%s: computing 'reduce_comp'", name);
200     MSG_task_execute(comp_task);
201     MSG_task_destroy(comp_task);
202     INFO1("%s: computed", name);
203   } else {
204     INFO2("%s: %s sends", name, process_name);
205     sprintf(task_name,"%s_%s", name, process_name);
206     INFO1("put on %s", task_name);
207     MSG_task_send(MSG_task_create(name, 0, parse_double(comm_size), NULL),
208                   task_name);
209   }
210
211   MSG_process_set_data(MSG_process_self(), (void*)counters);
212   free(name);
213 }
214
215 static void bcast (xbt_dynar_t action)
216 {
217   int i;
218   char *name;
219   const char* process_name;
220   char task_name[80];
221   char spawn_name[80];
222   char **myargv;
223   m_process_t comm_helper=NULL;
224   m_task_t task=NULL;
225   char *size = xbt_dynar_get_as(action, 2, char *);
226   coll_ctr counters =  (coll_ctr) MSG_process_get_data(MSG_process_self());
227
228   xbt_assert0(communicator_size, "Size of Communicator is not defined"
229               ", can't use collective operations");
230
231   MSG_process_self()->data=NULL;
232
233   process_name = MSG_process_self()->name;
234   if (!counters){
235     DEBUG0("Initialize the counters");
236     counters = (coll_ctr) calloc (1, sizeof(struct coll_ctr_t));
237   }
238
239   name = bprintf("bcast_%d", counters->bcast_counter++);
240   if (!strcmp(process_name, "process0")){
241     INFO2("%s: %s is the Root",name, process_name);
242
243     for(i=1;i<communicator_size;i++){
244       myargv = (char**) calloc (3, sizeof (char*));
245       myargv[0] = xbt_strdup(name);
246       myargv[1] = xbt_strdup(size);
247       myargv[2] = NULL;
248
249       sprintf(spawn_name,"%s_%d", myargv[0], i);
250       comm_helper = 
251         MSG_process_create_with_arguments(spawn_name, spawned_send, 
252                                           NULL, MSG_host_self(), 2, myargv);
253     }
254     
255     for(i=1;i<communicator_size;i++){
256       sprintf(task_name,"process%d_wait", i);
257       DEBUG1("get on %s", task_name);
258       MSG_task_receive(&task,task_name);      
259       MSG_task_destroy(task);
260       task=NULL;
261     }
262     INFO2("%s: all messages sent by %s have been received",
263           name, process_name);
264   } else {
265     INFO2("%s: %s receives", name, process_name);
266     MSG_task_receive(&task, name);
267     MSG_task_destroy(task);
268     INFO2("%s: %s has received", name,process_name);
269     sprintf(task_name,"%s_wait", process_name);
270     DEBUG1("put on %s", task_name);
271     MSG_task_send(MSG_task_create("waiter",0,0,NULL),task_name);
272   }
273
274   MSG_process_set_data(MSG_process_self(), (void*)counters);
275   free(name);
276 }
277
278
279 static void sleep(xbt_dynar_t action)
280 {
281   char *name = xbt_str_join(action, " ");
282   char *duration = xbt_dynar_get_as(action, 2, char *);
283   INFO1("sleep: %s", name);
284   MSG_process_sleep(parse_double(duration));
285   INFO1("sleept: %s", name);
286   free(name);
287 }
288
289 static void comm_size(xbt_dynar_t action)
290 {
291   char *size = xbt_dynar_get_as(action, 2, char *);
292   communicator_size = parse_double(size);
293 }
294
295 static void compute(xbt_dynar_t action)
296 {
297   char *name = xbt_str_join(action, " ");
298   char *amout = xbt_dynar_get_as(action, 2, char *);
299   m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
300   INFO1("computing: %s", name);
301   MSG_task_execute(task);
302   MSG_task_destroy(task);
303   INFO1("computed: %s", name);
304   free(name);
305 }
306
307 /** Main function */
308 int main(int argc, char *argv[])
309 {
310   MSG_error_t res = MSG_OK;
311   
312   /* Check the given arguments */
313   MSG_global_init(&argc, argv);
314   if (argc < 4) {
315     printf("Usage: %s platform_file deployment_file action_files\n", argv[0]);
316     printf("example: %s msg_platform.xml msg_deployment.xml actions\n",
317            argv[0]);
318     exit(1);
319   }
320
321   /*  Simulation setting */
322   MSG_create_environment(argv[1]);
323
324   /* No need to register functions as in classical MSG programs: the actions get started anyway */
325   MSG_launch_application(argv[2]);
326
327   /*   Action registration */
328   MSG_action_register("comm_size", comm_size);
329   MSG_action_register("send", send);
330   MSG_action_register("Isend", Isend);
331   MSG_action_register("recv", recv);
332   MSG_action_register("Irecv", Irecv);
333   MSG_action_register("wait", wait);
334   MSG_action_register("barrier", barrier);
335   MSG_action_register("bcast", bcast);
336   MSG_action_register("reduce", reduce);
337   MSG_action_register("sleep", sleep);
338   MSG_action_register("compute", compute);
339
340
341   /* Actually do the simulation using MSG_action_trace_run */
342   res = MSG_action_trace_run(argv[3]);
343
344   INFO1("Simulation time %g", MSG_get_clock());
345   MSG_clean();
346
347   if (res == MSG_OK)
348     return 0;
349   else
350     return 1;
351 }                               /* end_of_main */