Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Dirty hacks again: shot more bullets, it may be a zombie
[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
18 typedef enum {
19   PORT_22 = 0,
20   MAX_CHANNEL
21 } channel_t;
22
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);
28
29
30 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
31
32 /** Emitter function  */
33 int master(int argc, char *argv[])
34 {
35   int alias_count = 0;
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;
41   int i;
42   int read;
43
44   read = sscanf(argv[1], "%d", &number_of_tasks);
45   xbt_assert1(read, "Invalid argument %s\n", argv[1]);
46   read = sscanf(argv[2], "%lg", &task_comp_size);
47   xbt_assert1(read, "Invalid argument %s\n", argv[2]);
48   read = sscanf(argv[3], "%lg", &task_comm_size);
49   xbt_assert1(read, "Invalid argument %s\n", argv[3]);
50
51   {
52     /*  Task creation */
53     char sprintf_buffer[64];
54
55     todo = xbt_new0(m_task_t, number_of_tasks);
56
57     for (i = 0; i < number_of_tasks; i++) {
58       sprintf(sprintf_buffer, "Task_%d", i);
59       todo[i] =
60           MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
61                           NULL);
62     }
63   }
64
65   {
66     /* Process organisation */
67     alias_count = argc - 4;
68     aliases = xbt_new0(char *, alias_count);
69
70     for (i = 4; i < argc; i++) {
71       aliases[i - 4] = strdup(argv[i]);
72     }
73   }
74
75   XBT_INFO("Got %d aliases and %d tasks to process", alias_count,
76         number_of_tasks);
77
78   for (i = 0; i < alias_count; i++)
79     XBT_DEBUG("%s", aliases[i]);
80
81   for (i = 0; i < number_of_tasks; i++) {
82     XBT_INFO("Sending \"%s\" to \"%s\"", todo[i]->name,
83           aliases[i % alias_count]);
84
85     if (!strcmp
86         (MSG_host_get_name(MSG_host_self()), aliases[i % alias_count])) {
87       XBT_INFO("Hey ! It's me ! :)");
88     }
89
90     MSG_task_send(todo[i], aliases[i % alias_count]);
91     XBT_INFO("Sent");
92   }
93
94   XBT_INFO
95       ("All tasks have been dispatched. Let's tell everybody the computation is over.");
96
97   for (i = 0; i < alias_count; i++)
98     MSG_task_send(MSG_task_create("finalize", 0, 0, FINALIZE), aliases[i]);
99
100   XBT_INFO("Goodbye now!");
101
102   for (i = 0; i < alias_count; i++)
103     free(aliases[i]);
104
105   free(aliases);
106   free(todo);
107   return 0;
108 }                               /* end_of_master */
109
110 /** Receiver function  */
111 int slave(int argc, char *argv[])
112 {
113   m_task_t task = NULL;
114   int res;
115
116   while (1) {
117     res = MSG_task_receive(&(task), MSG_host_get_name(MSG_host_self()));
118     xbt_assert0(res == MSG_OK, "MSG_task_receive failed");
119
120     XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
121
122     if (!strcmp(MSG_task_get_name(task), "finalize")) {
123       MSG_task_destroy(task);
124       break;
125     }
126
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);
131     task = NULL;
132   }
133
134   XBT_INFO("I'm done. See you!");
135   return 0;
136 }                               /* end_of_slave */
137
138 /** Forwarder function */
139 int forwarder(int argc, char *argv[])
140 {
141   int i;
142   int alias_count;
143   char **aliases;
144
145   {                             /* Process organisation */
146     alias_count = argc - 1;
147     aliases = xbt_new0(char *, alias_count);
148
149     for (i = 1; i < argc; i++)
150       aliases[i - 1] = strdup(argv[i]);
151   }
152
153   i = 0;
154
155   while (1) {
156     m_task_t task = NULL;
157     int a;
158
159     a = MSG_task_receive(&(task), MSG_host_get_name(MSG_host_self()));
160
161     if (a == MSG_OK) {
162       XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
163
164       if (MSG_task_get_data(task) == FINALIZE) {
165         XBT_INFO
166             ("All tasks have been dispatched. Let's tell everybody the computation is over.");
167
168         for (i = 0; i < alias_count; i++)
169           MSG_task_send(MSG_task_create("finalize", 0, 0, FINALIZE),
170                         aliases[i]);
171
172         MSG_task_destroy(task);
173         break;
174       }
175
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]);
179       i++;
180     } else {
181       XBT_INFO("Hey ?! What's up ? ");
182       xbt_die( "Unexpected behavior");
183     }
184   }
185
186   for (i = 0; i < alias_count; i++)
187     free(aliases[i]);
188
189   XBT_INFO("I'm done. See you!");
190   return 0;
191
192 }                               /* end_of_forwarder */
193
194 /** Test function */
195 MSG_error_t test_all(const char *platform_file,
196                      const char *application_file)
197 {
198   MSG_error_t res = MSG_OK;
199
200   {                             /*  Simulation setting */
201     MSG_set_channel_number(MAX_CHANNEL);
202     MSG_paje_output("msg_test.trace");
203     MSG_create_environment(platform_file);
204   }
205
206   {
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);
212   }
213
214   res = MSG_main();
215
216   XBT_INFO("Simulation time %g", MSG_get_clock());
217   return res;
218 }                               /* end_of_test_all */
219
220
221 /** Main function */
222 int main(int argc, char *argv[])
223 {
224   MSG_error_t res = MSG_OK;
225
226   MSG_global_init(&argc, argv);
227
228   if (argc < 3) {
229     printf("Usage: %s platform_file deployment_file\n", argv[0]);
230     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
231     exit(1);
232   }
233
234   res = test_all(argv[1], argv[2]);
235   MSG_clean();
236
237   if (res == MSG_OK)
238     return 0;
239   else
240     return 1;
241 }                               /* end_of_main */