Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b428b60ff63fd42ea1b80bbe534267ecdceabac9
[simgrid.git] / examples / msg / masterslave / masterslave_forwarder.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <stdio.h>
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 */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15                              "Messages specific for this msg example");
16
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);
22
23 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
24
25 /** Emitter function  */
26 int master(int argc, char *argv[])
27 {
28   int slaves_count = 0;
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;
34
35   int i;
36
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]);
43
44   {                             /*  Task creation */
45     char sprintf_buffer[64];
46
47     todo = xbt_new0(m_task_t, number_of_tasks);
48
49     for (i = 0; i < number_of_tasks; i++) {
50       sprintf(sprintf_buffer, "Task_%d", i);
51       todo[i] =
52           MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
53                           NULL);
54     }
55   }
56
57   {                             /* Process organisation */
58     slaves_count = argc - 4;
59     slaves = xbt_new0(m_host_t, slaves_count);
60
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! ",
64                   argv[i]);
65     }
66   }
67
68   XBT_INFO("Got %d slaves and %d tasks to process", slaves_count,
69         number_of_tasks);
70   for (i = 0; i < slaves_count; i++)
71     XBT_DEBUG("%s", slaves[i]->name);
72
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 ! :)");
78     }
79
80     MSG_task_send(todo[i], MSG_host_get_name(slaves[i % slaves_count]));
81     XBT_INFO("Sent");
82   }
83
84   XBT_INFO
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]));
89   }
90
91   XBT_INFO("Goodbye now!");
92   free(slaves);
93   free(todo);
94   return 0;
95 }                               /* end_of_master */
96
97 /** Receiver function  */
98 int slave(int argc, char *argv[])
99 {
100   m_task_t task = NULL;
101   _XBT_GNUC_UNUSED int res;
102   while (1) {
103     res = MSG_task_receive(&(task),MSG_host_get_name(MSG_host_self()));
104     xbt_assert(res == MSG_OK, "MSG_task_get failed");
105
106     XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
107     if (!strcmp(MSG_task_get_name(task), "finalize")) {
108       MSG_task_destroy(task);
109       break;
110     }
111
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);
116     task = NULL;
117   }
118   XBT_INFO("I'm done. See you!");
119   return 0;
120 }                               /* end_of_slave */
121
122 /** Forwarder function */
123 int forwarder(int argc, char *argv[])
124 {
125   int i;
126   int slaves_count;
127   m_host_t *slaves;
128
129   {                             /* Process organisation */
130     slaves_count = argc - 1;
131     slaves = xbt_new0(m_host_t, slaves_count);
132
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]);
137         abort();
138       }
139     }
140   }
141
142   i = 0;
143   while (1) {
144     m_task_t task = NULL;
145     int a;
146     a = MSG_task_receive(&(task),MSG_host_get_name(MSG_host_self()));
147     if (a == MSG_OK) {
148       XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
149       if (MSG_task_get_data(task) == FINALIZE) {
150         XBT_INFO
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);
156         break;
157       }
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]));
161       i++;
162     } else {
163       XBT_INFO("Hey ?! What's up ? ");
164       xbt_die("Unexpected behavior");
165     }
166   }
167   xbt_free(slaves);
168
169   XBT_INFO("I'm done. See you!");
170   return 0;
171 }                               /* end_of_forwarder */
172
173 /** Test function */
174 MSG_error_t test_all(const char *platform_file,
175                      const char *application_file)
176 {
177   MSG_error_t res = MSG_OK;
178
179   /* MSG_config("workstation/model","KCCFLN05"); */
180   {                             /*  Simulation setting */
181     MSG_create_environment(platform_file);
182   }
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);
188   }
189   res = MSG_main();
190
191   XBT_INFO("Simulation time %g", MSG_get_clock());
192   return res;
193 }                               /* end_of_test_all */
194
195
196 /** Main function */
197 int main(int argc, char *argv[])
198 {
199   MSG_error_t res = MSG_OK;
200
201   MSG_global_init(&argc, argv);
202   if (argc < 3) {
203     printf("Usage: %s platform_file deployment_file\n", argv[0]);
204     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
205     exit(1);
206   }
207   res = test_all(argv[1], argv[2]);
208   MSG_clean();
209
210   if (res == MSG_OK)
211     return 0;
212   else
213     return 1;
214 }                               /* end_of_main */