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 "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);
23 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
25 /** Emitter function */
26 int master(int argc, char *argv[])
29 m_host_t *slaves = NULL;
30 m_task_t *todo = NULL;
31 int number_of_tasks = 0;
32 double task_comp_size = 0;
33 double task_comm_size = 0;
37 _XBT_GNUC_UNUSED int res = sscanf(argv[1], "%d", &number_of_tasks);
38 xbt_assert(res,"Invalid argument %s\n", argv[1]);
39 res = sscanf(argv[2], "%lg", &task_comp_size);
40 xbt_assert(res, "Invalid argument %s\n", argv[2]);
41 res = sscanf(argv[3], "%lg", &task_comm_size);
42 xbt_assert(res, "Invalid argument %s\n", argv[3]);
45 char sprintf_buffer[64];
47 todo = xbt_new0(m_task_t, number_of_tasks);
49 for (i = 0; i < number_of_tasks; i++) {
50 sprintf(sprintf_buffer, "Task_%d", i);
52 MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
57 { /* Process organisation */
58 slaves_count = argc - 4;
59 slaves = xbt_new0(m_host_t, slaves_count);
61 for (i = 4; i < argc; i++) {
62 slaves[i - 4] = MSG_get_host_by_name(argv[i]);
63 xbt_assert(slaves[i - 4] != NULL, "Unknown host %s. Stopping Now! ",
68 XBT_INFO("Got %d slaves and %d tasks to process", slaves_count,
70 for (i = 0; i < slaves_count; i++)
71 XBT_DEBUG("%s", slaves[i]->name);
73 for (i = 0; i < number_of_tasks; i++) {
74 XBT_INFO("Sending \"%s\" to \"%s\"",
75 todo[i]->name, slaves[i % slaves_count]->name);
76 if (MSG_host_self() == slaves[i % slaves_count]) {
77 XBT_INFO("Hey ! It's me ! :)");
80 MSG_task_send(todo[i], MSG_host_get_name(slaves[i % slaves_count]));
85 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
86 for (i = 0; i < slaves_count; i++) {
87 m_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
88 MSG_task_send(finalize, MSG_host_get_name(slaves[i]));
91 XBT_INFO("Goodbye now!");
97 /** Receiver function */
98 int slave(int argc, char *argv[])
100 m_task_t task = NULL;
101 _XBT_GNUC_UNUSED int res;
103 res = MSG_task_receive(&(task),MSG_host_get_name(MSG_host_self()));
104 xbt_assert(res == MSG_OK, "MSG_task_get failed");
106 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
107 if (!strcmp(MSG_task_get_name(task), "finalize")) {
108 MSG_task_destroy(task);
112 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
113 MSG_task_execute(task);
114 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
115 MSG_task_destroy(task);
118 XBT_INFO("I'm done. See you!");
122 /** Forwarder function */
123 int forwarder(int argc, char *argv[])
129 { /* Process organisation */
130 slaves_count = argc - 1;
131 slaves = xbt_new0(m_host_t, slaves_count);
133 for (i = 1; i < argc; i++) {
134 slaves[i - 1] = MSG_get_host_by_name(argv[i]);
135 if (slaves[i - 1] == NULL) {
136 XBT_INFO("Unknown host %s. Stopping Now! ", argv[i]);
144 m_task_t task = NULL;
146 a = MSG_task_receive(&(task),MSG_host_get_name(MSG_host_self()));
148 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
149 if (MSG_task_get_data(task) == FINALIZE) {
151 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
152 for (i = 0; i < slaves_count; i++)
153 MSG_task_send(MSG_task_create("finalize", 0, 0, FINALIZE),
154 MSG_host_get_name(slaves[i]));
155 MSG_task_destroy(task);
158 XBT_INFO("Sending \"%s\" to \"%s\"",
159 MSG_task_get_name(task), slaves[i % slaves_count]->name);
160 MSG_task_send(task, MSG_host_get_name(slaves[i % slaves_count]));
163 XBT_INFO("Hey ?! What's up ? ");
164 xbt_die("Unexpected behavior");
169 XBT_INFO("I'm done. See you!");
171 } /* end_of_forwarder */
174 MSG_error_t test_all(const char *platform_file,
175 const char *application_file)
177 MSG_error_t res = MSG_OK;
179 /* MSG_config("workstation/model","KCCFLN05"); */
180 { /* Simulation setting */
181 MSG_create_environment(platform_file);
183 { /* Application deployment */
184 MSG_function_register("master", master);
185 MSG_function_register("slave", slave);
186 MSG_function_register("forwarder", forwarder);
187 MSG_launch_application(application_file);
191 XBT_INFO("Simulation time %g", MSG_get_clock());
193 } /* end_of_test_all */
197 int main(int argc, char *argv[])
199 MSG_error_t res = MSG_OK;
201 MSG_global_init(&argc, argv);
203 printf("Usage: %s platform_file deployment_file\n", argv[0]);
204 printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
207 res = test_all(argv[1], argv[2]);