Logo AND Algorithmique Numérique Distribuée

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