Logo AND Algorithmique Numérique Distribuée

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