Logo AND Algorithmique Numérique Distribuée

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