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(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,"1");
41 STAG(platform_description);
43 /* <cpu name="Cpu A" power="100000000.00"/> */
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_SET(cpu_name, "Cpu B");
60 SURFXML_BUFFER_SET(cpu_power, "100000000.00");
61 SURFXML_BUFFER_SET(cpu_availability, "1.0");
62 SURFXML_BUFFER_SET(cpu_availability_file, "");
63 A_surfxml_cpu_state = A_surfxml_cpu_state_ON;
64 SURFXML_BUFFER_SET(cpu_state_file, "");
65 SURFXML_BUFFER_SET(cpu_interference_send, "1.0");
66 SURFXML_BUFFER_SET(cpu_interference_recv, "1.0");
67 SURFXML_BUFFER_SET(cpu_interference_send_recv, "1.0");
68 SURFXML_BUFFER_SET(cpu_max_outgoing_rate, "-1.0");
73 /* <network_link name="LinkA" bandwidth="10000000.0" latency="0.2"/> */
74 SURFXML_BUFFER_SET(network_link_name, "LinkA");
75 SURFXML_BUFFER_SET(network_link_bandwidth, "10000000.0");
76 SURFXML_BUFFER_SET(network_link_bandwidth_file, "");
77 SURFXML_BUFFER_SET(network_link_latency, "0.2");
78 SURFXML_BUFFER_SET(network_link_latency_file, "");
79 A_surfxml_network_link_state = A_surfxml_network_link_state_ON;
80 SURFXML_BUFFER_SET(network_link_state_file, "");
81 A_surfxml_network_link_sharing_policy = A_surfxml_network_link_sharing_policy_SHARED;
85 /* <route src="Cpu A" dst="Cpu B"><route_element name="LinkA"/></route> */
86 SURFXML_BUFFER_SET(route_src, "Cpu A");
87 SURFXML_BUFFER_SET(route_dst, "Cpu 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(route_element_name, "LinkA");
101 /* <route src="Cpu B" dst="Cpu A"><route_element name="LinkA"/></route> */
102 SURFXML_BUFFER_SET(route_src, "Cpu B");
103 SURFXML_BUFFER_SET(route_dst, "Cpu 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(route_element_name, "LinkA");
117 /* <process host="Cpu A" function="master"> */
118 SURFXML_BUFFER_SET(process_host, "Cpu A");
119 SURFXML_BUFFER_SET(process_function, "master");
120 SURFXML_BUFFER_SET(process_start_time, "-1.0");
121 SURFXML_BUFFER_SET(process_kill_time, "-1.0");
124 /* <argument value="20"/> */
125 SURFXML_BUFFER_SET(argument_value, "20");
129 /* <argument value="5000000"/> */
130 SURFXML_BUFFER_SET(argument_value, "5000000");
134 /* <argument value="100000"/> */
135 SURFXML_BUFFER_SET(argument_value, "100000");
139 /* <argument value="Cpu B"/> */
140 SURFXML_BUFFER_SET(argument_value, "Cpu B");
147 /* <process host="Cpu B" function="slave"/> */
148 SURFXML_BUFFER_SET(process_host, "Cpu B");
149 SURFXML_BUFFER_SET(process_function, "slave");
150 SURFXML_BUFFER_SET(process_start_time, "-1.0");
151 SURFXML_BUFFER_SET(process_kill_time, "-1.0");
155 /* </platform_description> */
156 ETAG(platform_description);
158 free(surfxml_bufferstack);
162 int master(int argc, char *argv[]);
163 int slave(int argc, char *argv[]);
164 MSG_error_t test_all(void);
171 /** Emitter function */
172 int master(int argc, char *argv[])
174 int slaves_count = 0;
175 m_host_t *slaves = NULL;
176 m_task_t *todo = NULL;
177 int number_of_tasks = 0;
178 double task_comp_size = 0;
179 double task_comm_size = 0;
184 xbt_assert1(sscanf(argv[1],"%d", &number_of_tasks),
185 "Invalid argument %s\n",argv[1]);
186 xbt_assert1(sscanf(argv[2],"%lg", &task_comp_size),
187 "Invalid argument %s\n",argv[2]);
188 xbt_assert1(sscanf(argv[3],"%lg", &task_comm_size),
189 "Invalid argument %s\n",argv[3]);
191 { /* Task creation */
192 char sprintf_buffer[64];
194 todo = calloc(number_of_tasks, sizeof(m_task_t));
196 for (i = 0; i < number_of_tasks; i++) {
197 sprintf(sprintf_buffer, "Task_%d", i);
198 todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
202 { /* Process organisation */
203 slaves_count = argc - 4;
204 slaves = calloc(slaves_count, sizeof(m_host_t));
206 for (i = 4; i < argc; i++) {
207 slaves[i-4] = MSG_get_host_by_name(argv[i]);
208 if(slaves[i-4]==NULL) {
209 INFO1("Unknown host %s. Stopping Now! ", argv[i]);
215 INFO1("Got %d slave(s) :", slaves_count);
216 for (i = 0; i < slaves_count; i++)
217 INFO1("\t %s", slaves[i]->name);
219 INFO1("Got %d task to process :", number_of_tasks);
221 for (i = 0; i < number_of_tasks; i++)
222 INFO1("\t\"%s\"", todo[i]->name);
224 for (i = 0; i < number_of_tasks; i++) {
225 INFO2("Sending \"%s\" to \"%s\"",
227 slaves[i % slaves_count]->name);
228 if(MSG_host_self()==slaves[i % slaves_count]) {
229 INFO0("Hey ! It's me ! :)");
231 MSG_task_put(todo[i], slaves[i % slaves_count],
233 INFO0("Send completed");
236 INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
237 for (i = 0; i < slaves_count; i++)
238 MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
241 INFO0("Goodbye now!");
245 } /* end_of_master */
247 /** Receiver function */
248 int slave(int argc, char *argv[])
251 m_task_t task = NULL;
253 a = MSG_task_get(&(task), PORT_22);
255 INFO1("Received \"%s\" ", MSG_task_get_name(task));
256 if(MSG_task_get_data(task)==FINALIZE) {
257 MSG_task_destroy(task);
260 INFO1("Processing \"%s\" ", MSG_task_get_name(task));
261 MSG_task_execute(task);
262 INFO1("\"%s\" done ", MSG_task_get_name(task));
263 MSG_task_destroy(task);
265 INFO0("Hey ?! What's up ? ");
266 xbt_assert0(0,"Unexpected behavior");
269 INFO0("I'm done. See you!");
274 MSG_error_t test_all(void)
276 MSG_error_t res = MSG_OK;
278 { /* Simulation setting */
279 MSG_set_channel_number(MAX_CHANNEL);
280 MSG_paje_output("msg_test.trace");
281 surf_parse = surf_parse_bypass;
282 MSG_create_environment(NULL);
284 { /* Application deployment */
285 MSG_function_register("master", master);
286 MSG_function_register("slave", slave);
287 MSG_launch_application(NULL);
291 INFO1("Simulation time %g",MSG_get_clock());
293 } /* end_of_test_all */
296 int main(int argc, char *argv[])
298 MSG_error_t res = MSG_OK;
300 MSG_global_init(&argc,argv);
304 if(res==MSG_OK) return 0;