Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'dvfs'
[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   /* allocating memory for the buffer, I think 2kB should be enough */
24   surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
25
26   XBT_DEBUG("<platform>");
27   SURFXML_BUFFER_SET(platform_version, "3");
28   SURFXML_START_TAG(platform);
29
30   XBT_DEBUG("<AS>");
31   SURFXML_BUFFER_SET(AS_id, "AS0");
32   A_surfxml_AS_routing = A_surfxml_AS_routing_Full;
33   SURFXML_START_TAG(AS);
34
35   XBT_DEBUG("<host id=\"host A\" power=\"100000000.00\"/>");
36   SURFXML_BUFFER_SET(host_id, "host A");
37   SURFXML_BUFFER_SET(host_power, "100000000.00");
38   SURFXML_BUFFER_SET(host_pstate, "0");
39   SURFXML_BUFFER_SET(host_availability, "1.0");
40   SURFXML_BUFFER_SET(host_availability___file, "");
41   SURFXML_BUFFER_SET(host_core, "1");
42   A_surfxml_host_state = A_surfxml_host_state_ON;
43   SURFXML_BUFFER_SET(host_state___file, "");
44   SURFXML_BUFFER_SET(host_coordinates, "");
45   SURFXML_START_TAG(host);
46   SURFXML_END_TAG(host);
47
48   XBT_DEBUG("<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_pstate, "0");
52   SURFXML_BUFFER_SET(host_availability, "1.0");
53   SURFXML_BUFFER_SET(host_availability___file, "");
54   SURFXML_BUFFER_SET(host_core, "1");
55   A_surfxml_host_state = A_surfxml_host_state_ON;
56   SURFXML_BUFFER_SET(host_state___file, "");
57   SURFXML_BUFFER_SET(host_coordinates, "");
58   SURFXML_START_TAG(host);
59   SURFXML_END_TAG(host);
60
61   XBT_DEBUG("<link id=\"LinkA\" bandwidth=\"10000000.0\" latency=\"0.2\"/>");
62   SURFXML_BUFFER_SET(link_id, "LinkA");
63   SURFXML_BUFFER_SET(link_bandwidth, "10000000.0");
64   SURFXML_BUFFER_SET(link_bandwidth___file, "");
65   SURFXML_BUFFER_SET(link_latency, "0.2");
66   SURFXML_BUFFER_SET(link_latency___file, "");
67   A_surfxml_link_state = A_surfxml_link_state_ON;
68   SURFXML_BUFFER_SET(link_state___file, "");
69   A_surfxml_link_sharing___policy = A_surfxml_link_sharing___policy_SHARED;
70   SURFXML_START_TAG(link);
71   SURFXML_END_TAG(link);
72
73   XBT_DEBUG("<route src=\"host A\" dst=\"host B\">");
74   SURFXML_BUFFER_SET(route_src, "host A");
75   SURFXML_BUFFER_SET(route_dst, "host B");
76   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_YES;
77   SURFXML_START_TAG(route);
78   XBT_DEBUG("  <link:ctn id=\"LinkA\"/>");
79   SURFXML_BUFFER_SET(link___ctn_id, "LinkA");
80   A_surfxml_link___ctn_direction = A_surfxml_link___ctn_direction_NONE;
81   SURFXML_START_TAG(link___ctn);
82   SURFXML_END_TAG(link___ctn);
83   XBT_DEBUG("</route>");
84   SURFXML_END_TAG(route);
85
86   XBT_DEBUG("</AS>");
87   SURFXML_END_TAG(AS);
88   XBT_DEBUG("</platfrom>");
89   SURFXML_END_TAG(platform);
90
91   free(surfxml_bufferstack);
92   return 0;
93 }
94
95 static int surf_parse_bypass_application(void)
96 {
97   static int AX_ptr;
98   static int surfxml_bufferstack_size = 2048;
99   static int surfxml_buffer_stack_stack_ptr = 0;
100   static int surfxml_buffer_stack_stack[1024];
101   /* allocating memory to the buffer, I think 2MB should be enough */
102   surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
103
104   /* <platform> */
105   SURFXML_BUFFER_SET(platform_version, "3");
106
107   SURFXML_START_TAG(platform);
108
109 /*   <process host="host A" function="master"> */
110   SURFXML_BUFFER_SET(process_host, "host A");
111   SURFXML_BUFFER_SET(process_function, "master");
112   SURFXML_BUFFER_SET(process_start___time, "-1.0");
113   SURFXML_BUFFER_SET(process_kill___time, "-1.0");
114   SURFXML_START_TAG(process);
115
116 /*      <argument value="20"/> */
117   SURFXML_BUFFER_SET(argument_value, "20");
118   SURFXML_START_TAG(argument);
119   SURFXML_END_TAG(argument);
120
121 /*      <argument value="5000000"/> */
122   SURFXML_BUFFER_SET(argument_value, "5000000");
123   SURFXML_START_TAG(argument);
124   SURFXML_END_TAG(argument);
125
126 /*      <argument value="100000"/> */
127   SURFXML_BUFFER_SET(argument_value, "100000");
128   SURFXML_START_TAG(argument);
129   SURFXML_END_TAG(argument);
130
131 /*      <argument value="host B"/> */
132   SURFXML_BUFFER_SET(argument_value, "host B");
133   SURFXML_START_TAG(argument);
134   SURFXML_END_TAG(argument);
135
136 /*   </process> */
137   SURFXML_END_TAG(process);
138
139 /*   <process host="host B" function="slave"/> */
140   SURFXML_BUFFER_SET(process_host, "host B");
141   SURFXML_BUFFER_SET(process_function, "slave");
142   SURFXML_BUFFER_SET(process_start___time, "-1.0");
143   SURFXML_BUFFER_SET(process_kill___time, "-1.0");
144   SURFXML_START_TAG(process);
145   SURFXML_END_TAG(process);
146
147 /* </platform> */
148   SURFXML_END_TAG(platform);
149
150   free(surfxml_bufferstack);
151   return 0;
152 }
153
154 int master(int argc, char *argv[]);
155 int slave(int argc, char *argv[]);
156 msg_error_t test_all(void);
157
158 /** Emitter function  */
159 int master(int argc, char *argv[])
160 {
161   int slaves_count = 0;
162   msg_host_t *slaves = NULL;
163   msg_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   _XBT_GNUC_UNUSED int read;
169
170   read = sscanf(argv[1], "%d", &number_of_tasks);
171   xbt_assert(read, "Invalid argument %s\n", argv[1]);
172   read = sscanf(argv[2], "%lg", &task_comp_size);
173   xbt_assert(read, "Invalid argument %s\n", argv[2]);
174   read = sscanf(argv[3], "%lg", &task_comm_size);
175   xbt_assert(read, "Invalid argument %s\n", argv[3]);
176
177   {                             /*  Task creation */
178     char sprintf_buffer[64];
179
180     todo = xbt_new0(msg_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(msg_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", MSG_host_get_name(slaves[i]));
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, MSG_host_get_name(slaves[i % slaves_count]));
215     if (MSG_host_self() == slaves[i % slaves_count]) {
216       XBT_INFO("Hey ! It's me ! :)");
217     }
218     MSG_task_send(todo[i], MSG_host_get_name(slaves[i % slaves_count]));
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_send(MSG_task_create("finalize", 0, 0, FINALIZE),
226         MSG_host_get_name(slaves[i]));
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     msg_task_t task = NULL;
240     int a;
241     a = MSG_task_receive(&task, MSG_host_get_name(MSG_host_self()));
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   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   XBT_INFO("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_init(&argc, argv);
288   res = test_all();
289
290   if (res == MSG_OK)
291     return 0;
292   else
293     return 1;
294 }                               /* end_of_main */