Logo AND Algorithmique Numérique Distribuée

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