Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a389a76cd8765bfec807b4a04b6f18ab18c1c0e7
[simgrid.git] / examples / msg / alias / masterslave_forwarder_with_alias.c
1 /*      $Id: masterslave_forwarder.c 5108 2007-12-04 16:29:05Z mquinson $        */\r
2 \r
3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved.        */\r
4 \r
5 /* This program is free software; you can redistribute it and/or modify it\r
6  * under the terms of the license (GNU LGPL) which comes with this package. */\r
7 \r
8 #include <stdio.h>\r
9 #include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */\r
10 #include "xbt/sysdep.h" /* calloc, printf */\r
11 \r
12 /* Create a log channel to have nice outputs. */\r
13 #include "xbt/log.h"\r
14 #include "xbt/asserts.h"\r
15 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,"Messages specific for this msg example");\r
16 \r
17 \r
18 typedef enum {\r
19   PORT_22 = 0,\r
20   MAX_CHANNEL\r
21 } channel_t;\r
22 \r
23 int master(int argc, char *argv[]);\r
24 int slave(int argc, char *argv[]);\r
25 int forwarder(int argc, char *argv[]);\r
26 MSG_error_t test_all(const char *platform_file, const char *application_file);\r
27 \r
28 \r
29 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */\r
30 \r
31 /** Emitter function  */\r
32 int master(int argc, char *argv[])\r
33 {\r
34         int alias_count = 0;\r
35         char** aliases = NULL;\r
36         m_task_t *todo = NULL;\r
37         int number_of_tasks = 0;\r
38         double task_comp_size = 0;\r
39         double task_comm_size = 0;\r
40 \r
41 \r
42         int i;\r
43 \r
44         xbt_assert1(sscanf(argv[1],"%d", &number_of_tasks),"Invalid argument %s\n",argv[1]);\r
45         xbt_assert1(sscanf(argv[2],"%lg", &task_comp_size),"Invalid argument %s\n",argv[2]);\r
46         xbt_assert1(sscanf(argv[3],"%lg", &task_comm_size),"Invalid argument %s\n",argv[3]);\r
47 \r
48         {\r
49                 /*  Task creation */\r
50                 char sprintf_buffer[64];\r
51 \r
52                 todo = xbt_new0(m_task_t,number_of_tasks);\r
53 \r
54                 for (i = 0; i < number_of_tasks; i++) \r
55                 {\r
56                         sprintf(sprintf_buffer, "Task_%d", i);\r
57                         todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);\r
58                 }\r
59         }\r
60 \r
61         {                  \r
62                 /* Process organisation */\r
63                 alias_count = argc - 4;\r
64                 aliases = xbt_new0(char*,alias_count);\r
65 \r
66                 for(i = 4; i < argc; i++) \r
67                 {\r
68                         aliases[i-4] = strdup(argv[i]);\r
69                 }\r
70         }\r
71 \r
72         INFO2("Got %d aliases and %d tasks to process", alias_count,number_of_tasks);\r
73         \r
74         for (i = 0; i < alias_count; i++)\r
75                 DEBUG1("%s", aliases[i]);\r
76 \r
77         for (i = 0; i < number_of_tasks; i++) \r
78         {\r
79                 INFO2("Sending \"%s\" to \"%s\"",todo[i]->name,aliases[i % alias_count]);\r
80                 \r
81                 if(!strcmp(MSG_host_get_name(MSG_host_self()), aliases[i % alias_count])) \r
82                 {\r
83                         INFO0("Hey ! It's me ! :)");\r
84                 }\r
85 \r
86                 MSG_task_send(todo[i], aliases[i % alias_count]);\r
87                 INFO0("Sent");\r
88         }\r
89 \r
90         INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");\r
91         \r
92         for (i = 0; i < alias_count; i++) \r
93                 MSG_task_send(MSG_task_create("finalize", 0, 0, FINALIZE),aliases[i]);\r
94 \r
95         INFO0("Goodbye now!");\r
96         \r
97         for(i = 0; i < alias_count; i++)\r
98                 free(aliases[i]);\r
99                 \r
100         free(aliases);\r
101         free(todo);\r
102         return 0;\r
103 } /* end_of_master */\r
104 \r
105 /** Receiver function  */\r
106 int slave(int argc, char *argv[])\r
107 {\r
108         m_task_t task = NULL;\r
109         int res;\r
110         \r
111         while(1) \r
112         {\r
113                 res = MSG_task_receive(&(task), MSG_host_get_name(MSG_host_self()));\r
114                 xbt_assert0(res == MSG_OK, "MSG_task_receive failed");\r
115         \r
116                 INFO1("Received \"%s\"", MSG_task_get_name(task));\r
117         \r
118                 if (!strcmp(MSG_task_get_name(task),"finalize")) \r
119                 {\r
120                         MSG_task_destroy(task);\r
121                         break;\r
122                 }\r
123 \r
124                 INFO1("Processing \"%s\"", MSG_task_get_name(task));\r
125                 MSG_task_execute(task);\r
126                 INFO1("\"%s\" done", MSG_task_get_name(task));\r
127                 MSG_task_destroy(task);\r
128                 task = NULL;\r
129         }\r
130         \r
131         INFO0("I'm done. See you!");\r
132         return 0;\r
133 } /* end_of_slave */\r
134 \r
135 /** Forwarder function */\r
136 int forwarder(int argc, char *argv[])\r
137 {\r
138         int i;\r
139         int alias_count;\r
140         char** aliases;\r
141         \r
142         {                  /* Process organisation */\r
143                 alias_count = argc - 1;\r
144                 aliases = xbt_new0(char*,alias_count);\r
145         \r
146                 for (i = 1; i < argc; i++) \r
147                         aliases[i-1] =strdup(argv[i]);\r
148         }\r
149 \r
150         i=0;\r
151         \r
152         while(1) \r
153         {\r
154                 m_task_t task = NULL;\r
155                 int a;\r
156                 \r
157                 a = MSG_task_receive(&(task),MSG_host_get_name(MSG_host_self()));\r
158                 \r
159                 if (a == MSG_OK) \r
160                 {\r
161                         INFO1("Received \"%s\"", MSG_task_get_name(task));\r
162                         \r
163                         if(MSG_task_get_data(task)==FINALIZE) \r
164                         {\r
165                                 INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");\r
166                                 \r
167                                 for (i = 0; i < alias_count; i++) \r
168                                         MSG_task_send(MSG_task_create("finalize", 0, 0, FINALIZE),aliases[i]);\r
169                                 \r
170                                 MSG_task_destroy(task);\r
171                                 break;\r
172                         }\r
173                 \r
174                         INFO2("Sending \"%s\" to \"%s\"",MSG_task_get_name(task),aliases[i% alias_count]);\r
175                         MSG_task_send(task, aliases[i % alias_count]);\r
176                         i++;\r
177                 } \r
178                 else \r
179                 {\r
180                         INFO0("Hey ?! What's up ? ");\r
181                         xbt_assert0(0,"Unexpected behavior");\r
182                 }\r
183         }\r
184         \r
185         for(i = 0; i < alias_count; i++)\r
186                 free(aliases[i]);\r
187 \r
188         INFO0("I'm done. See you!");\r
189         return 0;\r
190         \r
191 } /* end_of_forwarder */\r
192 \r
193 /** Test function */\r
194 MSG_error_t test_all(const char *platform_file,const char *application_file)\r
195 {\r
196         MSG_error_t res = MSG_OK;\r
197 \r
198         {       /*  Simulation setting */\r
199                 MSG_set_channel_number(MAX_CHANNEL);\r
200                 MSG_paje_output("msg_test.trace");\r
201                 MSG_create_environment(platform_file);\r
202         }\r
203         \r
204         {\r
205                 /*   Application deployment */\r
206                 MSG_function_register("master", master);\r
207                 MSG_function_register("slave", slave);\r
208                 MSG_function_register("forwarder", forwarder);\r
209                 MSG_launch_application(application_file);\r
210         }\r
211         \r
212         res = MSG_main();\r
213 \r
214         INFO1("Simulation time %g",MSG_get_clock());\r
215         return res;\r
216 } /* end_of_test_all */\r
217 \r
218 \r
219 /** Main function */\r
220 int main(int argc, char *argv[])\r
221 {\r
222         MSG_error_t res = MSG_OK;\r
223         \r
224         MSG_global_init(&argc,argv);\r
225         \r
226         if (argc < 3) \r
227         {\r
228                 printf ("Usage: %s platform_file deployment_file\n",argv[0]);\r
229                 printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);\r
230                 exit(1);\r
231         }\r
232         \r
233         res = test_all(argv[1],argv[2]);\r
234         MSG_clean();\r
235         \r
236         if(res==MSG_OK)\r
237                 return 0;\r
238         else\r
239                 return 1;\r
240 } /* end_of_main */\r