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");
23 int master(int argc, char *argv[]);
24 int slave(int argc, char *argv[]);
25 int forwarder(int argc, char *argv[]);
26 MSG_error_t test_all(const char *platform_file,
27 const char *application_file);
30 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
32 /** Emitter function */
33 int master(int argc, char *argv[])
36 char **aliases = NULL;
37 m_task_t *todo = NULL;
38 int number_of_tasks = 0;
39 double task_comp_size = 0;
40 double task_comm_size = 0;
44 read = sscanf(argv[1], "%d", &number_of_tasks);
45 xbt_assert(read, "Invalid argument %s\n", argv[1]);
46 read = sscanf(argv[2], "%lg", &task_comp_size);
47 xbt_assert(read, "Invalid argument %s\n", argv[2]);
48 read = sscanf(argv[3], "%lg", &task_comm_size);
49 xbt_assert(read, "Invalid argument %s\n", argv[3]);
53 char sprintf_buffer[64];
55 todo = xbt_new0(m_task_t, number_of_tasks);
57 for (i = 0; i < number_of_tasks; i++) {
58 sprintf(sprintf_buffer, "Task_%d", i);
60 MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
66 /* Process organisation */
67 alias_count = argc - 4;
68 aliases = xbt_new0(char *, alias_count);
70 for (i = 4; i < argc; i++) {
71 aliases[i - 4] = strdup(argv[i]);
75 XBT_INFO("Got %d aliases and %d tasks to process", alias_count,
78 for (i = 0; i < alias_count; i++)
79 XBT_DEBUG("%s", aliases[i]);
81 for (i = 0; i < number_of_tasks; i++) {
82 XBT_INFO("Sending \"%s\" to \"%s\"", todo[i]->name,
83 aliases[i % alias_count]);
86 (MSG_host_get_name(MSG_host_self()), aliases[i % alias_count])) {
87 XBT_INFO("Hey ! It's me ! :)");
90 MSG_task_send(todo[i], aliases[i % alias_count]);
95 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
97 for (i = 0; i < alias_count; i++)
98 MSG_task_send(MSG_task_create("finalize", 0, 0, FINALIZE), aliases[i]);
100 XBT_INFO("Goodbye now!");
102 for (i = 0; i < alias_count; i++)
108 } /* end_of_master */
110 /** Receiver function */
111 int slave(int argc, char *argv[])
113 m_task_t task = NULL;
117 res = MSG_task_receive(&(task), MSG_host_get_name(MSG_host_self()));
118 xbt_assert(res == MSG_OK, "MSG_task_receive failed");
120 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
122 if (!strcmp(MSG_task_get_name(task), "finalize")) {
123 MSG_task_destroy(task);
127 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
128 MSG_task_execute(task);
129 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
130 MSG_task_destroy(task);
134 XBT_INFO("I'm done. See you!");
138 /** Forwarder function */
139 int forwarder(int argc, char *argv[])
145 { /* Process organisation */
146 alias_count = argc - 1;
147 aliases = xbt_new0(char *, alias_count);
149 for (i = 1; i < argc; i++)
150 aliases[i - 1] = strdup(argv[i]);
156 m_task_t task = NULL;
159 a = MSG_task_receive(&(task), MSG_host_get_name(MSG_host_self()));
162 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
164 if (MSG_task_get_data(task) == FINALIZE) {
166 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
168 for (i = 0; i < alias_count; i++)
169 MSG_task_send(MSG_task_create("finalize", 0, 0, FINALIZE),
172 MSG_task_destroy(task);
176 XBT_INFO("Sending \"%s\" to \"%s\"", MSG_task_get_name(task),
177 aliases[i % alias_count]);
178 MSG_task_send(task, aliases[i % alias_count]);
181 XBT_INFO("Hey ?! What's up ? ");
182 xbt_die( "Unexpected behavior");
186 for (i = 0; i < alias_count; i++)
189 XBT_INFO("I'm done. See you!");
192 } /* end_of_forwarder */
195 MSG_error_t test_all(const char *platform_file,
196 const char *application_file)
198 MSG_error_t res = MSG_OK;
200 { /* Simulation setting */
201 MSG_set_channel_number(MAX_CHANNEL);
202 MSG_paje_output("msg_test.trace");
203 MSG_create_environment(platform_file);
207 /* Application deployment */
208 MSG_function_register("master", master);
209 MSG_function_register("slave", slave);
210 MSG_function_register("forwarder", forwarder);
211 MSG_launch_application(application_file);
216 XBT_INFO("Simulation time %g", MSG_get_clock());
218 } /* end_of_test_all */
222 int main(int argc, char *argv[])
224 MSG_error_t res = MSG_OK;
226 MSG_global_init(&argc, argv);
229 printf("Usage: %s platform_file deployment_file\n", argv[0]);
230 printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
234 res = test_all(argv[1], argv[2]);