1 /* Copyright (c) 2007-2013. 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_pstate, "0");
39 SURFXML_BUFFER_SET(host_availability, "1.0");
40 SURFXML_BUFFER_SET(host_availability___file, "");
41 SURFXML_BUFFER_SET(host_core, "1");
42 A_surfxml_host_state = A_surfxml_host_state_ON;
43 SURFXML_BUFFER_SET(host_state___file, "");
44 SURFXML_BUFFER_SET(host_coordinates, "");
45 SURFXML_START_TAG(host);
46 SURFXML_END_TAG(host);
48 XBT_DEBUG("<host id=\"host B\" power=\"100000000.00\"/>");
49 SURFXML_BUFFER_SET(host_id, "host B");
50 SURFXML_BUFFER_SET(host_power, "100000000.00");
51 SURFXML_BUFFER_SET(host_pstate, "0");
52 SURFXML_BUFFER_SET(host_availability, "1.0");
53 SURFXML_BUFFER_SET(host_availability___file, "");
54 SURFXML_BUFFER_SET(host_core, "1");
55 A_surfxml_host_state = A_surfxml_host_state_ON;
56 SURFXML_BUFFER_SET(host_state___file, "");
57 SURFXML_BUFFER_SET(host_coordinates, "");
58 SURFXML_START_TAG(host);
59 SURFXML_END_TAG(host);
61 XBT_DEBUG("<link id=\"LinkA\" bandwidth=\"10000000.0\" latency=\"0.2\"/>");
62 SURFXML_BUFFER_SET(link_id, "LinkA");
63 SURFXML_BUFFER_SET(link_bandwidth, "10000000.0");
64 SURFXML_BUFFER_SET(link_bandwidth___file, "");
65 SURFXML_BUFFER_SET(link_latency, "0.2");
66 SURFXML_BUFFER_SET(link_latency___file, "");
67 A_surfxml_link_state = A_surfxml_link_state_ON;
68 SURFXML_BUFFER_SET(link_state___file, "");
69 A_surfxml_link_sharing___policy = A_surfxml_link_sharing___policy_SHARED;
70 SURFXML_START_TAG(link);
71 SURFXML_END_TAG(link);
73 XBT_DEBUG("<route src=\"host A\" dst=\"host B\">");
74 SURFXML_BUFFER_SET(route_src, "host A");
75 SURFXML_BUFFER_SET(route_dst, "host B");
76 A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_YES;
77 SURFXML_START_TAG(route);
78 XBT_DEBUG(" <link:ctn id=\"LinkA\"/>");
79 SURFXML_BUFFER_SET(link___ctn_id, "LinkA");
80 A_surfxml_link___ctn_direction = A_surfxml_link___ctn_direction_NONE;
81 SURFXML_START_TAG(link___ctn);
82 SURFXML_END_TAG(link___ctn);
83 XBT_DEBUG("</route>");
84 SURFXML_END_TAG(route);
88 XBT_DEBUG("</platfrom>");
89 SURFXML_END_TAG(platform);
91 free(surfxml_bufferstack);
95 static int surf_parse_bypass_application(void)
98 static int surfxml_bufferstack_size = 2048;
99 static int surfxml_buffer_stack_stack_ptr = 0;
100 static int surfxml_buffer_stack_stack[1024];
101 /* allocating memory to the buffer, I think 2MB should be enough */
102 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
105 SURFXML_BUFFER_SET(platform_version, "3");
107 SURFXML_START_TAG(platform);
109 /* <process host="host A" function="master"> */
110 SURFXML_BUFFER_SET(process_host, "host A");
111 SURFXML_BUFFER_SET(process_function, "master");
112 SURFXML_BUFFER_SET(process_start___time, "-1.0");
113 SURFXML_BUFFER_SET(process_kill___time, "-1.0");
114 SURFXML_START_TAG(process);
116 /* <argument value="20"/> */
117 SURFXML_BUFFER_SET(argument_value, "20");
118 SURFXML_START_TAG(argument);
119 SURFXML_END_TAG(argument);
121 /* <argument value="5000000"/> */
122 SURFXML_BUFFER_SET(argument_value, "5000000");
123 SURFXML_START_TAG(argument);
124 SURFXML_END_TAG(argument);
126 /* <argument value="100000"/> */
127 SURFXML_BUFFER_SET(argument_value, "100000");
128 SURFXML_START_TAG(argument);
129 SURFXML_END_TAG(argument);
131 /* <argument value="host B"/> */
132 SURFXML_BUFFER_SET(argument_value, "host B");
133 SURFXML_START_TAG(argument);
134 SURFXML_END_TAG(argument);
137 SURFXML_END_TAG(process);
139 /* <process host="host B" function="slave"/> */
140 SURFXML_BUFFER_SET(process_host, "host B");
141 SURFXML_BUFFER_SET(process_function, "slave");
142 SURFXML_BUFFER_SET(process_start___time, "-1.0");
143 SURFXML_BUFFER_SET(process_kill___time, "-1.0");
144 SURFXML_START_TAG(process);
145 SURFXML_END_TAG(process);
148 SURFXML_END_TAG(platform);
150 free(surfxml_bufferstack);
154 int master(int argc, char *argv[]);
155 int slave(int argc, char *argv[]);
156 msg_error_t test_all(void);
158 /** Emitter function */
159 int master(int argc, char *argv[])
161 int slaves_count = 0;
162 msg_host_t *slaves = NULL;
163 msg_task_t *todo = NULL;
164 int number_of_tasks = 0;
165 double task_comp_size = 0;
166 double task_comm_size = 0;
168 _XBT_GNUC_UNUSED int read;
170 read = sscanf(argv[1], "%d", &number_of_tasks);
171 xbt_assert(read, "Invalid argument %s\n", argv[1]);
172 read = sscanf(argv[2], "%lg", &task_comp_size);
173 xbt_assert(read, "Invalid argument %s\n", argv[2]);
174 read = sscanf(argv[3], "%lg", &task_comm_size);
175 xbt_assert(read, "Invalid argument %s\n", argv[3]);
177 { /* Task creation */
178 char sprintf_buffer[64];
180 todo = xbt_new0(msg_task_t, number_of_tasks);
182 for (i = 0; i < number_of_tasks; i++) {
183 sprintf(sprintf_buffer, "Task_%d", i);
185 MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
190 { /* Process organisation */
191 slaves_count = argc - 4;
192 slaves = xbt_new0(msg_host_t, slaves_count);
194 for (i = 4; i < argc; i++) {
195 slaves[i - 4] = MSG_get_host_by_name(argv[i]);
196 if (slaves[i - 4] == NULL) {
197 XBT_INFO("Unknown host %s. Stopping Now! ", argv[i]);
203 XBT_INFO("Got %d slave(s) :", slaves_count);
204 for (i = 0; i < slaves_count; i++)
205 XBT_INFO("\t %s", MSG_host_get_name(slaves[i]));
207 XBT_INFO("Got %d task to process :", number_of_tasks);
209 for (i = 0; i < number_of_tasks; i++)
210 XBT_INFO("\t\"%s\"", todo[i]->name);
212 for (i = 0; i < number_of_tasks; i++) {
213 XBT_INFO("Sending \"%s\" to \"%s\"",
214 todo[i]->name, MSG_host_get_name(slaves[i % slaves_count]));
215 if (MSG_host_self() == slaves[i % slaves_count]) {
216 XBT_INFO("Hey ! It's me ! :)");
218 MSG_task_send(todo[i], MSG_host_get_name(slaves[i % slaves_count]));
219 XBT_INFO("Send completed");
223 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
224 for (i = 0; i < slaves_count; i++)
225 MSG_task_send(MSG_task_create("finalize", 0, 0, FINALIZE),
226 MSG_host_get_name(slaves[i]));
228 XBT_INFO("Goodbye now!");
232 } /* end_of_master */
234 /** Receiver function */
235 int slave(int argc, char *argv[])
237 XBT_INFO("I'm a slave");
239 msg_task_t task = NULL;
241 a = MSG_task_receive(&task, MSG_host_get_name(MSG_host_self()));
243 XBT_INFO("Received \"%s\" ", MSG_task_get_name(task));
244 if (MSG_task_get_data(task) == FINALIZE) {
245 MSG_task_destroy(task);
248 XBT_INFO("Processing \"%s\" ", MSG_task_get_name(task));
249 MSG_task_execute(task);
250 XBT_INFO("\"%s\" done ", MSG_task_get_name(task));
251 MSG_task_destroy(task);
253 XBT_INFO("Hey ?! What's up ? ");
254 xbt_die( "Unexpected behavior");
257 XBT_INFO("I'm done. See you!");
262 msg_error_t test_all(void)
264 msg_error_t res = MSG_OK;
266 /* Simulation setting */
267 surf_parse = surf_parse_bypass_platform;
268 MSG_create_environment(NULL);
270 /* Application deployment */
271 MSG_function_register("master", master);
272 MSG_function_register("slave", slave);
273 surf_parse = surf_parse_bypass_application;
274 MSG_launch_application(NULL);
278 XBT_INFO("Simulation time %g", MSG_get_clock());
280 } /* end_of_test_all */
283 int main(int argc, char *argv[])
285 msg_error_t res = MSG_OK;
287 MSG_init(&argc, argv);