Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Stupid bug
[simgrid.git] / examples / msg / msg_test_surfxml_bypassed.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 "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 */
10 #include "surf/surfxml.h"       /* to hijack surf_parse_lex */
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(void)
18 {
19 /* <platform_description> */
20   A_surfxml_platform_description_version = "1";
21   STag_surfxml_platform_description();
22
23
24 /*   <cpu name="Cpu A" power="100000000.00" availability_file="trace_A.txt"/> */
25   A_surfxml_cpu_name = "Cpu A";
26   A_surfxml_cpu_power= "100000000.00";
27   A_surfxml_cpu_availability = "1.0";
28   A_surfxml_cpu_availability_file = "";
29   A_surfxml_cpu_state = A_surfxml_cpu_state_ON;
30   A_surfxml_cpu_state_file = NULL;
31   A_surfxml_cpu_interference_send = "1.0";
32   A_surfxml_cpu_interference_recv = "1.0";
33   A_surfxml_cpu_interference_send_recv = "1.0";
34   A_surfxml_cpu_max_outgoing_rate = "-1.0";
35
36   STag_surfxml_cpu();
37   ETag_surfxml_cpu();
38
39 /*   <cpu name="Cpu B" power="100000000.00" availability_file="trace_B.txt"/> */
40   A_surfxml_cpu_name = "Cpu B";
41   A_surfxml_cpu_power= "100000000.00";
42   A_surfxml_cpu_availability = "1.0";
43   A_surfxml_cpu_availability_file = "";
44   A_surfxml_cpu_state = A_surfxml_cpu_state_ON;
45   A_surfxml_cpu_state_file = NULL;
46   A_surfxml_cpu_interference_send = "1.0";
47   A_surfxml_cpu_interference_recv = "1.0";
48   A_surfxml_cpu_interference_send_recv = "1.0";
49   A_surfxml_cpu_max_outgoing_rate = "-1.0";
50
51   STag_surfxml_cpu();
52   ETag_surfxml_cpu();
53
54 /*   <network_link name="LinkA" bandwidth="10000000.0" latency="0.2"/> */
55   A_surfxml_network_link_name = "LinkA";
56   A_surfxml_network_link_bandwidth = "10000000.0";
57   A_surfxml_network_link_bandwidth_file = NULL;
58   A_surfxml_network_link_latency = "0.2";
59   A_surfxml_network_link_latency_file = NULL;
60   A_surfxml_network_link_state = A_surfxml_network_link_state_ON;
61   A_surfxml_network_link_state_file = NULL;
62   A_surfxml_network_link_sharing_policy = A_surfxml_network_link_sharing_policy_SHARED;
63   STag_surfxml_network_link();
64   ETag_surfxml_network_link();
65
66 /*   <route src="Cpu A" dst="Cpu B"><route_element name="LinkA"/></route> */
67   A_surfxml_route_src = "Cpu A";
68   A_surfxml_route_dst = "Cpu B";
69   A_surfxml_route_impact_on_src = "0.0";
70   A_surfxml_route_impact_on_dst = "0.0";
71   A_surfxml_route_impact_on_src_with_other_recv = "0.0";
72   A_surfxml_route_impact_on_dst_with_other_send = "0.0";
73
74   STag_surfxml_route();
75
76   A_surfxml_route_element_name = "LinkA";
77   STag_surfxml_route_element();
78   ETag_surfxml_route_element();
79
80   ETag_surfxml_route();
81
82 /*   <route src="Cpu B" dst="Cpu A"><route_element name="LinkA"/></route> */
83   A_surfxml_route_src = "Cpu B";
84   A_surfxml_route_dst = "Cpu A";
85   A_surfxml_route_impact_on_src = "0.0";
86   A_surfxml_route_impact_on_dst = "0.0";
87   A_surfxml_route_impact_on_src_with_other_recv = "0.0";
88   A_surfxml_route_impact_on_dst_with_other_send = "0.0";
89
90   STag_surfxml_route();
91
92   A_surfxml_route_element_name = "LinkA";
93   STag_surfxml_route_element();
94   ETag_surfxml_route_element();
95
96   ETag_surfxml_route();
97
98 /*   <process host="Cpu A" function="master"> */
99   A_surfxml_process_host = "Cpu A";
100   A_surfxml_process_function = "master";
101   A_surfxml_process_start_time = "-1.0";
102   A_surfxml_process_kill_time = "-1.0";
103   STag_surfxml_process();
104
105 /*      <argument value="20"/> */
106   A_surfxml_argument_value = "20";
107   STag_surfxml_argument();
108   ETag_surfxml_argument();
109
110 /*      <argument value="50000"/> */
111   A_surfxml_argument_value = "50000000000";
112   STag_surfxml_argument();
113   ETag_surfxml_argument();
114
115 /*      <argument value="10"/> */
116   A_surfxml_argument_value = "10000000";
117   STag_surfxml_argument();
118   ETag_surfxml_argument();
119
120 /*      <argument value="Cpu B"/> */
121   A_surfxml_argument_value = "Cpu B";
122   STag_surfxml_argument();
123   ETag_surfxml_argument();
124
125 /*   </process> */
126   ETag_surfxml_process();
127
128 /*   <process host="Cpu B" function="slave"/> */
129   A_surfxml_process_host = "Cpu B";
130   A_surfxml_process_function = "slave";
131   A_surfxml_process_start_time = "-1.0";
132   A_surfxml_process_kill_time = "-1.0";
133   STag_surfxml_process();
134   ETag_surfxml_process();
135
136 /* </platform_description> */
137   ETag_surfxml_platform_description();
138
139   return 0;
140 }
141
142 int master(int argc, char *argv[]);
143 int slave(int argc, char *argv[]);
144 MSG_error_t test_all(void);
145
146 typedef enum {
147   PORT_22 = 0,
148   MAX_CHANNEL
149 } channel_t;
150
151 /** Emitter function  */
152 int master(int argc, char *argv[])
153 {
154   int slaves_count = 0;
155   m_host_t *slaves = NULL;
156   m_task_t *todo = NULL;
157   int number_of_tasks = 0;
158   double task_comp_size = 0;
159   double task_comm_size = 0;
160
161
162   int i;
163
164   xbt_assert1(sscanf(argv[1],"%d", &number_of_tasks),
165          "Invalid argument %s\n",argv[1]);
166   xbt_assert1(sscanf(argv[2],"%lg", &task_comp_size),
167          "Invalid argument %s\n",argv[2]);
168   xbt_assert1(sscanf(argv[3],"%lg", &task_comm_size),
169          "Invalid argument %s\n",argv[3]);
170
171   {                  /*  Task creation */
172     char sprintf_buffer[64];
173
174     todo = calloc(number_of_tasks, sizeof(m_task_t));
175
176     for (i = 0; i < number_of_tasks; i++) {
177       sprintf(sprintf_buffer, "Task_%d", i);
178       todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
179     }
180   }
181
182   {                  /* Process organisation */
183     slaves_count = argc - 4;
184     slaves = calloc(slaves_count, sizeof(m_host_t));
185     
186     for (i = 4; i < argc; i++) {
187       slaves[i-4] = MSG_get_host_by_name(argv[i]);
188       if(slaves[i-4]==NULL) {
189         INFO1("Unknown host %s. Stopping Now! ", argv[i]);
190         abort();
191       }
192     }
193   }
194
195   INFO1("Got %d slave(s) :", slaves_count);
196   for (i = 0; i < slaves_count; i++)
197     INFO1("\t %s", slaves[i]->name);
198
199   INFO1("Got %d task to process :", number_of_tasks);
200
201   for (i = 0; i < number_of_tasks; i++)
202     INFO1("\t\"%s\"", todo[i]->name);
203
204   for (i = 0; i < number_of_tasks; i++) {
205     INFO2("Sending \"%s\" to \"%s\"",
206                   todo[i]->name,
207                   slaves[i % slaves_count]->name);
208     if(MSG_host_self()==slaves[i % slaves_count]) {
209       INFO0("Hey ! It's me ! :)");
210     }
211     MSG_task_put(todo[i], slaves[i % slaves_count],
212                  PORT_22);
213     INFO0("Send completed");
214   }
215   
216   INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
217   for (i = 0; i < slaves_count; i++) 
218     MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
219                  slaves[i], PORT_22);
220   
221   INFO0("Goodbye now!");
222   free(slaves);
223   free(todo);
224   return 0;
225 } /* end_of_master */
226
227 /** Receiver function  */
228 int slave(int argc, char *argv[])
229 {
230   while(1) {
231     m_task_t task = NULL;
232     int a;
233     a = MSG_task_get(&(task), PORT_22);
234     if (a == MSG_OK) {
235       INFO1("Received \"%s\" ", MSG_task_get_name(task));
236       if(MSG_task_get_data(task)==FINALIZE) {
237         MSG_task_destroy(task);
238         break;
239       }
240       INFO1("Processing \"%s\" ", MSG_task_get_name(task));
241       MSG_task_execute(task);
242       INFO1("\"%s\" done ", MSG_task_get_name(task));
243       MSG_task_destroy(task);
244     } else {
245       INFO0("Hey ?! What's up ? ");
246       xbt_assert0(0,"Unexpected behavior");
247     }
248   }
249   INFO0("I'm done. See you!");
250   return 0;
251 } /* end_of_slave */
252
253 /** Test function */
254 MSG_error_t test_all(void)
255 {
256   MSG_error_t res = MSG_OK;
257
258   {                             /*  Simulation setting */
259     MSG_set_channel_number(MAX_CHANNEL);
260     MSG_paje_output("msg_test.trace");
261     surf_parse = surf_parse_bypass;
262     MSG_create_environment(NULL);
263   }
264   {                            /*   Application deployment */
265     MSG_function_register("master", master);
266     MSG_function_register("slave", slave);
267     MSG_launch_application(NULL);
268   }
269   res = MSG_main();
270   
271   INFO1("Simulation time %g",MSG_get_clock());
272   return res;
273 } /* end_of_test_all */
274
275 /** Main function */
276 int main(int argc, char *argv[])
277 {
278   MSG_error_t res = MSG_OK;
279
280   MSG_global_init(&argc,argv);
281   res = test_all();
282   MSG_clean();
283
284   if(res==MSG_OK) return 0; 
285   else return 1;
286 } /* end_of_main */
287