1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
9 #include "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
11 /* Create a log channel to have nice outputs. */
13 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
14 "Messages specific for this msg example");
15 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
17 static int surf_parse_bypass_platform(void)
19 static int AX_ptr = 0;
20 static int surfxml_bufferstack_size = 2048;
22 /* allocating memory for the buffer, I think 2kB should be enough */
23 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
25 XBT_DEBUG("<platform>");
26 SURFXML_BUFFER_SET(platform_version, "3");
27 SURFXML_START_TAG(platform);
30 SURFXML_BUFFER_SET(AS_id, "AS0");
31 SURFXML_BUFFER_SET(AS_routing, "Full");
32 SURFXML_START_TAG(AS);
34 XBT_DEBUG("<host id=\"host A\" power=\"100000000.00\"/>");
35 SURFXML_BUFFER_SET(host_id, "host A");
36 SURFXML_BUFFER_SET(host_power, "100000000.00");
37 SURFXML_BUFFER_SET(host_availability, "1.0");
38 SURFXML_BUFFER_SET(host_availability_file, "");
39 SURFXML_BUFFER_SET(host_core, "1");
40 A_surfxml_host_state = A_surfxml_host_state_ON;
41 SURFXML_BUFFER_SET(host_state_file, "");
42 SURFXML_START_TAG(host);
43 SURFXML_END_TAG(host);
45 XBT_DEBUG("<host id=\"host B\" power=\"100000000.00\"/>");
46 SURFXML_BUFFER_SET(host_id, "host B");
47 SURFXML_BUFFER_SET(host_power, "100000000.00");
48 SURFXML_BUFFER_SET(host_availability, "1.0");
49 SURFXML_BUFFER_SET(host_availability_file, "");
50 SURFXML_BUFFER_SET(host_core, "1");
51 A_surfxml_host_state = A_surfxml_host_state_ON;
52 SURFXML_BUFFER_SET(host_state_file, "");
53 SURFXML_START_TAG(host);
54 SURFXML_END_TAG(host);
56 XBT_DEBUG("<link id=\"LinkA\" bandwidth=\"10000000.0\" latency=\"0.2\"/>");
57 SURFXML_BUFFER_SET(link_id, "LinkA");
58 SURFXML_BUFFER_SET(link_bandwidth, "10000000.0");
59 SURFXML_BUFFER_SET(link_bandwidth_file, "");
60 SURFXML_BUFFER_SET(link_latency, "0.2");
61 SURFXML_BUFFER_SET(link_latency_file, "");
62 A_surfxml_link_state = A_surfxml_link_state_ON;
63 SURFXML_BUFFER_SET(link_state_file, "");
64 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
65 SURFXML_START_TAG(link);
66 SURFXML_END_TAG(link);
68 XBT_DEBUG("<route src=\"host A\" dst=\"host B\">");
69 SURFXML_BUFFER_SET(route_src, "host A");
70 SURFXML_BUFFER_SET(route_dst, "host B");
71 A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_YES;
72 SURFXML_START_TAG(route);
73 XBT_DEBUG(" <link:ctn id=\"LinkA\"/>");
74 SURFXML_BUFFER_SET(link_ctn_id, "LinkA");
75 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
76 SURFXML_START_TAG(link_ctn);
77 SURFXML_END_TAG(link_ctn);
78 XBT_DEBUG("</route>");
79 SURFXML_END_TAG(route);
83 XBT_DEBUG("</platfrom>");
84 SURFXML_END_TAG(platform);
86 free(surfxml_bufferstack);
90 static int surf_parse_bypass_application(void)
93 static int surfxml_bufferstack_size = 2048;
94 static int surfxml_buffer_stack_stack_ptr = 0;
95 static int surfxml_buffer_stack_stack[1024];
96 /* allocating memory to the buffer, I think 2MB should be enough */
97 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
100 SURFXML_BUFFER_SET(platform_version, "3");
102 SURFXML_START_TAG(platform);
104 /* <process host="host A" function="master"> */
105 SURFXML_BUFFER_SET(process_host, "host A");
106 SURFXML_BUFFER_SET(process_function, "master");
107 SURFXML_BUFFER_SET(process_start_time, "-1.0");
108 SURFXML_BUFFER_SET(process_kill_time, "-1.0");
109 SURFXML_START_TAG(process);
111 /* <argument value="20"/> */
112 SURFXML_BUFFER_SET(argument_value, "20");
113 SURFXML_START_TAG(argument);
114 SURFXML_END_TAG(argument);
116 /* <argument value="5000000"/> */
117 SURFXML_BUFFER_SET(argument_value, "5000000");
118 SURFXML_START_TAG(argument);
119 SURFXML_END_TAG(argument);
121 /* <argument value="100000"/> */
122 SURFXML_BUFFER_SET(argument_value, "100000");
123 SURFXML_START_TAG(argument);
124 SURFXML_END_TAG(argument);
126 /* <argument value="host B"/> */
127 SURFXML_BUFFER_SET(argument_value, "host B");
128 SURFXML_START_TAG(argument);
129 SURFXML_END_TAG(argument);
132 SURFXML_END_TAG(process);
134 /* <process host="host B" function="slave"/> */
135 SURFXML_BUFFER_SET(process_host, "host B");
136 SURFXML_BUFFER_SET(process_function, "slave");
137 SURFXML_BUFFER_SET(process_start_time, "-1.0");
138 SURFXML_BUFFER_SET(process_kill_time, "-1.0");
139 SURFXML_START_TAG(process);
140 SURFXML_END_TAG(process);
143 SURFXML_END_TAG(platform);
145 free(surfxml_bufferstack);
149 int master(int argc, char *argv[]);
150 int slave(int argc, char *argv[]);
151 MSG_error_t test_all(void);
158 /** Emitter function */
159 int master(int argc, char *argv[])
161 int slaves_count = 0;
162 m_host_t *slaves = NULL;
163 m_task_t *todo = NULL;
164 int number_of_tasks = 0;
165 double task_comp_size = 0;
166 double task_comm_size = 0;
171 xbt_assert1(sscanf(argv[1], "%d", &number_of_tasks),
172 "Invalid argument %s\n", argv[1]);
173 xbt_assert1(sscanf(argv[2], "%lg", &task_comp_size),
174 "Invalid argument %s\n", argv[2]);
175 xbt_assert1(sscanf(argv[3], "%lg", &task_comm_size),
176 "Invalid argument %s\n", argv[3]);
178 { /* Task creation */
179 char sprintf_buffer[64];
181 todo = xbt_new0(m_task_t, number_of_tasks);
183 for (i = 0; i < number_of_tasks; i++) {
184 sprintf(sprintf_buffer, "Task_%d", i);
186 MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
191 { /* Process organisation */
192 slaves_count = argc - 4;
193 slaves = xbt_new0(m_host_t, slaves_count);
195 for (i = 4; i < argc; i++) {
196 slaves[i - 4] = MSG_get_host_by_name(argv[i]);
197 if (slaves[i - 4] == NULL) {
198 XBT_INFO("Unknown host %s. Stopping Now! ", argv[i]);
204 XBT_INFO("Got %d slave(s) :", slaves_count);
205 for (i = 0; i < slaves_count; i++)
206 XBT_INFO("\t %s", slaves[i]->name);
208 XBT_INFO("Got %d task to process :", number_of_tasks);
210 for (i = 0; i < number_of_tasks; i++)
211 XBT_INFO("\t\"%s\"", todo[i]->name);
213 for (i = 0; i < number_of_tasks; i++) {
214 XBT_INFO("Sending \"%s\" to \"%s\"",
215 todo[i]->name, slaves[i % slaves_count]->name);
216 if (MSG_host_self() == slaves[i % slaves_count]) {
217 XBT_INFO("Hey ! It's me ! :)");
219 MSG_task_put(todo[i], slaves[i % slaves_count], PORT_22);
220 XBT_INFO("Send completed");
224 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
225 for (i = 0; i < slaves_count; i++)
226 MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
229 XBT_INFO("Goodbye now!");
233 } /* end_of_master */
235 /** Receiver function */
236 int slave(int argc, char *argv[])
238 XBT_INFO("I'm a slave");
240 m_task_t task = NULL;
242 a = MSG_task_get(&(task), PORT_22);
244 XBT_INFO("Received \"%s\" ", MSG_task_get_name(task));
245 if (MSG_task_get_data(task) == FINALIZE) {
246 MSG_task_destroy(task);
249 XBT_INFO("Processing \"%s\" ", MSG_task_get_name(task));
250 MSG_task_execute(task);
251 XBT_INFO("\"%s\" done ", MSG_task_get_name(task));
252 MSG_task_destroy(task);
254 XBT_INFO("Hey ?! What's up ? ");
255 xbt_assert0(0, "Unexpected behavior");
258 XBT_INFO("I'm done. See you!");
263 MSG_error_t test_all(void)
265 MSG_error_t res = MSG_OK;
267 /* Simulation setting */
268 MSG_set_channel_number(MAX_CHANNEL);
269 surf_parse = surf_parse_bypass_platform;
270 MSG_create_environment(NULL);
272 /* Application deployment */
273 MSG_function_register("master", master);
274 MSG_function_register("slave", slave);
275 surf_parse = surf_parse_bypass_application;
276 MSG_launch_application(NULL);
280 XBT_INFO("Simulation time %g", MSG_get_clock());
282 } /* end_of_test_all */
285 int main(int argc, char *argv[])
287 MSG_error_t res = MSG_OK;
289 MSG_global_init(&argc, argv);