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;
21 static int surfxml_buffer_stack_stack_ptr = 0;
22 static int surfxml_buffer_stack_stack[1024];
23 /* allocating memory for the buffer, I think 2kB should be enough */
24 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
26 XBT_DEBUG("<platform>");
27 SURFXML_BUFFER_SET(platform_version, "3");
28 SURFXML_START_TAG(platform);
31 SURFXML_BUFFER_SET(AS_id, "AS0");
32 A_surfxml_AS_routing = A_surfxml_AS_routing_Full;
33 SURFXML_START_TAG(AS);
35 XBT_DEBUG("<host id=\"host A\" power=\"100000000.00\"/>");
36 SURFXML_BUFFER_SET(host_id, "host A");
37 SURFXML_BUFFER_SET(host_power, "100000000.00");
38 SURFXML_BUFFER_SET(host_availability, "1.0");
39 SURFXML_BUFFER_SET(host_availability___file, "");
40 SURFXML_BUFFER_SET(host_core, "1");
41 A_surfxml_host_state = A_surfxml_host_state_ON;
42 SURFXML_BUFFER_SET(host_state___file, "");
43 SURFXML_BUFFER_SET(host_coordinates, "");
44 SURFXML_START_TAG(host);
45 SURFXML_END_TAG(host);
47 XBT_DEBUG("<host id=\"host B\" power=\"100000000.00\"/>");
48 SURFXML_BUFFER_SET(host_id, "host B");
49 SURFXML_BUFFER_SET(host_power, "100000000.00");
50 SURFXML_BUFFER_SET(host_availability, "1.0");
51 SURFXML_BUFFER_SET(host_availability___file, "");
52 SURFXML_BUFFER_SET(host_core, "1");
53 A_surfxml_host_state = A_surfxml_host_state_ON;
54 SURFXML_BUFFER_SET(host_state___file, "");
55 SURFXML_BUFFER_SET(host_coordinates, "");
56 SURFXML_START_TAG(host);
57 SURFXML_END_TAG(host);
59 XBT_DEBUG("<link id=\"LinkA\" bandwidth=\"10000000.0\" latency=\"0.2\"/>");
60 SURFXML_BUFFER_SET(link_id, "LinkA");
61 SURFXML_BUFFER_SET(link_bandwidth, "10000000.0");
62 SURFXML_BUFFER_SET(link_bandwidth___file, "");
63 SURFXML_BUFFER_SET(link_latency, "0.2");
64 SURFXML_BUFFER_SET(link_latency___file, "");
65 A_surfxml_link_state = A_surfxml_link_state_ON;
66 SURFXML_BUFFER_SET(link_state___file, "");
67 A_surfxml_link_sharing___policy = A_surfxml_link_sharing___policy_SHARED;
68 SURFXML_START_TAG(link);
69 SURFXML_END_TAG(link);
71 XBT_DEBUG("<route src=\"host A\" dst=\"host B\">");
72 SURFXML_BUFFER_SET(route_src, "host A");
73 SURFXML_BUFFER_SET(route_dst, "host B");
74 A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_YES;
75 SURFXML_START_TAG(route);
76 XBT_DEBUG(" <link:ctn id=\"LinkA\"/>");
77 SURFXML_BUFFER_SET(link___ctn_id, "LinkA");
78 A_surfxml_link___ctn_direction = A_surfxml_link___ctn_direction_NONE;
79 SURFXML_START_TAG(link___ctn);
80 SURFXML_END_TAG(link___ctn);
81 XBT_DEBUG("</route>");
82 SURFXML_END_TAG(route);
86 XBT_DEBUG("</platfrom>");
87 SURFXML_END_TAG(platform);
89 free(surfxml_bufferstack);
93 static int surf_parse_bypass_application(void)
96 static int surfxml_bufferstack_size = 2048;
97 static int surfxml_buffer_stack_stack_ptr = 0;
98 static int surfxml_buffer_stack_stack[1024];
99 /* allocating memory to the buffer, I think 2MB should be enough */
100 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
103 SURFXML_BUFFER_SET(platform_version, "3");
105 SURFXML_START_TAG(platform);
107 /* <process host="host A" function="master"> */
108 SURFXML_BUFFER_SET(process_host, "host A");
109 SURFXML_BUFFER_SET(process_function, "master");
110 SURFXML_BUFFER_SET(process_start___time, "-1.0");
111 SURFXML_BUFFER_SET(process_kill___time, "-1.0");
112 SURFXML_START_TAG(process);
114 /* <argument value="20"/> */
115 SURFXML_BUFFER_SET(argument_value, "20");
116 SURFXML_START_TAG(argument);
117 SURFXML_END_TAG(argument);
119 /* <argument value="5000000"/> */
120 SURFXML_BUFFER_SET(argument_value, "5000000");
121 SURFXML_START_TAG(argument);
122 SURFXML_END_TAG(argument);
124 /* <argument value="100000"/> */
125 SURFXML_BUFFER_SET(argument_value, "100000");
126 SURFXML_START_TAG(argument);
127 SURFXML_END_TAG(argument);
129 /* <argument value="host B"/> */
130 SURFXML_BUFFER_SET(argument_value, "host B");
131 SURFXML_START_TAG(argument);
132 SURFXML_END_TAG(argument);
135 SURFXML_END_TAG(process);
137 /* <process host="host B" function="slave"/> */
138 SURFXML_BUFFER_SET(process_host, "host B");
139 SURFXML_BUFFER_SET(process_function, "slave");
140 SURFXML_BUFFER_SET(process_start___time, "-1.0");
141 SURFXML_BUFFER_SET(process_kill___time, "-1.0");
142 SURFXML_START_TAG(process);
143 SURFXML_END_TAG(process);
146 SURFXML_END_TAG(platform);
148 free(surfxml_bufferstack);
152 int master(int argc, char *argv[]);
153 int slave(int argc, char *argv[]);
154 msg_error_t test_all(void);
156 /** Emitter function */
157 int master(int argc, char *argv[])
159 int slaves_count = 0;
160 msg_host_t *slaves = NULL;
161 msg_task_t *todo = NULL;
162 int number_of_tasks = 0;
163 double task_comp_size = 0;
164 double task_comm_size = 0;
166 _XBT_GNUC_UNUSED int read;
168 read = sscanf(argv[1], "%d", &number_of_tasks);
169 xbt_assert(read, "Invalid argument %s\n", argv[1]);
170 read = sscanf(argv[2], "%lg", &task_comp_size);
171 xbt_assert(read, "Invalid argument %s\n", argv[2]);
172 read = sscanf(argv[3], "%lg", &task_comm_size);
173 xbt_assert(read, "Invalid argument %s\n", argv[3]);
175 { /* Task creation */
176 char sprintf_buffer[64];
178 todo = xbt_new0(msg_task_t, number_of_tasks);
180 for (i = 0; i < number_of_tasks; i++) {
181 sprintf(sprintf_buffer, "Task_%d", i);
183 MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
188 { /* Process organisation */
189 slaves_count = argc - 4;
190 slaves = xbt_new0(msg_host_t, slaves_count);
192 for (i = 4; i < argc; i++) {
193 slaves[i - 4] = MSG_get_host_by_name(argv[i]);
194 if (slaves[i - 4] == NULL) {
195 XBT_INFO("Unknown host %s. Stopping Now! ", argv[i]);
201 XBT_INFO("Got %d slave(s) :", slaves_count);
202 for (i = 0; i < slaves_count; i++)
203 XBT_INFO("\t %s", MSG_host_get_name(slaves[i]));
205 XBT_INFO("Got %d task to process :", number_of_tasks);
207 for (i = 0; i < number_of_tasks; i++)
208 XBT_INFO("\t\"%s\"", todo[i]->name);
210 for (i = 0; i < number_of_tasks; i++) {
211 XBT_INFO("Sending \"%s\" to \"%s\"",
212 todo[i]->name, MSG_host_get_name(slaves[i % slaves_count]));
213 if (MSG_host_self() == slaves[i % slaves_count]) {
214 XBT_INFO("Hey ! It's me ! :)");
216 MSG_task_send(todo[i], MSG_host_get_name(slaves[i % slaves_count]));
217 XBT_INFO("Send completed");
221 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
222 for (i = 0; i < slaves_count; i++)
223 MSG_task_send(MSG_task_create("finalize", 0, 0, FINALIZE),
224 MSG_host_get_name(slaves[i]));
226 XBT_INFO("Goodbye now!");
230 } /* end_of_master */
232 /** Receiver function */
233 int slave(int argc, char *argv[])
235 XBT_INFO("I'm a slave");
237 msg_task_t task = NULL;
239 a = MSG_task_receive(&task, MSG_host_get_name(MSG_host_self()));
241 XBT_INFO("Received \"%s\" ", MSG_task_get_name(task));
242 if (MSG_task_get_data(task) == FINALIZE) {
243 MSG_task_destroy(task);
246 XBT_INFO("Processing \"%s\" ", MSG_task_get_name(task));
247 MSG_task_execute(task);
248 XBT_INFO("\"%s\" done ", MSG_task_get_name(task));
249 MSG_task_destroy(task);
251 XBT_INFO("Hey ?! What's up ? ");
252 xbt_die( "Unexpected behavior");
255 XBT_INFO("I'm done. See you!");
260 msg_error_t test_all(void)
262 msg_error_t res = MSG_OK;
264 /* Simulation setting */
265 surf_parse = surf_parse_bypass_platform;
266 MSG_create_environment(NULL);
268 /* Application deployment */
269 MSG_function_register("master", master);
270 MSG_function_register("slave", slave);
271 surf_parse = surf_parse_bypass_application;
272 MSG_launch_application(NULL);
276 XBT_INFO("Simulation time %g", MSG_get_clock());
278 } /* end_of_test_all */
281 int main(int argc, char *argv[])
283 msg_error_t res = MSG_OK;
285 MSG_init(&argc, argv);