Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr/gitroot/simgrid/simgrid
[simgrid.git] / examples / msg / masterslave / masterslave_failure.c
1 /* Copyright (c) 2007-2014. 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 "simgrid/msg.h"        /* Yeah! If you want to use msg, you need to include simgrid/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 msg_error_t test_all(const char *platform_file,
20                      const char *application_file);
21
22 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
23
24 /** Emitter function  */
25 int master(int argc, char *argv[])
26 {
27   int slaves_count = 0;
28   msg_host_t *slaves = NULL;
29   int number_of_tasks = 0;
30   double task_comp_size = 0;
31   double task_comm_size = 0;
32   int i;
33   _XBT_GNUC_UNUSED int read;
34
35   read = sscanf(argv[1], "%d", &number_of_tasks);
36   xbt_assert(read, "Invalid argument %s\n", argv[1]);
37   read = sscanf(argv[2], "%lg", &task_comp_size);
38   xbt_assert(read, "Invalid argument %s\n", argv[2]);
39   read = sscanf(argv[3], "%lg", &task_comm_size);
40   xbt_assert(read, "Invalid argument %s\n", argv[3]);
41
42   {                             /* Process organisation */
43     slaves_count = argc - 4;
44     slaves = xbt_new0(msg_host_t, slaves_count);
45
46     for (i = 4; i < argc; i++) {
47       slaves[i - 4] = MSG_host_by_name(argv[i]);
48       if (slaves[i - 4] == NULL) {
49         XBT_INFO("Unknown host %s. Stopping Now! ", argv[i]);
50         abort();
51       }
52     }
53   }
54
55   XBT_INFO("Got %d slave(s) :", slaves_count);
56   for (i = 0; i < slaves_count; i++)
57     XBT_INFO("%s", MSG_host_get_name(slaves[i]));
58
59   XBT_INFO("Got %d task to process :", number_of_tasks);
60
61   for (i = 0; i < number_of_tasks; i++) {
62     msg_task_t task = MSG_task_create("Task", task_comp_size, task_comm_size,
63                                     xbt_new0(double, 1));
64     msg_error_t a;
65     *((double *) task->data) = MSG_get_clock();
66
67     a = MSG_task_send_with_timeout(task,MSG_host_get_name(slaves[i % slaves_count]),10.0);
68
69     if (a == MSG_OK) {
70       XBT_INFO("Send completed");
71     } else if (a == MSG_HOST_FAILURE) {
72       XBT_INFO
73           ("Gloups. The cpu on which I'm running just turned off!. See you!");
74       free(task->data);
75       MSG_task_destroy(task);
76       free(slaves);
77       return 0;
78     } else if (a == MSG_TRANSFER_FAILURE) {
79       XBT_INFO
80           ("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!",
81               MSG_host_get_name(slaves[i % slaves_count]));
82       free(task->data);
83       MSG_task_destroy(task);
84     } else if (a == MSG_TIMEOUT) {
85       XBT_INFO
86           ("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!",
87               MSG_host_get_name(slaves[i % slaves_count]));
88       free(task->data);
89       MSG_task_destroy(task);
90     } else {
91       XBT_INFO("Hey ?! What's up ? ");
92       xbt_die( "Unexpected behavior");
93     }
94   }
95
96   XBT_INFO
97       ("All tasks have been dispatched. Let's tell everybody the computation is over.");
98   for (i = 0; i < slaves_count; i++) {
99     msg_task_t task = MSG_task_create("finalize", 0, 0, FINALIZE);
100     int a = MSG_task_send_with_timeout(task,MSG_host_get_name(slaves[i]),1.0);
101     if (a == MSG_OK)
102       continue;
103     if (a == MSG_HOST_FAILURE) {
104       XBT_INFO
105           ("Gloups. The cpu on which I'm running just turned off!. See you!");
106       MSG_task_destroy(task);
107       free(slaves);
108       return 0;
109     } else if (a == MSG_TRANSFER_FAILURE) {
110       XBT_INFO("Mmh. Can't reach '%s'! Nevermind. Let's keep going!",
111           MSG_host_get_name(slaves[i]));
112       MSG_task_destroy(task);
113     } else if (a == MSG_TIMEOUT) {
114       XBT_INFO
115           ("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!",
116               MSG_host_get_name(slaves[i % slaves_count]));
117       MSG_task_destroy(task);
118     } else {
119       XBT_INFO("Hey ?! What's up ? ");
120       xbt_die("Unexpected behavior with '%s': %d", MSG_host_get_name(slaves[i]), a);
121     }
122   }
123
124   XBT_INFO("Goodbye now!");
125   free(slaves);
126   return 0;
127 }                               /* end_of_master */
128
129 /** Receiver function  */
130 int slave(int argc, char *argv[])
131 {
132   while (1) {
133     msg_task_t task = NULL;
134     int a;
135     double time1, time2;
136
137     time1 = MSG_get_clock();
138     a = MSG_task_receive( &(task), MSG_host_get_name(MSG_host_self()) );
139     time2 = MSG_get_clock();
140     if (a == MSG_OK) {
141       XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
142       if (MSG_task_get_data(task) == FINALIZE) {
143         MSG_task_destroy(task);
144         break;
145       }
146       if (time1 < *((double *) task->data))
147         time1 = *((double *) task->data);
148       XBT_INFO("Communication time : \"%f\"", time2 - time1);
149       XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
150       a = MSG_task_execute(task);
151       if (a == MSG_OK) {
152         XBT_INFO("\"%s\" done", MSG_task_get_name(task));
153         free(task->data);
154         MSG_task_destroy(task);
155       } else if (a == MSG_HOST_FAILURE) {
156         XBT_INFO
157             ("Gloups. The cpu on which I'm running just turned off!. See you!");
158         free(task->data);
159         MSG_task_destroy(task);
160         return 0;
161       } else {
162         XBT_INFO("Hey ?! What's up ? ");
163         xbt_die("Unexpected behavior");
164       }
165     } else if (a == MSG_HOST_FAILURE) {
166       XBT_INFO
167           ("Gloups. The cpu on which I'm running just turned off!. See you!");
168       return 0;
169     } else if (a == MSG_TRANSFER_FAILURE) {
170       XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
171     } else {
172       XBT_INFO("Hey ?! What's up ? ");
173       xbt_die("Unexpected behavior");
174     }
175   }
176   XBT_INFO("I'm done. See you!");
177   return 0;
178 }                               /* end_of_slave */
179
180 /** Test function */
181 msg_error_t test_all(const char *platform_file,
182                      const char *application_file)
183 {
184   msg_error_t res = MSG_OK;
185
186   {                             /*  Simulation setting */
187     MSG_create_environment(platform_file);
188   }
189   {                             /*   Application deployment */
190     MSG_function_register("master", master);
191     MSG_function_register("slave", slave);
192     MSG_launch_application(application_file);
193   }
194   res = MSG_main();
195
196   XBT_INFO("Simulation time %g", MSG_get_clock());
197   return res;
198 }                               /* end_of_test_all */
199
200
201 /** Main function */
202 int main(int argc, char *argv[])
203 {
204   msg_error_t res = MSG_OK;
205
206   MSG_init(&argc, argv);
207   if (argc < 3) {
208     printf("Usage: %s platform_file deployment_file\n", argv[0]);
209     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
210     exit(1);
211   }
212   res = test_all(argv[1], argv[2]);
213
214   if (res == MSG_OK)
215     return 0;
216   else
217     return 1;
218 }                               /* end_of_main */