Logo AND Algorithmique Numérique Distribuée

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