Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use new style logging macros.
[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
168
169   int i;
170
171   xbt_assert1(sscanf(argv[1], "%d", &number_of_tasks),
172               "Invalid argument %s\n", argv[1]);
173   xbt_assert1(sscanf(argv[2], "%lg", &task_comp_size),
174               "Invalid argument %s\n", argv[2]);
175   xbt_assert1(sscanf(argv[3], "%lg", &task_comm_size),
176               "Invalid argument %s\n", argv[3]);
177
178   {                             /*  Task creation */
179     char sprintf_buffer[64];
180
181     todo = xbt_new0(m_task_t, number_of_tasks);
182
183     for (i = 0; i < number_of_tasks; i++) {
184       sprintf(sprintf_buffer, "Task_%d", i);
185       todo[i] =
186           MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
187                           NULL);
188     }
189   }
190
191   {                             /* Process organisation */
192     slaves_count = argc - 4;
193     slaves = xbt_new0(m_host_t, slaves_count);
194
195     for (i = 4; i < argc; i++) {
196       slaves[i - 4] = MSG_get_host_by_name(argv[i]);
197       if (slaves[i - 4] == NULL) {
198         XBT_INFO("Unknown host %s. Stopping Now! ", argv[i]);
199         abort();
200       }
201     }
202   }
203
204   XBT_INFO("Got %d slave(s) :", slaves_count);
205   for (i = 0; i < slaves_count; i++)
206     XBT_INFO("\t %s", slaves[i]->name);
207
208   XBT_INFO("Got %d task to process :", number_of_tasks);
209
210   for (i = 0; i < number_of_tasks; i++)
211     XBT_INFO("\t\"%s\"", todo[i]->name);
212
213   for (i = 0; i < number_of_tasks; i++) {
214     XBT_INFO("Sending \"%s\" to \"%s\"",
215           todo[i]->name, slaves[i % slaves_count]->name);
216     if (MSG_host_self() == slaves[i % slaves_count]) {
217       XBT_INFO("Hey ! It's me ! :)");
218     }
219     MSG_task_put(todo[i], slaves[i % slaves_count], PORT_22);
220     XBT_INFO("Send completed");
221   }
222
223   XBT_INFO
224       ("All tasks have been dispatched. Let's tell everybody the computation is over.");
225   for (i = 0; i < slaves_count; i++)
226     MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
227                  slaves[i], PORT_22);
228
229   XBT_INFO("Goodbye now!");
230   free(slaves);
231   free(todo);
232   return 0;
233 }                               /* end_of_master */
234
235 /** Receiver function  */
236 int slave(int argc, char *argv[])
237 {
238   XBT_INFO("I'm a slave");
239   while (1) {
240     m_task_t task = NULL;
241     int a;
242     a = MSG_task_get(&(task), PORT_22);
243     if (a == MSG_OK) {
244       XBT_INFO("Received \"%s\" ", MSG_task_get_name(task));
245       if (MSG_task_get_data(task) == FINALIZE) {
246         MSG_task_destroy(task);
247         break;
248       }
249       XBT_INFO("Processing \"%s\" ", MSG_task_get_name(task));
250       MSG_task_execute(task);
251       XBT_INFO("\"%s\" done ", MSG_task_get_name(task));
252       MSG_task_destroy(task);
253     } else {
254       XBT_INFO("Hey ?! What's up ? ");
255       xbt_assert0(0, "Unexpected behavior");
256     }
257   }
258   XBT_INFO("I'm done. See you!");
259   return 0;
260 }                               /* end_of_slave */
261
262 /** Test function */
263 MSG_error_t test_all(void)
264 {
265   MSG_error_t res = MSG_OK;
266
267   /*  Simulation setting */
268   MSG_set_channel_number(MAX_CHANNEL);
269   surf_parse = surf_parse_bypass_platform;
270   MSG_create_environment(NULL);
271
272   /*   Application deployment */
273   MSG_function_register("master", master);
274   MSG_function_register("slave", slave);
275   surf_parse = surf_parse_bypass_application;
276   MSG_launch_application(NULL);
277
278   res = MSG_main();
279
280   XBT_INFO("Simulation time %g", MSG_get_clock());
281   return res;
282 }                               /* end_of_test_all */
283
284 /** Main function */
285 int main(int argc, char *argv[])
286 {
287   MSG_error_t res = MSG_OK;
288
289   MSG_global_init(&argc, argv);
290   res = test_all();
291   MSG_clean();
292
293   if (res == MSG_OK)
294     return 0;
295   else
296     return 1;
297 }                               /* end_of_main */