Logo AND Algorithmique Numérique Distribuée

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