Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
merge back the master trunk into the smpi branch
[simgrid.git] / examples / msg / masterslave / masterslave_forwarder.c
1 /* Copyright (c) 2007, 2008, 2009, 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 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   int i;
41
42   _XBT_GNUC_UNUSED int res = sscanf(argv[1], "%d", &number_of_tasks);
43   xbt_assert(res,"Invalid argument %s\n", argv[1]);
44   res = sscanf(argv[2], "%lg", &task_comp_size);
45   xbt_assert(res, "Invalid argument %s\n", argv[2]);
46   res = sscanf(argv[3], "%lg", &task_comm_size);
47   xbt_assert(res, "Invalid argument %s\n", argv[3]);
48
49   {                             /*  Task creation */
50     char sprintf_buffer[64];
51
52     todo = xbt_new0(m_task_t, number_of_tasks);
53
54     for (i = 0; i < number_of_tasks; i++) {
55       sprintf(sprintf_buffer, "Task_%d", i);
56       todo[i] =
57           MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
58                           NULL);
59     }
60   }
61
62   {                             /* Process organisation */
63     slaves_count = argc - 4;
64     slaves = xbt_new0(m_host_t, slaves_count);
65
66     for (i = 4; i < argc; i++) {
67       slaves[i - 4] = MSG_get_host_by_name(argv[i]);
68       xbt_assert(slaves[i - 4] != NULL, "Unknown host %s. Stopping Now! ",
69                   argv[i]);
70     }
71   }
72
73   XBT_INFO("Got %d slaves and %d tasks to process", slaves_count,
74         number_of_tasks);
75   for (i = 0; i < slaves_count; i++)
76     XBT_DEBUG("%s", slaves[i]->name);
77
78   for (i = 0; i < number_of_tasks; i++) {
79     XBT_INFO("Sending \"%s\" to \"%s\"",
80           todo[i]->name, slaves[i % slaves_count]->name);
81     if (MSG_host_self() == slaves[i % slaves_count]) {
82       XBT_INFO("Hey ! It's me ! :)");
83     }
84
85     MSG_task_put(todo[i], slaves[i % slaves_count], PORT_22);
86     XBT_INFO("Sent");
87   }
88
89   XBT_INFO
90       ("All tasks have been dispatched. Let's tell everybody the computation is over.");
91   for (i = 0; i < slaves_count; i++) {
92     m_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
93     MSG_task_put(finalize, slaves[i], PORT_22);
94   }
95
96   XBT_INFO("Goodbye now!");
97   free(slaves);
98   free(todo);
99   return 0;
100 }                               /* end_of_master */
101
102 /** Receiver function  */
103 int slave(int argc, char *argv[])
104 {
105   m_task_t task = NULL;
106   _XBT_GNUC_UNUSED int res;
107   while (1) {
108     res = MSG_task_get(&(task), PORT_22);
109     xbt_assert(res == MSG_OK, "MSG_task_get failed");
110
111     XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
112     if (!strcmp(MSG_task_get_name(task), "finalize")) {
113       MSG_task_destroy(task);
114       break;
115     }
116
117     XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
118     MSG_task_execute(task);
119     XBT_INFO("\"%s\" done", MSG_task_get_name(task));
120     MSG_task_destroy(task);
121     task = NULL;
122   }
123   XBT_INFO("I'm done. See you!");
124   return 0;
125 }                               /* end_of_slave */
126
127 /** Forwarder function */
128 int forwarder(int argc, char *argv[])
129 {
130   int i;
131   int slaves_count;
132   m_host_t *slaves;
133
134   {                             /* Process organisation */
135     slaves_count = argc - 1;
136     slaves = xbt_new0(m_host_t, slaves_count);
137
138     for (i = 1; i < argc; i++) {
139       slaves[i - 1] = MSG_get_host_by_name(argv[i]);
140       if (slaves[i - 1] == NULL) {
141         XBT_INFO("Unknown host %s. Stopping Now! ", argv[i]);
142         abort();
143       }
144     }
145   }
146
147   i = 0;
148   while (1) {
149     m_task_t task = NULL;
150     int a;
151     a = MSG_task_get(&(task), PORT_22);
152     if (a == MSG_OK) {
153       XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
154       if (MSG_task_get_data(task) == FINALIZE) {
155         XBT_INFO
156             ("All tasks have been dispatched. Let's tell everybody the computation is over.");
157         for (i = 0; i < slaves_count; i++)
158           MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
159                        slaves[i], PORT_22);
160         MSG_task_destroy(task);
161         break;
162       }
163       XBT_INFO("Sending \"%s\" to \"%s\"",
164             MSG_task_get_name(task), slaves[i % slaves_count]->name);
165       MSG_task_put(task, slaves[i % slaves_count], PORT_22);
166       i++;
167     } else {
168       XBT_INFO("Hey ?! What's up ? ");
169       xbt_die("Unexpected behavior");
170     }
171   }
172   xbt_free(slaves);
173
174   XBT_INFO("I'm done. See you!");
175   return 0;
176 }                               /* end_of_forwarder */
177
178 /** Test function */
179 MSG_error_t test_all(const char *platform_file,
180                      const char *application_file)
181 {
182   MSG_error_t res = MSG_OK;
183
184   /* MSG_config("workstation/model","KCCFLN05"); */
185   {                             /*  Simulation setting */
186     MSG_set_channel_number(MAX_CHANNEL);
187     MSG_create_environment(platform_file);
188   }
189   {                             /*   Application deployment */
190     MSG_function_register("master", master);
191     MSG_function_register("slave", slave);
192     MSG_function_register("forwarder", forwarder);
193     MSG_launch_application(application_file);
194   }
195   res = MSG_main();
196
197   XBT_INFO("Simulation time %g", MSG_get_clock());
198   return res;
199 }                               /* end_of_test_all */
200
201
202 /** Main function */
203 int main(int argc, char *argv[])
204 {
205   MSG_error_t res = MSG_OK;
206
207   MSG_global_init(&argc, argv);
208   if (argc < 3) {
209     printf("Usage: %s platform_file deployment_file\n", argv[0]);
210     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
211     exit(1);
212   }
213   res = test_all(argv[1], argv[2]);
214   MSG_clean();
215
216   if (res == MSG_OK)
217     return 0;
218   else
219     return 1;
220 }                               /* end_of_main */