1 /* Copyright (c) 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 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15 "Messages specific for this msg example");
17 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
19 MSG_error_t test_all(const char *platform_file);
22 int master(int argc, char *argv[]);
23 int slave(int argc, char *argv[]);
30 /** Emitter function */
31 int master(int argc, char *argv[])
34 m_host_t *slaves = NULL;
35 m_task_t *todo = NULL;
36 int number_of_tasks = 0;
37 double task_comp_size = 0;
38 double task_comm_size = 0;
43 xbt_assert1(sscanf(argv[1], "%d", &number_of_tasks),
44 "Invalid argument %s\n", argv[1]);
45 xbt_assert1(sscanf(argv[2], "%lg", &task_comp_size),
46 "Invalid argument %s\n", argv[2]);
47 xbt_assert1(sscanf(argv[3], "%lg", &task_comm_size),
48 "Invalid argument %s\n", argv[3]);
51 char sprintf_buffer[64];
53 todo = xbt_new0(m_task_t, number_of_tasks);
55 for (i = 0; i < number_of_tasks; i++) {
56 sprintf(sprintf_buffer, "Task_%d", i);
58 MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
63 { /* Process organisation */
64 slaves_count = argc - 4;
65 slaves = xbt_new0(m_host_t, slaves_count);
67 for (i = 4; i < argc; i++) {
68 slaves[i - 4] = MSG_get_host_by_name(argv[i]);
69 if (slaves[i - 4] == NULL) {
70 INFO1("Unknown host %s. Stopping Now! ", argv[i]);
76 INFO1("Got %d slave(s) :", slaves_count);
77 for (i = 0; i < slaves_count; i++)
78 INFO1("\t %s", slaves[i]->name);
80 INFO1("Got %d task to process :", number_of_tasks);
82 for (i = 0; i < number_of_tasks; i++)
83 INFO1("\t\"%s\"", todo[i]->name);
85 for (i = 0; i < number_of_tasks; i++) {
86 INFO2("Sending \"%s\" to \"%s\"",
87 todo[i]->name, slaves[i % slaves_count]->name);
88 if (MSG_host_self() == slaves[i % slaves_count]) {
89 INFO0("Hey ! It's me ! :)");
91 MSG_task_put(todo[i], slaves[i % slaves_count], PORT_22);
92 INFO0("Send completed");
96 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
97 for (i = 0; i < slaves_count; i++)
98 MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
101 INFO0("Goodbye now!");
105 } /* end_of_master */
107 /** Receiver function */
108 int slave(int argc, char *argv[])
110 INFO0("I'm a slave");
112 m_task_t task = NULL;
114 a = MSG_task_get(&(task), PORT_22);
116 INFO1("Received \"%s\" ", MSG_task_get_name(task));
117 if (MSG_task_get_data(task) == FINALIZE) {
118 MSG_task_destroy(task);
121 INFO1("Processing \"%s\" ", MSG_task_get_name(task));
122 MSG_task_execute(task);
123 INFO1("\"%s\" done ", MSG_task_get_name(task));
124 MSG_task_destroy(task);
126 INFO0("Hey ?! What's up ? ");
127 xbt_assert0(0, "Unexpected behavior");
130 INFO0("I'm done. See you!");
135 /** Bypass deployment **/
136 static int bypass_deployment(void)
140 static int surfxml_bufferstack_size = 2048;
141 static int surfxml_buffer_stack_stack_ptr = 0;
142 static int surfxml_buffer_stack_stack[1024];
143 /* allocating memory to the buffer, I think 2MB should be enough */
144 surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
145 nb_host = MSG_get_host_number();
148 SURFXML_BUFFER_SET(platform_version, "3");
149 SURFXML_START_TAG(platform);
150 DEBUG0("<platform version=\"3\">");
152 DEBUG0(" <process host=\"c-0.me\" function=\"master\">");
153 SURFXML_BUFFER_SET(process_host, "c-0.me");
154 SURFXML_BUFFER_SET(process_function, "master");
155 SURFXML_BUFFER_SET(process_start_time, "-1.0");
156 SURFXML_BUFFER_SET(process_kill_time, "-1.0");
157 SURFXML_START_TAG(process);
159 DEBUG1(" <argument value=\"%s\"/>",bprintf("%d",nb_host-1));
160 SURFXML_BUFFER_SET(argument_value, bprintf("%d",nb_host-1));
161 SURFXML_START_TAG(argument);
162 SURFXML_END_TAG(argument);
164 DEBUG0(" <argument value=\"5000000\"/>");
165 SURFXML_BUFFER_SET(argument_value, "5000000");
166 SURFXML_START_TAG(argument);
167 SURFXML_END_TAG(argument);
169 DEBUG0(" <argument value=\"100000\"/>");
170 SURFXML_BUFFER_SET(argument_value, "100000");
171 SURFXML_START_TAG(argument);
172 SURFXML_END_TAG(argument);
174 for(i=1 ; i<nb_host ; i++)
176 DEBUG1(" <argument value=\"%s.me\"/>",bprintf("c-%d",i));
177 SURFXML_BUFFER_SET(argument_value, bprintf("c-%d.me",i));
178 SURFXML_START_TAG(argument);
179 SURFXML_END_TAG(argument);
181 DEBUG0(" </process>");
182 SURFXML_END_TAG(process);
184 for(i=1 ; i<nb_host ; i++)
186 DEBUG1(" <process host=\"%s.me\" function=\"slave\"/>",bprintf("c-%d",i));
187 SURFXML_BUFFER_SET(process_host, bprintf("c-%d.me",i));
188 SURFXML_BUFFER_SET(process_function, "slave");
189 SURFXML_BUFFER_SET(process_start_time, "-1.0");
190 SURFXML_BUFFER_SET(process_kill_time, "-1.0");
191 SURFXML_START_TAG(process);
192 SURFXML_END_TAG(process);
195 DEBUG0("</platform>");
196 SURFXML_END_TAG(platform);
198 free(surfxml_bufferstack);
203 MSG_error_t test_all(const char *platform_file)
205 MSG_error_t res = MSG_OK;
206 MSG_set_channel_number(MAX_CHANNEL);
207 MSG_create_environment(platform_file);
208 MSG_function_register("master", master);
209 MSG_function_register("slave", slave);
210 surf_parse = bypass_deployment;
211 MSG_launch_application(NULL);
215 INFO1("Simulation time %g", MSG_get_clock());
217 } /* end_of_test_all */
221 int main(int argc, char *argv[])
223 MSG_error_t res = MSG_OK;
225 MSG_global_init(&argc, argv);
226 res = test_all(argv[1]);