Logo AND Algorithmique Numérique Distribuée

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