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, 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 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
24
25 /** Emitter function  */
26 int master(int argc, char *argv[])
27 {
28   int slaves_count = 0;
29   m_host_t *slaves = NULL;
30   int number_of_tasks = 0;
31   double task_comp_size = 0;
32   double task_comm_size = 0;
33   int i;
34   _XBT_GNUC_UNUSED int read;
35
36   read = sscanf(argv[1], "%d", &number_of_tasks);
37   xbt_assert(read, "Invalid argument %s\n", argv[1]);
38   read = sscanf(argv[2], "%lg", &task_comp_size);
39   xbt_assert(read, "Invalid argument %s\n", argv[2]);
40   read = sscanf(argv[3], "%lg", &task_comm_size);
41   xbt_assert(read, "Invalid argument %s\n", argv[3]);
42
43   {                             /* Process organisation */
44     slaves_count = argc - 4;
45     slaves = xbt_new0(m_host_t, slaves_count);
46
47     for (i = 4; i < argc; i++) {
48       slaves[i - 4] = MSG_get_host_by_name(argv[i]);
49       if (slaves[i - 4] == NULL) {
50         XBT_INFO("Unknown host %s. Stopping Now! ", argv[i]);
51         abort();
52       }
53     }
54   }
55
56   XBT_INFO("Got %d slave(s) :", slaves_count);
57   for (i = 0; i < slaves_count; i++)
58     XBT_INFO("%s", slaves[i]->name);
59
60   XBT_INFO("Got %d task to process :", number_of_tasks);
61
62   for (i = 0; i < number_of_tasks; i++) {
63     m_task_t task = MSG_task_create("Task", task_comp_size, task_comm_size,
64                                     xbt_new0(double, 1));
65     int a;
66     *((double *) task->data) = MSG_get_clock();
67
68     a = MSG_task_send_with_timeout(task,MSG_host_get_name(slaves[i % slaves_count]),10.0);
69
70     if (a == MSG_OK) {
71       XBT_INFO("Send completed");
72     } else if (a == MSG_HOST_FAILURE) {
73       XBT_INFO
74           ("Gloups. The cpu on which I'm running just turned off!. See you!");
75       free(task->data);
76       MSG_task_destroy(task);
77       free(slaves);
78       return 0;
79     } else if (a == MSG_TRANSFER_FAILURE) {
80       XBT_INFO
81           ("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!",
82            slaves[i % slaves_count]->name);
83       free(task->data);
84       MSG_task_destroy(task);
85     } else if (a == MSG_TIMEOUT) {
86       XBT_INFO
87           ("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!",
88            slaves[i % slaves_count]->name);
89       free(task->data);
90       MSG_task_destroy(task);
91     } else {
92       XBT_INFO("Hey ?! What's up ? ");
93       xbt_die( "Unexpected behavior");
94     }
95   }
96
97   XBT_INFO
98       ("All tasks have been dispatched. Let's tell everybody the computation is over.");
99   for (i = 0; i < slaves_count; i++) {
100     m_task_t task = MSG_task_create("finalize", 0, 0, FINALIZE);
101     int a = MSG_task_send_with_timeout(task,MSG_host_get_name(slaves[i]),1.0);
102     if (a == MSG_OK)
103       continue;
104     if (a == MSG_HOST_FAILURE) {
105       XBT_INFO
106           ("Gloups. The cpu on which I'm running just turned off!. See you!");
107       MSG_task_destroy(task);
108       free(slaves);
109       return 0;
110     } else if (a == MSG_TRANSFER_FAILURE) {
111       XBT_INFO("Mmh. Can't reach '%s'! Nevermind. Let's keep going!",
112             slaves[i]->name);
113       MSG_task_destroy(task);
114     } else if (a == MSG_TIMEOUT) {
115       XBT_INFO
116           ("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!",
117            slaves[i % slaves_count]->name);
118       MSG_task_destroy(task);
119     } else {
120       XBT_INFO("Hey ?! What's up ? ");
121       xbt_die("Unexpected behavior with '%s': %d", slaves[i]->name, a);
122     }
123   }
124
125   XBT_INFO("Goodbye now!");
126   free(slaves);
127   return 0;
128 }                               /* end_of_master */
129
130 /** Receiver function  */
131 int slave(int argc, char *argv[])
132 {
133   while (1) {
134     m_task_t task = NULL;
135     int a;
136     double time1, time2;
137
138     time1 = MSG_get_clock();
139     a = MSG_task_receive( &(task), MSG_host_get_name(MSG_host_self()) );
140     time2 = MSG_get_clock();
141     if (a == MSG_OK) {
142       XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
143       if (MSG_task_get_data(task) == FINALIZE) {
144         MSG_task_destroy(task);
145         break;
146       }
147       if (time1 < *((double *) task->data))
148         time1 = *((double *) task->data);
149       XBT_INFO("Communication time : \"%f\"", time2 - time1);
150       XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
151       a = MSG_task_execute(task);
152       if (a == MSG_OK) {
153         XBT_INFO("\"%s\" done", MSG_task_get_name(task));
154         free(task->data);
155         MSG_task_destroy(task);
156       } else if (a == MSG_HOST_FAILURE) {
157         XBT_INFO
158             ("Gloups. The cpu on which I'm running just turned off!. See you!");
159         return 0;
160       } else {
161         XBT_INFO("Hey ?! What's up ? ");
162         xbt_die("Unexpected behavior");
163       }
164     } else if (a == MSG_HOST_FAILURE) {
165       XBT_INFO
166           ("Gloups. The cpu on which I'm running just turned off!. See you!");
167       return 0;
168     } else if (a == MSG_TRANSFER_FAILURE) {
169       XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
170     } else {
171       XBT_INFO("Hey ?! What's up ? ");
172       xbt_die("Unexpected behavior");
173     }
174   }
175   XBT_INFO("I'm done. See you!");
176   return 0;
177 }                               /* end_of_slave */
178
179 /** Test function */
180 MSG_error_t test_all(const char *platform_file,
181                      const char *application_file)
182 {
183   MSG_error_t res = MSG_OK;
184
185   /* MSG_config("workstation/model","KCCFLN05"); */
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_global_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   MSG_clean();
214
215   if (res == MSG_OK)
216     return 0;
217   else
218     return 1;
219 }                               /* end_of_main */