3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved. */
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. */
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 #include "surf/surfxml.h" /* to hijack surf_parse_lex */
13 /* Create a log channel to have nice outputs. */
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 */
18 static int surf_parse_bypass_platform(void)
20 static int AX_ptr = 0;
21 static int surfxml_bufferstack_size = 2048;
23 /* allocating memory for the buffer, I think 2kB should be enough */
24 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
26 /* <platform_description> */
27 SURFXML_BUFFER_SET(platform_description_version,"2");
29 SURFXML_START_TAG(platform_description);
31 /* <host id="host A" power="100000000.00"/> */
32 SURFXML_BUFFER_SET(host_id,"host A");
33 SURFXML_BUFFER_SET(host_power, "100000000.00");
34 SURFXML_BUFFER_SET(host_availability, "1.0");
35 SURFXML_BUFFER_SET(host_availability_file, "");
36 A_surfxml_host_state = A_surfxml_host_state_ON;
37 SURFXML_BUFFER_SET(host_state_file, "");
38 SURFXML_BUFFER_SET(host_interference_send, "1.0");
39 SURFXML_BUFFER_SET(host_interference_recv, "1.0");
40 SURFXML_BUFFER_SET(host_interference_send_recv, "1.0");
41 SURFXML_BUFFER_SET(host_max_outgoing_rate, "-1.0");
43 SURFXML_START_TAG(host);
44 SURFXML_END_TAG(host);
46 /* <host id="host B" power="100000000.00"/> */
47 SURFXML_BUFFER_SET(host_id, "host B");
48 SURFXML_BUFFER_SET(host_power, "100000000.00");
49 SURFXML_BUFFER_SET(host_availability, "1.0");
50 SURFXML_BUFFER_SET(host_availability_file, "");
51 A_surfxml_host_state = A_surfxml_host_state_ON;
52 SURFXML_BUFFER_SET(host_state_file, "");
53 SURFXML_BUFFER_SET(host_interference_send, "1.0");
54 SURFXML_BUFFER_SET(host_interference_recv, "1.0");
55 SURFXML_BUFFER_SET(host_interference_send_recv, "1.0");
56 SURFXML_BUFFER_SET(host_max_outgoing_rate, "-1.0");
58 SURFXML_START_TAG(host);
59 SURFXML_END_TAG(host);
61 /* <link id="LinkA" bandwidth="10000000.0" latency="0.2"/> */
62 SURFXML_BUFFER_SET(link_id, "LinkA");
63 SURFXML_BUFFER_SET(link_bandwidth, "10000000.0");
64 SURFXML_BUFFER_SET(link_bandwidth_file, "");
65 SURFXML_BUFFER_SET(link_latency, "0.2");
66 SURFXML_BUFFER_SET(link_latency_file, "");
67 A_surfxml_link_state = A_surfxml_link_state_ON;
68 SURFXML_BUFFER_SET(link_state_file, "");
69 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
70 SURFXML_START_TAG(link);
71 SURFXML_END_TAG(link);
73 /* <route src="host A" dst="host B"><link:ctn id="LinkA"/></route> */
74 SURFXML_BUFFER_SET(route_src, "host A");
75 SURFXML_BUFFER_SET(route_dst, "host B");
76 SURFXML_BUFFER_SET(route_impact_on_src, "0.0");
77 SURFXML_BUFFER_SET(route_impact_on_dst, "0.0");
78 SURFXML_BUFFER_SET(route_impact_on_src_with_other_recv, "0.0");
79 SURFXML_BUFFER_SET(route_impact_on_dst_with_other_send, "0.0");
81 SURFXML_START_TAG(route);
83 SURFXML_BUFFER_SET(link_c_ctn_id, "LinkA");
84 SURFXML_START_TAG(link_c_ctn);
85 SURFXML_END_TAG(link_c_ctn);
87 SURFXML_END_TAG(route);
89 /* <route src="host B" dst="host A"><link:ctn id="LinkA"/></route> */
90 SURFXML_BUFFER_SET(route_src, "host B");
91 SURFXML_BUFFER_SET(route_dst, "host A");
92 SURFXML_BUFFER_SET(route_impact_on_src, "0.0");
93 SURFXML_BUFFER_SET(route_impact_on_dst, "0.0");
94 SURFXML_BUFFER_SET(route_impact_on_src_with_other_recv, "0.0");
95 SURFXML_BUFFER_SET(route_impact_on_dst_with_other_send, "0.0");
97 SURFXML_START_TAG(route);
99 SURFXML_BUFFER_SET(link_c_ctn_id, "LinkA");
100 SURFXML_START_TAG(link_c_ctn);
101 SURFXML_END_TAG(link_c_ctn);
103 SURFXML_END_TAG(route);
104 /* </platform_description> */
105 SURFXML_END_TAG(platform_description);
107 free(surfxml_bufferstack);
111 static int surf_parse_bypass_application(void) {
113 static int surfxml_bufferstack_size = 2048;
115 /* allocating memory to the buffer, I think 2MB should be enough */
116 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
118 /* <platform_description> */
119 SURFXML_BUFFER_SET(platform_description_version,"2");
121 SURFXML_START_TAG(platform_description);
123 /* <process host="host A" function="master"> */
124 SURFXML_BUFFER_SET(process_host, "host A");
125 SURFXML_BUFFER_SET(process_function, "master");
126 SURFXML_BUFFER_SET(process_start_time, "-1.0");
127 SURFXML_BUFFER_SET(process_kill_time, "-1.0");
128 SURFXML_START_TAG(process);
130 /* <argument value="20"/> */
131 SURFXML_BUFFER_SET(argument_value, "20");
132 SURFXML_START_TAG(argument);
133 SURFXML_END_TAG(argument);
135 /* <argument value="5000000"/> */
136 SURFXML_BUFFER_SET(argument_value, "5000000");
137 SURFXML_START_TAG(argument);
138 SURFXML_END_TAG(argument);
140 /* <argument value="100000"/> */
141 SURFXML_BUFFER_SET(argument_value, "100000");
142 SURFXML_START_TAG(argument);
143 SURFXML_END_TAG(argument);
145 /* <argument value="host B"/> */
146 SURFXML_BUFFER_SET(argument_value, "host B");
147 SURFXML_START_TAG(argument);
148 SURFXML_END_TAG(argument);
151 SURFXML_END_TAG(process);
153 /* <process host="host B" function="slave"/> */
154 SURFXML_BUFFER_SET(process_host, "host 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 SURFXML_START_TAG(process);
159 SURFXML_END_TAG(process);
161 /* </platform_description> */
162 SURFXML_END_TAG(platform_description);
164 free(surfxml_bufferstack);
168 int master(int argc, char *argv[]);
169 int slave(int argc, char *argv[]);
170 MSG_error_t test_all(void);
177 /** Emitter function */
178 int master(int argc, char *argv[])
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;
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]);
197 { /* Task creation */
198 char sprintf_buffer[64];
200 todo = calloc(number_of_tasks, sizeof(m_task_t));
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);
208 { /* Process organisation */
209 slaves_count = argc - 4;
210 slaves = calloc(slaves_count, sizeof(m_host_t));
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]);
221 INFO1("Got %d slave(s) :", slaves_count);
222 for (i = 0; i < slaves_count; i++)
223 INFO1("\t %s", slaves[i]->name);
225 INFO1("Got %d task to process :", number_of_tasks);
227 for (i = 0; i < number_of_tasks; i++)
228 INFO1("\t\"%s\"", todo[i]->name);
230 for (i = 0; i < number_of_tasks; i++) {
231 INFO2("Sending \"%s\" to \"%s\"",
233 slaves[i % slaves_count]->name);
234 if(MSG_host_self()==slaves[i % slaves_count]) {
235 INFO0("Hey ! It's me ! :)");
237 MSG_task_put(todo[i], slaves[i % slaves_count],
239 INFO0("Send completed");
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),
247 INFO0("Goodbye now!");
251 } /* end_of_master */
253 /** Receiver function */
254 int slave(int argc, char *argv[])
256 INFO0("I'm a slave");
258 m_task_t task = NULL;
260 a = MSG_task_get(&(task), PORT_22);
262 INFO1("Received \"%s\" ", MSG_task_get_name(task));
263 if(MSG_task_get_data(task)==FINALIZE) {
264 MSG_task_destroy(task);
267 INFO1("Processing \"%s\" ", MSG_task_get_name(task));
268 MSG_task_execute(task);
269 INFO1("\"%s\" done ", MSG_task_get_name(task));
270 MSG_task_destroy(task);
272 INFO0("Hey ?! What's up ? ");
273 xbt_assert0(0,"Unexpected behavior");
276 INFO0("I'm done. See you!");
281 MSG_error_t test_all(void)
283 MSG_error_t res = MSG_OK;
285 /* Simulation setting */
286 MSG_set_channel_number(MAX_CHANNEL);
287 MSG_paje_output("msg_test.trace");
288 surf_parse = surf_parse_bypass_platform;
289 MSG_create_environment(NULL);
291 /* Application deployment */
292 MSG_function_register("master", master);
293 MSG_function_register("slave", slave);
294 surf_parse = surf_parse_bypass_application;
295 MSG_launch_application(NULL);
299 INFO1("Simulation time %g",MSG_get_clock());
301 } /* end_of_test_all */
304 int main(int argc, char *argv[])
306 MSG_error_t res = MSG_OK;
308 MSG_global_init(&argc,argv);
312 if(res==MSG_OK) return 0;