Logo AND Algorithmique Numérique Distribuée

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