Logo AND Algorithmique Numérique Distribuée

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