Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d881c4cc504e2151a192d56c7d2e9c4518476507
[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 */
11 #include "surf/surfxml.h"       /* to hijack surf_parse_lex */
12
13 /* Create a log channel to have nice outputs. */
14 #include "xbt/log.h"
15 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,"Messages specific for this msg example");
16 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
17
18 #define SURFXML_BUFFER_SET(key,val) do { \
19   AX_surfxml_##key=AX_ptr; \
20   strcpy(A_surfxml_##key,val); \
21   AX_ptr+=strlen(val)+1; } while(0)
22
23 #define SURFXML_BUFFER_RESET() do { \
24   AX_ptr = 0; \
25   memset(surfxml_bufferstack,0,surfxml_bufferstack_size); } while(0)
26      
27
28 static int surf_parse_bypass(void)
29 {
30   static int AX_ptr;
31   static int surfxml_bufferstack_size = 2048;
32
33   /* allocating memory to the buffer, I think 2MB should be enough */
34   surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
35   
36   /* <platform_description> */
37   SURFXML_BUFFER_RESET();
38   SURFXML_BUFFER_SET(platform_description_version,"1");
39
40   STag_surfxml_platform_description();
41
42 /*   <cpu name="Cpu A" power="100000000.00"/> */
43   SURFXML_BUFFER_RESET();
44   SURFXML_BUFFER_SET(cpu_name,"Cpu A");
45   SURFXML_BUFFER_SET(cpu_power, "100000000.00");
46   SURFXML_BUFFER_SET(cpu_availability, "1.0");
47   SURFXML_BUFFER_SET(cpu_availability_file, "");
48   A_surfxml_cpu_state = A_surfxml_cpu_state_ON;
49   SURFXML_BUFFER_SET(cpu_state_file, "");
50   SURFXML_BUFFER_SET(cpu_interference_send, "1.0");
51   SURFXML_BUFFER_SET(cpu_interference_recv, "1.0");
52   SURFXML_BUFFER_SET(cpu_interference_send_recv, "1.0");
53   SURFXML_BUFFER_SET(cpu_max_outgoing_rate, "-1.0");
54
55   STag_surfxml_cpu();
56   ETag_surfxml_cpu();
57
58 /*   <cpu name="Cpu B" power="100000000.00"/> */
59   SURFXML_BUFFER_RESET();
60   SURFXML_BUFFER_SET(cpu_name, "Cpu B");
61   SURFXML_BUFFER_SET(cpu_power, "100000000.00");
62   SURFXML_BUFFER_SET(cpu_availability, "1.0");
63   SURFXML_BUFFER_SET(cpu_availability_file, "");
64   A_surfxml_cpu_state = A_surfxml_cpu_state_ON;
65   SURFXML_BUFFER_SET(cpu_state_file, "");
66   SURFXML_BUFFER_SET(cpu_interference_send, "1.0");
67   SURFXML_BUFFER_SET(cpu_interference_recv, "1.0");
68   SURFXML_BUFFER_SET(cpu_interference_send_recv, "1.0");
69   SURFXML_BUFFER_SET(cpu_max_outgoing_rate, "-1.0");
70
71   STag_surfxml_cpu();
72   ETag_surfxml_cpu();
73
74 /*   <network_link name="LinkA" bandwidth="10000000.0" latency="0.2"/> */
75   SURFXML_BUFFER_RESET();
76   SURFXML_BUFFER_SET(network_link_name, "LinkA");
77   SURFXML_BUFFER_SET(network_link_bandwidth, "10000000.0");
78   SURFXML_BUFFER_SET(network_link_bandwidth_file, "");
79   SURFXML_BUFFER_SET(network_link_latency, "0.2");
80   SURFXML_BUFFER_SET(network_link_latency_file, "");
81   A_surfxml_network_link_state = A_surfxml_network_link_state_ON;
82   SURFXML_BUFFER_SET(network_link_state_file, "");
83   A_surfxml_network_link_sharing_policy = A_surfxml_network_link_sharing_policy_SHARED;
84   STag_surfxml_network_link();
85   ETag_surfxml_network_link();
86
87 /*   <route src="Cpu A" dst="Cpu B"><route_element name="LinkA"/></route> */
88   SURFXML_BUFFER_RESET();
89   SURFXML_BUFFER_SET(route_src, "Cpu A");
90   SURFXML_BUFFER_SET(route_dst, "Cpu B");
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   STag_surfxml_route();
97
98   SURFXML_BUFFER_SET(route_element_name, "LinkA");
99   STag_surfxml_route_element();
100   ETag_surfxml_route_element();
101
102   ETag_surfxml_route();
103
104 /*   <route src="Cpu B" dst="Cpu A"><route_element name="LinkA"/></route> */
105   SURFXML_BUFFER_RESET();
106   SURFXML_BUFFER_SET(route_src, "Cpu B");
107   SURFXML_BUFFER_SET(route_dst, "Cpu A");
108   SURFXML_BUFFER_SET(route_impact_on_src, "0.0");
109   SURFXML_BUFFER_SET(route_impact_on_dst, "0.0");
110   SURFXML_BUFFER_SET(route_impact_on_src_with_other_recv, "0.0");
111   SURFXML_BUFFER_SET(route_impact_on_dst_with_other_send, "0.0");
112
113   STag_surfxml_route();
114
115   SURFXML_BUFFER_SET(route_element_name, "LinkA");
116   STag_surfxml_route_element();
117   ETag_surfxml_route_element();
118
119   ETag_surfxml_route();
120
121 /*   <process host="Cpu A" function="master"> */
122   SURFXML_BUFFER_RESET();
123   SURFXML_BUFFER_SET(process_host, "Cpu 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   STag_surfxml_process();
128
129 /*      <argument value="20"/> */
130   SURFXML_BUFFER_SET(argument_value, "20");
131   STag_surfxml_argument();
132   ETag_surfxml_argument();
133
134 /*      <argument value="5000000"/> */
135   SURFXML_BUFFER_SET(argument_value, "5000000");
136   STag_surfxml_argument();
137   ETag_surfxml_argument();
138
139 /*      <argument value="100000"/> */
140   SURFXML_BUFFER_SET(argument_value, "100000");
141   STag_surfxml_argument();
142   ETag_surfxml_argument();
143
144 /*      <argument value="Cpu B"/> */
145   SURFXML_BUFFER_SET(argument_value, "Cpu B");
146   STag_surfxml_argument();
147   ETag_surfxml_argument();
148
149 /*   </process> */
150   ETag_surfxml_process();
151
152 /*   <process host="Cpu B" function="slave"/> */
153   SURFXML_BUFFER_RESET();
154   SURFXML_BUFFER_SET(process_host, "Cpu B");
155   SURFXML_BUFFER_SET(process_function, "slave");
156   SURFXML_BUFFER_SET(process_start_time, "-1.0");
157   SURFXML_BUFFER_SET(process_kill_time, "-1.0");
158   STag_surfxml_process();
159   ETag_surfxml_process();
160
161 /* </platform_description> */
162   ETag_surfxml_platform_description();
163
164   free(surfxml_bufferstack);
165   return 0;
166 }
167
168 int master(int argc, char *argv[]);
169 int slave(int argc, char *argv[]);
170 MSG_error_t test_all(void);
171
172 typedef enum {
173   PORT_22 = 0,
174   MAX_CHANNEL
175 } channel_t;
176
177 /** Emitter function  */
178 int master(int argc, char *argv[])
179 {
180   int slaves_count = 0;
181   m_host_t *slaves = NULL;
182   m_task_t *todo = NULL;
183   int number_of_tasks = 0;
184   double task_comp_size = 0;
185   double task_comm_size = 0;
186
187
188   int i;
189
190   xbt_assert1(sscanf(argv[1],"%d", &number_of_tasks),
191          "Invalid argument %s\n",argv[1]);
192   xbt_assert1(sscanf(argv[2],"%lg", &task_comp_size),
193          "Invalid argument %s\n",argv[2]);
194   xbt_assert1(sscanf(argv[3],"%lg", &task_comm_size),
195          "Invalid argument %s\n",argv[3]);
196
197   {                  /*  Task creation */
198     char sprintf_buffer[64];
199
200     todo = calloc(number_of_tasks, sizeof(m_task_t));
201
202     for (i = 0; i < number_of_tasks; i++) {
203       sprintf(sprintf_buffer, "Task_%d", i);
204       todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
205     }
206   }
207
208   {                  /* Process organisation */
209     slaves_count = argc - 4;
210     slaves = calloc(slaves_count, sizeof(m_host_t));
211     
212     for (i = 4; i < argc; i++) {
213       slaves[i-4] = MSG_get_host_by_name(argv[i]);
214       if(slaves[i-4]==NULL) {
215         INFO1("Unknown host %s. Stopping Now! ", argv[i]);
216         abort();
217       }
218     }
219   }
220
221   INFO1("Got %d slave(s) :", slaves_count);
222   for (i = 0; i < slaves_count; i++)
223     INFO1("\t %s", slaves[i]->name);
224
225   INFO1("Got %d task to process :", number_of_tasks);
226
227   for (i = 0; i < number_of_tasks; i++)
228     INFO1("\t\"%s\"", todo[i]->name);
229
230   for (i = 0; i < number_of_tasks; i++) {
231     INFO2("Sending \"%s\" to \"%s\"",
232                   todo[i]->name,
233                   slaves[i % slaves_count]->name);
234     if(MSG_host_self()==slaves[i % slaves_count]) {
235       INFO0("Hey ! It's me ! :)");
236     }
237     MSG_task_put(todo[i], slaves[i % slaves_count],
238                  PORT_22);
239     INFO0("Send completed");
240   }
241   
242   INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
243   for (i = 0; i < slaves_count; i++) 
244     MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
245                  slaves[i], PORT_22);
246   
247   INFO0("Goodbye now!");
248   free(slaves);
249   free(todo);
250   return 0;
251 } /* end_of_master */
252
253 /** Receiver function  */
254 int slave(int argc, char *argv[])
255 {
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;
288     MSG_create_environment(NULL);
289   }
290   {                            /*   Application deployment */
291     MSG_function_register("master", master);
292     MSG_function_register("slave", slave);
293     MSG_launch_application(NULL);
294   }
295   res = MSG_main();
296   
297   INFO1("Simulation time %g",MSG_get_clock());
298   return res;
299 } /* end_of_test_all */
300
301 /** Main function */
302 int main(int argc, char *argv[])
303 {
304   MSG_error_t res = MSG_OK;
305
306   MSG_global_init(&argc,argv);
307   res = test_all();
308   MSG_clean();
309
310   if(res==MSG_OK) return 0; 
311   else return 1;
312 } /* end_of_main */
313