Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2ce186e30d6fb2e30e9dc73de3f63147b79c001f
[simgrid.git] / examples / msg / masterslave / masterslave_bypass.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. 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 "msg/msg.h"  /* Yeah! If you want to use msg, you need to include msg/msg.h */
10 #include "surf/surfxml_parse.h" /* to override surf_parse */
11 #include "surf/surfxml.h"       /* to hijack surf_parse_lex */
12
13 /* Create a log channel to have nice outputs. */
14 #include "xbt/log.h"
15 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,"Messages specific for this msg example");
16 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
17
18 #define SURFXML_BUFFER_SET(key,val) do { \
19   AX_surfxml_##key=AX_ptr; \
20   strcpy(A_surfxml_##key,val); \
21   AX_ptr+=strlen(val)+1; } while(0)
22
23 #define SURFXML_BUFFER_RESET() do { \
24   AX_ptr = 0; \
25   memset(surfxml_bufferstack,0,surfxml_bufferstack_size); } while(0)
26
27 #define STAG(tag)  STag_surfxml_##tag();
28 #define ETAG(tag)  do { ETag_surfxml_##tag(); SURFXML_BUFFER_RESET(); } while(0)     
29
30 static int surf_parse_bypass_platform(void)
31 {
32   static int AX_ptr;
33   static int surfxml_bufferstack_size = 2048;
34
35   /* allocating memory to the buffer, I think 2MB should be enough */
36   surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
37   
38   /* <platform_description> */
39   SURFXML_BUFFER_SET(platform_description_version,"2");
40
41   STAG(platform_description);
42
43 /*   <host id="host A" power="100000000.00"/> */
44   SURFXML_BUFFER_SET(host_id,"host A");
45   SURFXML_BUFFER_SET(host_power, "100000000.00");
46   SURFXML_BUFFER_SET(host_availability, "1.0");
47   SURFXML_BUFFER_SET(host_availability_file, "");
48   A_surfxml_host_state = A_surfxml_host_state_ON;
49   SURFXML_BUFFER_SET(host_state_file, "");
50   SURFXML_BUFFER_SET(host_interference_send, "1.0");
51   SURFXML_BUFFER_SET(host_interference_recv, "1.0");
52   SURFXML_BUFFER_SET(host_interference_send_recv, "1.0");
53   SURFXML_BUFFER_SET(host_max_outgoing_rate, "-1.0");
54
55   STAG(host);
56   ETAG(host);
57
58 /*   <host id="host B" power="100000000.00"/> */
59   SURFXML_BUFFER_SET(host_id, "host B");
60   SURFXML_BUFFER_SET(host_power, "100000000.00");
61   SURFXML_BUFFER_SET(host_availability, "1.0");
62   SURFXML_BUFFER_SET(host_availability_file, "");
63   A_surfxml_host_state = A_surfxml_host_state_ON;
64   SURFXML_BUFFER_SET(host_state_file, "");
65   SURFXML_BUFFER_SET(host_interference_send, "1.0");
66   SURFXML_BUFFER_SET(host_interference_recv, "1.0");
67   SURFXML_BUFFER_SET(host_interference_send_recv, "1.0");
68   SURFXML_BUFFER_SET(host_max_outgoing_rate, "-1.0");
69
70   STAG(host);
71   ETAG(host);
72
73 /*   <link id="LinkA" bandwidth="10000000.0" latency="0.2"/> */
74   SURFXML_BUFFER_SET(link_id, "LinkA");
75   SURFXML_BUFFER_SET(link_bandwidth, "10000000.0");
76   SURFXML_BUFFER_SET(link_bandwidth_file, "");
77   SURFXML_BUFFER_SET(link_latency, "0.2");
78   SURFXML_BUFFER_SET(link_latency_file, "");
79   A_surfxml_link_state = A_surfxml_link_state_ON;
80   SURFXML_BUFFER_SET(link_state_file, "");
81   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
82   STAG(link);
83   ETAG(link);
84
85 /*   <route src="host A" dst="host B"><link:ctn id="LinkA"/></route> */
86   SURFXML_BUFFER_SET(route_src, "host A");
87   SURFXML_BUFFER_SET(route_dst, "host B");
88   SURFXML_BUFFER_SET(route_impact_on_src, "0.0");
89   SURFXML_BUFFER_SET(route_impact_on_dst, "0.0");
90   SURFXML_BUFFER_SET(route_impact_on_src_with_other_recv, "0.0");
91   SURFXML_BUFFER_SET(route_impact_on_dst_with_other_send, "0.0");
92
93   STAG(route);
94
95   SURFXML_BUFFER_SET(link_c_ctn_id, "LinkA");
96   STAG(link_c_ctn);
97   ETAG(link_c_ctn);
98
99   ETAG(route);
100
101 /*   <route src="host B" dst="host A"><link:ctn id="LinkA"/></route> */
102   SURFXML_BUFFER_SET(route_src, "host B");
103   SURFXML_BUFFER_SET(route_dst, "host A");
104   SURFXML_BUFFER_SET(route_impact_on_src, "0.0");
105   SURFXML_BUFFER_SET(route_impact_on_dst, "0.0");
106   SURFXML_BUFFER_SET(route_impact_on_src_with_other_recv, "0.0");
107   SURFXML_BUFFER_SET(route_impact_on_dst_with_other_send, "0.0");
108
109   STAG(route);
110
111   SURFXML_BUFFER_SET(link_c_ctn_id, "LinkA");
112   STAG(link_c_ctn);
113   ETAG(link_c_ctn);
114
115   ETAG(route);
116 /* </platform_description> */
117   ETAG(platform_description);
118
119   free(surfxml_bufferstack);
120   return 0;
121 }
122
123 static int surf_parse_bypass_application(void) {   
124   static int AX_ptr;
125   static int surfxml_bufferstack_size = 2048;
126
127   /* allocating memory to the buffer, I think 2MB should be enough */
128   surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
129   
130   /* <platform_description> */
131   SURFXML_BUFFER_SET(platform_description_version,"2");
132
133   STAG(platform_description);
134    
135 /*   <process host="host A" function="master"> */
136   SURFXML_BUFFER_SET(process_host, "host A");
137   SURFXML_BUFFER_SET(process_function, "master");
138   SURFXML_BUFFER_SET(process_start_time, "-1.0");
139   SURFXML_BUFFER_SET(process_kill_time, "-1.0");
140   STAG(process);
141
142 /*      <argument value="20"/> */
143   SURFXML_BUFFER_SET(argument_value, "20");
144   STAG(argument);
145   ETAG(argument);
146
147 /*      <argument value="5000000"/> */
148   SURFXML_BUFFER_SET(argument_value, "5000000");
149   STAG(argument);
150   ETAG(argument);
151
152 /*      <argument value="100000"/> */
153   SURFXML_BUFFER_SET(argument_value, "100000");
154   STAG(argument);
155   ETAG(argument);
156
157 /*      <argument value="host B"/> */
158   SURFXML_BUFFER_SET(argument_value, "host B");
159   STAG(argument);
160   ETAG(argument);
161
162 /*   </process> */
163   ETAG(process);
164
165 /*   <process host="host B" function="slave"/> */
166   SURFXML_BUFFER_SET(process_host, "host B");
167   SURFXML_BUFFER_SET(process_function, "slave");
168   SURFXML_BUFFER_SET(process_start_time, "-1.0");
169   SURFXML_BUFFER_SET(process_kill_time, "-1.0");
170   STAG(process);
171   ETAG(process);
172
173 /* </platform_description> */
174   ETAG(platform_description);
175
176   free(surfxml_bufferstack);
177   return 0;
178 }
179
180 int master(int argc, char *argv[]);
181 int slave(int argc, char *argv[]);
182 MSG_error_t test_all(void);
183
184 typedef enum {
185   PORT_22 = 0,
186   MAX_CHANNEL
187 } channel_t;
188
189 /** Emitter function  */
190 int master(int argc, char *argv[])
191 {
192   int slaves_count = 0;
193   m_host_t *slaves = NULL;
194   m_task_t *todo = NULL;
195   int number_of_tasks = 0;
196   double task_comp_size = 0;
197   double task_comm_size = 0;
198
199
200   int i;
201
202   xbt_assert1(sscanf(argv[1],"%d", &number_of_tasks),
203          "Invalid argument %s\n",argv[1]);
204   xbt_assert1(sscanf(argv[2],"%lg", &task_comp_size),
205          "Invalid argument %s\n",argv[2]);
206   xbt_assert1(sscanf(argv[3],"%lg", &task_comm_size),
207          "Invalid argument %s\n",argv[3]);
208
209   {                  /*  Task creation */
210     char sprintf_buffer[64];
211
212     todo = calloc(number_of_tasks, sizeof(m_task_t));
213
214     for (i = 0; i < number_of_tasks; i++) {
215       sprintf(sprintf_buffer, "Task_%d", i);
216       todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
217     }
218   }
219
220   {                  /* Process organisation */
221     slaves_count = argc - 4;
222     slaves = calloc(slaves_count, sizeof(m_host_t));
223     
224     for (i = 4; i < argc; i++) {
225       slaves[i-4] = MSG_get_host_by_name(argv[i]);
226       if(slaves[i-4]==NULL) {
227         INFO1("Unknown host %s. Stopping Now! ", argv[i]);
228         abort();
229       }
230     }
231   }
232
233   INFO1("Got %d slave(s) :", slaves_count);
234   for (i = 0; i < slaves_count; i++)
235     INFO1("\t %s", slaves[i]->name);
236
237   INFO1("Got %d task to process :", number_of_tasks);
238
239   for (i = 0; i < number_of_tasks; i++)
240     INFO1("\t\"%s\"", todo[i]->name);
241
242   for (i = 0; i < number_of_tasks; i++) {
243     INFO2("Sending \"%s\" to \"%s\"",
244                   todo[i]->name,
245                   slaves[i % slaves_count]->name);
246     if(MSG_host_self()==slaves[i % slaves_count]) {
247       INFO0("Hey ! It's me ! :)");
248     }
249     MSG_task_put(todo[i], slaves[i % slaves_count],
250                  PORT_22);
251     INFO0("Send completed");
252   }
253   
254   INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
255   for (i = 0; i < slaves_count; i++) 
256     MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
257                  slaves[i], PORT_22);
258   
259   INFO0("Goodbye now!");
260   free(slaves);
261   free(todo);
262   return 0;
263 } /* end_of_master */
264
265 /** Receiver function  */
266 int slave(int argc, char *argv[])
267 {
268   INFO0("I'm a slave");
269   while(1) {
270     m_task_t task = NULL;
271     int a;
272     a = MSG_task_get(&(task), PORT_22);
273     if (a == MSG_OK) {
274       INFO1("Received \"%s\" ", MSG_task_get_name(task));
275       if(MSG_task_get_data(task)==FINALIZE) {
276         MSG_task_destroy(task);
277         break;
278       }
279       INFO1("Processing \"%s\" ", MSG_task_get_name(task));
280       MSG_task_execute(task);
281       INFO1("\"%s\" done ", MSG_task_get_name(task));
282       MSG_task_destroy(task);
283     } else {
284       INFO0("Hey ?! What's up ? ");
285       xbt_assert0(0,"Unexpected behavior");
286     }
287   }
288   INFO0("I'm done. See you!");
289   return 0;
290 } /* end_of_slave */
291
292 /** Test function */
293 MSG_error_t test_all(void)
294 {
295   MSG_error_t res = MSG_OK;
296
297    /*  Simulation setting */
298    MSG_set_channel_number(MAX_CHANNEL);
299    MSG_paje_output("msg_test.trace");
300    surf_parse = surf_parse_bypass_platform;
301    MSG_create_environment(NULL);
302   
303    /*   Application deployment */
304    MSG_function_register("master", master);
305    MSG_function_register("slave", slave);
306    surf_parse = surf_parse_bypass_application;
307    MSG_launch_application(NULL);
308    
309    res = MSG_main();
310   
311   INFO1("Simulation time %g",MSG_get_clock());
312   return res;
313 } /* end_of_test_all */
314
315 /** Main function */
316 int main(int argc, char *argv[])
317 {
318   MSG_error_t res = MSG_OK;
319
320   MSG_global_init(&argc,argv);
321   res = test_all();
322   MSG_clean();
323
324   if(res==MSG_OK) return 0; 
325   else return 1;
326 } /* end_of_main */
327