Logo AND Algorithmique Numérique Distribuée

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