Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix the lastest documentation warning
[simgrid.git] / examples / msg / alias / masterslave_forwarder_with_alias.c
1 /* Copyright (c) 2008, 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
24 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
25
26 /** Emitter function  */
27 int master(int argc, char *argv[])
28 {
29   int alias_count = 0;
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;
35   int i;
36   int read;
37
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]);
44
45   {
46     /*  Task creation */
47     char sprintf_buffer[64];
48
49     todo = xbt_new0(m_task_t, number_of_tasks);
50
51     for (i = 0; i < number_of_tasks; i++) {
52       sprintf(sprintf_buffer, "Task_%d", i);
53       todo[i] =
54           MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
55                           NULL);
56     }
57   }
58
59   {
60     /* Process organisation */
61     alias_count = argc - 4;
62     aliases = xbt_new0(char *, alias_count);
63
64     for (i = 4; i < argc; i++) {
65       aliases[i - 4] = strdup(argv[i]);
66     }
67   }
68
69   XBT_INFO("Got %d aliases and %d tasks to process", alias_count,
70         number_of_tasks);
71
72   for (i = 0; i < alias_count; i++)
73     XBT_DEBUG("%s", aliases[i]);
74
75   for (i = 0; i < number_of_tasks; i++) {
76     XBT_INFO("Sending \"%s\" to \"%s\"", todo[i]->name,
77           aliases[i % alias_count]);
78
79     if (!strcmp
80         (MSG_host_get_name(MSG_host_self()), aliases[i % alias_count])) {
81       XBT_INFO("Hey ! It's me ! :)");
82     }
83
84     MSG_task_send(todo[i], aliases[i % alias_count]);
85     XBT_INFO("Sent");
86   }
87
88   XBT_INFO
89       ("All tasks have been dispatched. Let's tell everybody the computation is over.");
90
91   for (i = 0; i < alias_count; i++)
92     MSG_task_send(MSG_task_create("finalize", 0, 0, FINALIZE), aliases[i]);
93
94   XBT_INFO("Goodbye now!");
95
96   for (i = 0; i < alias_count; i++)
97     free(aliases[i]);
98
99   free(aliases);
100   free(todo);
101   return 0;
102 }                               /* end_of_master */
103
104 /** Receiver function  */
105 int slave(int argc, char *argv[])
106 {
107   m_task_t task = NULL;
108   int res;
109
110   while (1) {
111     res = MSG_task_receive(&(task), MSG_host_get_name(MSG_host_self()));
112     xbt_assert(res == MSG_OK, "MSG_task_receive failed");
113
114     XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
115
116     if (!strcmp(MSG_task_get_name(task), "finalize")) {
117       MSG_task_destroy(task);
118       break;
119     }
120
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);
125     task = NULL;
126   }
127
128   XBT_INFO("I'm done. See you!");
129   return 0;
130 }                               /* end_of_slave */
131
132 /** Forwarder function */
133 int forwarder(int argc, char *argv[])
134 {
135   int i;
136   int alias_count;
137   char **aliases;
138
139   {                             /* Process organisation */
140     alias_count = argc - 1;
141     aliases = xbt_new0(char *, alias_count);
142
143     for (i = 1; i < argc; i++)
144       aliases[i - 1] = strdup(argv[i]);
145   }
146
147   i = 0;
148
149   while (1) {
150     m_task_t task = NULL;
151     int a;
152
153     a = MSG_task_receive(&(task), MSG_host_get_name(MSG_host_self()));
154
155     if (a == MSG_OK) {
156       XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
157
158       if (MSG_task_get_data(task) == FINALIZE) {
159         XBT_INFO
160             ("All tasks have been dispatched. Let's tell everybody the computation is over.");
161
162         for (i = 0; i < alias_count; i++)
163           MSG_task_send(MSG_task_create("finalize", 0, 0, FINALIZE),
164                         aliases[i]);
165
166         MSG_task_destroy(task);
167         break;
168       }
169
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]);
173       i++;
174     } else {
175       XBT_INFO("Hey ?! What's up ? ");
176       xbt_die( "Unexpected behavior");
177     }
178   }
179
180   for (i = 0; i < alias_count; i++)
181     free(aliases[i]);
182
183   XBT_INFO("I'm done. See you!");
184   return 0;
185
186 }                               /* end_of_forwarder */
187
188 /** Test function */
189 MSG_error_t test_all(const char *platform_file,
190                      const char *application_file)
191 {
192   MSG_error_t res = MSG_OK;
193
194   {                             /*  Simulation setting */
195     MSG_paje_output("msg_test.trace");
196     MSG_create_environment(platform_file);
197   }
198
199   {
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);
205   }
206
207   res = MSG_main();
208
209   XBT_INFO("Simulation time %g", MSG_get_clock());
210   return res;
211 }                               /* end_of_test_all */
212
213
214 /** Main function */
215 int main(int argc, char *argv[])
216 {
217   MSG_error_t res = MSG_OK;
218
219   MSG_global_init(&argc, argv);
220
221   if (argc < 3) {
222     printf("Usage: %s platform_file deployment_file\n", argv[0]);
223     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
224     exit(1);
225   }
226
227   res = test_all(argv[1], argv[2]);
228   MSG_clean();
229
230   if (res == MSG_OK)
231     return 0;
232   else
233     return 1;
234 }                               /* end_of_main */