Logo AND Algorithmique Numérique Distribuée

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