Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
628e77d154893f2751771b0f9120dfe9647d5c1e
[simgrid.git] / examples / msg / masterslave / masterslave_failure.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   int number_of_tasks = 0;
36   double task_comp_size = 0;
37   double task_comm_size = 0;
38   int i;
39   int read;
40
41   read = sscanf(argv[1], "%d", &number_of_tasks);
42   xbt_assert1(read, "Invalid argument %s\n", argv[1]);
43   read = sscanf(argv[2], "%lg", &task_comp_size);
44   xbt_assert1(read, "Invalid argument %s\n", argv[2]);
45   read = sscanf(argv[3], "%lg", &task_comm_size);
46   xbt_assert1(read, "Invalid argument %s\n", argv[3]);
47
48   {                             /* Process organisation */
49     slaves_count = argc - 4;
50     slaves = xbt_new0(m_host_t, slaves_count);
51
52     for (i = 4; i < argc; i++) {
53       slaves[i - 4] = MSG_get_host_by_name(argv[i]);
54       if (slaves[i - 4] == NULL) {
55         XBT_INFO("Unknown host %s. Stopping Now! ", argv[i]);
56         abort();
57       }
58     }
59   }
60
61   XBT_INFO("Got %d slave(s) :", slaves_count);
62   for (i = 0; i < slaves_count; i++)
63     XBT_INFO("%s", slaves[i]->name);
64
65   XBT_INFO("Got %d task to process :", number_of_tasks);
66
67   for (i = 0; i < number_of_tasks; i++) {
68     m_task_t task = MSG_task_create("Task", task_comp_size, task_comm_size,
69                                     xbt_new0(double, 1));
70     int a;
71     *((double *) task->data) = MSG_get_clock();
72
73     a = MSG_task_put_with_timeout(task, slaves[i % slaves_count], PORT_22,
74                                   10.0);
75     if (a == MSG_OK) {
76       XBT_INFO("Send completed");
77     } else if (a == MSG_HOST_FAILURE) {
78       XBT_INFO
79           ("Gloups. The cpu on which I'm running just turned off!. See you!");
80       free(task->data);
81       MSG_task_destroy(task);
82       free(slaves);
83       return 0;
84     } else if (a == MSG_TRANSFER_FAILURE) {
85       XBT_INFO
86           ("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!",
87            slaves[i % slaves_count]->name);
88       free(task->data);
89       MSG_task_destroy(task);
90     } else if (a == MSG_TIMEOUT) {
91       XBT_INFO
92           ("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!",
93            slaves[i % slaves_count]->name);
94       free(task->data);
95       MSG_task_destroy(task);
96     } else {
97       XBT_INFO("Hey ?! What's up ? ");
98       xbt_die( "Unexpected behavior");
99     }
100   }
101
102   XBT_INFO
103       ("All tasks have been dispatched. Let's tell everybody the computation is over.");
104   for (i = 0; i < slaves_count; i++) {
105     m_task_t task = MSG_task_create("finalize", 0, 0, FINALIZE);
106     int a = MSG_task_put_with_timeout(task, slaves[i], PORT_22, 1.0);
107     if (a == MSG_OK)
108       continue;
109     if (a == MSG_HOST_FAILURE) {
110       XBT_INFO
111           ("Gloups. The cpu on which I'm running just turned off!. See you!");
112       MSG_task_destroy(task);
113       free(slaves);
114       return 0;
115     } else if (a == MSG_TRANSFER_FAILURE) {
116       XBT_INFO("Mmh. Can't reach '%s'! Nevermind. Let's keep going!",
117             slaves[i]->name);
118       MSG_task_destroy(task);
119     } else if (a == MSG_TIMEOUT) {
120       XBT_INFO
121           ("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!",
122            slaves[i % slaves_count]->name);
123       MSG_task_destroy(task);
124     } else {
125       XBT_INFO("Hey ?! What's up ? ");
126       xbt_die("Unexpected behavior with '%s': %d", slaves[i]->name, a);
127     }
128   }
129
130   XBT_INFO("Goodbye now!");
131   free(slaves);
132   return 0;
133 }                               /* end_of_master */
134
135 /** Receiver function  */
136 int slave(int argc, char *argv[])
137 {
138   while (1) {
139     m_task_t task = NULL;
140     int a;
141     double time1, time2;
142
143     time1 = MSG_get_clock();
144     a = MSG_task_get(&(task), PORT_22);
145     time2 = MSG_get_clock();
146     if (a == MSG_OK) {
147       XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
148       if (MSG_task_get_data(task) == FINALIZE) {
149         MSG_task_destroy(task);
150         break;
151       }
152       if (time1 < *((double *) task->data))
153         time1 = *((double *) task->data);
154       XBT_INFO("Communication time : \"%f\"", time2 - time1);
155       XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
156       a = MSG_task_execute(task);
157       if (a == MSG_OK) {
158         XBT_INFO("\"%s\" done", MSG_task_get_name(task));
159         free(task->data);
160         MSG_task_destroy(task);
161       } else if (a == MSG_HOST_FAILURE) {
162         XBT_INFO
163             ("Gloups. The cpu on which I'm running just turned off!. See you!");
164         return 0;
165       } else {
166         XBT_INFO("Hey ?! What's up ? ");
167         xbt_die("Unexpected behavior");
168       }
169     } else if (a == MSG_HOST_FAILURE) {
170       XBT_INFO
171           ("Gloups. The cpu on which I'm running just turned off!. See you!");
172       return 0;
173     } else if (a == MSG_TRANSFER_FAILURE) {
174       XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
175     } else {
176       XBT_INFO("Hey ?! What's up ? ");
177       xbt_die("Unexpected behavior");
178     }
179   }
180   XBT_INFO("I'm done. See you!");
181   return 0;
182 }                               /* end_of_slave */
183
184 /** Test function */
185 MSG_error_t test_all(const char *platform_file,
186                      const char *application_file)
187 {
188   MSG_error_t res = MSG_OK;
189
190   /* MSG_config("workstation/model","KCCFLN05"); */
191   {                             /*  Simulation setting */
192     MSG_set_channel_number(MAX_CHANNEL);
193     MSG_create_environment(platform_file);
194   }
195   {                             /*   Application deployment */
196     MSG_function_register("master", master);
197     MSG_function_register("slave", slave);
198     MSG_launch_application(application_file);
199   }
200   res = MSG_main();
201
202   XBT_INFO("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
212   MSG_global_init(&argc, argv);
213   if (argc < 3) {
214     printf("Usage: %s platform_file deployment_file\n", argv[0]);
215     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
216     exit(1);
217   }
218   res = test_all(argv[1], argv[2]);
219   MSG_clean();
220
221   if (res == MSG_OK)
222     return 0;
223   else
224     return 1;
225 }                               /* end_of_main */