1 /* Copyright (c) 2007-2015. 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 "simgrid/msg.h" /* Yeah! If you want to use msg, you need to include simgrid/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 /** @addtogroup MSG_examples
19 * - <b>masterslave/masterslave_forwarder.c: Master/slaves
20 * example</b>. This good old example is also very simple. Its basic
21 * version is fully commented on this page: \ref MSG_ex_master_slave,
22 * but several variants can be found in the same directory.
26 int master(int argc, char *argv[]);
27 int slave(int argc, char *argv[]);
28 int forwarder(int argc, char *argv[]);
29 msg_error_t test_all(const char *platform_file,
30 const char *application_file);
32 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
34 /** Emitter function */
35 int master(int argc, char *argv[])
38 msg_host_t *slaves = NULL;
39 msg_task_t *todo = NULL;
40 int number_of_tasks = 0;
41 double task_comp_size = 0;
42 double task_comm_size = 0;
46 XBT_ATTRIB_UNUSED int res = sscanf(argv[1], "%d", &number_of_tasks);
47 xbt_assert(res,"Invalid argument %s\n", argv[1]);
48 res = sscanf(argv[2], "%lg", &task_comp_size);
49 xbt_assert(res, "Invalid argument %s\n", argv[2]);
50 res = sscanf(argv[3], "%lg", &task_comm_size);
51 xbt_assert(res, "Invalid argument %s\n", argv[3]);
54 char sprintf_buffer[64];
56 todo = xbt_new0(msg_task_t, number_of_tasks);
58 for (i = 0; i < number_of_tasks; i++) {
59 sprintf(sprintf_buffer, "Task_%d", i);
61 MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
66 { /* Process organization */
67 slaves_count = argc - 4;
68 slaves = xbt_new0(msg_host_t, slaves_count);
70 for (i = 4; i < argc; i++) {
71 slaves[i - 4] = MSG_host_by_name(argv[i]);
72 xbt_assert(slaves[i - 4] != NULL, "Unknown host %s. Stopping Now! ",
77 XBT_INFO("Got %d slaves and %d tasks to process", slaves_count,
79 for (i = 0; i < slaves_count; i++)
80 XBT_DEBUG("%s", MSG_host_get_name(slaves[i]));
82 for (i = 0; i < number_of_tasks; i++) {
83 XBT_INFO("Sending \"%s\" to \"%s\"",
84 todo[i]->name, MSG_host_get_name(slaves[i % slaves_count]));
85 if (MSG_host_self() == slaves[i % slaves_count]) {
86 XBT_INFO("Hey ! It's me ! :)");
89 MSG_task_send(todo[i], MSG_host_get_name(slaves[i % slaves_count]));
94 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
95 for (i = 0; i < slaves_count; i++) {
96 msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
97 MSG_task_send(finalize, MSG_host_get_name(slaves[i]));
100 XBT_INFO("Goodbye now!");
104 } /* end_of_master */
106 /** Receiver function */
107 int slave(int argc, char *argv[])
109 msg_task_t task = NULL;
110 XBT_ATTRIB_UNUSED int res;
112 res = MSG_task_receive(&(task),MSG_host_get_name(MSG_host_self()));
113 xbt_assert(res == MSG_OK, "MSG_task_get failed");
115 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);
127 XBT_INFO("I'm done. See you!");
131 /** Forwarder function */
132 int forwarder(int argc, char *argv[])
138 { /* Process organization */
139 slaves_count = argc - 1;
140 slaves = xbt_new0(msg_host_t, slaves_count);
142 for (i = 1; i < argc; i++) {
143 slaves[i - 1] = MSG_host_by_name(argv[i]);
144 if (slaves[i - 1] == NULL) {
145 XBT_INFO("Unknown host %s. Stopping Now! ", argv[i]);
153 msg_task_t task = NULL;
155 a = MSG_task_receive(&(task),MSG_host_get_name(MSG_host_self()));
157 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.");
161 for (i = 0; i < slaves_count; i++)
162 MSG_task_send(MSG_task_create("finalize", 0, 0, FINALIZE),
163 MSG_host_get_name(slaves[i]));
164 MSG_task_destroy(task);
167 XBT_INFO("Sending \"%s\" to \"%s\"",
168 MSG_task_get_name(task), MSG_host_get_name(slaves[i % slaves_count]));
169 MSG_task_send(task, MSG_host_get_name(slaves[i % slaves_count]));
172 XBT_INFO("Hey ?! What's up ? ");
173 xbt_die("Unexpected behavior");
178 XBT_INFO("I'm done. See you!");
180 } /* end_of_forwarder */
183 msg_error_t test_all(const char *platform_file,
184 const char *application_file)
186 msg_error_t res = MSG_OK;
188 { /* Simulation setting */
189 MSG_create_environment(platform_file);
191 { /* Application deployment */
192 MSG_function_register("master", master);
193 MSG_function_register("slave", slave);
194 MSG_function_register("forwarder", forwarder);
195 MSG_launch_application(application_file);
199 XBT_INFO("Simulation time %g", MSG_get_clock());
201 } /* end_of_test_all */
205 int main(int argc, char *argv[])
207 msg_error_t res = MSG_OK;
209 MSG_init(&argc, argv);
211 printf("Usage: %s platform_file deployment_file\n", argv[0]);
212 printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
215 res = test_all(argv[1], argv[2]);