1 /* Copyright (c) 2008, 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 "xbt/sysdep.h" /* calloc, printf */
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 int master(int argc, char *argv[]);
18 int slave(int argc, char *argv[]);
19 int forwarder(int argc, char *argv[]);
20 MSG_error_t test_all(const char *platform_file,
21 const char *application_file);
24 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
26 /** Emitter function */
27 int master(int argc, char *argv[])
30 char **aliases = NULL;
31 m_task_t *todo = NULL;
32 int number_of_tasks = 0;
33 double task_comp_size = 0;
34 double task_comm_size = 0;
38 read = sscanf(argv[1], "%d", &number_of_tasks);
39 xbt_assert(read, "Invalid argument %s\n", argv[1]);
40 read = sscanf(argv[2], "%lg", &task_comp_size);
41 xbt_assert(read, "Invalid argument %s\n", argv[2]);
42 read = sscanf(argv[3], "%lg", &task_comm_size);
43 xbt_assert(read, "Invalid argument %s\n", argv[3]);
47 char sprintf_buffer[64];
49 todo = xbt_new0(m_task_t, number_of_tasks);
51 for (i = 0; i < number_of_tasks; i++) {
52 sprintf(sprintf_buffer, "Task_%d", i);
54 MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
60 /* Process organisation */
61 alias_count = argc - 4;
62 aliases = xbt_new0(char *, alias_count);
64 for (i = 4; i < argc; i++) {
65 aliases[i - 4] = strdup(argv[i]);
69 XBT_INFO("Got %d aliases and %d tasks to process", alias_count,
72 for (i = 0; i < alias_count; i++)
73 XBT_DEBUG("%s", aliases[i]);
75 for (i = 0; i < number_of_tasks; i++) {
76 XBT_INFO("Sending \"%s\" to \"%s\"", todo[i]->name,
77 aliases[i % alias_count]);
80 (MSG_host_get_name(MSG_host_self()), aliases[i % alias_count])) {
81 XBT_INFO("Hey ! It's me ! :)");
84 MSG_task_send(todo[i], aliases[i % alias_count]);
89 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
91 for (i = 0; i < alias_count; i++)
92 MSG_task_send(MSG_task_create("finalize", 0, 0, FINALIZE), aliases[i]);
94 XBT_INFO("Goodbye now!");
96 for (i = 0; i < alias_count; i++)
102 } /* end_of_master */
104 /** Receiver function */
105 int slave(int argc, char *argv[])
107 m_task_t task = NULL;
111 res = MSG_task_receive(&(task), MSG_host_get_name(MSG_host_self()));
112 xbt_assert(res == MSG_OK, "MSG_task_receive failed");
114 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
116 if (!strcmp(MSG_task_get_name(task), "finalize")) {
117 MSG_task_destroy(task);
121 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
122 MSG_task_execute(task);
123 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
124 MSG_task_destroy(task);
128 XBT_INFO("I'm done. See you!");
132 /** Forwarder function */
133 int forwarder(int argc, char *argv[])
139 { /* Process organisation */
140 alias_count = argc - 1;
141 aliases = xbt_new0(char *, alias_count);
143 for (i = 1; i < argc; i++)
144 aliases[i - 1] = strdup(argv[i]);
150 m_task_t task = NULL;
153 a = MSG_task_receive(&(task), MSG_host_get_name(MSG_host_self()));
156 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
158 if (MSG_task_get_data(task) == FINALIZE) {
160 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
162 for (i = 0; i < alias_count; i++)
163 MSG_task_send(MSG_task_create("finalize", 0, 0, FINALIZE),
166 MSG_task_destroy(task);
170 XBT_INFO("Sending \"%s\" to \"%s\"", MSG_task_get_name(task),
171 aliases[i % alias_count]);
172 MSG_task_send(task, aliases[i % alias_count]);
175 XBT_INFO("Hey ?! What's up ? ");
176 xbt_die( "Unexpected behavior");
180 for (i = 0; i < alias_count; i++)
183 XBT_INFO("I'm done. See you!");
186 } /* end_of_forwarder */
189 MSG_error_t test_all(const char *platform_file,
190 const char *application_file)
192 MSG_error_t res = MSG_OK;
194 { /* Simulation setting */
195 MSG_paje_output("msg_test.trace");
196 MSG_create_environment(platform_file);
200 /* Application deployment */
201 MSG_function_register("master", master);
202 MSG_function_register("slave", slave);
203 MSG_function_register("forwarder", forwarder);
204 MSG_launch_application(application_file);
209 XBT_INFO("Simulation time %g", MSG_get_clock());
211 } /* end_of_test_all */
215 int main(int argc, char *argv[])
217 MSG_error_t res = MSG_OK;
219 MSG_global_init(&argc, argv);
222 printf("Usage: %s platform_file deployment_file\n", argv[0]);
223 printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
227 res = test_all(argv[1], argv[2]);