Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Adapt masterslave_bypass to the brand new AS tag.
[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, const char *application_file);
21
22 typedef enum {
23   PORT_22 = 0,
24   MAX_CHANNEL
25 } channel_t;
26
27 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
28
29 /** Emitter function  */
30 int master(int argc, char *argv[])
31 {
32   int slaves_count = 0;
33   m_host_t *slaves = NULL;
34   int number_of_tasks = 0;
35   double task_comp_size = 0;
36   double task_comm_size = 0;
37
38
39   int i;
40
41   xbt_assert1(sscanf(argv[1], "%d", &number_of_tasks),
42               "Invalid argument %s\n", argv[1]);
43   xbt_assert1(sscanf(argv[2], "%lg", &task_comp_size),
44               "Invalid argument %s\n", argv[2]);
45   xbt_assert1(sscanf(argv[3], "%lg", &task_comm_size),
46               "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         INFO1("Unknown host %s. Stopping Now! ", argv[i]);
56         abort();
57       }
58     }
59   }
60
61   INFO1("Got %d slave(s) :", slaves_count);
62   for (i = 0; i < slaves_count; i++)
63     INFO1("%s", slaves[i]->name);
64
65   INFO1("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 =
74       MSG_task_put_with_timeout(task, slaves[i % slaves_count], PORT_22,
75                                 10.0);
76     if (a == MSG_OK) {
77       INFO0("Send completed");
78     } else if (a == MSG_HOST_FAILURE) {
79       INFO0
80         ("Gloups. The cpu on which I'm running just turned off!. See you!");
81       free(slaves);
82       return 0;
83     } else if (a == MSG_TRANSFER_FAILURE) {
84       INFO1
85         ("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!",
86          slaves[i % slaves_count]->name);
87       MSG_task_destroy(task);
88     } else if (a == MSG_TIMEOUT) {
89       INFO1
90         ("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!",
91          slaves[i % slaves_count]->name);
92       MSG_task_destroy(task);
93     } else {
94       INFO0("Hey ?! What's up ? ");
95       xbt_assert0(0, "Unexpected behavior");
96     }
97   }
98
99   INFO0
100     ("All tasks have been dispatched. Let's tell everybody the computation is over.");
101   for (i = 0; i < slaves_count; i++) {
102     m_task_t task = MSG_task_create("finalize", 0, 0, FINALIZE);
103     int a = MSG_task_put_with_timeout(task, slaves[i], PORT_22, 1.0);
104     if (a == MSG_OK)
105       continue;
106     if (a == MSG_HOST_FAILURE) {
107       INFO0
108         ("Gloups. The cpu on which I'm running just turned off!. See you!");
109       return 0;
110     } else if (a == MSG_TRANSFER_FAILURE) {
111       INFO1("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       INFO1
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       INFO0("Hey ?! What's up ? ");
121       xbt_assert2(0, "Unexpected behavior with '%s': %d", slaves[i]->name, a);
122     }
123   }
124
125   INFO0("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_get(&(task), PORT_22);
140     time2 = MSG_get_clock();
141     if (a == MSG_OK) {
142       INFO1("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       INFO1("Communication time : \"%f\"", time2 - time1);
150       INFO1("Processing \"%s\"", MSG_task_get_name(task));
151       a = MSG_task_execute(task);
152       if (a == MSG_OK) {
153         INFO1("\"%s\" done", MSG_task_get_name(task));
154         free(task->data);
155         MSG_task_destroy(task);
156       } else if (a == MSG_HOST_FAILURE) {
157         INFO0
158           ("Gloups. The cpu on which I'm running just turned off!. See you!");
159         return 0;
160       } else {
161         INFO0("Hey ?! What's up ? ");
162         xbt_assert0(0, "Unexpected behavior");
163       }
164     } else if (a == MSG_HOST_FAILURE) {
165       INFO0
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       INFO0("Mmh. Something went wrong. Nevermind. Let's keep going!");
170     } else {
171       INFO0("Hey ?! What's up ? ");
172       xbt_assert0(0, "Unexpected behavior");
173     }
174   }
175   INFO0("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, 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_launch_application(application_file);
193   }
194   res = MSG_main();
195
196   INFO1("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 */