Logo AND Algorithmique Numérique Distribuée

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