Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix stupid comments
[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(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,"1");
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
117 /*   <process host="host A" function="master"> */
118   SURFXML_BUFFER_SET(process_host, "host A");
119   SURFXML_BUFFER_SET(process_function, "master");
120   SURFXML_BUFFER_SET(process_start_time, "-1.0");
121   SURFXML_BUFFER_SET(process_kill_time, "-1.0");
122   STAG(process);
123
124 /*      <argument value="20"/> */
125   SURFXML_BUFFER_SET(argument_value, "20");
126   STAG(argument);
127   ETAG(argument);
128
129 /*      <argument value="5000000"/> */
130   SURFXML_BUFFER_SET(argument_value, "5000000");
131   STAG(argument);
132   ETAG(argument);
133
134 /*      <argument value="100000"/> */
135   SURFXML_BUFFER_SET(argument_value, "100000");
136   STAG(argument);
137   ETAG(argument);
138
139 /*      <argument value="host B"/> */
140   SURFXML_BUFFER_SET(argument_value, "host B");
141   STAG(argument);
142   ETAG(argument);
143
144 /*   </process> */
145   ETAG(process);
146
147 /*   <process host="host B" function="slave"/> */
148   SURFXML_BUFFER_SET(process_host, "host B");
149   SURFXML_BUFFER_SET(process_function, "slave");
150   SURFXML_BUFFER_SET(process_start_time, "-1.0");
151   SURFXML_BUFFER_SET(process_kill_time, "-1.0");
152   STAG(process);
153   ETAG(process);
154
155 /* </platform_description> */
156   ETAG(platform_description);
157
158   free(surfxml_bufferstack);
159   return 0;
160 }
161
162 int master(int argc, char *argv[]);
163 int slave(int argc, char *argv[]);
164 MSG_error_t test_all(void);
165
166 typedef enum {
167   PORT_22 = 0,
168   MAX_CHANNEL
169 } channel_t;
170
171 /** Emitter function  */
172 int master(int argc, char *argv[])
173 {
174   int slaves_count = 0;
175   m_host_t *slaves = NULL;
176   m_task_t *todo = NULL;
177   int number_of_tasks = 0;
178   double task_comp_size = 0;
179   double task_comm_size = 0;
180
181
182   int i;
183
184   xbt_assert1(sscanf(argv[1],"%d", &number_of_tasks),
185          "Invalid argument %s\n",argv[1]);
186   xbt_assert1(sscanf(argv[2],"%lg", &task_comp_size),
187          "Invalid argument %s\n",argv[2]);
188   xbt_assert1(sscanf(argv[3],"%lg", &task_comm_size),
189          "Invalid argument %s\n",argv[3]);
190
191   {                  /*  Task creation */
192     char sprintf_buffer[64];
193
194     todo = calloc(number_of_tasks, sizeof(m_task_t));
195
196     for (i = 0; i < number_of_tasks; i++) {
197       sprintf(sprintf_buffer, "Task_%d", i);
198       todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
199     }
200   }
201
202   {                  /* Process organisation */
203     slaves_count = argc - 4;
204     slaves = calloc(slaves_count, sizeof(m_host_t));
205     
206     for (i = 4; i < argc; i++) {
207       slaves[i-4] = MSG_get_host_by_name(argv[i]);
208       if(slaves[i-4]==NULL) {
209         INFO1("Unknown host %s. Stopping Now! ", argv[i]);
210         abort();
211       }
212     }
213   }
214
215   INFO1("Got %d slave(s) :", slaves_count);
216   for (i = 0; i < slaves_count; i++)
217     INFO1("\t %s", slaves[i]->name);
218
219   INFO1("Got %d task to process :", number_of_tasks);
220
221   for (i = 0; i < number_of_tasks; i++)
222     INFO1("\t\"%s\"", todo[i]->name);
223
224   for (i = 0; i < number_of_tasks; i++) {
225     INFO2("Sending \"%s\" to \"%s\"",
226                   todo[i]->name,
227                   slaves[i % slaves_count]->name);
228     if(MSG_host_self()==slaves[i % slaves_count]) {
229       INFO0("Hey ! It's me ! :)");
230     }
231     MSG_task_put(todo[i], slaves[i % slaves_count],
232                  PORT_22);
233     INFO0("Send completed");
234   }
235   
236   INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
237   for (i = 0; i < slaves_count; i++) 
238     MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
239                  slaves[i], PORT_22);
240   
241   INFO0("Goodbye now!");
242   free(slaves);
243   free(todo);
244   return 0;
245 } /* end_of_master */
246
247 /** Receiver function  */
248 int slave(int argc, char *argv[])
249 {
250   while(1) {
251     m_task_t task = NULL;
252     int a;
253     a = MSG_task_get(&(task), PORT_22);
254     if (a == MSG_OK) {
255       INFO1("Received \"%s\" ", MSG_task_get_name(task));
256       if(MSG_task_get_data(task)==FINALIZE) {
257         MSG_task_destroy(task);
258         break;
259       }
260       INFO1("Processing \"%s\" ", MSG_task_get_name(task));
261       MSG_task_execute(task);
262       INFO1("\"%s\" done ", MSG_task_get_name(task));
263       MSG_task_destroy(task);
264     } else {
265       INFO0("Hey ?! What's up ? ");
266       xbt_assert0(0,"Unexpected behavior");
267     }
268   }
269   INFO0("I'm done. See you!");
270   return 0;
271 } /* end_of_slave */
272
273 /** Test function */
274 MSG_error_t test_all(void)
275 {
276   MSG_error_t res = MSG_OK;
277
278   {                             /*  Simulation setting */
279     MSG_set_channel_number(MAX_CHANNEL);
280     MSG_paje_output("msg_test.trace");
281     surf_parse = surf_parse_bypass;
282     MSG_create_environment(NULL);
283   }
284   {                            /*   Application deployment */
285     MSG_function_register("master", master);
286     MSG_function_register("slave", slave);
287     MSG_launch_application(NULL);
288   }
289   res = MSG_main();
290   
291   INFO1("Simulation time %g",MSG_get_clock());
292   return res;
293 } /* end_of_test_all */
294
295 /** Main function */
296 int main(int argc, char *argv[])
297 {
298   MSG_error_t res = MSG_OK;
299
300   MSG_global_init(&argc,argv);
301   res = test_all();
302   MSG_clean();
303
304   if(res==MSG_OK) return 0; 
305   else return 1;
306 } /* end_of_main */
307