Logo AND Algorithmique Numérique Distribuée

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