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)
28 static int surf_parse_bypass(void)
31 static int surfxml_bufferstack_size = 2048;
33 /* allocating memory to the buffer, I think 2MB should be enough */
34 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
36 /* <platform_description> */
37 SURFXML_BUFFER_RESET();
38 SURFXML_BUFFER_SET(platform_description_version,"1");
40 STag_surfxml_platform_description();
42 /* <cpu name="Cpu A" power="100000000.00"/> */
43 SURFXML_BUFFER_RESET();
44 SURFXML_BUFFER_SET(cpu_name,"Cpu A");
45 SURFXML_BUFFER_SET(cpu_power, "100000000.00");
46 SURFXML_BUFFER_SET(cpu_availability, "1.0");
47 SURFXML_BUFFER_SET(cpu_availability_file, "");
48 A_surfxml_cpu_state = A_surfxml_cpu_state_ON;
49 SURFXML_BUFFER_SET(cpu_state_file, "");
50 SURFXML_BUFFER_SET(cpu_interference_send, "1.0");
51 SURFXML_BUFFER_SET(cpu_interference_recv, "1.0");
52 SURFXML_BUFFER_SET(cpu_interference_send_recv, "1.0");
53 SURFXML_BUFFER_SET(cpu_max_outgoing_rate, "-1.0");
58 /* <cpu name="Cpu B" power="100000000.00"/> */
59 SURFXML_BUFFER_RESET();
60 SURFXML_BUFFER_SET(cpu_name, "Cpu B");
61 SURFXML_BUFFER_SET(cpu_power, "100000000.00");
62 SURFXML_BUFFER_SET(cpu_availability, "1.0");
63 SURFXML_BUFFER_SET(cpu_availability_file, "");
64 A_surfxml_cpu_state = A_surfxml_cpu_state_ON;
65 SURFXML_BUFFER_SET(cpu_state_file, "");
66 SURFXML_BUFFER_SET(cpu_interference_send, "1.0");
67 SURFXML_BUFFER_SET(cpu_interference_recv, "1.0");
68 SURFXML_BUFFER_SET(cpu_interference_send_recv, "1.0");
69 SURFXML_BUFFER_SET(cpu_max_outgoing_rate, "-1.0");
74 /* <network_link name="LinkA" bandwidth="10000000.0" latency="0.2"/> */
75 SURFXML_BUFFER_RESET();
76 SURFXML_BUFFER_SET(network_link_name, "LinkA");
77 SURFXML_BUFFER_SET(network_link_bandwidth, "10000000.0");
78 SURFXML_BUFFER_SET(network_link_bandwidth_file, "");
79 SURFXML_BUFFER_SET(network_link_latency, "0.2");
80 SURFXML_BUFFER_SET(network_link_latency_file, "");
81 A_surfxml_network_link_state = A_surfxml_network_link_state_ON;
82 SURFXML_BUFFER_SET(network_link_state_file, "");
83 A_surfxml_network_link_sharing_policy = A_surfxml_network_link_sharing_policy_SHARED;
84 STag_surfxml_network_link();
85 ETag_surfxml_network_link();
87 /* <route src="Cpu A" dst="Cpu B"><route_element name="LinkA"/></route> */
88 SURFXML_BUFFER_RESET();
89 SURFXML_BUFFER_SET(route_src, "Cpu A");
90 SURFXML_BUFFER_SET(route_dst, "Cpu B");
91 SURFXML_BUFFER_SET(route_impact_on_src, "0.0");
92 SURFXML_BUFFER_SET(route_impact_on_dst, "0.0");
93 SURFXML_BUFFER_SET(route_impact_on_src_with_other_recv, "0.0");
94 SURFXML_BUFFER_SET(route_impact_on_dst_with_other_send, "0.0");
98 SURFXML_BUFFER_SET(route_element_name, "LinkA");
99 STag_surfxml_route_element();
100 ETag_surfxml_route_element();
102 ETag_surfxml_route();
104 /* <route src="Cpu B" dst="Cpu A"><route_element name="LinkA"/></route> */
105 SURFXML_BUFFER_RESET();
106 SURFXML_BUFFER_SET(route_src, "Cpu B");
107 SURFXML_BUFFER_SET(route_dst, "Cpu A");
108 SURFXML_BUFFER_SET(route_impact_on_src, "0.0");
109 SURFXML_BUFFER_SET(route_impact_on_dst, "0.0");
110 SURFXML_BUFFER_SET(route_impact_on_src_with_other_recv, "0.0");
111 SURFXML_BUFFER_SET(route_impact_on_dst_with_other_send, "0.0");
113 STag_surfxml_route();
115 SURFXML_BUFFER_SET(route_element_name, "LinkA");
116 STag_surfxml_route_element();
117 ETag_surfxml_route_element();
119 ETag_surfxml_route();
121 /* <process host="Cpu A" function="master"> */
122 SURFXML_BUFFER_RESET();
123 SURFXML_BUFFER_SET(process_host, "Cpu A");
124 SURFXML_BUFFER_SET(process_function, "master");
125 SURFXML_BUFFER_SET(process_start_time, "-1.0");
126 SURFXML_BUFFER_SET(process_kill_time, "-1.0");
127 STag_surfxml_process();
129 /* <argument value="20"/> */
130 SURFXML_BUFFER_SET(argument_value, "20");
131 STag_surfxml_argument();
132 ETag_surfxml_argument();
134 /* <argument value="5000000"/> */
135 SURFXML_BUFFER_SET(argument_value, "5000000");
136 STag_surfxml_argument();
137 ETag_surfxml_argument();
139 /* <argument value="100000"/> */
140 SURFXML_BUFFER_SET(argument_value, "100000");
141 STag_surfxml_argument();
142 ETag_surfxml_argument();
144 /* <argument value="Cpu B"/> */
145 SURFXML_BUFFER_SET(argument_value, "Cpu B");
146 STag_surfxml_argument();
147 ETag_surfxml_argument();
150 ETag_surfxml_process();
152 /* <process host="Cpu B" function="slave"/> */
153 SURFXML_BUFFER_RESET();
154 SURFXML_BUFFER_SET(process_host, "Cpu 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 STag_surfxml_process();
159 ETag_surfxml_process();
161 /* </platform_description> */
162 ETag_surfxml_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[])
257 m_task_t task = NULL;
259 a = MSG_task_get(&(task), PORT_22);
261 INFO1("Received \"%s\" ", MSG_task_get_name(task));
262 if(MSG_task_get_data(task)==FINALIZE) {
263 MSG_task_destroy(task);
266 INFO1("Processing \"%s\" ", MSG_task_get_name(task));
267 MSG_task_execute(task);
268 INFO1("\"%s\" done ", MSG_task_get_name(task));
269 MSG_task_destroy(task);
271 INFO0("Hey ?! What's up ? ");
272 xbt_assert0(0,"Unexpected behavior");
275 INFO0("I'm done. See you!");
280 MSG_error_t test_all(void)
282 MSG_error_t res = MSG_OK;
284 { /* Simulation setting */
285 MSG_set_channel_number(MAX_CHANNEL);
286 MSG_paje_output("msg_test.trace");
287 surf_parse = surf_parse_bypass;
288 MSG_create_environment(NULL);
290 { /* Application deployment */
291 MSG_function_register("master", master);
292 MSG_function_register("slave", slave);
293 MSG_launch_application(NULL);
297 INFO1("Simulation time %g",MSG_get_clock());
299 } /* end_of_test_all */
302 int main(int argc, char *argv[])
304 MSG_error_t res = MSG_OK;
306 MSG_global_init(&argc,argv);
310 if(res==MSG_OK) return 0;