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 */
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 #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)
23 #define SURFXML_BUFFER_RESET() do { \
25 memset(surfxml_bufferstack,0,surfxml_bufferstack_size); } while(0)
27 #define STAG(tag) STag_surfxml_##tag();
28 #define ETAG(tag) do { ETag_surfxml_##tag(); SURFXML_BUFFER_RESET(); } while(0)
30 static int surf_parse_bypass_platform(void)
33 static int surfxml_bufferstack_size = 2048;
35 /* allocating memory to the buffer, I think 2MB should be enough */
36 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
38 /* <platform_description> */
39 SURFXML_BUFFER_SET(platform_description_version,"2");
41 STAG(platform_description);
43 /* <host id="host A" power="100000000.00"/> */
44 SURFXML_BUFFER_SET(host_id,"host A");
45 SURFXML_BUFFER_SET(host_power, "100000000.00");
46 SURFXML_BUFFER_SET(host_availability, "1.0");
47 SURFXML_BUFFER_SET(host_availability_file, "");
48 A_surfxml_host_state = A_surfxml_host_state_ON;
49 SURFXML_BUFFER_SET(host_state_file, "");
50 SURFXML_BUFFER_SET(host_interference_send, "1.0");
51 SURFXML_BUFFER_SET(host_interference_recv, "1.0");
52 SURFXML_BUFFER_SET(host_interference_send_recv, "1.0");
53 SURFXML_BUFFER_SET(host_max_outgoing_rate, "-1.0");
58 /* <host id="host B" power="100000000.00"/> */
59 SURFXML_BUFFER_SET(host_id, "host B");
60 SURFXML_BUFFER_SET(host_power, "100000000.00");
61 SURFXML_BUFFER_SET(host_availability, "1.0");
62 SURFXML_BUFFER_SET(host_availability_file, "");
63 A_surfxml_host_state = A_surfxml_host_state_ON;
64 SURFXML_BUFFER_SET(host_state_file, "");
65 SURFXML_BUFFER_SET(host_interference_send, "1.0");
66 SURFXML_BUFFER_SET(host_interference_recv, "1.0");
67 SURFXML_BUFFER_SET(host_interference_send_recv, "1.0");
68 SURFXML_BUFFER_SET(host_max_outgoing_rate, "-1.0");
73 /* <link id="LinkA" bandwidth="10000000.0" latency="0.2"/> */
74 SURFXML_BUFFER_SET(link_id, "LinkA");
75 SURFXML_BUFFER_SET(link_bandwidth, "10000000.0");
76 SURFXML_BUFFER_SET(link_bandwidth_file, "");
77 SURFXML_BUFFER_SET(link_latency, "0.2");
78 SURFXML_BUFFER_SET(link_latency_file, "");
79 A_surfxml_link_state = A_surfxml_link_state_ON;
80 SURFXML_BUFFER_SET(link_state_file, "");
81 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
85 /* <route src="host A" dst="host B"><link:ctn id="LinkA"/></route> */
86 SURFXML_BUFFER_SET(route_src, "host A");
87 SURFXML_BUFFER_SET(route_dst, "host B");
88 SURFXML_BUFFER_SET(route_impact_on_src, "0.0");
89 SURFXML_BUFFER_SET(route_impact_on_dst, "0.0");
90 SURFXML_BUFFER_SET(route_impact_on_src_with_other_recv, "0.0");
91 SURFXML_BUFFER_SET(route_impact_on_dst_with_other_send, "0.0");
95 SURFXML_BUFFER_SET(link_c_ctn_id, "LinkA");
101 /* <route src="host B" dst="host A"><link:ctn id="LinkA"/></route> */
102 SURFXML_BUFFER_SET(route_src, "host B");
103 SURFXML_BUFFER_SET(route_dst, "host A");
104 SURFXML_BUFFER_SET(route_impact_on_src, "0.0");
105 SURFXML_BUFFER_SET(route_impact_on_dst, "0.0");
106 SURFXML_BUFFER_SET(route_impact_on_src_with_other_recv, "0.0");
107 SURFXML_BUFFER_SET(route_impact_on_dst_with_other_send, "0.0");
111 SURFXML_BUFFER_SET(link_c_ctn_id, "LinkA");
116 /* </platform_description> */
117 ETAG(platform_description);
119 free(surfxml_bufferstack);
123 static int surf_parse_bypass_application(void) {
125 static int surfxml_bufferstack_size = 2048;
127 /* allocating memory to the buffer, I think 2MB should be enough */
128 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
130 /* <platform_description> */
131 SURFXML_BUFFER_SET(platform_description_version,"2");
133 STAG(platform_description);
135 /* <process host="host A" function="master"> */
136 SURFXML_BUFFER_SET(process_host, "host A");
137 SURFXML_BUFFER_SET(process_function, "master");
138 SURFXML_BUFFER_SET(process_start_time, "-1.0");
139 SURFXML_BUFFER_SET(process_kill_time, "-1.0");
142 /* <argument value="20"/> */
143 SURFXML_BUFFER_SET(argument_value, "20");
147 /* <argument value="5000000"/> */
148 SURFXML_BUFFER_SET(argument_value, "5000000");
152 /* <argument value="100000"/> */
153 SURFXML_BUFFER_SET(argument_value, "100000");
157 /* <argument value="host B"/> */
158 SURFXML_BUFFER_SET(argument_value, "host B");
165 /* <process host="host B" function="slave"/> */
166 SURFXML_BUFFER_SET(process_host, "host B");
167 SURFXML_BUFFER_SET(process_function, "slave");
168 SURFXML_BUFFER_SET(process_start_time, "-1.0");
169 SURFXML_BUFFER_SET(process_kill_time, "-1.0");
173 /* </platform_description> */
174 ETAG(platform_description);
176 free(surfxml_bufferstack);
180 int master(int argc, char *argv[]);
181 int slave(int argc, char *argv[]);
182 MSG_error_t test_all(void);
189 /** Emitter function */
190 int master(int argc, char *argv[])
192 int slaves_count = 0;
193 m_host_t *slaves = NULL;
194 m_task_t *todo = NULL;
195 int number_of_tasks = 0;
196 double task_comp_size = 0;
197 double task_comm_size = 0;
202 xbt_assert1(sscanf(argv[1],"%d", &number_of_tasks),
203 "Invalid argument %s\n",argv[1]);
204 xbt_assert1(sscanf(argv[2],"%lg", &task_comp_size),
205 "Invalid argument %s\n",argv[2]);
206 xbt_assert1(sscanf(argv[3],"%lg", &task_comm_size),
207 "Invalid argument %s\n",argv[3]);
209 { /* Task creation */
210 char sprintf_buffer[64];
212 todo = calloc(number_of_tasks, sizeof(m_task_t));
214 for (i = 0; i < number_of_tasks; i++) {
215 sprintf(sprintf_buffer, "Task_%d", i);
216 todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
220 { /* Process organisation */
221 slaves_count = argc - 4;
222 slaves = calloc(slaves_count, sizeof(m_host_t));
224 for (i = 4; i < argc; i++) {
225 slaves[i-4] = MSG_get_host_by_name(argv[i]);
226 if(slaves[i-4]==NULL) {
227 INFO1("Unknown host %s. Stopping Now! ", argv[i]);
233 INFO1("Got %d slave(s) :", slaves_count);
234 for (i = 0; i < slaves_count; i++)
235 INFO1("\t %s", slaves[i]->name);
237 INFO1("Got %d task to process :", number_of_tasks);
239 for (i = 0; i < number_of_tasks; i++)
240 INFO1("\t\"%s\"", todo[i]->name);
242 for (i = 0; i < number_of_tasks; i++) {
243 INFO2("Sending \"%s\" to \"%s\"",
245 slaves[i % slaves_count]->name);
246 if(MSG_host_self()==slaves[i % slaves_count]) {
247 INFO0("Hey ! It's me ! :)");
249 MSG_task_put(todo[i], slaves[i % slaves_count],
251 INFO0("Send completed");
254 INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
255 for (i = 0; i < slaves_count; i++)
256 MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
259 INFO0("Goodbye now!");
263 } /* end_of_master */
265 /** Receiver function */
266 int slave(int argc, char *argv[])
268 INFO0("I'm a slave");
270 m_task_t task = NULL;
272 a = MSG_task_get(&(task), PORT_22);
274 INFO1("Received \"%s\" ", MSG_task_get_name(task));
275 if(MSG_task_get_data(task)==FINALIZE) {
276 MSG_task_destroy(task);
279 INFO1("Processing \"%s\" ", MSG_task_get_name(task));
280 MSG_task_execute(task);
281 INFO1("\"%s\" done ", MSG_task_get_name(task));
282 MSG_task_destroy(task);
284 INFO0("Hey ?! What's up ? ");
285 xbt_assert0(0,"Unexpected behavior");
288 INFO0("I'm done. See you!");
293 MSG_error_t test_all(void)
295 MSG_error_t res = MSG_OK;
297 /* Simulation setting */
298 MSG_set_channel_number(MAX_CHANNEL);
299 MSG_paje_output("msg_test.trace");
300 surf_parse = surf_parse_bypass_platform;
301 MSG_create_environment(NULL);
303 /* Application deployment */
304 MSG_function_register("master", master);
305 MSG_function_register("slave", slave);
306 surf_parse = surf_parse_bypass_application;
307 MSG_launch_application(NULL);
311 INFO1("Simulation time %g",MSG_get_clock());
313 } /* end_of_test_all */
316 int main(int argc, char *argv[])
318 MSG_error_t res = MSG_OK;
320 MSG_global_init(&argc,argv);
324 if(res==MSG_OK) return 0;