Logo AND Algorithmique Numérique Distribuée

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